Add attack

This commit is contained in:
Ketchetwahmeegwun T. Southall 2022-01-30 11:03:11 -08:00
parent a09935e768
commit 493e993e45

View File

@ -132,7 +132,7 @@ export class AnimalEntity extends Entity {
this.actionCooldown += delta
if (this.actionCooldown >= 750 - 750 * this.mode.cdr) {
if (shouldAttack) {
this.attack()
this.attack(ctx)
this.actionCooldown = 0
} else if (shouldMove) {
this.stepSoundElapsed += delta
@ -287,7 +287,7 @@ export class AnimalEntity extends Entity {
this.sprite.setCtor(`${this.sprite.spriteKey}.animal.${this.sprite.setKey}.${this.sprite.subsetKey}.${this.sprite.frameIndex}`)
}
}
attack() {
attack(ctx: WorldContext) {
this.yell(1)
if (this.isMonster) {
let v = 1 + Math.floor(Math.random() * 4)
@ -297,6 +297,36 @@ export class AnimalEntity extends Entity {
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}`)
// Time to get extra lame...
let c = this.getCardinal(this.direction)
if (this.body) {
let p = this.body.getPosition()
let box = [
[p.x-10, p.x+10],
[p.y-10, p.y+10]
]
if (c === 'east') {
box[0][0] += 20
box[0][1] += 20
} else if (c === 'west') {
box[0][0] -= 20
box[0][1] -= 20
} else if (c === 'north') {
box[1][0] -= 10
box[1][1] -= 10
} else {
box[1][0] += 20
box[1][1] += 20
}
let entities: AnimalEntity[] = ctx.queryEntities([box[0][0], box[1][0]], [box[0][1], box[1][1]]).filter(v=>v instanceof AnimalEntity && v !== this) as AnimalEntity[]
if (entities.length > 0) {
entities[0].dead = true
entities[0].shouldGib = true
}
}
} else {
}