Add random yelling
This commit is contained in:
parent
4691b13b52
commit
e15f0eec17
|
@ -1,5 +1,6 @@
|
|||
import * as planck from 'planck'
|
||||
import { AnimalDefinition, CreatureDefinition } from '../data/animals'
|
||||
import { playSong, playSound } from '../shared/audio'
|
||||
import { Action, adjustAction } from "./Action"
|
||||
import { Entity, Sensor } from "./Entity"
|
||||
import { PuddleEntity } from './PuddleEntity'
|
||||
|
@ -18,6 +19,9 @@ export class AnimalEntity extends Entity {
|
|||
def: AnimalDefinition
|
||||
mode: CreatureDefinition
|
||||
desiredActions: Action[] = []
|
||||
stepSoundElapsed: number = 0
|
||||
lastYellElapsed: number = 0
|
||||
nextYell: number = 1000
|
||||
|
||||
constructor(def: AnimalDefinition) {
|
||||
super(`${def.name}.animal.stand.west.0`)
|
||||
|
@ -40,6 +44,7 @@ export class AnimalEntity extends Entity {
|
|||
}
|
||||
update(delta: number, ctx: WorldContext) {
|
||||
super.update(delta)
|
||||
this.lastYellElapsed += delta
|
||||
|
||||
let waterZones = this.zones.filter(v=>v.type==='fluid')
|
||||
if (waterZones.length) {
|
||||
|
@ -98,6 +103,9 @@ export class AnimalEntity extends Entity {
|
|||
}
|
||||
shouldMove = true
|
||||
break
|
||||
case 'attack':
|
||||
this.yell(1)
|
||||
break
|
||||
}
|
||||
if (this.direction > 360) {
|
||||
this.direction = 0
|
||||
|
@ -107,6 +115,7 @@ 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
|
||||
|
@ -119,6 +128,11 @@ export class AnimalEntity extends Entity {
|
|||
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.round(Math.random()*3)
|
||||
playSound('action/footstep-tiny-v'+v, 0.5)
|
||||
this.stepSoundElapsed = 0
|
||||
}
|
||||
this.sprite.animate = true
|
||||
} else {
|
||||
this.sprite.animate = false
|
||||
|
@ -162,6 +176,7 @@ export class AnimalEntity extends Entity {
|
|||
}
|
||||
this.desiredActions = adjustAction([], this.getCardinal(a), 0.85)
|
||||
this.desiredActions = adjustAction(this.desiredActions, this.getCardinal(Math.random()*360), Math.random())
|
||||
this.yell(1)
|
||||
} else if (this.smelledTarget) {
|
||||
let r = Math.atan2(this.y-this.smelledTarget.y, this.x-this.smelledTarget.x)
|
||||
let a = r * (180 / Math.PI)
|
||||
|
@ -185,6 +200,9 @@ export class AnimalEntity extends Entity {
|
|||
} else {
|
||||
this.desiredActions = adjustAction([], this.getCardinal(Math.random()*360), 1)
|
||||
}
|
||||
if (Math.random() > 1 - this.mode.noisiness) {
|
||||
this.yell(0.25)
|
||||
}
|
||||
this.wanderTimer = 0
|
||||
}
|
||||
}
|
||||
|
@ -229,6 +247,18 @@ export class AnimalEntity extends Entity {
|
|||
this.seenTarget = undefined
|
||||
}
|
||||
}
|
||||
yell(volume: number) {
|
||||
if (this.lastYellElapsed > this.nextYell) {
|
||||
this.nextYell = 500 + Math.random()*3000
|
||||
this.lastYellElapsed = 0
|
||||
let v = 1+Math.round(Math.random()*2)
|
||||
if (this.isMonster) {
|
||||
playSound(`monsters/evil-${this.def.name}-v${v}`, volume)
|
||||
} else {
|
||||
playSound(`animals/${this.def.name}-v${v}`, volume)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isAnimalEntity(o: any): o is AnimalEntity {
|
||||
|
|
Loading…
Reference in New Issue
Block a user