From 26e1fb2675466cbf6a845cfc8fee5ff99b0fea62 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Wed, 29 Apr 2026 12:24:00 +0000 Subject: [PATCH] Cooldown --- src/common/rpg/components/cooldown.ts | 41 ++++++++++++++++++++++++ src/common/rpg/systems/cooldownSystem.ts | 10 ++++++ 2 files changed, 51 insertions(+) create mode 100644 src/common/rpg/components/cooldown.ts create mode 100644 src/common/rpg/systems/cooldownSystem.ts diff --git a/src/common/rpg/components/cooldown.ts b/src/common/rpg/components/cooldown.ts new file mode 100644 index 0000000..0e8c6bf --- /dev/null +++ b/src/common/rpg/components/cooldown.ts @@ -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'); + } + } +} \ No newline at end of file diff --git a/src/common/rpg/systems/cooldownSystem.ts b/src/common/rpg/systems/cooldownSystem.ts new file mode 100644 index 0000000..eaccccb --- /dev/null +++ b/src/common/rpg/systems/cooldownSystem.ts @@ -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); + } + } +} \ No newline at end of file