2022-12-20 11:27:51 +01:00
|
|
|
import { Component, random, randomRange, Vec3, _decorator } from "cc";
|
2022-12-20 13:37:56 +01:00
|
|
|
import { ItemSettings } from "../Data/GameSettings";
|
2022-12-20 11:27:51 +01:00
|
|
|
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";
|
2022-12-20 13:37:56 +01:00
|
|
|
import { HealthPotion } from "./HealthPotion/HealthPotion";
|
|
|
|
import { HealthPotionSpawner } from "./HealthPotion/HealthPotionSpawner";
|
2022-12-20 11:27:51 +01:00
|
|
|
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;
|
2022-12-20 13:37:56 +01:00
|
|
|
@property(HealthPotionSpawner) private healthPotionSpawner: HealthPotionSpawner;
|
2022-12-20 11:27:51 +01:00
|
|
|
@property(PickupEffectManager) private pickupEffectManager: PickupEffectManager;
|
|
|
|
|
|
|
|
private player: Player;
|
|
|
|
private gameResult: GameResult;
|
2022-12-20 13:37:56 +01:00
|
|
|
private healthPerPotion: number;
|
2022-12-20 11:27:51 +01:00
|
|
|
|
2022-12-20 13:37:56 +01:00
|
|
|
public init(enemyManager: EnemyManager, player: Player, gameResult: GameResult, settings: ItemSettings): void {
|
2022-12-20 11:27:51 +01:00
|
|
|
this.player = player;
|
|
|
|
this.gameResult = gameResult;
|
2022-12-20 13:37:56 +01:00
|
|
|
this.healthPerPotion = settings.healthPerPotion;
|
2022-12-20 11:27:51 +01:00
|
|
|
|
|
|
|
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
|
|
|
|
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
|
|
|
|
|
|
|
|
this.xpSpawner.init();
|
|
|
|
this.goldSpawner.init();
|
2022-12-20 13:37:56 +01:00
|
|
|
this.healthPotionSpawner.init();
|
2022-12-20 11:27:51 +01:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:37:56 +01:00
|
|
|
public pickupHealthPotion(healthPotion: HealthPotion): void {
|
|
|
|
this.pickupEffectManager.showEffect(healthPotion.node.worldPosition);
|
|
|
|
|
|
|
|
healthPotion.pickup();
|
|
|
|
this.player.Health.heal(this.healthPerPotion);
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:27:51 +01:00
|
|
|
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);
|
2022-12-20 13:37:56 +01:00
|
|
|
this.trySpawnHealthPotion(enemy);
|
2022-12-20 11:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:37:56 +01:00
|
|
|
private trySpawnHealthPotion(enemy: Enemy): void {
|
|
|
|
if (enemy.HealthPotionRewardChance <= 0) return;
|
|
|
|
|
|
|
|
console.log("random: " + random() + " chance " + enemy.HealthPotionRewardChance);
|
|
|
|
if (random() < enemy.HealthPotionRewardChance) {
|
|
|
|
this.healthPotionSpawner.spawn(enemy.node.worldPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 11:27:51 +01:00
|
|
|
private getRandomPosition(enemy: Enemy): Vec3 {
|
|
|
|
const position: Vec3 = enemy.node.worldPosition;
|
|
|
|
position.x += randomRange(-10, 10);
|
|
|
|
position.y += randomRange(-10, 10);
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|