mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-27 01:16:07 +00:00
new structure, spawner, rebind
This commit is contained in:
45
assets/Scripts/Game/Enemy/Enemy.ts
Normal file
45
assets/Scripts/Game/Enemy/Enemy.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { BoxCollider2D, Component, _decorator } from "cc";
|
||||
import { ISignal } from "../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../Services/EventSystem/Signal";
|
||||
import { UnitHealth } from "../Player/UnitHealth";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Enemy")
|
||||
export class Enemy extends Component implements IDamageDealing {
|
||||
@property(BoxCollider2D) public collider: BoxCollider2D;
|
||||
|
||||
private health: UnitHealth = new UnitHealth(1);
|
||||
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
|
||||
|
||||
public setup(): void {
|
||||
this.node.active = true;
|
||||
this.health = new UnitHealth(1);
|
||||
}
|
||||
|
||||
public get Collider(): BoxCollider2D {
|
||||
return this.collider;
|
||||
}
|
||||
|
||||
public get Damage(): number {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public get Health(): UnitHealth {
|
||||
return this.health;
|
||||
}
|
||||
|
||||
public get DeathEvent(): ISignal<Enemy> {
|
||||
return this.deathEvent;
|
||||
}
|
||||
|
||||
public dealDamage(points: number): void {
|
||||
this.health.damage(points);
|
||||
if (!this.health.IsAlive) {
|
||||
this.deathEvent.trigger(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDamageDealing {
|
||||
Damage: number;
|
||||
}
|
||||
9
assets/Scripts/Game/Enemy/Enemy.ts.meta
Normal file
9
assets/Scripts/Game/Enemy/Enemy.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e6f969c0-2783-4eb3-b467-356c68db4512",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
41
assets/Scripts/Game/Enemy/EnemySpawner.ts
Normal file
41
assets/Scripts/Game/Enemy/EnemySpawner.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
|
||||
import { GameTimer } from "../../Services/GameTimer";
|
||||
import { ObjectPool } from "../../Services/ObjectPool";
|
||||
import { Enemy } from "./Enemy";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("EnemySpawner")
|
||||
export class EnemySpawner extends Component {
|
||||
@property(Prefab) private enemies: Prefab[] = [];
|
||||
|
||||
private enemyPool: ObjectPool<Enemy>;
|
||||
private spawnTimer: GameTimer;
|
||||
|
||||
public init(): void {
|
||||
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, Enemy);
|
||||
this.spawnTimer = new GameTimer(5);
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.spawnTimer.gameTick(deltaTime);
|
||||
if (this.spawnTimer.tryFinishPeriod()) {
|
||||
this.spawnNewEnemy();
|
||||
}
|
||||
}
|
||||
|
||||
private spawnNewEnemy(): void {
|
||||
const enemy = this.enemyPool.borrow();
|
||||
enemy.node.active = true;
|
||||
enemy.node.setPosition(new Vec3(randomRange(-300, 300), randomRange(-300, 300)));
|
||||
|
||||
enemy.setup();
|
||||
|
||||
enemy.DeathEvent.on(this.returnEnemyToPool, this);
|
||||
}
|
||||
|
||||
private returnEnemyToPool(enemy: Enemy): void {
|
||||
console.log("Return to enemy pool");
|
||||
enemy.DeathEvent.off(this.returnEnemyToPool);
|
||||
this.enemyPool.return(enemy);
|
||||
}
|
||||
}
|
||||
9
assets/Scripts/Game/Enemy/EnemySpawner.ts.meta
Normal file
9
assets/Scripts/Game/Enemy/EnemySpawner.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8aa2a393-cd51-4bb4-a979-095e6fce95a9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user