mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 08:36:14 +00:00
Item manager
This commit is contained in:
33
assets/Scripts/Game/Items/XP/XP.ts
Normal file
33
assets/Scripts/Game/Items/XP/XP.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Animation, Component, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("XP")
|
||||
export class XP extends Component {
|
||||
@property(Animation) private animation: Animation;
|
||||
|
||||
private pickUpEvent: Signal<XP> = new Signal<XP>();
|
||||
private value = 2;
|
||||
|
||||
public setup(position: Vec3, value: number): void {
|
||||
this.node.setWorldPosition(position);
|
||||
this.value = value;
|
||||
this.node.active = true;
|
||||
this.animation.play("DropStart");
|
||||
}
|
||||
|
||||
public get Value(): number {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public get PickupEvent(): ISignal<XP> {
|
||||
return this.pickUpEvent;
|
||||
}
|
||||
|
||||
public pickup(): void {
|
||||
this.pickUpEvent.trigger(this);
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/XP/XP.ts.meta
Normal file
9
assets/Scripts/Game/Items/XP/XP.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7a5361b6-3ae7-45b6-94ec-a05f322d7896",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
26
assets/Scripts/Game/Items/XP/XPSpawner.ts
Normal file
26
assets/Scripts/Game/Items/XP/XPSpawner.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Component, Prefab, Vec3, _decorator } from "cc";
|
||||
import { ObjectPool } from "../../../Services/ObjectPool";
|
||||
|
||||
import { XP } from "./XP";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("XPSpawner")
|
||||
export class XPSpawner extends Component {
|
||||
@property(Prefab) public xpPrefab: Prefab;
|
||||
|
||||
private xpPool: ObjectPool<XP>;
|
||||
public init(): void {
|
||||
this.xpPool = new ObjectPool<XP>(this.xpPrefab, this.node, 5, "XP");
|
||||
}
|
||||
|
||||
public spawnXp(position: Vec3, value: number): void {
|
||||
const xp: XP = this.xpPool.borrow();
|
||||
xp.setup(position, value);
|
||||
xp.PickupEvent.on(this.returnXp, this);
|
||||
}
|
||||
|
||||
private returnXp(xp: XP): void {
|
||||
xp.PickupEvent.off(this.returnXp);
|
||||
this.xpPool.return(xp);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/XP/XPSpawner.ts.meta
Normal file
9
assets/Scripts/Game/Items/XP/XPSpawner.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ab9e206b-f11a-417c-814b-07d29c59ed6e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user