Thursday, 19 February 2015

Procedural Generation : Part Six : Release The Ninjas!

Last time we dealt with browser issues, this time we will put some characters into the dungeon map and let them run around.

The Ninjas will be similar to the player character. From a procedural generation point of view, we have to be careful where we initially place them. We don't want them stuck in a wall or sharing a space with another character! We also need something to tell the Ninja to move.

Placing the Ninja takes place after we have built the map. Each block has a member variable to track if something/someone is standing on that spot. This is separate from the list of items and is used only as a starting position as our characters will soon be mobile.

class Block {

  int base = VOID;
  int character = VOID;
  List items = null;


    if (items==null) items = new List();

We can reuse the same class (RPGCharacter) for the Player for the Ninja. Yay - code reuse!

Movement of the Ninja is a simple affair. In the logic update for the game in main.dart (triggered by a recurring timer), each Ninja is moved 1 block in a random direction. In a real game, we would perhaps move them towards the player if they are close enough.


  Timer logup = new Timer.periodic(new Duration(milliseconds: 1000), gameLogicUpdate);


void gameLogicUpdate(e) {
  if (mymap != null) mymap.updatePlayer(p1, playSnd);

  ninjas.forEach((n) {
    int m = RND(4);
    if (m == 1) n.moveU();
    if (m == 2) n.moveD();
    if (m == 3) n.moveL();
    if (m == 4) n.moveR();

I should repeat an important point from a previous post in this series, that if this was to be developed into a full game, it really should be split off into several packages to improve the structure. Save some pain! :-)

Code is available on Github and a live demo is available here. Use arrow keys to move!

The next part of this series we will take a break from dungeons and see if we can generate and entire world!