Slash-The-Hordes/assets/Scripts/Game/Weapon.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-11-16 11:26:20 +00:00
import { Animation, AnimationState, BoxCollider2D, Collider2D, Component, _decorator } from "cc";
2022-11-08 18:45:57 +00:00
import { GameTimer } from "../Services/GameTimer";
2022-11-03 15:55:49 +00:00
const { ccclass, property } = _decorator;
@ccclass("Weapon")
export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private collider: BoxCollider2D;
2022-11-03 15:55:49 +00:00
2022-11-08 18:45:57 +00:00
private strikeTimer: GameTimer;
2022-11-16 11:26:20 +00:00
private strikeState: AnimationState;
2022-11-03 15:55:49 +00:00
public init(strikeDelay: number): void {
2022-11-08 18:45:57 +00:00
this.strikeTimer = new GameTimer(strikeDelay);
2022-11-14 15:35:47 +00:00
this.node.active = false;
2022-11-03 15:55:49 +00:00
2022-11-16 11:26:20 +00:00
this.weaponAnimation.on(Animation.EventType.FINISHED, this.endStrike, this);
this.strikeState = this.weaponAnimation.getState(this.weaponAnimation.clips[0].name);
this.strikeState.speed = 1;
}
2022-11-14 15:35:47 +00:00
2022-11-16 11:26:20 +00:00
public gameTick(deltaTime: number): void {
2022-11-08 18:45:57 +00:00
this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) {
2022-11-16 11:26:20 +00:00
this.strike();
2022-11-03 15:55:49 +00:00
}
}
public get Collider(): Collider2D {
return this.collider;
}
2022-11-08 18:45:57 +00:00
public get Damage(): number {
return 5;
}
2022-11-16 11:26:20 +00:00
private strike(): void {
2022-11-14 15:35:47 +00:00
this.node.active = true;
2022-11-16 11:26:20 +00:00
this.weaponAnimation.play(this.strikeState.name);
}
2022-11-14 15:35:47 +00:00
2022-11-16 11:26:20 +00:00
private endStrike(): void {
2022-11-14 15:35:47 +00:00
this.node.active = false;
2022-11-03 15:55:49 +00:00
}
}