SpriteCache class

Hi,

I got a design issue with my SpriteCache class.
It has a map to store Sprites. Sprites is the top object where all my sprites are extended from.

private HashMap<String, Sprite> spriteCache;

and my method to retrieve or when none there add the sprite to the spriteCache:

public Sprite getSprite(String name) {
if (spriteCache.containsKey(name)) {
return spriteCache.get(name);
} else {
logger.fine(name + " " + ERR_NOT_FOUND + " in spritesCache Making new one");
Sprite sprite = new Sprite(resources.getAnim(name), new Pt(0,0));
spriteCache.put(name, sprite);
return sprite;
}
}

When I call this Function to create a Cursor Sprite for example I get a Sprite and a classCast exception
Sprite curs = spriteCache.getSprite(“Cursor”);
Cursor cursor = (Cursor) curs;

What is the best way to work around this problem?

Should I add Specific Sprites like Cursor to the cache before calling spriteCache.get
Should I insert a parameter of the sprite to be created?
something like spriteCache.getSprite(“Cursor”,curs.class)

This is my design:
http://www.kwbbz.be/Downloads/Main.jpg

Depends on what you want, but I would suggest to populate the cache with correctly typed instances at the game start. If you are really keen on using lazy initialization you can implement your spriteCache.getSprite(“Cursor”,Cursor.class) strategy using reflection, but keep in mind that you might get into a quandary when trying something like


spriteCache.getSprite("Leo",RifleMan.class) // This "Leo" is created as a RifleMan, which is not a Tank
// (some other code)
spriteCache.getSprite("Leo",Tank.class) // This call will return an already created RifleMan, but not a new Tank

This can be considered as coding error, but you will at least have to check the type of the already cached Sprite and handle the type mismatch in some way.

Also I think you are overdesigning your Sprite hierarchie. A Sprite is a Sprite not much more, but it seems you are mixing sprites with entities. A Sprite is something graphics related and can easily be retrieved from a cache, an Entity is something in your game world that has different attributes like armor, damage status, behaviour, sound and visual appearance (the Sprite). Management and creation of Entities is quite different than the same for Sprites.

an Entity is something in your game world that has different attributes

Thanks that realy helped!
Your absolutely right ;D Every Entity has a Sprite. So I create Sprites inside the Entity
wich then can be updated(entity) and drawen(sprite) and there can be entitys with different code but the same Sprite that’s why we cache them afterall!
I understand it a bit better now :slight_smile:
Also removed the staticSprite everything on the screen is a sprite now.

Now i’m having another design problem lol.

I need the resources from the ResourceManager, and the spriteCache into the Entity class…
and I cannot make it a static class function because it containts another function
So I pass it from game, to playerManager, to Player(as on the uml).
But this realy looks bad. and directly is also not an option I think, it’s not the way my game is setup… How would you get resources into the entity class?

I create these objects in my top class, and I can’t create a new one because I only want to have 1 object.

This is a typical situation for a Singleton http://en.wikipedia.org/wiki/Singleton_pattern. Some people consider Singletons to be a lazy man’s excuse for bad OO design, but I think otherwise (at least with things like caches, resource loaders etc.).

Bullcrap. A singleton would be a horrible solution to this - theres absolutely no reason why you should enforce a single cache or provide global access to it, other than introduce awkward bugs and behaviour for yourself in the long run.

stef569: Passing the resources and cache to each entity is perfectly reasonable and sensible. Your entities depend on them, so all you’re doing is making the dependancy explicit instead of hiding it behind a global or similar.

Alternatively, have your game state (or whatever hold the entities) hold onto the resources and cache, and instead pass itself to the entity. Then provide methods in the game state which just delagate straight through as needed. Or provider a higher level interface (eg. GameState.makeSprite(String imageName)) which deals with the actual initialisation of sprites and their images.

As I said, there are strong opinions on this matter. I for myself find it passable to use a Singleton for resource management, others don’t. I respect this opinion, even if I find a “Bullcrap” response slightly offensive :P.Some arguments agains Singletons can be found in the “Just Create One” article that can be found http://butunclebob.com/ArticleS.UncleBob.SingletonVsJustCreateOne.

Suggesting being lazy and using a singleton when someone already has a working and superiour solution already implemented is just plain silly. :stuck_out_tongue: