December 2018
Intermediate to advanced
196 pages
4h 48m
English
The space pirates are vicious, diving in and firing randomly. Our player needs to be alert and quick to survive. The programming for the enemy needs to handle spawning, flying about, and firing. We can borrow some concepts from player.ts, but most of them will require new code. Open up enemy.ts and create a new function updateEnemyState with the same signature as updatePlayerState. The first thing in this function is to determine whether we should spawn a new enemy ship:
| | // Enemy appears, moves down 1/3 of the map, then turns |
| | // Spawn a new enemy ship if needed |
| | if (gameState.enemy.x <= -config.enemy.width || |
| | gameState.enemy.x > config.canvas.width || |
| | !gameState.enemy.alive |
| | ) { ... |
Read now
Unlock full access