Jump to content
  • Advertisement
Sign in to follow this  

Making a tile class

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I know this have been posted before...but I didn't find any tread...yes I've looked =) and search is temporary disabled...so here goes. I want to make a tile class! Now what do I have to have in it? I thought this: I will have to have this: A struct that represents each sprite, it has to know x,y positions width,height values (if not all tiles is the same size) and have a id (to know wich kind of tile it is, brick, sky...). I thought that in every game that uses the tile class the user makes a enumeration with all tiles the game uses and use that to deside which tile to load and draw. Then you can use the class in any game that will use tiles. Then in the main class. Load, draw and collisioncheck functions. What else? Then I thought making some other classes that inhearit from the base class. Classes for animated tiles, player, enemies, (npc,s) something else? Du you have any suggestions or tips etc. please post. //walle

Share this post


Link to post
Share on other sites
Advertisement
I think the best solution is to sit down, away from the comoputer, with a pencil and paper, and just write down every possible thing you could need for a tile. Let your mind wander and try and think up something long lasting and useful.

Here are some ideas to start you off:

- Can the player walk on the tile?
- Will the player be hurt by walking on the tile?
- How high is the tile?
- Maybe you could make a whole map filetype off the tile class
- Maybe it could have layers
- You could give each tile a z-value type sorting thing
- You could make a tile have a check to see if it is a roof tile. If it is, automatically move it up to where is looks like it is off the ground, and stop drawing it if the player is under it
- Make someway to have drawing tiles simple and quick. Make a function that takes an x y value and the specific tile, and have it take care of the details

There are many many things you can do, and almost all of them will occur to you away from the keyboard. Remember, you can do amazing things if you just keep at them. You will amaze yourself.

Share this post


Link to post
Share on other sites
I agree with Foot Soldier - sit down grab a piece of paper and start thinking of what kind of stuff you would like your tile to contain, be able to do, etc.

I think it's nice to use a set of bitflags defining the different state, and possibilities for a tile.


enum TILEFLAG
{
TF_ANIMATED = 1, // Tile is animated
TF_BREAKABLE = 2, // Tile can be destroyed
TF_WALKABLE = 4, // Entityes may walk on tile
TF_VISIBLE = 8, // Tile is currently visible
TF_TRANSPARENT = 16, // Tile is transparent
TF_PICKABLE = 32, // Tile may be clicked
TF_MOVABLE = 64, // Tile may be moved / is moving?
TF_USING_FX = 128, // Tile is using somekind of fx
TF_CAN_TAKE = 256 // This tile object can be taken
};

You could have a sound triggered when an entity approches/walks on the tile, or perhaps the tile can contain other stuff? The possibilities are endless:)
Use somekind of tiletransition.

Share this post


Link to post
Share on other sites
Ok...I think this is a good advice, that is what I 've done to come up with what I have allready...just that I didn't think as far.
I'll sit down and write down what I want the tiles to be able to do, do whith tiles. And don't care if I can code it or not otherwise I wouldn't learn anything. =) Then I'll try to implement all the stuff I want...right?

But what do you think about the struct and to derive (inhearit) different types of tiles like a player tile and other from a base class?

Thanks.

//walle

Share this post


Link to post
Share on other sites
Quote:


But what do you think about the struct and to derive (inhearit) different types of tiles like a player tile and other from a base class?



For ease I think you should separate your tile / sprite structures.
Build a base sprite class for all moving entities in your map (player,npc,etc) and derive from that one.
Don't know if this snip. could be of any usage.



class CBaseSprite
{
private:

CVector2i m_vPos;
CVector2i m_vDir;

public:

virtual void Render() = 0;
virtual void Move() = 0;
};

class CPlayer : public CBaseSprite
{
private:

char * m_szName[31];

int m_nExperience;
int m_nHealth;

public:
void Render();
void Move();
void Animate();
...
};


Share this post


Link to post
Share on other sites
Ok...I've got some code together. =)

I thought about it a bit, and I figured that the tile class isn't the most important, the map class is the more importand becuse it places all the tiles and so...the tile class "only" draws the map.
So my class design looks a little different then I first planned.

But I thought that I could post what I've got and if anyone has any suggestins they can tell me what they tink.

The "Tile engine.h" file

#ifndef TILE_ENGINE_H
#define TILE_ENGINE_H

#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <vector>

// This class will hold a texture for the tiles
class texture
{
public:
texture();
texture(char *file);
~texture();

SDL_Surface init_texture(char *file); // Initialize the texture to a image without transparency.
SDL_Surface init_texture(char *file, int r, int g, int b); // Initialize the texture to a image with transparency.
private:
SDL_Surface *m_texture;
};

// This is the base class for every tile.
class tile
{
public:
tile();
tile(int x, int y, int w, int h, int id, int texture, int layer);
~tile();

void init_tile(int x, int y, int w, int h, int id, int texture, int layer);

void draw_tile();

int get_x() { return m_x; }
int get_y() { return m_y; }
int get_w() { return m_w; }
int get_h() { return m_h; }
int get_id() { return m_id_nr; }
int get_texture() { return m_texture; }

void set_x(int x) { m_x=x; }
void set_y(int y) { m_y=y; }
void set_w(int w) { m_w=w; }
void set_h(int h) { m_h=h; }
void set_id(int id) { m_id_nr=id; }
void set_texture(int texture) { m_texture=texture; }

bool check_for_collision(tile tile_to_check);
private:
int m_x;
int m_y;
int m_w;
int m_h;

int m_id_nr;
int m_texture;
int m_layer;
};

// This is a class to implement a resizeable 2D vector using std::vector
class vector_2d
{
public:
vector_2d();
vector_2d(int w, int h);
//~vector_2d();

void resize(int w, int h);
void resize_width(int w);
void resize_height(int y);
private:
std::vector<int> m_w_vector;
std::vector<int> m_h_vector;
};

// This class will describe my map file format.
class tile_map
{
public:
tile_map();
~tile_map();

bool write_map(char *file);
bool load_map(char *file);

int get_map_width(int w) { return m_map_width; }
int get_map_height(int h) { return m_map_height; }

void set_map_width(int w) { m_map_width=w; }
void set_map_height(int h) { m_map_height=h; }
private:
int m_map_width;
int m_map_height;

vector_2d map;

SDL_Surface *m_background;
SDL_Surface *m_layer1;
SDL_Surface *m_layer2;
SDL_Surface *m_layer3;
SDL_Surface *m_layer4;
SDL_Surface *m_layer5;
SDL_Surface *m_layer6;
};

// A enumeration for the different camera aligns.
enum camera_align { PLAYER_CENTER,
PLAYER_CENTER_LEFT,
PLAYER_CENTER_RIGHT,
PLAYER_CENTER_TOP,
PLAYER_CENTER_BOTTOM,
PLAYER_TOP_LEFT,
PLAYER_TOP_RIGHT,
PLAYER_BOTTOM_LEFT,
PLAYER_BOTTOM_RIGHT
};

// This class will control the camera.
class camera
{
public:
camera();
~camera();

void use_camera(camera_align mode);
void move_camera(int x, int y);

int get_camera_x() { return m_camera_x; }
int get_camera_y() { return m_camera_y; }
int get_camera_w() { return m_camera_w; }
int get_camera_h() { return m_camera_h; }

void set_camera_x(int x) { m_camera_x=x; }
void set_camera_y(int y) { m_camera_y=y; }
void set_camera_w(int w) { m_camera_w=w; }
void set_camera_h(int h) { m_camera_h=h; }
private:
int m_camera_x;
int m_camera_y;
int m_camera_w;
int m_camera_h;

camera_align m_camera_mode;
};


#endif




Well, it's a work in progress...

//walle

[Edited by - walle on March 11, 2005 1:24:57 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!