mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-12-08 13:49:29 +00:00
Added health potion
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { Component, random, randomRange, Vec3, _decorator } from "cc";
|
||||
import { ItemSettings } from "../Data/GameSettings";
|
||||
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 { HealthPotion } from "./HealthPotion/HealthPotion";
|
||||
import { HealthPotionSpawner } from "./HealthPotion/HealthPotionSpawner";
|
||||
import { PickupEffectManager } from "./PickupEffect/PickupEffectManager";
|
||||
import { XP } from "./XP/XP";
|
||||
import { XPSpawner } from "./XP/XPSpawner";
|
||||
@@ -14,20 +17,24 @@ const { ccclass, property } = _decorator;
|
||||
export class ItemManager extends Component {
|
||||
@property(XPSpawner) private xpSpawner: XPSpawner;
|
||||
@property(GoldSpawner) private goldSpawner: GoldSpawner;
|
||||
@property(HealthPotionSpawner) private healthPotionSpawner: HealthPotionSpawner;
|
||||
@property(PickupEffectManager) private pickupEffectManager: PickupEffectManager;
|
||||
|
||||
private player: Player;
|
||||
private gameResult: GameResult;
|
||||
private healthPerPotion: number;
|
||||
|
||||
public init(enemyManager: EnemyManager, player: Player, gameResult: GameResult): void {
|
||||
public init(enemyManager: EnemyManager, player: Player, gameResult: GameResult, settings: ItemSettings): void {
|
||||
this.player = player;
|
||||
this.gameResult = gameResult;
|
||||
this.healthPerPotion = settings.healthPerPotion;
|
||||
|
||||
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
|
||||
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
|
||||
|
||||
this.xpSpawner.init();
|
||||
this.goldSpawner.init();
|
||||
this.healthPotionSpawner.init();
|
||||
this.pickupEffectManager.init();
|
||||
}
|
||||
|
||||
@@ -45,6 +52,13 @@ export class ItemManager extends Component {
|
||||
this.gameResult.goldCoins++;
|
||||
}
|
||||
|
||||
public pickupHealthPotion(healthPotion: HealthPotion): void {
|
||||
this.pickupEffectManager.showEffect(healthPotion.node.worldPosition);
|
||||
|
||||
healthPotion.pickup();
|
||||
this.player.Health.heal(this.healthPerPotion);
|
||||
}
|
||||
|
||||
private addEnemyListeners(enemy: Enemy): void {
|
||||
enemy.DeathEvent.on(this.trySpawnItems, this);
|
||||
}
|
||||
@@ -56,6 +70,7 @@ export class ItemManager extends Component {
|
||||
private trySpawnItems(enemy: Enemy): void {
|
||||
this.trySpawnXP(enemy);
|
||||
this.trySpawnGold(enemy);
|
||||
this.trySpawnHealthPotion(enemy);
|
||||
}
|
||||
|
||||
private trySpawnXP(enemy: Enemy): void {
|
||||
@@ -78,6 +93,15 @@ export class ItemManager extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private getRandomPosition(enemy: Enemy): Vec3 {
|
||||
const position: Vec3 = enemy.node.worldPosition;
|
||||
position.x += randomRange(-10, 10);
|
||||
|
||||
Reference in New Issue
Block a user