Advertisement Jump to content
Sign in to follow this  

Making a tile class

This topic is 5065 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 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
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.

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?



Share this post

Link to post
Share on other sites

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

CVector2i m_vPos;
CVector2i m_vDir;


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

class CPlayer : public CBaseSprite

char * m_szName[31];

int m_nExperience;
int m_nHealth;

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


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

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

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.
SDL_Surface *m_texture;

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

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);
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
vector_2d(int w, int h);

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

// This class will describe my map file format.
class 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; }
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,

// This class will control the camera.
class 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; }
int m_camera_x;
int m_camera_y;
int m_camera_w;
int m_camera_h;

camera_align m_camera_mode;


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


[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, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!