mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 08:36:14 +00:00
Folder structure
This commit is contained in:
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { BoxCollider2D, Collider2D, Component, Contact2DType, _decorator } from "cc";
|
||||
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("UpgradableCollider")
|
||||
export class UpgradableCollider extends Component {
|
||||
@property(BoxCollider2D) private colliders: BoxCollider2D[] = [];
|
||||
private contactBeginEvent: Signal<Collider2D> = new Signal<Collider2D>();
|
||||
private currentUpgradeLevel = 0;
|
||||
|
||||
public init(): void {
|
||||
this.setUpgradeLevel();
|
||||
|
||||
for (const collider of this.colliders) {
|
||||
collider.on(Contact2DType.BEGIN_CONTACT, this.onColliderContactBegin, this);
|
||||
}
|
||||
}
|
||||
|
||||
public get ContactBeginEvent(): ISignal<Collider2D> {
|
||||
return this.contactBeginEvent;
|
||||
}
|
||||
|
||||
public upgrade(): void {
|
||||
if (this.currentUpgradeLevel == this.colliders.length - 1) throw new Error("Already at max upgrade! " + this.currentUpgradeLevel);
|
||||
|
||||
this.currentUpgradeLevel++;
|
||||
this.setUpgradeLevel();
|
||||
}
|
||||
|
||||
private setUpgradeLevel(): void {
|
||||
for (const collider of this.colliders) {
|
||||
collider.node.active = false;
|
||||
}
|
||||
|
||||
this.colliders[this.currentUpgradeLevel].node.active = true;
|
||||
}
|
||||
|
||||
private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
this.contactBeginEvent.trigger(otherCollider);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bba49b00-3fe5-4042-85f3-7d1f017d75d3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Animation, AnimationState, Component, _decorator } from "cc";
|
||||
import { GameTimer } from "../../../../Services/GameTimer";
|
||||
import { WeaponSettings } from "../../../Data/GameSettings";
|
||||
|
||||
import { UpgradableCollider } from "./UpgradableCollider";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Weapon")
|
||||
export class Weapon extends Component {
|
||||
@property(Animation) private weaponAnimation: Animation;
|
||||
@property(UpgradableCollider) private upgradableCollider: UpgradableCollider;
|
||||
|
||||
private strikeTimer: GameTimer;
|
||||
private strikeState: AnimationState;
|
||||
private damage: number;
|
||||
|
||||
public init(settings: WeaponSettings): void {
|
||||
this.strikeTimer = new GameTimer(settings.strikeDelay);
|
||||
this.damage = settings.damage;
|
||||
this.node.active = false;
|
||||
|
||||
this.weaponAnimation.on(Animation.EventType.FINISHED, this.endStrike, this);
|
||||
this.strikeState = this.weaponAnimation.getState(this.weaponAnimation.clips[0].name);
|
||||
this.strikeState.speed = 1;
|
||||
|
||||
this.upgradableCollider.init();
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.strikeTimer.gameTick(deltaTime);
|
||||
if (this.strikeTimer.tryFinishPeriod()) {
|
||||
this.strike();
|
||||
}
|
||||
}
|
||||
|
||||
public get Collider(): UpgradableCollider {
|
||||
return this.upgradableCollider;
|
||||
}
|
||||
|
||||
public get Damage(): number {
|
||||
return this.damage;
|
||||
}
|
||||
|
||||
public upgradeWeaponDamage(): void {
|
||||
this.damage++;
|
||||
}
|
||||
public upgradeWeaponLength(): void {
|
||||
this.upgradableCollider.upgrade();
|
||||
}
|
||||
|
||||
private strike(): void {
|
||||
this.node.active = true;
|
||||
this.weaponAnimation.play(this.strikeState.name);
|
||||
}
|
||||
|
||||
private endStrike(): void {
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2391b8b7-f9fa-42a8-b046-55ff57b07d02",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user