generated from remarkablegames/kaplay-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(events): modularize enemy states
- Loading branch information
1 parent
1a0df01
commit 0384a36
Showing
9 changed files
with
82 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Animation as State } from './animations' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './attack' | ||
export * from './cursors' | ||
export * from './states' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Animation, Sprite, State } from '../constants' | ||
import { addProjectile, getPlayer } from '../gameobjects' | ||
import type { Enemy } from '../types' | ||
|
||
export function addEnemyState(enemy: Enemy) { | ||
enemy.onStateEnter(State.Idle, async () => { | ||
enemy.play(Animation.Idle) | ||
await wait(rand(0.3, 1)) | ||
enemy.enterState(State.Move) | ||
}) | ||
|
||
enemy.onStateEnter(State.Move, () => { | ||
enemy.play(Animation.Move) | ||
}) | ||
|
||
enemy.onStateEnter(State.Attack, async () => { | ||
const player = getPlayer() | ||
|
||
if (!player?.exists()) { | ||
return | ||
} | ||
|
||
enemy.play(Animation.Attack) | ||
|
||
if (enemy.sprite === Sprite.Pokey) { | ||
addProjectile(enemy, player) | ||
} | ||
|
||
await wait(0.2) | ||
enemy.enterState(State.Cooldown) | ||
}) | ||
|
||
enemy.onStateEnter(State.Cooldown, async () => { | ||
enemy.play(Animation.Cooldown) | ||
await wait(rand(1, 3)) | ||
enemy.enterState(State.Move) | ||
}) | ||
|
||
enemy.onStateEnter(State.Stunned, async () => { | ||
enemy.play(Animation.Stunned) | ||
await wait(rand(0.3, 1)) | ||
enemy.enterState(State.Move) | ||
}) | ||
|
||
enemy.onStateUpdate(State.Move, async () => { | ||
const player = getPlayer() | ||
|
||
if (!player?.exists()) { | ||
return | ||
} | ||
|
||
if (enemy.sprite === Sprite.Pokey && Number(rand()) < 0.005) { | ||
return enemy.enterState(State.Attack) | ||
} | ||
|
||
const direction = player.pos.sub(enemy.pos).unit() | ||
enemy.move(direction.scale(enemy.speed)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters