diff --git a/Engine/src/live/AnimalEntity.ts b/Engine/src/live/AnimalEntity.ts index 5baf73d..8be2779 100644 --- a/Engine/src/live/AnimalEntity.ts +++ b/Engine/src/live/AnimalEntity.ts @@ -25,6 +25,7 @@ export class AnimalEntity extends Entity { nextYell: number = 1000 dead: boolean = false shouldGib: boolean = false + actionCooldown: number = 0 constructor(def: AnimalDefinition) { super(`${def.name}.animal.stand.west.0`) @@ -73,6 +74,7 @@ export class AnimalEntity extends Entity { } } let shouldMove = false + let shouldAttack = false if (this.action) { // FIXME: Use physics. switch(this.action.type) { @@ -117,7 +119,7 @@ export class AnimalEntity extends Entity { shouldMove = true break case 'attack': - this.yell(1) + shouldAttack = true break } if (this.direction > 360) { @@ -127,30 +129,42 @@ export class AnimalEntity extends Entity { } } - if (shouldMove) { - this.stepSoundElapsed += delta - let r = this.direction * (Math.PI/180) - if (Math.abs(this.velocity[0]) < this.mode.maxSpeed) { - this.velocity[0] -= Math.cos(r) * this.mode.acceleration + this.actionCooldown += delta + if (this.actionCooldown >= 750 - 750 * this.mode.cdr) { + if (shouldAttack) { + this.attack() + this.actionCooldown = 0 + } else if (shouldMove) { + this.stepSoundElapsed += delta + let r = this.direction * (Math.PI/180) + if (Math.abs(this.velocity[0]) < this.mode.maxSpeed) { + this.velocity[0] -= Math.cos(r) * this.mode.acceleration + } + if (Math.abs(this.velocity[1]) < this.mode.maxSpeed) { + this.velocity[1] -= Math.sin(r) * this.mode.acceleration + } + let cardinal = this.getCardinal(this.direction) + this.sprite.setKey = 'run' + if (this.sprite.subsetKey !== cardinal || this.sprite.subsetKey !== 'run') { + this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${cardinal}.${this.sprite.frameIndex}`) + } + if (this.stepSoundElapsed >= 200*(this.isMonster?1.5:1)) { + let v = 1 + Math.floor(Math.random() * 4) + if (this.isMonster) { + playSound('action/footstep-big-v'+v, 0.5) + } else { + playSound('action/footstep-tiny-v'+v, 0.5) + } + this.stepSoundElapsed = 0 + } + this.sprite.animate = true + this.sprite.loop = true + } else { + this.sprite.animate = false + this.sprite.loop = true + this.sprite.setKey = 'stand' + this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${this.getCardinal(this.direction)}.0`) } - if (Math.abs(this.velocity[1]) < this.mode.maxSpeed) { - this.velocity[1] -= Math.sin(r) * this.mode.acceleration - } - let cardinal = this.getCardinal(this.direction) - this.sprite.setKey = 'run' - if (this.sprite.subsetKey !== cardinal || this.sprite.subsetKey !== 'run') { - this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${cardinal}.${this.sprite.frameIndex}`) - } - if (this.stepSoundElapsed >= 200) { - let v = 1 + Math.floor(Math.random() * 3) - playSound('action/footstep-tiny-v'+v, 0.5) - this.stepSoundElapsed = 0 - } - this.sprite.animate = true - } else { - this.sprite.animate = false - this.sprite.setKey = 'stand' - this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${this.getCardinal(this.direction)}.0`) } // @@ -263,11 +277,35 @@ export class AnimalEntity extends Entity { this.seenTarget = undefined } } + turn() { + this.isMonster = !this.isMonster + if (this.isMonster) { + console.log(`${this.sprite.spriteKey}.monster.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`) + this.sprite.setCtor(`${this.sprite.spriteKey}.monster.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`) + } else { + console.log(`${this.sprite.spriteKey}.animal.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`) + this.sprite.setCtor(`${this.sprite.spriteKey}.animal.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`) + } + } + attack() { + this.yell(1) + if (this.isMonster) { + let v = 1 + Math.floor(Math.random() * 4) + playSound(`action/hit-v${v}`, 1.0) + this.sprite.animate = true + this.sprite.loop = false + this.sprite.setKey = 'attack' + this.sprite.frameIndex = 0 + this.sprite.setCtor(`${this.sprite.spriteKey}.${this.sprite.animationKey}.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`) + } else { + + } + } yell(volume: number) { if (this.lastYellElapsed > this.nextYell) { this.nextYell = 500 + Math.random()*3000 this.lastYellElapsed = 0 - let v = 1 + Math.floor(Math.random() * 2) + let v = 1 + Math.floor(Math.random() * 3) if (this.isMonster) { playSound(`monsters/evil-${this.def.name}-v${v}`, volume) } else {