mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 00:26:04 +00:00
Gold gathering
This commit is contained in:
@@ -4,15 +4,16 @@ import { Player } from "../Unit/Player/Player";
|
||||
import { GameTimer } from "../../Services/GameTimer";
|
||||
import { XP } from "../XP/XP";
|
||||
import { Enemy } from "../Unit/Enemy/Enemy";
|
||||
import { Gold } from "../Gold/Gold";
|
||||
import { GameResult } from "../Game";
|
||||
|
||||
export class PlayerCollisionSystem {
|
||||
private playerContacts: Collider2D[] = [];
|
||||
private collisionTimer: GameTimer;
|
||||
private player: Player;
|
||||
|
||||
private groupToResolver: Map<number, (collider: Collider2D) => void> = new Map<number, (collider: Collider2D) => void>();
|
||||
|
||||
public constructor(player: Player, collisionDelay: number) {
|
||||
public constructor(private player: Player, collisionDelay: number, private gameResult: GameResult) {
|
||||
this.player = player;
|
||||
|
||||
player.Collider.on(Contact2DType.BEGIN_CONTACT, this.onPlayerContactBegin, this);
|
||||
@@ -22,6 +23,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));
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
@@ -70,4 +72,12 @@ export class PlayerCollisionSystem {
|
||||
|
||||
console.log("Collided with xp: " + xp);
|
||||
}
|
||||
|
||||
private resolveGoldContact(goldCollider: Collider2D): void {
|
||||
const gold: Gold = goldCollider.node.getComponent(Gold);
|
||||
gold.pickup();
|
||||
this.gameResult.goldCoins++;
|
||||
|
||||
console.log("Collided with gold " + gold);
|
||||
}
|
||||
}
|
||||
|
@@ -65,6 +65,7 @@ export class Game extends Component {
|
||||
translationData: TranslationData,
|
||||
testValues?: TestValues
|
||||
): Promise<GameResult> {
|
||||
const gameResult = new GameResult();
|
||||
const metaUpgrades = new MetaUpgrades(userData.game.metaUpgrades, settings.metaUpgrades);
|
||||
|
||||
this.virtualJoystic.init();
|
||||
@@ -75,7 +76,7 @@ export class Game extends Component {
|
||||
|
||||
this.player.init(multiInput, this.createPlayerData(settings.player, metaUpgrades));
|
||||
|
||||
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay);
|
||||
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, settings.player.collisionDelay, gameResult);
|
||||
new WeaponCollisionSystem(this.player.Weapon);
|
||||
|
||||
this.enemyManager.init(this.player.node, settings.enemyManager);
|
||||
@@ -128,11 +129,11 @@ export class Game extends Component {
|
||||
|
||||
this.gamePauser.resume();
|
||||
|
||||
// while not dead
|
||||
await delay(1000000);
|
||||
while (this.player.Health.IsAlive) await delay(100);
|
||||
this.gamePauser.pause();
|
||||
Game.instance = null;
|
||||
return { goldCoins: 1, score: Math.floor(this.timeAlive) };
|
||||
gameResult.score = this.timeAlive;
|
||||
return gameResult;
|
||||
}
|
||||
|
||||
public update(deltaTime: number): void {
|
||||
|
12
assets/Scripts/Game/Gold.meta
Normal file
12
assets/Scripts/Game/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": {}
|
||||
}
|
||||
}
|
23
assets/Scripts/Game/Gold/Gold.ts
Normal file
23
assets/Scripts/Game/Gold/Gold.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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/Gold/Gold.ts.meta
Normal file
9
assets/Scripts/Game/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/Gold/GoldSpawner.ts
Normal file
26
assets/Scripts/Game/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/Gold/GoldSpawner.ts.meta
Normal file
9
assets/Scripts/Game/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": {}
|
||||
}
|
@@ -7,5 +7,6 @@ export enum GroupType {
|
||||
WEAPON = 1 << 3,
|
||||
XP = 1 << 4,
|
||||
PLAYER_PROJECTILE = 1 << 5,
|
||||
ENEMY_PROJECTILE = 1 << 6
|
||||
ENEMY_PROJECTILE = 1 << 6,
|
||||
GOLD = 1 << 7
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import { Component, Node, randomRange, Vec3, _decorator } from "cc";
|
||||
import { Component, Node, random, randomRange, Vec3, _decorator } from "cc";
|
||||
|
||||
import { EnemyManagerSettings } from "../../Data/GameSettings";
|
||||
import { GoldSpawner } from "../../Gold/GoldSpawner";
|
||||
import { XPSpawner } from "../../XP/XPSpawner";
|
||||
import { Enemy } from "./Enemy";
|
||||
import { EnemyMovementType } from "./EnemyMovementType";
|
||||
@@ -20,6 +21,7 @@ const { ccclass, property } = _decorator;
|
||||
export class EnemyManager extends Component {
|
||||
@property(EnemySpawner) private enemySpawner: EnemySpawner;
|
||||
@property(XPSpawner) private xpSpawner: XPSpawner;
|
||||
@property(GoldSpawner) private goldSpawner: GoldSpawner;
|
||||
|
||||
private movementTypeToMover: Map<EnemyMovementType, EnemyMover> = new Map<EnemyMovementType, EnemyMover>();
|
||||
|
||||
@@ -50,6 +52,7 @@ export class EnemyManager extends Component {
|
||||
this.movementTypeToMover.set(EnemyMovementType.PeriodicFollow, new PeriodicFollowTargetEnemyMover(targetNode, 5, 5));
|
||||
|
||||
this.xpSpawner.init();
|
||||
this.goldSpawner.init();
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
@@ -72,6 +75,21 @@ export class EnemyManager extends Component {
|
||||
position.y += randomRange(-10, 10);
|
||||
this.xpSpawner.spawnXp(position, 1);
|
||||
}
|
||||
|
||||
if (0 < enemy.GoldReward) {
|
||||
if (enemy.GoldReward < 1) {
|
||||
if (random() < enemy.GoldReward) {
|
||||
this.goldSpawner.spawn(enemy.node.worldPosition);
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < enemy.GoldReward; i++) {
|
||||
const position: Vec3 = enemy.node.worldPosition;
|
||||
position.x += randomRange(-10, 10);
|
||||
position.y += randomRange(-10, 10);
|
||||
this.goldSpawner.spawn(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private onEnemyLifetimeEnded(enemy: Enemy): void {
|
||||
|
Reference in New Issue
Block a user