2022-11-08 10:42:14 +00:00
|
|
|
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
|
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
|
|
|
|
|
|
|
private strikeDelay: number;
|
|
|
|
private currentDelay = 0;
|
|
|
|
|
|
|
|
public init(strikeDelay: number): void {
|
|
|
|
this.strikeDelay = strikeDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
public gameTick(deltaTime: number, movement: Vec2): void {
|
|
|
|
this.currentDelay += deltaTime;
|
2022-11-08 10:42:14 +00:00
|
|
|
if (this.strikeDelay / 4 <= this.currentDelay) {
|
2022-11-03 15:55:49 +00:00
|
|
|
this.currentDelay = 0;
|
|
|
|
this.strike(movement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:42:14 +00:00
|
|
|
public get Collider(): Collider2D {
|
|
|
|
return this.collider;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|