Skip to main content
added 430 characters in body
Source Link

No.

Grass, Sand and Water aren't different TYPES of tiles, they are different tiles.

i.e.

class Tile
{
private:
  Sprite sprite;
  bool collidable;
  bool flamable;
  bool walkable;
public:

 Tile(Sprite s, bool col, bool flam, bool walk)
 {
   this.sprite = s;
   this.collidable = col;
   this.flamable = flam;
   this.walkable = walk;
 }

 bool isCollidable()
 {
   return this.collidable;
 }


 bool isFlamable()
 {
   return this.flamable;
 }


 bool isWalkable()
 {
   return this.walkable;
 }
}

grass = Tile(Sprite(...),false,true,true);
sand = Tile(Sprite(...),false,true,true);
water = Tile(Sprite(...),false,false,false);

In general you only want to derive a class if the type adds some functionality. Since all the tiles you describe have the same attributes, they are instances not derived objects. Like a tile that can be walkable sometimes, but not others would be a reason to derive a new class.

I added sprite to represent the things I didn't add to the class, but a tile having a sprite isn't what I'd really do. Intead, Tile would have:

  • Static image for tiles the sprite sheet.
  • Static constant length and width of a tile.
  • tile_x and tile_y for where on the sprite sheet to find it
  • x and y of where this tile is placed.

Having a "sprite" was just a way in the above example to make all that easier.

No.

Grass, Sand and Water aren't different TYPES of tiles, they are different tiles.

i.e.

class Tile
{
private:
  Sprite sprite;
  bool collidable;
  bool flamable;
  bool walkable;
public:

 Tile(Sprite s, bool col, bool flam, bool walk)
 {
   this.sprite = s;
   this.collidable = col;
   this.flamable = flam;
   this.walkable = walk;
 }

 bool isCollidable()
 {
   return this.collidable;
 }


 bool isFlamable()
 {
   return this.flamable;
 }


 bool isWalkable()
 {
   return this.walkable;
 }
}

grass = Tile(Sprite(...),false,true,true);
sand = Tile(Sprite(...),false,true,true);
water = Tile(Sprite(...),false,false,false);

In general you only want to derive a class if the type adds some functionality. Since all the tiles you describe have the same attributes, they are instances not derived objects. Like a tile that can be walkable sometimes, but not others would be a reason to derive a new class.

No.

Grass, Sand and Water aren't different TYPES of tiles, they are different tiles.

i.e.

class Tile
{
private:
  Sprite sprite;
  bool collidable;
  bool flamable;
  bool walkable;
public:

 Tile(Sprite s, bool col, bool flam, bool walk)
 {
   this.sprite = s;
   this.collidable = col;
   this.flamable = flam;
   this.walkable = walk;
 }

 bool isCollidable()
 {
   return this.collidable;
 }


 bool isFlamable()
 {
   return this.flamable;
 }


 bool isWalkable()
 {
   return this.walkable;
 }
}

grass = Tile(Sprite(...),false,true,true);
sand = Tile(Sprite(...),false,true,true);
water = Tile(Sprite(...),false,false,false);

In general you only want to derive a class if the type adds some functionality. Since all the tiles you describe have the same attributes, they are instances not derived objects. Like a tile that can be walkable sometimes, but not others would be a reason to derive a new class.

I added sprite to represent the things I didn't add to the class, but a tile having a sprite isn't what I'd really do. Intead, Tile would have:

  • Static image for tiles the sprite sheet.
  • Static constant length and width of a tile.
  • tile_x and tile_y for where on the sprite sheet to find it
  • x and y of where this tile is placed.

Having a "sprite" was just a way in the above example to make all that easier.

Source Link

No.

Grass, Sand and Water aren't different TYPES of tiles, they are different tiles.

i.e.

class Tile
{
private:
  Sprite sprite;
  bool collidable;
  bool flamable;
  bool walkable;
public:

 Tile(Sprite s, bool col, bool flam, bool walk)
 {
   this.sprite = s;
   this.collidable = col;
   this.flamable = flam;
   this.walkable = walk;
 }

 bool isCollidable()
 {
   return this.collidable;
 }


 bool isFlamable()
 {
   return this.flamable;
 }


 bool isWalkable()
 {
   return this.walkable;
 }
}

grass = Tile(Sprite(...),false,true,true);
sand = Tile(Sprite(...),false,true,true);
water = Tile(Sprite(...),false,false,false);

In general you only want to derive a class if the type adds some functionality. Since all the tiles you describe have the same attributes, they are instances not derived objects. Like a tile that can be walkable sometimes, but not others would be a reason to derive a new class.