Added health potion

This commit is contained in:
Martin 2022-12-20 13:37:56 +01:00
parent 1c009e9fa4
commit dbb55957a0
17 changed files with 409 additions and 195 deletions

View File

@ -145,7 +145,8 @@
"speed": 60, "speed": 60,
"lifetime": -1, "lifetime": -1,
"xpReward": 1, "xpReward": 1,
"goldReward": 0 "goldReward": 0,
"healthPotionRewardChance": 0.5
}, },
{ {
"id": "StandardEnemy", "id": "StandardEnemy",
@ -156,7 +157,8 @@
"speed": 65, "speed": 65,
"lifetime": -1, "lifetime": -1,
"xpReward": 1, "xpReward": 1,
"goldReward": 0.1 "goldReward": 0.1,
"healthPotionRewardChance": 0
}, },
{ {
"id": "FastEnemy", "id": "FastEnemy",
@ -167,7 +169,8 @@
"speed": 75, "speed": 75,
"lifetime": -1, "lifetime": -1,
"xpReward": 1, "xpReward": 1,
"goldReward": 0.1 "goldReward": 0.1,
"healthPotionRewardChance": 0
}, },
{ {
"id": "CircleEnemy", "id": "CircleEnemy",
@ -178,7 +181,8 @@
"speed": 25, "speed": 25,
"lifetime": 29, "lifetime": 29,
"xpReward": 1, "xpReward": 1,
"goldReward": 1 "goldReward": 1,
"healthPotionRewardChance": 0
}, },
{ {
"id": "CircleEnemyStandard", "id": "CircleEnemyStandard",
@ -189,7 +193,8 @@
"speed": 35, "speed": 35,
"lifetime": 27, "lifetime": 27,
"xpReward": 1, "xpReward": 1,
"goldReward": 2 "goldReward": 2,
"healthPotionRewardChance": 0
}, },
{ {
"id": "WaveEnemy", "id": "WaveEnemy",
@ -200,7 +205,8 @@
"speed": 390, "speed": 390,
"lifetime": 20, "lifetime": 20,
"xpReward": 1, "xpReward": 1,
"goldReward": 0.2 "goldReward": 0.2,
"healthPotionRewardChance": 0
}, },
{ {
"id": "WaveEnemyArmor", "id": "WaveEnemyArmor",
@ -211,7 +217,8 @@
"speed": 70, "speed": 70,
"lifetime": 30, "lifetime": 30,
"xpReward": 1, "xpReward": 1,
"goldReward": 1 "goldReward": 1,
"healthPotionRewardChance": 0
}, },
{ {
"id": "WaveEnemyArmorFast", "id": "WaveEnemyArmorFast",
@ -222,7 +229,8 @@
"speed": 320, "speed": 320,
"lifetime": 30, "lifetime": 30,
"xpReward": 1, "xpReward": 1,
"goldReward": 1 "goldReward": 1,
"healthPotionRewardChance": 0
}, },
{ {
"id": "BasicBoss", "id": "BasicBoss",
@ -233,7 +241,8 @@
"speed": 65, "speed": 65,
"lifetime": -1, "lifetime": -1,
"xpReward": 1, "xpReward": 1,
"goldReward": 3 "goldReward": 3,
"healthPotionRewardChance": 0
}, },
{ {
"id": "StandardBoss", "id": "StandardBoss",
@ -244,7 +253,8 @@
"speed": 75, "speed": 75,
"lifetime": -1, "lifetime": -1,
"xpReward": 2, "xpReward": 2,
"goldReward": 5 "goldReward": 5,
"healthPotionRewardChance": 0
}, },
{ {
"id": "BasicCheetah", "id": "BasicCheetah",
@ -255,7 +265,8 @@
"speed": 120, "speed": 120,
"lifetime": -1, "lifetime": -1,
"xpReward": 1, "xpReward": 1,
"goldReward": 1 "goldReward": 1,
"healthPotionRewardChance": 0
} }
], ],
"periodicFollowMovers": [ "periodicFollowMovers": [
@ -483,5 +494,8 @@
"enemiesToSpawn": 14 "enemiesToSpawn": 14
} }
] ]
},
"items": {
"healthPerPotion": 10
} }
} }

View File

@ -85,10 +85,10 @@
"__type__": "cc.RealCurve", "__type__": "cc.RealCurve",
"_times": [ "_times": [
0, 0,
0.16666666666666666, 0.1666666716337204,
0.3333333333333333, 0.3333333432674408,
0.45, 0.44999998807907104,
0.55 0.550000011920929
], ],
"_values": [ "_values": [
{ {
@ -268,7 +268,7 @@
"__type__": "cc.RealCurve", "__type__": "cc.RealCurve",
"_times": [ "_times": [
0, 0,
0.16666666666666666 0.1666666716337204
], ],
"_values": [ "_values": [
{ {

View File

@ -1,7 +1,7 @@
[ [
{ {
"__type__": "cc.Prefab", "__type__": "cc.Prefab",
"_name": "Health", "_name": "HealthPotion",
"_objFlags": 0, "_objFlags": 0,
"_native": "", "_native": "",
"data": { "data": {
@ -13,7 +13,7 @@
}, },
{ {
"__type__": "cc.Node", "__type__": "cc.Node",
"_name": "Health", "_name": "HealthPotion",
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"_parent": null, "_parent": null,
@ -314,15 +314,15 @@
"__id__": 15 "__id__": 15
}, },
"tag": 0, "tag": 0,
"_group": 16, "_group": 256,
"_density": 1, "_density": 1,
"_sensor": false, "_sensor": false,
"_friction": 0.2, "_friction": 0.2,
"_restitution": 0, "_restitution": 0,
"_offset": { "_offset": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": 0, "x": 0.5,
"y": -7.5 "y": -3.4
}, },
"_radius": 8, "_radius": 8,
"_id": "" "_id": ""
@ -331,23 +331,6 @@
"__type__": "cc.CompPrefabInfo", "__type__": "cc.CompPrefabInfo",
"fileId": "75WVRoLrtKLrJihCVsxa4D" "fileId": "75WVRoLrtKLrJihCVsxa4D"
}, },
{
"__type__": "7a536G2OudFtpTsoF8yLXiW",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 17
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "72AObOIbxCf6LIYsYqoFZF"
},
{ {
"__type__": "cc.Animation", "__type__": "cc.Animation",
"_name": "", "_name": "",
@ -357,7 +340,7 @@
}, },
"_enabled": true, "_enabled": true,
"__prefab": { "__prefab": {
"__id__": 19 "__id__": 17
}, },
"playOnLoad": true, "playOnLoad": true,
"_clips": [ "_clips": [
@ -376,6 +359,26 @@
"__type__": "cc.CompPrefabInfo", "__type__": "cc.CompPrefabInfo",
"fileId": "3ag4rexLNJW6A/sp6OpwZ8" "fileId": "3ag4rexLNJW6A/sp6OpwZ8"
}, },
{
"__type__": "18f8d5p42FOc4uYonNdBzCm",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"__prefab": {
"__id__": 19
},
"animation": {
"__id__": 16
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "65lXWGNONBy5ucPc5KmOaR"
},
{ {
"__type__": "cc.PrefabInfo", "__type__": "cc.PrefabInfo",
"root": { "root": {

View File

@ -8,6 +8,6 @@
], ],
"subMetas": {}, "subMetas": {},
"userData": { "userData": {
"syncNodeName": "Health" "syncNodeName": "HealthPotion"
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ import { Signal } from "../../Services/EventSystem/Signal";
import { GameTimer } from "../../Services/GameTimer"; import { GameTimer } from "../../Services/GameTimer";
import { GroupType } from "../GroupType"; import { GroupType } from "../GroupType";
import { Gold } from "../Items/Gold/Gold"; import { Gold } from "../Items/Gold/Gold";
import { HealthPotion } from "../Items/HealthPotion/HealthPotion";
import { ItemManager } from "../Items/ItemManager"; import { ItemManager } from "../Items/ItemManager";
import { XP } from "../Items/XP/XP"; import { XP } from "../Items/XP/XP";
import { Enemy } from "../Unit/Enemy/Enemy"; 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.ENEMY, this.resolveEnemyContact.bind(this));
this.groupToResolver.set(GroupType.XP, this.resolveXpContact.bind(this)); this.groupToResolver.set(GroupType.XP, this.resolveXpContact.bind(this));
this.groupToResolver.set(GroupType.GOLD, this.resolveGoldContact.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 { public gameTick(deltaTime: number): void {
@ -81,4 +83,8 @@ export class PlayerCollisionSystem {
private resolveGoldContact(goldCollider: Collider2D): void { private resolveGoldContact(goldCollider: Collider2D): void {
this.itemManager.pickupGold(goldCollider.node.getComponent(Gold)); this.itemManager.pickupGold(goldCollider.node.getComponent(Gold));
} }
private resolveHealthPotionContact(healthPotionCollider: Collider2D): void {
this.itemManager.pickupHealthPotion(healthPotionCollider.node.getComponent(HealthPotion));
}
} }

View File

@ -3,6 +3,7 @@ export class GameSettings {
public upgrades: UpgradeSettings = new UpgradeSettings(); public upgrades: UpgradeSettings = new UpgradeSettings();
public metaUpgrades: MetaUpgradesSettings = new MetaUpgradesSettings(); public metaUpgrades: MetaUpgradesSettings = new MetaUpgradesSettings();
public enemyManager: EnemyManagerSettings = new EnemyManagerSettings(); public enemyManager: EnemyManagerSettings = new EnemyManagerSettings();
public items: ItemSettings = new ItemSettings();
} }
export class PlayerSettings { export class PlayerSettings {
@ -111,6 +112,12 @@ export class EnemySettings {
public damage = 0; public damage = 0;
public speed = 0; public speed = 0;
public lifetime = 0; public lifetime = 0;
public xpReward = 0; public xpReward = 0;
public goldReward = 0; public goldReward = 0;
public healthPotionRewardChance = 0;
}
export class ItemSettings {
public healthPerPotion = 0;
} }

View File

@ -79,7 +79,7 @@ export class Game extends Component {
this.player.init(multiInput, this.createPlayerData(settings.player, metaUpgrades)); this.player.init(multiInput, this.createPlayerData(settings.player, metaUpgrades));
this.enemyManager.init(this.player.node, settings.enemyManager); 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); this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay, this.itemManager);
new WeaponCollisionSystem(this.player.Weapon); new WeaponCollisionSystem(this.player.Weapon);

View File

@ -8,5 +8,6 @@ export enum GroupType {
XP = 1 << 4, XP = 1 << 4,
PLAYER_PROJECTILE = 1 << 5, PLAYER_PROJECTILE = 1 << 5,
ENEMY_PROJECTILE = 1 << 6, ENEMY_PROJECTILE = 1 << 6,
GOLD = 1 << 7 GOLD = 1 << 7,
HEALTH_POTION = 1 << 8
} }

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "5f34fac0-e23f-42da-9c26-d55b9df6ef32",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View 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;
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "18f8de69-e361-4e73-8b98-a2735d0730a6",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "1e3b0c7f-c831-4778-8688-d8e2c0f6dc00",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,10 +1,13 @@
import { Component, random, randomRange, Vec3, _decorator } from "cc"; import { Component, random, randomRange, Vec3, _decorator } from "cc";
import { ItemSettings } from "../Data/GameSettings";
import { GameResult } from "../Game"; import { GameResult } from "../Game";
import { Enemy } from "../Unit/Enemy/Enemy"; import { Enemy } from "../Unit/Enemy/Enemy";
import { EnemyManager } from "../Unit/Enemy/EnemyManager"; import { EnemyManager } from "../Unit/Enemy/EnemyManager";
import { Player } from "../Unit/Player/Player"; import { Player } from "../Unit/Player/Player";
import { Gold } from "./Gold/Gold"; import { Gold } from "./Gold/Gold";
import { GoldSpawner } from "./Gold/GoldSpawner"; import { GoldSpawner } from "./Gold/GoldSpawner";
import { HealthPotion } from "./HealthPotion/HealthPotion";
import { HealthPotionSpawner } from "./HealthPotion/HealthPotionSpawner";
import { PickupEffectManager } from "./PickupEffect/PickupEffectManager"; import { PickupEffectManager } from "./PickupEffect/PickupEffectManager";
import { XP } from "./XP/XP"; import { XP } from "./XP/XP";
import { XPSpawner } from "./XP/XPSpawner"; import { XPSpawner } from "./XP/XPSpawner";
@ -14,20 +17,24 @@ const { ccclass, property } = _decorator;
export class ItemManager extends Component { export class ItemManager extends Component {
@property(XPSpawner) private xpSpawner: XPSpawner; @property(XPSpawner) private xpSpawner: XPSpawner;
@property(GoldSpawner) private goldSpawner: GoldSpawner; @property(GoldSpawner) private goldSpawner: GoldSpawner;
@property(HealthPotionSpawner) private healthPotionSpawner: HealthPotionSpawner;
@property(PickupEffectManager) private pickupEffectManager: PickupEffectManager; @property(PickupEffectManager) private pickupEffectManager: PickupEffectManager;
private player: Player; private player: Player;
private gameResult: GameResult; 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.player = player;
this.gameResult = gameResult; this.gameResult = gameResult;
this.healthPerPotion = settings.healthPerPotion;
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this); enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this); enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
this.xpSpawner.init(); this.xpSpawner.init();
this.goldSpawner.init(); this.goldSpawner.init();
this.healthPotionSpawner.init();
this.pickupEffectManager.init(); this.pickupEffectManager.init();
} }
@ -45,6 +52,13 @@ export class ItemManager extends Component {
this.gameResult.goldCoins++; 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 { private addEnemyListeners(enemy: Enemy): void {
enemy.DeathEvent.on(this.trySpawnItems, this); enemy.DeathEvent.on(this.trySpawnItems, this);
} }
@ -56,6 +70,7 @@ export class ItemManager extends Component {
private trySpawnItems(enemy: Enemy): void { private trySpawnItems(enemy: Enemy): void {
this.trySpawnXP(enemy); this.trySpawnXP(enemy);
this.trySpawnGold(enemy); this.trySpawnGold(enemy);
this.trySpawnHealthPotion(enemy);
} }
private trySpawnXP(enemy: Enemy): void { 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 { private getRandomPosition(enemy: Enemy): Vec3 {
const position: Vec3 = enemy.node.worldPosition; const position: Vec3 = enemy.node.worldPosition;
position.x += randomRange(-10, 10); position.x += randomRange(-10, 10);

View File

@ -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 { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal"; import { Signal } from "../../../Services/EventSystem/Signal";
import { delay } from "../../../Services/Utils/AsyncUtils"; import { delay } from "../../../Services/Utils/AsyncUtils";
@ -28,6 +28,7 @@ export class Enemy extends Component {
private xpReward: number; private xpReward: number;
private goldReward: number; private goldReward: number;
private healthPotionRewardChance: number;
private endOfLifetimeTriggered = false; private endOfLifetimeTriggered = false;
@ -42,6 +43,7 @@ export class Enemy extends Component {
this.xpReward = settings.xpReward; this.xpReward = settings.xpReward;
this.goldReward = settings.goldReward; this.goldReward = settings.goldReward;
this.healthPotionRewardChance = settings.healthPotionRewardChance;
this.node.setWorldPosition(position); this.node.setWorldPosition(position);
this.node.active = true; this.node.active = true;
@ -82,6 +84,10 @@ export class Enemy extends Component {
return this.goldReward; return this.goldReward;
} }
public get HealthPotionRewardChance(): number {
return this.healthPotionRewardChance;
}
public get LifetimeEndedEvent(): ISignal<Enemy> { public get LifetimeEndedEvent(): ISignal<Enemy> {
return this.lifetimeEndedEvent; return this.lifetimeEndedEvent;
} }

View File

@ -28,17 +28,22 @@
{ {
"index": 7, "index": 7,
"name": "GOLD" "name": "GOLD"
},
{
"index": 8,
"name": "HEALTH_POTION"
} }
], ],
"collisionMatrix": { "collisionMatrix": {
"0": 0, "0": 0,
"1": 212, "1": 468,
"2": 42, "2": 42,
"3": 4, "3": 4,
"4": 2, "4": 2,
"5": 4, "5": 4,
"6": 2, "6": 2,
"7": 2 "7": 2,
"8": 2
} }
}, },
"general": { "general": {