| 1 | import sprites from '../sprites.js' |
| 2 | import Actor from './Actor.js' |
| 3 | |
| 4 | export default class Bird extends Actor { |
| 5 | static maxBirdHeight = Math.max(sprites.birdUp.h, sprites.birdDown.h) / 2 |
| 6 | |
| 7 | // pixels that are added/removed to `y` when switching between wings up and wings down |
| 8 | static wingSpriteYShift = 6 |
| 9 | |
| 10 | constructor(imageData) { |
| 11 | super(imageData) |
| 12 | this.wingFrames = 0 |
| 13 | this.wingDirection = 'Up' |
| 14 | this.sprite = `bird${this.wingDirection}` |
| 15 | // these are dynamically set by the game |
| 16 | this.x = null |
| 17 | this.y = null |
| 18 | this.speed = null |
| 19 | this.wingsRate = null |
| 20 | } |
| 21 | |
| 22 | nextFrame() { |
| 23 | this.x -= this.speed |
| 24 | this.determineSprite() |
| 25 | } |
| 26 | |
| 27 | determineSprite() { |
| 28 | const oldHeight = this.height |
| 29 | |
| 30 | if (this.wingFrames >= this.wingsRate) { |
| 31 | this.wingDirection = this.wingDirection === 'Up' ? 'Down' : 'Up' |
| 32 | this.wingFrames = 0 |
| 33 | } |
| 34 | |
| 35 | this.sprite = `bird${this.wingDirection}` |
| 36 | this.wingFrames++ |
| 37 | |
| 38 | // if we're switching sprites, y needs to be |
| 39 | // updated for the height difference |
| 40 | if (this.height !== oldHeight) { |
| 41 | let adjustment = Bird.wingSpriteYShift |
| 42 | if (this.wingDirection === 'Up') { |
| 43 | adjustment *= -1 |
| 44 | } |
| 45 | |
| 46 | this.y += adjustment |
| 47 | } |
| 48 | } |
| 49 | } |