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:
12
assets/Scripts/Game/Items/Gold.meta
Normal file
12
assets/Scripts/Game/Items/Gold.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "51704601-c62e-4fff-93ee-73a81cac6e1b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
24
assets/Scripts/Game/Items/Gold/Gold.ts
Normal file
24
assets/Scripts/Game/Items/Gold/Gold.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Component, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Gold")
|
||||
export class Gold extends Component {
|
||||
private pickUpEvent: Signal<Gold> = new Signal<Gold>();
|
||||
|
||||
public setup(position: Vec3): void {
|
||||
this.node.setWorldPosition(position);
|
||||
this.node.active = true;
|
||||
}
|
||||
|
||||
public get PickupEvent(): ISignal<Gold> {
|
||||
return this.pickUpEvent;
|
||||
}
|
||||
|
||||
public pickup(): void {
|
||||
this.pickUpEvent.trigger(this);
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/Gold/Gold.ts.meta
Normal file
9
assets/Scripts/Game/Items/Gold/Gold.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "41f6700e-1184-4c4c-b5f0-35fec21bd88e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
26
assets/Scripts/Game/Items/Gold/GoldSpawner.ts
Normal file
26
assets/Scripts/Game/Items/Gold/GoldSpawner.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Component, Prefab, Vec3, _decorator } from "cc";
|
||||
import { ObjectPool } from "../../../Services/ObjectPool";
|
||||
import { Gold } from "./Gold";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("GoldSpawner")
|
||||
export class GoldSpawner extends Component {
|
||||
@property(Prefab) public goldPrefab: Prefab;
|
||||
|
||||
private goldPool: ObjectPool<Gold>;
|
||||
public init(): void {
|
||||
this.goldPool = new ObjectPool<Gold>(this.goldPrefab, this.node, 5, "Gold");
|
||||
}
|
||||
|
||||
public spawn(position: Vec3): void {
|
||||
const gold: Gold = this.goldPool.borrow();
|
||||
gold.setup(position);
|
||||
gold.PickupEvent.on(this.return, this);
|
||||
}
|
||||
|
||||
private return(gold: Gold): void {
|
||||
gold.PickupEvent.off(this.return);
|
||||
this.goldPool.return(gold);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/Gold/GoldSpawner.ts.meta
Normal file
9
assets/Scripts/Game/Items/Gold/GoldSpawner.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "abf1ff90-c9a3-4d65-8bb9-1ac9ebeb7d51",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
88
assets/Scripts/Game/Items/ItemManager.ts
Normal file
88
assets/Scripts/Game/Items/ItemManager.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { Component, random, randomRange, Vec3, _decorator } from "cc";
|
||||
import { GameResult } from "../Game";
|
||||
import { Enemy } from "../Unit/Enemy/Enemy";
|
||||
import { EnemyManager } from "../Unit/Enemy/EnemyManager";
|
||||
import { Player } from "../Unit/Player/Player";
|
||||
import { Gold } from "./Gold/Gold";
|
||||
import { GoldSpawner } from "./Gold/GoldSpawner";
|
||||
import { PickupEffectManager } from "./PickupEffect/PickupEffectManager";
|
||||
import { XP } from "./XP/XP";
|
||||
import { XPSpawner } from "./XP/XPSpawner";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("ItemManager")
|
||||
export class ItemManager extends Component {
|
||||
@property(XPSpawner) private xpSpawner: XPSpawner;
|
||||
@property(GoldSpawner) private goldSpawner: GoldSpawner;
|
||||
@property(PickupEffectManager) private pickupEffectManager: PickupEffectManager;
|
||||
|
||||
private player: Player;
|
||||
private gameResult: GameResult;
|
||||
|
||||
public init(enemyManager: EnemyManager, player: Player, gameResult: GameResult): void {
|
||||
this.player = player;
|
||||
this.gameResult = gameResult;
|
||||
|
||||
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
|
||||
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
|
||||
|
||||
this.xpSpawner.init();
|
||||
this.goldSpawner.init();
|
||||
this.pickupEffectManager.init();
|
||||
}
|
||||
|
||||
public pickupXP(xp: XP): void {
|
||||
this.pickupEffectManager.showEffect(xp.node.worldPosition);
|
||||
|
||||
this.player.Level.addXp(xp.Value);
|
||||
xp.pickup();
|
||||
}
|
||||
|
||||
public pickupGold(gold: Gold): void {
|
||||
this.pickupEffectManager.showEffect(gold.node.worldPosition);
|
||||
|
||||
gold.pickup();
|
||||
this.gameResult.goldCoins++;
|
||||
}
|
||||
|
||||
private addEnemyListeners(enemy: Enemy): void {
|
||||
enemy.DeathEvent.on(this.trySpawnItems, this);
|
||||
}
|
||||
|
||||
private removeEnemyListeners(enemy: Enemy): void {
|
||||
enemy.DeathEvent.off(this.trySpawnItems);
|
||||
}
|
||||
|
||||
private trySpawnItems(enemy: Enemy): void {
|
||||
this.trySpawnXP(enemy);
|
||||
this.trySpawnGold(enemy);
|
||||
}
|
||||
|
||||
private trySpawnXP(enemy: Enemy): void {
|
||||
for (let index = 0; index < enemy.XPReward; index++) {
|
||||
this.xpSpawner.spawnXp(this.getRandomPosition(enemy), 1);
|
||||
}
|
||||
}
|
||||
|
||||
private trySpawnGold(enemy: Enemy): void {
|
||||
if (enemy.GoldReward <= 0) return;
|
||||
|
||||
if (enemy.GoldReward < 1) {
|
||||
if (random() < enemy.GoldReward) {
|
||||
this.goldSpawner.spawn(enemy.node.worldPosition);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < enemy.GoldReward; i++) {
|
||||
this.goldSpawner.spawn(this.getRandomPosition(enemy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getRandomPosition(enemy: Enemy): Vec3 {
|
||||
const position: Vec3 = enemy.node.worldPosition;
|
||||
position.x += randomRange(-10, 10);
|
||||
position.y += randomRange(-10, 10);
|
||||
|
||||
return position;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Items/ItemManager.ts.meta
Normal file
9
assets/Scripts/Game/Items/ItemManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7f3f8f02-b61e-4a07-8d52-1c326f889fca",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Items/PickupEffect.meta
Normal file
12
assets/Scripts/Game/Items/PickupEffect.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "264a4734-6753-4278-b3b7-b6b100e059a4",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
10
assets/Scripts/Game/Items/PickupEffect/PickupEffect.ts
Normal file
10
assets/Scripts/Game/Items/PickupEffect/PickupEffect.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Animation, Component, _decorator } from "cc";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PickupEffect")
|
||||
export class PickupEffect extends Component {
|
||||
@property(Animation) private animation: Animation;
|
||||
public init(): void {
|
||||
this.animation.play("PickBonus");
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0f2b2cd0-1d1e-4862-821f-5fcb83c4ec23",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
import { _decorator, Component, Node, Prefab, Vec3 } from "cc";
|
||||
import { ObjectPool } from "../../../Services/ObjectPool";
|
||||
import { delay } from "../../../Services/Utils/AsyncUtils";
|
||||
import { PickupEffect } from "./PickupEffect";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PickupEffectManager")
|
||||
export class PickupEffectManager extends Component {
|
||||
@property(Prefab) private pickupEffect: Prefab;
|
||||
|
||||
private effectPool: ObjectPool<PickupEffect>;
|
||||
|
||||
public init(): void {
|
||||
this.effectPool = new ObjectPool(this.pickupEffect, this.node, 5, "PickupEffect");
|
||||
}
|
||||
|
||||
public async showEffect(position: Vec3): Promise<void> {
|
||||
const effect = this.effectPool.borrow();
|
||||
effect.node.setWorldPosition(position);
|
||||
effect.node.active = true;
|
||||
|
||||
await delay(450);
|
||||
|
||||
this.effectPool.return(effect);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "65b4121f-a4cf-47e1-97d4-1e0d3fc72eec",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Items/XP.meta
Normal file
12
assets/Scripts/Game/Items/XP.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "1e85dd0e-3cbc-4870-9a6a-50e0c29cfeb7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
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