mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-12-08 13:49:29 +00:00
Item refactoring
This commit is contained in:
26
assets/Scripts/Game/Items/ItemSpawner.ts
Normal file
26
assets/Scripts/Game/Items/ItemSpawner.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Component, Prefab, Vec3, _decorator } from "cc";
|
||||
import { ObjectPool } from "../../Services/ObjectPool";
|
||||
import { Item } from "./Item";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("ItemSpawner")
|
||||
export class ItemSpawner extends Component {
|
||||
@property(Prefab) public itemPrefab: Prefab;
|
||||
|
||||
private itemPool: ObjectPool<Item>;
|
||||
public init(): void {
|
||||
this.itemPool = new ObjectPool<Item>(this.itemPrefab, this.node, 5, "Item");
|
||||
}
|
||||
|
||||
public spawn(position: Vec3): void {
|
||||
const item: Item = this.itemPool.borrow();
|
||||
item.setup(position);
|
||||
item.PickupEvent.on(this.return, this);
|
||||
}
|
||||
|
||||
private return(gold: Item): void {
|
||||
gold.PickupEvent.off(this.return);
|
||||
this.itemPool.return(gold);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user