Rob Colbert
2 years ago
17 changed files with 163 additions and 13 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,99 @@ |
|||
// lib/game-enemy-formation.js
|
|||
// Copyright (C) 2022 Rob Colbert @[email protected]
|
|||
// License: Apache-2.0
|
|||
|
|||
'use strict'; |
|||
|
|||
import NiceVector2d from '/dtp-nice-game/nice-vector-2d.js'; |
|||
|
|||
/** |
|||
* A formation is a collection of enemies with tiers, and enemies are |
|||
* added to the tiers. They then render as a "formation" in the view, |
|||
* and can "pop out" of formation to execute a "run" then arrive back to |
|||
* the formation. |
|||
*/ |
|||
export default class GameEnemyFormation { |
|||
|
|||
constructor (game, position) { |
|||
this.game = game; |
|||
this.position = position; |
|||
this.moveSpeed = 2; |
|||
this.tiers = [ ]; |
|||
} |
|||
|
|||
/** |
|||
* |
|||
* @param {*} tierHeight |
|||
* @returns |
|||
*/ |
|||
addTier (tierHeight = 64) { |
|||
const idx = this.tiers.length; |
|||
this.tiers.push({ height: tierHeight, enemies: [ ] }); |
|||
return idx; |
|||
} |
|||
|
|||
/** |
|||
* Adds an enemy object to the specified tier at the specified position |
|||
* offset. This is where the enemy will reside while in formation. |
|||
* @param {Number} tierIndex the index of the tier to which the enemy should be |
|||
* added |
|||
* @param {NiceSprite} enemy The enemy to be added to the specified tier |
|||
* @param {NiceVector2d} offset The positional offset from the formation's origin at |
|||
* which the enemy should reside while in formation |
|||
* @returns the index of the enemy added within the tier |
|||
*/ |
|||
addEnemyToTier (tierIndex, enemy, offset) { |
|||
const idx = this.tiers[tierIndex].enemies.length; |
|||
this.tiers[tierIndex].enemies.push({ |
|||
enemy, offset, |
|||
isInFormation: true, |
|||
}); |
|||
return idx; |
|||
} |
|||
|
|||
update ( ) { |
|||
const MARGIN_RIGHT = 480 + 200; |
|||
const MARGIN_LEFT = 480 - 200; |
|||
|
|||
/* |
|||
* Update the position of the formation itself |
|||
*/ |
|||
this.position.x += this.moveSpeed; |
|||
if (this.position.x > MARGIN_RIGHT) { |
|||
this.moveSpeed = -this.moveSpeed; |
|||
this.position.x = MARGIN_RIGHT; |
|||
} |
|||
if (this.position.x < MARGIN_LEFT) { |
|||
this.moveSpeed = -this.moveSpeed; |
|||
this.position.x = MARGIN_LEFT; |
|||
} |
|||
|
|||
/* |
|||
* Iterate every enemy in the formation, and update their position to where |
|||
* they should be if currently in-formation. |
|||
*/ |
|||
for (const tier of this.tiers) { |
|||
for (const enemy of tier) { |
|||
if (!enemy.isInFormation) { |
|||
continue; |
|||
} |
|||
enemy.position.x = this.position.x + enemy.position.x; |
|||
enemy.position.y = this.position.y + enemy.position.y; |
|||
} |
|||
} |
|||
|
|||
/* |
|||
* Randomly or as scheduled, execute an "attack run" by selecting some |
|||
* enemies from the required tiers (if availble), and then starting the |
|||
* attack behavior. |
|||
*/ |
|||
} |
|||
|
|||
render (ctx) { |
|||
for (const tier of this.tiers) { |
|||
for (const enemy of tier) { |
|||
enemy.render(ctx); |
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue