So, I have a player class with basic actions. During the gameplay he unlocks new abilities. I don't know how to store new actions within the player class. To be precise - I know how, but my method is too messy and i want to clean up code. Also, I want to add to the enemies some of those abilities. In other words, every game entity(expect walls, perhaps :D) should have a collection to which i can add methods that extend entity functionality. How do i make this?
-
3\$\begingroup\$ It could help if you post the code you already have \$\endgroup\$– ThomasCommented Jan 27, 2013 at 15:21
-
\$\begingroup\$ @Thomas has a point, even if it's just some basic pseudo-code so we have a better idea of what path you're on. \$\endgroup\$– user24851Commented Jan 27, 2013 at 16:42
-
\$\begingroup\$ @Thomas well, i posted it below :D \$\endgroup\$– edwingCommented Jan 27, 2013 at 19:08
2 Answers
I recommend you approach used in the Artemis Entity System Framework. You can think about your player, enemy, wall etc. like about container (entity) for components. Then you precise actions (systems) for entities with particular set of components.
For example you can create system that allows player to fly when the X key is pressed (in-game condition) and when entity represented player has component flying (in-engine condition). Assume that player has ability for fly only if he has some wings in inventory. If this happen just add component flying to player entity and this will unlock flying for player.
-
\$\begingroup\$ This is my first game and I want to make everything(within reasonable limits) by myself, but thanks! \$\endgroup\$– edwingCommented Jan 27, 2013 at 18:38
-
\$\begingroup\$ @edwing, I think it would be a good idea to familiarize yourself with the concept of an entity system framework like Artemis. This way you wouldn't have to discover things the hard (and long) way. \$\endgroup\$ Commented Jun 22, 2017 at 10:14
I've received a great reply on StackOverflow This problem can be solved with dictionary and lambda functions
public Dictionary<string,Action> Powers = new Dictionary<string,Action>();
And add new powers to this dictionary:
Powers["Double Jump"] = () => { //jump code };
Then just call whenever you want Powers["Double Jump"]();