mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 16:46:00 +00:00
Added health potion
This commit is contained in:
@@ -4,6 +4,7 @@ import { Signal } from "../../Services/EventSystem/Signal";
|
||||
import { GameTimer } from "../../Services/GameTimer";
|
||||
import { GroupType } from "../GroupType";
|
||||
import { Gold } from "../Items/Gold/Gold";
|
||||
import { HealthPotion } from "../Items/HealthPotion/HealthPotion";
|
||||
import { ItemManager } from "../Items/ItemManager";
|
||||
import { XP } from "../Items/XP/XP";
|
||||
import { Enemy } from "../Unit/Enemy/Enemy";
|
||||
@@ -28,6 +29,7 @@ export class PlayerCollisionSystem {
|
||||
this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this));
|
||||
this.groupToResolver.set(GroupType.XP, this.resolveXpContact.bind(this));
|
||||
this.groupToResolver.set(GroupType.GOLD, this.resolveGoldContact.bind(this));
|
||||
this.groupToResolver.set(GroupType.HEALTH_POTION, this.resolveHealthPotionContact.bind(this));
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
@@ -81,4 +83,8 @@ export class PlayerCollisionSystem {
|
||||
private resolveGoldContact(goldCollider: Collider2D): void {
|
||||
this.itemManager.pickupGold(goldCollider.node.getComponent(Gold));
|
||||
}
|
||||
|
||||
private resolveHealthPotionContact(healthPotionCollider: Collider2D): void {
|
||||
this.itemManager.pickupHealthPotion(healthPotionCollider.node.getComponent(HealthPotion));
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ export class GameSettings {
|
||||
public upgrades: UpgradeSettings = new UpgradeSettings();
|
||||
public metaUpgrades: MetaUpgradesSettings = new MetaUpgradesSettings();
|
||||
public enemyManager: EnemyManagerSettings = new EnemyManagerSettings();
|
||||
public items: ItemSettings = new ItemSettings();
|
||||
}
|
||||
|
||||
export class PlayerSettings {
|
||||
@@ -111,6 +112,12 @@ export class EnemySettings {
|
||||
public damage = 0;
|
||||
public speed = 0;
|
||||
public lifetime = 0;
|
||||
|
||||
public xpReward = 0;
|
||||
public goldReward = 0;
|
||||
public healthPotionRewardChance = 0;
|
||||
}
|
||||
|
||||
export class ItemSettings {
|
||||
public healthPerPotion = 0;
|
||||
}
|
||||
|
@@ -79,7 +79,7 @@ export class Game extends Component {
|
||||
|
||||
this.player.init(multiInput, this.createPlayerData(settings.player, metaUpgrades));
|
||||
this.enemyManager.init(this.player.node, settings.enemyManager);
|
||||
this.itemManager.init(this.enemyManager, this.player, gameResult);
|
||||
this.itemManager.init(this.enemyManager, this.player, gameResult, settings.items);
|
||||
|
||||
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay, this.itemManager);
|
||||
new WeaponCollisionSystem(this.player.Weapon);
|
||||
|
@@ -8,5 +8,6 @@ export enum GroupType {
|
||||
XP = 1 << 4,
|
||||
PLAYER_PROJECTILE = 1 << 5,
|
||||
ENEMY_PROJECTILE = 1 << 6,
|
||||
GOLD = 1 << 7
|
||||
GOLD = 1 << 7,
|
||||
HEALTH_POTION = 1 << 8
|
||||
}
|
||||
|
12
assets/Scripts/Game/Items/HealthPotion.meta
Normal file
12
assets/Scripts/Game/Items/HealthPotion.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "5f34fac0-e23f-42da-9c26-d55b9df6ef32",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
27
assets/Scripts/Game/Items/HealthPotion/HealthPotion.ts
Normal file
27
assets/Scripts/Game/Items/HealthPotion/HealthPotion.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Animation, Component, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("HealthPotion")
|
||||
export class HealthPotion extends Component {
|
||||
@property(Animation) private animation: Animation;
|
||||
|
||||
private pickUpEvent: Signal<HealthPotion> = new Signal<HealthPotion>();
|
||||
|
||||
public setup(position: Vec3): void {
|
||||
this.node.setWorldPosition(position);
|
||||
this.node.active = true;
|
||||
this.animation.play("DropStart");
|
||||
}
|
||||
|
||||
public get PickupEvent(): ISignal<HealthPotion> {
|
||||
return this.pickUpEvent;
|
||||
}
|
||||
|
||||
public pickup(): void {
|
||||
this.pickUpEvent.trigger(this);
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "18f8de69-e361-4e73-8b98-a2735d0730a6",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
import { Component, Prefab, Vec3, _decorator } from "cc";
|
||||
import { ObjectPool } from "../../../Services/ObjectPool";
|
||||
import { HealthPotion } from "./HealthPotion";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("HealthPotionSpawner")
|
||||
export class HealthPotionSpawner extends Component {
|
||||
@property(Prefab) public healthPotionPrefab: Prefab;
|
||||
|
||||
private healthPotionPool: ObjectPool<HealthPotion>;
|
||||
|
||||
public init(): void {
|
||||
this.healthPotionPool = new ObjectPool<HealthPotion>(this.healthPotionPrefab, this.node, 5, "HealthPotion");
|
||||
}
|
||||
|
||||
public spawn(position: Vec3): void {
|
||||
const healthPotion: HealthPotion = this.healthPotionPool.borrow();
|
||||
healthPotion.setup(position);
|
||||
healthPotion.PickupEvent.on(this.return, this);
|
||||
}
|
||||
|
||||
private return(healthPotion: HealthPotion): void {
|
||||
healthPotion.PickupEvent.off(this.return);
|
||||
this.healthPotionPool.return(healthPotion);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1e3b0c7f-c831-4778-8688-d8e2c0f6dc00",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -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);
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { BoxCollider2D, Color, Component, Material, randomRange, Sprite, Vec3, _decorator } from "cc";
|
||||
import { BoxCollider2D, Component, Material, randomRange, Sprite, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
import { delay } from "../../../Services/Utils/AsyncUtils";
|
||||
@@ -28,6 +28,7 @@ export class Enemy extends Component {
|
||||
|
||||
private xpReward: number;
|
||||
private goldReward: number;
|
||||
private healthPotionRewardChance: number;
|
||||
|
||||
private endOfLifetimeTriggered = false;
|
||||
|
||||
@@ -42,6 +43,7 @@ export class Enemy extends Component {
|
||||
|
||||
this.xpReward = settings.xpReward;
|
||||
this.goldReward = settings.goldReward;
|
||||
this.healthPotionRewardChance = settings.healthPotionRewardChance;
|
||||
|
||||
this.node.setWorldPosition(position);
|
||||
this.node.active = true;
|
||||
@@ -82,6 +84,10 @@ export class Enemy extends Component {
|
||||
return this.goldReward;
|
||||
}
|
||||
|
||||
public get HealthPotionRewardChance(): number {
|
||||
return this.healthPotionRewardChance;
|
||||
}
|
||||
|
||||
public get LifetimeEndedEvent(): ISignal<Enemy> {
|
||||
return this.lifetimeEndedEvent;
|
||||
}
|
||||
|
Reference in New Issue
Block a user