Skip to content

Commit

Permalink
chore(gameobjects): add enemy damage over time
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Jan 25, 2025
1 parent 3a7df08 commit fc61e7b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/events/attack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import { addBubble } from '../gameobjects'
import type { Player } from '../types'

export function addAttack(player: Player) {
const attack = new Attack(player)
const attack = new Attack()

onClick(() => {
if (attack.canAttack()) {
attack.update()
play(Sound.Shoot, { detune: rand(-100, 100) })
addBubble(player)
attack.update()
}
})
}

class Attack {
private attackDelay = 1
private lastAttacked = 0
private player
private attackDelay
private lastAttacked

constructor(player: Player) {
this.player = player
constructor(attackDelay = 1, lastAttacked = 0) {
this.attackDelay = attackDelay
this.lastAttacked = lastAttacked
}

update() {
Expand Down
2 changes: 1 addition & 1 deletion src/events/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function addEnemyState(enemy: Enemy) {
enemy.enterState(State.Move)
})

enemy.onStateUpdate(State.Move, async () => {
enemy.onStateUpdate(State.Move, () => {
const player = getPlayer()

if (!player?.exists()) {
Expand Down
10 changes: 9 additions & 1 deletion src/gameobjects/enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function addEnemy(x: number, y: number) {

addEnemyState(enemy)

enemy.onCollide(Tag.Player, async () => {
enemy.onCollide(Tag.Player, () => {
if (enemy.bubble) {
return
}
Expand All @@ -45,6 +45,14 @@ export function addEnemy(x: number, y: number) {
getPlayer()?.hurt(enemy.damage)
})

enemy.onCollideUpdate(Tag.Player, () => {
if (enemy.bubble) {
return
}

getPlayer()?.hurt(enemy.damage / 1000)
})

enemy.onHurt(() => {
enemy.enterState(State.Stunned)
})
Expand Down
1 change: 1 addition & 0 deletions src/gameobjects/projectile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function addProjectile(enemy: Enemy, player: Player) {
projectile.onCollide(Tag.Enemy, (enemy) => {
play(Sound.Hit)
enemy.removeAll(Tag.EnemyBubble)
enemy.bubble = false
})

return projectile
Expand Down

0 comments on commit fc61e7b

Please sign in to comment.