Page 1 of 1
Simple question
Posted: Sat Mar 20, 2010 11:58 am
by Zer0XoL
is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
Re: Simple question
Posted: Sat Mar 20, 2010 2:41 pm
by MrDeathNote
Zer0XoL wrote:is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
I'm not really sure thats the best way to do it. A lot of ppl make a generic sprite class and then make every object that will appear on screen inherit from it but i don't think it's a good idea to make every object inherit from it. For example there are alot of things that may be implemented behind the scenes and that will never appear on screen so it wouldn't be necessary to have such direct access to them(for collision detection etc.), too much access can be a bad thing, that's why we create private members/functions. Saying that i could be completely wrong this is just an opinion
.
Re: Simple question
Posted: Sat Mar 20, 2010 3:24 pm
by XianForce
MrDeathNote wrote:Zer0XoL wrote:is it good if i make every object and things inherit from the class entity( wich i made)
and then make a vector of entity pointers and have all my game objects in that vector, so it would be easier to check collision and things?
what is good and what i bad about this and can it be done in a better way? :P
thx
I'm not really sure thats the best way to do it. A lot of ppl make a generic sprite class and then make every object that will appear on screen inherit from it but i don't think it's a good idea to make every object inherit from it. For example there are alot of things that may be implemented behind the scenes and that will never appear on screen so it wouldn't be necessary to have such direct access to them(for collision detection etc.), too much access can be a bad thing, that's why we create private members/functions. Saying that i could be completely wrong this is just an opinion
.
Well, you probably don't want EVERYTHING to inherit from an Entity class.
For me, I have an Entity class, and an Entity is anything that has the ability to move (aka has a velocity vector).
Then I create an Entity Manager class which holds usually a map of integers and entity pointers. The integer is the ID number of the entity. Each entity has a unique ID, so they can be referenced by that.
Re: Simple question
Posted: Sat Mar 20, 2010 3:25 pm
by XianForce
EDIT: crap, double post >.>
Re: Simple question
Posted: Thu Mar 25, 2010 11:55 am
by xiphirx
Did you get this idea from quake?
Like others have said, I would only use it for stuff that is active (moving etc).
a sample class would be
Code: Select all
/*
UNTESTED CODE, PROBABLY REALLY CRAPPY STUFF D:
*/
class Entity
{
public:
Entity(SDL_Surface* image);
Entity();
~Entity();
void move(float nvx, float nvy);
private:
int x, y;
float vx, vy;
// Assuming you use SDL
SDL_Surface* sprite;
}
Entity(SDL_Surface* image) // load with sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = image;
}
Entity() // Blank sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = NULL;
}
~Entity()
{
SDL_FreeSurface(sprite);
}
void Entity::move(float nvx, float nvy)
{
vx = nvx;
vy = nvy;
x += int(vx);
y += int(vy);
vx = vy = 0.0;
}
Re: Simple question
Posted: Sat Mar 27, 2010 10:19 am
by Zer0XoL
xiphirx wrote:Did you get this idea from quake?
Like others have said, I would only use it for stuff that is active (moving etc).
a sample class would be
Code: Select all
/*
UNTESTED CODE, PROBABLY REALLY CRAPPY STUFF D:
*/
class Entity
{
public:
Entity(SDL_Surface* image);
Entity();
~Entity();
void move(float nvx, float nvy);
private:
int x, y;
float vx, vy;
// Assuming you use SDL
SDL_Surface* sprite;
}
Entity(SDL_Surface* image) // load with sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = image;
}
Entity() // Blank sprite
{
x = y = 0;
vx = vy = 0.0;
sprite = NULL;
}
~Entity()
{
SDL_FreeSurface(sprite);
}
void Entity::move(float nvx, float nvy)
{
vx = nvx;
vy = nvy;
x += int(vx);
y += int(vy);
vx = vy = 0.0;
}
I did not get the idea from quake, but that class you made is almost the same as the one im using :D
I thought that entities would have physics and abillities like chests containg items and suff :P and tiles being solid or non solid, does anyone know a good way of making some tiles animated?
Re: Simple question
Posted: Sat Mar 27, 2010 1:37 pm
by xiphirx
Yes, you might want to look into spritesheets and SDL_Rect s.
Re: Simple question
Posted: Sat Mar 27, 2010 4:02 pm
by Zer0XoL
xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmm
Re: Simple question
Posted: Sat Mar 27, 2010 6:55 pm
by xiphirx
Zer0XoL wrote:xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmm
Well, if your tile is a class that inherits properties from entity, then you can add a SDL_Rect to hold clips if the tile's image is wider than a normal tile.
So, in your constructor, you can have something like
Code: Select all
//clips is a SDL_Rect array declared in the tile class assuming it inherits things from entity
if (sprite->w > YOUR_NORMAL_TILE_DIMENSION)
{
animated = true; //bool that can tell your rendering process to render the animation
for (int i = 0; i < sprite->w/2; i++)
{
clips[i].x = i*YOUR_NORMAL_TILE_DIMENSION;
clips[i].y = 0;
clips[i].w = clips[i].h = YOUR_NORMAL_TILE_DIMENSION;
}
}
Note: that code is not tested, so do not go and copypasta it.
Including that in your tile constructor should give you a tile, with clips of the animation if there is one.
Re: Simple question
Posted: Sat Mar 27, 2010 9:31 pm
by MrDeathNote
Zer0XoL wrote:xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmm
You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.
Re: Simple question
Posted: Sun Mar 28, 2010 7:13 am
by Zer0XoL
MrDeathNote wrote:Zer0XoL wrote:xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmm
You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.
Thanks for the idea :D
I think im going to inherit the original tile class and have it contain a value of how many tiles the animation is and a value for the speed of animating it :D
Re: Simple question
Posted: Sun Mar 28, 2010 7:20 am
by MrDeathNote
Zer0XoL wrote:MrDeathNote wrote:Zer0XoL wrote:xiphirx wrote:Yes, you might want to look into spritesheets and SDL_Rect s.
Yes, ofc i know what that is and how to use it, its just i ask how to keep it somewhat structured as tiles and still have it animated.. hmm
You would just create another class eg. animatedTile and have code very similar to your sprite animation. The tile itself would need to be a spritesheet so it mite actually be better to create that tile as an animated sprite. But really it's up to you there are numerous ways to deal with this, i'd prob create a new class for animated tiles but thats just my opinion.
Thanks for the idea :D
I think im going to inherit the original tile class and have it contain a value of how many tiles the animation is and a value for the speed of animating it :D
Souunds like a good plan