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

37 lines
1.1 KiB
TypeScript
Raw Normal View History

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;
@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;
if (this.strikeDelay / 4 <= this.currentDelay) {
2022-11-03 15:55:49 +00:00
this.currentDelay = 0;
this.strike(movement);
}
}
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);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
2022-11-03 15:55:49 +00:00
this.weaponAnimation.play("WeaponSwing");
}
}