2.2.4 Cycle 4 - Creating Enemies

Design

Objectives

In this cycle I aim to be able to add the enemy ships to the top of the screen and for them to move downwards, and whilst doing this they will spawn in patterns occasionally, with other ships around them.

Using patterns will make the game more immersive as it adds another layer to the game where the player may recognise patterns and use this to their advantage to play more strategically.

Usability Features

Key Variables

Variable Name
Use

switchXspawn

This is the indicator that tells which X spawn to use.

switchToNewPattern

This indicates which pattern to use ( 0 , 10, 20, 30).

enemyXSpawn

These are different base places to use on the screen.

Enemy_X

This is the enemies x coordinate from enemyXSpawn.

Pseudocode

procedure create
...
    add enemies to new group
    enable enemies group
    add physics to enemies group
    create 500 enemies within group called enemyShip
    set the scale of each enemyShip
    run deployEnemyShips procedure
    
    procedure deployEnemyShips
        add patterns to enemies group
        spawn at different spots specified by pattern
    end procedure
...
end procedure
    

Development

Outcome

Game.js
create(){
...
  enemies = game.add.group();
  enemies.enableBody = true;
  game.physics.arcade.enable(enemies, Phaser.Physics.ARCADE);
  enemies.createMultiple(500, 'enemyShip');
  enemies.scale.set(0.5);
  enemies.setAll('anchor.x', 0.5);
  enemies.setAll('anchor.y', 0.5);

  enemies.setAll('outOfBoundsKill', true);
  enemies.setAll('checkWorldBounds', true);
  enemies.setAll('angle', 180);

  deployEnemyShips();
...
}
...
// Function deployEnemyShips adds enemies to the game space
let switchXSpawn = 0;
let switchToNewPattern = 0;
let enemyXSpawn = 200;
let ENEMY_X = 0;

function deployEnemyShips() {
  let ENEMY_SPEED = 300;

  let enemy = enemies.getFirstExists(false);
  if (enemy) {
    enemy.reset(enemyXSpawn, 0);
    enemy.body.velocity.x = ENEMY_X;
    enemy.body.velocity.y = ENEMY_SPEED;
    enemy.body.drag.x = 0;

    if (switchXSpawn === 0 && switchToNewPattern < 10) {
      game.time.events.add(300, function() {
        ENEMY_X = 0;
        enemyXSpawn = 200;
        switchXSpawn = 1;
        switchToNewPattern++;

        deployEnemyShips();
      });
    } else if (switchXSpawn === 1 && switchToNewPattern < 10) {
      game.time.events.add(300, function() {
        ENEMY_X = 0;
        enemyXSpawn = 100;
        switchXSpawn = 2;
        switchToNewPattern++;

        deployEnemyShips();
      });
    } else if (switchXSpawn === 2 && switchToNewPattern < 10) {
      game.time.events.add(300, function() {
        ENEMY_X = 0;
        enemyXSpawn = 300;
        switchXSpawn = 0;
        switchToNewPattern++;

        deployEnemyShips();
      });
    } else if (switchToNewPattern === 10) {
      game.time.events.add(250, function() {
        enemyXSpawn = 380;
        ENEMY_X = -80;

        game.time.events.add(2000, function() {
          switchToNewPattern = 20;
        });
        deployEnemyShips();
      });
    } else if (switchToNewPattern === 20) {
      game.time.events.add(250, function() {
        enemyXSpawn = 0;
        ENEMY_X = 80;

        game.time.events.add(2000, function() {
          switchToNewPattern = 30;
        });
        deployEnemyShips();
      });
    }
  }
}
...

Challenges

It was very challenging to work out the logic behind the spawning of the enemies, and how to implement different patterns of the different waves of enemies. This was challenging as I didn't just want to use a json file to set where each enemy would be, I wanted it to be arithmetic based, so that it would adapt to different resolutions and widths of the game, which a json wouldn't be able to do.

From this I thought that the best way to do this would be to have a set of different base places the enemies would spawn from, and then I would add or subtract to that to create patterns.

This method worked suprisingly well when I tested it and I'm really proud of how it worked, as it took a long time to create and think up of.

Testing

During testing I found that the pattern worked really well after many hours of trying different tweaks to the method.

Tests

Test
Instructions
What I expect
What actually happens
Pass/Fail

Spawn enemy

Simply begin the game.

The enemies should begin spawning in, and there will be a range of basic patterns that you will see as the game progresses

The enemy doesn't appear

Fail

Spawn enemy

Simply begin the game.

The enemies should begin spawning in, and there will be a range of basic patterns that you will see as the game progresses

As expected

Pass

Evidence

Enemies spawning in, with one of the patterns beginning to form at the top of the screen.

What Next?

  • Next I'm going to implement sounds into the game to make it more immersive and realistic

  • Sound will also allow the player to know when they've died and when they shoot

Last updated