2022-11-08 10:42:14 +00:00
|
|
|
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-03 15:55:49 +00:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass("Weapon")
|
|
|
|
export class Weapon extends Component {
|
|
|
|
@property(Animation) private weaponAnimation: Animation;
|
2022-11-08 10:42:14 +00:00
|
|
|
@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-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-03 15:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public gameTick(deltaTime: number, movement: Vec2): void {
|
2022-11-08 18:45:57 +00:00
|
|
|
this.strikeTimer.gameTick(deltaTime);
|
|
|
|
if (this.strikeTimer.tryFinishPeriod()) {
|
2022-11-03 15:55:49 +00:00
|
|
|
this.strike(movement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:42:14 +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-03 15:55:49 +00:00
|
|
|
private strike(movement: Vec2): void {
|
|
|
|
const direction: Vec2 = movement.normalize();
|
|
|
|
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI;
|
|
|
|
this.node.eulerAngles = new Vec3(0, 0, angle);
|
|
|
|
|
2022-11-08 10:42:14 +00:00
|
|
|
this.weaponAnimation.getState("WeaponSwing").speed = 4;
|
2022-11-03 15:55:49 +00:00
|
|
|
this.weaponAnimation.play("WeaponSwing");
|
|
|
|
}
|
|
|
|
}
|