Jump to content
  • Advertisement
Sign in to follow this  
Kris_A

Sprite animation/OO annoyances

This topic is 3837 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

Hi, yet another question of design, one I can't seem to get my head around! I've got a simple sprite animation system consisting of: SpriteAnim - An animation SpriteAnimSet - A collection of animations referenced by ID (int) Sprite - An instance of a sprite When a Sprite is created it takes a SpriteAnimSet* and stores this internally. Calls to Sprite->SetAnim() take an int parameter, which corresponds to the ID of the animation in that set. i.e:
const int AnimStopped = 1, AnimShooting = 2;

SpriteAnimSet * set = new SpriteAnimSet(someImage);
set->AddAnim(/* co-ordinates etc */, AnimStopped);
set->AddAnim(/* co-ordinates etc */, AnimShooting);

Sprite * sprite = new Sprite(set);
sprite->SetAnim(AnimStopped);
sprite->Draw(surface);


This system has worked fine so far, but there's a bit of problem. When an object is created, its Sprite must be created too. This needs a pointer to the SpriteAnimSet in order to get animation details (number of frames, speed, etc). The problem is, my SpriteAnimSets are stored in the Game State instance, so they aren't immediately visible to all objects (I want to leave them in there if possible, and I don't relaly want to make them global).
// This is fine because it's in GameState...
void GameState::DoSomething() {
	Bullet * bullet = new Bullet(new Sprite(bulletAnimSet)); 
}

// But what about this?
void Enemy::Shoot() {
	Bullet * = new Bullet(new Sprite(???)); 
}


I want to avoid having to pass lots of SpriteAnimSet instances to functions, because the object in question could be using any one of tens or hundreds of animation sets. It seems that this would be the only way to do it with my current system, so I'm wondering if the system itself needs changing. Any advice/opinions would be appreciated! Thanks Kris ps: I realise a simpler hard-coded system would probably fix this problem, but I need to be able to load animations of any length/description dynamically, because it's going to be pretty user-editable.

Share this post


Link to post
Share on other sites
Advertisement
Give your anim-sets to an entity factory that exposes functions to create the objects you want:

void GameState::DoSomething() {
Bullet * bullet = entityFactory.createBullet();
}

void Enemy::Shoot() {
Bullet * bullet = entityFactory.createBullet();
}

You could design the factory's interface in several ways, for example instead of createBullet and createRocket you might have createProjectile("Bullet") and createProjectile("Rocket"). It depends, as always, on the needs of your system.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kris_A
I want to avoid having to pass lots of SpriteAnimSet instances to functions, because the object in question could be using any one of tens or hundreds of animation sets.


Can it really?

What sorts of animation-sets could a Bullet have? I think it really only has the one. You can give that set every animation that Bullet ever uses, and then if a particular Bullet instance doesn't need one of them, it just doesn't use it.

Then, that SpriteAnimSet (the bulletAnimSet in your code) can become, for example, a static member of the Bullet class, and you don't even need to point to it within your Bullet instances.

If you do need that flexibility, then the question boils down to "how does the enemy know what kinds of animations the bullet should have?" In that case, you can have it pass some kind of handle to the Bullet constructor (which then goes to look it up somewhere), or you can pass the SpriteAnimSet into Enemy::shoot() (but that seems quite strange), or (the usual solution) you can have the Enemy store a pointer to the necessary SpriteAnimSet upon its construction (by having the GameState pass it to the Enemy constructor or something like that). I mean, the Enemy isn't going to decide from several options for the Bullet's animation, is it? (If it is, maybe you actually just need derived classes of Bullet per animation type, or something.)

In the really really complicated cases, we use the Factory pattern, as described briefly by dmatter.

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.

GameDev.net 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!