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 player attack logic
- Loading branch information
1 parent
0f00bee
commit aee14ab
Showing
3 changed files
with
36 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { addBullet } from '../gameobjects' | ||
import type { Player } from '../types' | ||
|
||
export function addAttack(player: Player) { | ||
const attack = new Attack(player) | ||
|
||
onClick(() => { | ||
if (attack.canAttack()) { | ||
attack.update() | ||
addBullet(player) | ||
} | ||
}) | ||
} | ||
|
||
class Attack { | ||
private attackDelay = 1 | ||
private lastAttacked = 0 | ||
private player | ||
|
||
constructor(player: Player) { | ||
this.player = player | ||
} | ||
|
||
update() { | ||
this.lastAttacked = time() | ||
} | ||
|
||
canAttack() { | ||
return !this.lastAttacked | ||
? true | ||
: this.lastAttacked + this.attackDelay < time() | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './attack' | ||
export * from './cursors' |
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