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

54 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
2022-11-08 18:45:57 +00:00
import { GameTimer } from "../Services/GameTimer";
2022-11-14 15:35:47 +00:00
import { delay } from "../Services/Utils/AsyncUtils";
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-14 15:35:47 +00:00
private lastDirection = new Vec2();
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
}
public gameTick(deltaTime: number, movement: Vec2): void {
2022-11-14 15:35:47 +00:00
let direction: Vec2 = movement.normalize();
if (direction.x == 0 && direction.y == 0) {
direction = this.lastDirection;
} else {
this.lastDirection = direction;
}
2022-11-08 18:45:57 +00:00
this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) {
2022-11-14 15:35:47 +00:00
this.strike(direction);
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-14 15:35:47 +00:00
private async strike(direction: Vec2): Promise<void> {
this.node.active = true;
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI - 45;
2022-11-03 15:55:49 +00:00
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
2022-11-03 15:55:49 +00:00
this.weaponAnimation.play("WeaponSwing");
2022-11-14 15:35:47 +00:00
await delay(1000);
this.node.active = false;
2022-11-03 15:55:49 +00:00
}
}