• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

C++ One enum name for more types

7 posts in this topic

Hello.

The title is of, i am aware but i dont know how to describe the issue.

 

Intro:

I am making a 2d tiled map. Each tile has type that is a enum type.

class Tile{public: int type;}

The want to do:

I want to make a enum defined like this to be able to have state?

When i check collision, i want to check player V.S. the tile he is gonna move to, if the tile he wants to move to is "unwalkable" deny the  movement, else relocate.

enum Tile_Type
{

grass = 1,
water = 2,
stone = 3

};
 
bool CollisionChecking()

{

if(VectorOfTiles[playerPositionInMap].TypeID] == my_enums::UNWALKABLE)

{ // Deny movement
}
else

{ // Relocate

}

}

The issue is that i want to set types for enum "Tile_Type" something like this

grass = WALKABLE;
water = UNWALKABLE;
stone = UNWALKABLE;

Any ideas how to achieve something similar to this?

 

 

0

Share this post


Link to post
Share on other sites

Encode the information in an array and look it up from there.

enum {
    grass,
    tallGrass,
    water,
    stone
};
 
struct tile_information_t {
    bool walkable;    // is the tile walkable?
    bool swimable;    // is the tile swimable?
    int cost;    // if the tile is walk/swimable, how much movement does it cost to move
    // add as much information about the tiles you need here
} const tile_information[] = {
    {true, false, 1},  // grass is walkable and it costs one movement
    {true, false, 2},  // tall grass is walkable, but it costs two movement to wade through the tall grass
    {false, true, 1}, // water is swimable and it costs one movement
    {false, false, 0} // stone is not movable at all
};

Then look up the tile information based on the tile.

tile_information_t const *t = tile_information[tileID]
 
if(t->walkable) {
    // tile with id tileID is walkable, and it costs t->cost to move
}
 
if(hasSwimmingGear && t->swimable) {
    // tile with id tileID is swimable, and it costs t->cost to swim
}

While I made the tile information hard coded, you can easily parse it from a file so you can change it on the fly without recompiling.

What you just provided is flexible and so good thanks!

 

EDIT::
Aniway, this like is invalid

tile_information_t const *t = tile_information[tileID]
Edited by BaneTrapper
0

Share this post


Link to post
Share on other sites
public struct TileInfo
{
	public bool walkable;
        public bool destructable;
	
	 public static TileInfo Grass()
        {
            return new TileInfo ()
            {
                walkable = true,
                destructable = false;
            }
         }

	 public static TileInfo Water()
        {
            return new TileInfo ()
            {
                walkable = false,
                destructable = false;
            }
         }
}

 

Then, where I want info for a tile, I have a TileInfo variable in a generic tile class, which I set like:

 

TileInfo tileInfo = TileInfo.Grass();

 

 

This makes it very easy and decouples tile meta data into nice segments which you could possibly parse from a file.

 

EDIT: To then do the actual check with player is trivial, since all instances of Tile will have a set TileInfo data, we can just use:

 

if(tileInfo.walkable)

{

//allow player movement

}

 

etc.

Edited by dAND3h
0

Share this post


Link to post
Share on other sites


Aniway, this like is invalid
tile_information_t const *t = tile_information[tileID]

 

I think references would be nice here.

 

const tile_information_t& t = tile_information[tileID];  

 

and then use t.swimable etc

2

Share this post


Link to post
Share on other sites

 


Aniway, this like is invalid
tile_information_t const *t = tile_information[tileID]
 

 

I think references would be nice here.

 

const tile_information_t& t = tile_information[tileID];  

 

and then use t.swimable etc

 

Not only would a reference be nice, but a pointer is plain wrong in that particular case: tile_information + tileID would be suitable for a pointer though :)

 

Joke aside, yes, my post was more for information and not a particular implementation with tested and verified code and I totally agree with using references instead.

0

Share this post


Link to post
Share on other sites

You can also add some data to the tile object itself, if theres something you need to change a lot without changing the type. Some things you can implement using either separate types or data in the tile itself.

 

For example, you could have either of:

-Types OpenDoor and ClosedDoor

or

-Tile {TileType type; bool collides;}

 

Now, what if you not only had a "Door" type, but also a "Deth lazor" (active/inactive) type? you dont want to add separate bools because both only use one.

In this particular situation, you could use an union

union{

bool collides; //if door

bool activated; //if deth lazor

}; //i dont know if this is valid syntax for unions :P

 

So, eventually you will have something like:

 

union{

DoorData door; //contains bool for open/closed for example

DethLazorData lazor; //bool for activated, int for "energy left"

GrassData grass; //int for growth

BasicData basic; //for the most basic blocks like dirt, stone... i guess.

TeleportData tele; //x,y coordinates of destination

ChestData chest; //could contain an ID to point to an externally stored inventory object

}

 

The size of your tile type will be dictated by the biggest of the above. For this reason, when adding a new type of tile, you want to decide wether to store its data externally or within the tile. For example in the above, if the ChestData class contained a big array of objects inside it, every single tile would be that big and that wouldnt be nice. So you instead store it externally and just point to it.

 

The above seems like a nice solution. You store the tile type, followed by a section for type specific data (the union) which is big enough to work with all the types.

0

Share this post


Link to post
Share on other sites

Incase someone googles this page i am posting results how ive done it:

// HEADER
class Tile
{
public:
    Tile();
    Tile(int id);
    sf::Uint8 ID;
};

enum TileType
{
    grassLT = 0,
    grassT = 1,
    grassRT = 2,
    grassLB = 40,
    grassB = 41,
    grassRB = 42,
    SandLT = 3
    
};

class TileInfo
{
public:
    TileInfo(int infoID);
    bool walkable;
    bool swimable;
};
// CPP
Tile::Tile()
{
    ID = 0;
}

Tile::Tile(int id)
{
    ID = id;
}


TileInfo::TileInfo(int infoID)
{
    switch(infoID)
    {
    case grassLT:
        walkable = true;
        swimable = false;
        break;
    case grassT:
        walkable = true;
        swimable = false;
        break;
    case grassRT:
        walkable = true;
        swimable = false;
        break;
    case grassLB:
        walkable = true;
        swimable = false;
        break;
    case grassB:
        walkable = true;
        swimable = false;
        break;
    case grassRB:
        walkable = true;
        swimable = false;
        break;
    case SandLT:
        walkable = true;
        swimable = false;
        break;

    default:
        walkable = false;
        swimable = false;
        break;
    }
}
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0