Cooldown
This commit is contained in:
parent
c2e9a597fa
commit
26e1fb2675
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { component } from "../core/registry";
|
||||||
|
import { Component } from "../core/world";
|
||||||
|
import { action, variable } from "../utils/decorators";
|
||||||
|
|
||||||
|
@component
|
||||||
|
export class Cooldown extends Component<{
|
||||||
|
remaining: number;
|
||||||
|
duration: number;
|
||||||
|
}> {
|
||||||
|
constructor(duration: number) {
|
||||||
|
super({ remaining: duration, duration });
|
||||||
|
}
|
||||||
|
|
||||||
|
@variable('.')
|
||||||
|
get ready(): boolean {
|
||||||
|
return this.state.remaining <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
reset(duration?: number): void {
|
||||||
|
if (duration != null) this.state.duration = duration;
|
||||||
|
this.state.remaining = this.state.duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
clear(): void {
|
||||||
|
if (this.state.remaining <= 0) return;
|
||||||
|
this.state.remaining = 0;
|
||||||
|
this.emit('ready');
|
||||||
|
}
|
||||||
|
|
||||||
|
@action
|
||||||
|
update(dt: number): void {
|
||||||
|
if (this.state.remaining <= 0) return;
|
||||||
|
this.state.remaining -= dt;
|
||||||
|
if (this.state.remaining <= 0) {
|
||||||
|
this.state.remaining = 0;
|
||||||
|
this.emit('ready');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
import { Cooldown } from "../components/cooldown";
|
||||||
|
import { System, type World } from "../core/world";
|
||||||
|
|
||||||
|
export class CooldownSystem extends System {
|
||||||
|
override update(world: World, dt: number): void {
|
||||||
|
for (const [, , cooldown] of world.query(Cooldown)) {
|
||||||
|
cooldown.update(dt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue