Day 8: Let’s kill some enemies.
Posted by Plaidman on 5th April 2010
Now the good stuff. Let’s make some enemies! This is actually really easy with polymorphism, or inheritance of object properties.
Monsters will extend the Character class to inherit any Character stats and routines (most importantly the movement functions), similar to the Player extending the Character class. Also similar to the Player class, the Monster class will add a few extra things that are specific to monsters (like the AI routine). Really all we need to do is to define the sprites for this Monster and give it an artificial intelligence. That is the beauty of polymorphism.
We can do some fun stuff with the AI routine. For instance, we could have the monster pursue the character, or look for other enemies of its own type and group up before attacking, or hide and ambush the player solo. For now, let’s give our simple monster a routine that if Red is within two spaces of him (using our Coord’s distance function) he’ll chase Red and attack, otherwise wander randomly. Again, since we have everything set up in Coord, we just need to hook it all together and we’re done. Other monsters will have different attack strategies, but I won’t reveal them just yet.
Now let’s update our Player object. Since we have enemies, we want this character to be able to kill them and level up. The Player is inheriting the movement routine from the Character class, but we want to override it so we can check to see if we’re close to a monster, and if so, attack it. Here we only need to define our attack routine to define how much damage we do, and we’re set.
Back to the main game handler, we’ll keep track of a list of monsters, so we can cycle through them to draw or do the AI. We should also give the game an objective: kill as many monsters as you can before they kill you, and spawn them each time the map is cleared. Sounds fun doesn’t it? Yes, it does.
Some other updates since last time:
- Added a HUD to display Red’s level, gold, and health.
- Optimized the game’s state handler to kill some potential memory issues down the line.
- Added a save-game for when we exit the game. Haven’t created the load-game yet, so this is useless for now, but it will be useful later.
- Moved the animation frame tracker to all Character classes (Monsters and Player) so they can keep track of their individual animations.
- Updated the draw routine to make all monsters walk between tiles (instead of teleport, as Red used to do).
Current demo: Enemies have a health stat, and they fight back if they aren’t killed quickly. The game ends when Red loses all his health.
Posted in Android | No Comments »