Hey, everybody!
I’m making a little headway into my game’s engine code. However, I’m running into a roadblock (of sorts, anyhow).
Currently, I’m working on implementing the model by which entities in the game expend functionality. I have an Entity class which extends to classes like Sprite, Creature, Player (in that order), ItemEntity (which models items dropped in the game world with their own collisions to detect if the player is near), WorldObject (which is anything not bound to my game’s tile map which can basically have its own functionality but is also well-suited to complex decoration), and so forth.
There is a slight problem I’m having, however. The issue is that I feel in order to access the functionality I’m trying to give my Entities so that, say, Entity A can spawn an ItemEntity at coordinates X and Y in the game world, I have to access several layers beyond where that Entity lies (for example, making calls to access the GameManager and ResourceManager to access the current TileMap in use, etc.) to get to that functionality in the first place. My entities have what I have deemed to be called EntityActions, which are basically actions entities can execute at any given time. Say I want to spawn an item when an enemy dies… upon that entity’s death, it will call the perform() function of that corresponding EntityAction, which would look something similar to
// called in the main "GameManager" class
entity.die();
// this code exists in the entity's file
public void die()
{
// sift through the entity's actions to get a spawn action
for (int i = 0; i < actions.size(); i++)
{
if (actions.get(i) == Action.SPAWN)
{
action.get(i).perform();
}
}
}
// the perform function is defined in any entity subclass and is basically a big switch statement
public void perform()
{
switch (action)
{
case SPAWN:
// handle spawning code here for items
break;
}
}
I guess I could theoretically get this whole design working with parameters that point to the game’s GameManager and TileMap classes and whatnot, since those are what will be in charge of managing spawned items, but this just seems entirely efficient, and I feel like my design is fundamentally flawed; in fact, I’m sure it is. I was wondering if you guys could offer me some suggestions on where my design might be tweaked so that it makes more sense or is even more robust and generic on account of it! Thank you guys so much for your time!
Best regards,
Colton