mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-27 01:16:07 +00:00
added temp enemy mover
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { BoxCollider2D, Component, _decorator } from "cc";
|
||||
import { BoxCollider2D, Component, Vec2, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../Services/EventSystem/Signal";
|
||||
import { UnitHealth } from "../Player/UnitHealth";
|
||||
@@ -38,6 +38,14 @@ export class Enemy extends Component implements IDamageDealing {
|
||||
this.deathEvent.trigger(this);
|
||||
}
|
||||
}
|
||||
|
||||
public moveBy(move: Vec3): void {
|
||||
const newPosition: Vec3 = this.node.worldPosition;
|
||||
newPosition.x += move.x;
|
||||
newPosition.y += move.y;
|
||||
|
||||
this.node.setWorldPosition(newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDamageDealing {
|
||||
|
||||
28
assets/Scripts/Game/Enemy/EnemyMover.ts
Normal file
28
assets/Scripts/Game/Enemy/EnemyMover.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Node, Vec3 } from "cc";
|
||||
import { Enemy } from "./Enemy";
|
||||
|
||||
export class EnemyMover {
|
||||
private targetNode: Node;
|
||||
private enemies: Enemy[] = [];
|
||||
public constructor(targetNode: Node) {
|
||||
this.targetNode = targetNode;
|
||||
}
|
||||
public addEnemy(enemy: Enemy): void {
|
||||
this.enemies.push(enemy);
|
||||
}
|
||||
|
||||
public removeEnemy(enemy: Enemy): void {
|
||||
const index: number = this.enemies.indexOf(enemy);
|
||||
if (index != -1) {
|
||||
this.enemies.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.enemies.forEach((enemy) => {
|
||||
let direction: Vec3 = new Vec3();
|
||||
direction = Vec3.subtract(direction, this.targetNode.position, enemy.node.position);
|
||||
enemy.moveBy(direction.multiplyScalar(deltaTime).normalize());
|
||||
});
|
||||
}
|
||||
}
|
||||
9
assets/Scripts/Game/Enemy/EnemyMover.ts.meta
Normal file
9
assets/Scripts/Game/Enemy/EnemyMover.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "50aa2052-8c6f-4405-8732-346ac2a942c9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
|
||||
import { GameTimer } from "../../Services/GameTimer";
|
||||
import { ObjectPool } from "../../Services/ObjectPool";
|
||||
import { Enemy } from "./Enemy";
|
||||
import { EnemyMover } from "./EnemyMover";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("EnemySpawner")
|
||||
@@ -10,10 +11,12 @@ export class EnemySpawner extends Component {
|
||||
|
||||
private enemyPool: ObjectPool<Enemy>;
|
||||
private spawnTimer: GameTimer;
|
||||
private enemyMover: EnemyMover;
|
||||
|
||||
public init(): void {
|
||||
public init(enemyMover: EnemyMover): void {
|
||||
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, Enemy);
|
||||
this.spawnTimer = new GameTimer(5);
|
||||
this.spawnTimer = new GameTimer(1);
|
||||
this.enemyMover = enemyMover;
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
@@ -21,6 +24,8 @@ export class EnemySpawner extends Component {
|
||||
if (this.spawnTimer.tryFinishPeriod()) {
|
||||
this.spawnNewEnemy();
|
||||
}
|
||||
|
||||
this.enemyMover.gameTick(deltaTime);
|
||||
}
|
||||
|
||||
private spawnNewEnemy(): void {
|
||||
@@ -31,11 +36,14 @@ export class EnemySpawner extends Component {
|
||||
enemy.setup();
|
||||
|
||||
enemy.DeathEvent.on(this.returnEnemyToPool, this);
|
||||
|
||||
this.enemyMover.addEnemy(enemy);
|
||||
}
|
||||
|
||||
private returnEnemyToPool(enemy: Enemy): void {
|
||||
console.log("Return to enemy pool");
|
||||
enemy.DeathEvent.off(this.returnEnemyToPool);
|
||||
this.enemyPool.return(enemy);
|
||||
|
||||
this.enemyMover.removeEnemy(enemy);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user