2022-11-28 11:49:16 +01:00
|
|
|
import { Animation, AnimationState, Component, _decorator } from "cc";
|
|
|
|
import { GameTimer } from "../../../Services/GameTimer";
|
|
|
|
import { WeaponSettings } from "../../Data/GameSettings";
|
|
|
|
|
|
|
|
import { UpgradableCollider } from "./UpgradableCollider";
|
2022-11-03 16:55:49 +01:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass("Weapon")
|
|
|
|
export class Weapon extends Component {
|
|
|
|
@property(Animation) private weaponAnimation: Animation;
|
2022-11-28 11:49:16 +01:00
|
|
|
@property(UpgradableCollider) private upgradableCollider: UpgradableCollider;
|
2022-11-03 16:55:49 +01:00
|
|
|
|
2022-11-08 19:45:57 +01:00
|
|
|
private strikeTimer: GameTimer;
|
2022-11-16 12:26:20 +01:00
|
|
|
private strikeState: AnimationState;
|
2022-11-28 11:49:16 +01:00
|
|
|
private damage: number;
|
2022-11-03 16:55:49 +01:00
|
|
|
|
2022-11-28 11:49:16 +01:00
|
|
|
public init(settings: WeaponSettings): void {
|
|
|
|
this.strikeTimer = new GameTimer(settings.strikeDelay);
|
|
|
|
this.damage = settings.damage;
|
2022-11-14 16:35:47 +01:00
|
|
|
this.node.active = false;
|
2022-11-03 16:55:49 +01:00
|
|
|
|
2022-11-16 12:26:20 +01: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-28 11:49:16 +01:00
|
|
|
|
|
|
|
this.upgradableCollider.init();
|
2022-11-16 12:26:20 +01:00
|
|
|
}
|
2022-11-14 16:35:47 +01:00
|
|
|
|
2022-11-16 12:26:20 +01:00
|
|
|
public gameTick(deltaTime: number): void {
|
2022-11-08 19:45:57 +01:00
|
|
|
this.strikeTimer.gameTick(deltaTime);
|
|
|
|
if (this.strikeTimer.tryFinishPeriod()) {
|
2022-11-16 12:26:20 +01:00
|
|
|
this.strike();
|
2022-11-03 16:55:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 11:49:16 +01:00
|
|
|
public get Collider(): UpgradableCollider {
|
|
|
|
return this.upgradableCollider;
|
2022-11-08 11:42:14 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 19:45:57 +01:00
|
|
|
public get Damage(): number {
|
2022-11-28 11:49:16 +01:00
|
|
|
return this.damage;
|
2022-11-08 19:45:57 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 11:49:16 +01:00
|
|
|
public upgradeWeaponDamage(): void {
|
|
|
|
this.damage++;
|
|
|
|
}
|
|
|
|
public upgradeWeaponLength(): void {
|
|
|
|
this.upgradableCollider.upgrade();
|
|
|
|
}
|
2022-11-23 09:01:01 +01:00
|
|
|
|
2022-11-16 12:26:20 +01:00
|
|
|
private strike(): void {
|
2022-11-14 16:35:47 +01:00
|
|
|
this.node.active = true;
|
2022-11-16 12:26:20 +01:00
|
|
|
this.weaponAnimation.play(this.strikeState.name);
|
|
|
|
}
|
2022-11-14 16:35:47 +01:00
|
|
|
|
2022-11-16 12:26:20 +01:00
|
|
|
private endStrike(): void {
|
2022-11-14 16:35:47 +01:00
|
|
|
this.node.active = false;
|
2022-11-03 16:55:49 +01:00
|
|
|
}
|
|
|
|
}
|