| 1 | import Actor from './Actor.js' |
| 2 | |
| 3 | export default class Dino extends Actor { |
| 4 | constructor(imageData) { |
| 5 | super(imageData) |
| 6 | this.isDucking = false |
| 7 | this.legFrames = 0 |
| 8 | this.legShowing = 'Left' |
| 9 | this.sprite = `dino${this.legShowing}Leg` |
| 10 | this.vVelocity = null |
| 11 | this.baseY = 0 |
| 12 | this.relativeY = 0 |
| 13 | // these are dynamically set by the game |
| 14 | this.legsRate = null |
| 15 | this.lift = null |
| 16 | this.gravity = null |
| 17 | this.fastFallMultiplier = 4 |
| 18 | } |
| 19 | |
| 20 | get y() { |
| 21 | return this.baseY - this.height + this.relativeY |
| 22 | } |
| 23 | |
| 24 | set y(value) { |
| 25 | this.baseY = value |
| 26 | } |
| 27 | |
| 28 | reset() { |
| 29 | this.isDucking = false |
| 30 | this.legFrames = 0 |
| 31 | this.legShowing = 'Left' |
| 32 | this.sprite = `dino${this.legShowing}Leg` |
| 33 | this.vVelocity = null |
| 34 | this.relativeY = 0 |
| 35 | } |
| 36 | |
| 37 | jump() { |
| 38 | if (this.relativeY === 0) { |
| 39 | this.vVelocity = -this.lift |
| 40 | return true |
| 41 | } |
| 42 | return false |
| 43 | } |
| 44 | |
| 45 | duck(value) { |
| 46 | this.isDucking = Boolean(value) |
| 47 | } |
| 48 | |
| 49 | nextFrame() { |
| 50 | if (this.vVelocity !== null) { |
| 51 | // use gravity to gradually decrease vVelocity |
| 52 | const gravityForce = this.isDucking ? this.gravity * this.fastFallMultiplier : this.gravity |
| 53 | this.vVelocity += gravityForce |
| 54 | this.relativeY += this.vVelocity |
| 55 | } |
| 56 | |
| 57 | // stop falling once back down to the ground |
| 58 | if (this.relativeY > 0) { |
| 59 | this.vVelocity = null |
| 60 | this.relativeY = 0 |
| 61 | } |
| 62 | |
| 63 | this.determineSprite() |
| 64 | } |
| 65 | |
| 66 | determineSprite() { |
| 67 | if (this.relativeY < 0) { |
| 68 | // in the air stiff |
| 69 | this.sprite = 'dino' |
| 70 | } else { |
| 71 | // on the ground running |
| 72 | if (this.legFrames >= this.legsRate) { |
| 73 | this.legShowing = this.legShowing === 'Left' ? 'Right' : 'Left' |
| 74 | this.legFrames = 0 |
| 75 | } |
| 76 | |
| 77 | if (this.isDucking) { |
| 78 | this.sprite = `dinoDuck${this.legShowing}Leg` |
| 79 | } else { |
| 80 | this.sprite = `dino${this.legShowing}Leg` |
| 81 | } |
| 82 | |
| 83 | this.legFrames++ |
| 84 | } |
| 85 | } |
| 86 | } |