mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-09-24 04:39:05 +00:00
Folder structure
This commit is contained in:
23
assets/Scripts/Game/Unit/Enemy/EnemyMover/EnemyMover.ts
Normal file
23
assets/Scripts/Game/Unit/Enemy/EnemyMover/EnemyMover.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Node } from "cc";
|
||||
import { Enemy } from "../Enemy";
|
||||
|
||||
export abstract class EnemyMover {
|
||||
protected targetNode: Node;
|
||||
protected 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 abstract gameTick(deltaTime: number): void;
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "179f7bfa-44fc-4f41-9432-4aec4f3991a6",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
import { Vec3 } from "cc";
|
||||
import { EnemyMover } from "./EnemyMover";
|
||||
|
||||
export class FollowTargetEnemyMover extends EnemyMover {
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.enemies.forEach((enemy) => {
|
||||
let direction: Vec3 = new Vec3();
|
||||
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
|
||||
enemy.moveBy(direction.normalize(), deltaTime);
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "20a3f56b-402d-4639-9629-c90f70f55206",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
36
assets/Scripts/Game/Unit/Enemy/EnemyMover/WaveEnemyMover.ts
Normal file
36
assets/Scripts/Game/Unit/Enemy/EnemyMover/WaveEnemyMover.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Vec3 } from "cc";
|
||||
import { Enemy } from "../Enemy";
|
||||
import { EnemyMover } from "./EnemyMover";
|
||||
|
||||
export class WaveEnemyMover extends EnemyMover {
|
||||
private enemyToDirection: Map<Enemy, Vec3> = new Map<Enemy, Vec3>();
|
||||
private lastTargetPosition: Vec3 = new Vec3();
|
||||
private lastDirection: Vec3 = new Vec3();
|
||||
|
||||
public addEnemy(enemy: Enemy): void {
|
||||
let direction: Vec3 = new Vec3();
|
||||
|
||||
// if the enemy is added soon enough, move as a single group towards one direction
|
||||
if (Vec3.distance(this.lastTargetPosition, this.targetNode.worldPosition) < 10) {
|
||||
direction = this.lastDirection;
|
||||
} else {
|
||||
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
|
||||
this.lastDirection = direction;
|
||||
this.lastTargetPosition = this.targetNode.worldPosition.clone();
|
||||
}
|
||||
|
||||
this.enemyToDirection.set(enemy, direction.normalize());
|
||||
super.addEnemy(enemy);
|
||||
}
|
||||
|
||||
public removeEnemy(enemy: Enemy): void {
|
||||
this.enemyToDirection.delete(enemy);
|
||||
super.removeEnemy(enemy);
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
for (const enemyAndDirection of this.enemyToDirection) {
|
||||
enemyAndDirection[0].moveBy(enemyAndDirection[1], deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "5215f6f3-855e-4d8e-afad-80915ca7b2f2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user