added temp enemy mover

This commit is contained in:
Martin 2022-11-14 16:35:47 +01:00
parent 279218b4c3
commit 3dd10f13ef
16 changed files with 212 additions and 23 deletions

View File

@ -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 { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal"; import { Signal } from "../../Services/EventSystem/Signal";
import { UnitHealth } from "../Player/UnitHealth"; import { UnitHealth } from "../Player/UnitHealth";
@ -38,6 +38,14 @@ export class Enemy extends Component implements IDamageDealing {
this.deathEvent.trigger(this); 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 { export interface IDamageDealing {

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

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "50aa2052-8c6f-4405-8732-346ac2a942c9",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -2,6 +2,7 @@ import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
import { GameTimer } from "../../Services/GameTimer"; import { GameTimer } from "../../Services/GameTimer";
import { ObjectPool } from "../../Services/ObjectPool"; import { ObjectPool } from "../../Services/ObjectPool";
import { Enemy } from "./Enemy"; import { Enemy } from "./Enemy";
import { EnemyMover } from "./EnemyMover";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("EnemySpawner") @ccclass("EnemySpawner")
@ -10,10 +11,12 @@ export class EnemySpawner extends Component {
private enemyPool: ObjectPool<Enemy>; private enemyPool: ObjectPool<Enemy>;
private spawnTimer: GameTimer; 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.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 { public gameTick(deltaTime: number): void {
@ -21,6 +24,8 @@ export class EnemySpawner extends Component {
if (this.spawnTimer.tryFinishPeriod()) { if (this.spawnTimer.tryFinishPeriod()) {
this.spawnNewEnemy(); this.spawnNewEnemy();
} }
this.enemyMover.gameTick(deltaTime);
} }
private spawnNewEnemy(): void { private spawnNewEnemy(): void {
@ -31,11 +36,14 @@ export class EnemySpawner extends Component {
enemy.setup(); enemy.setup();
enemy.DeathEvent.on(this.returnEnemyToPool, this); enemy.DeathEvent.on(this.returnEnemyToPool, this);
this.enemyMover.addEnemy(enemy);
} }
private returnEnemyToPool(enemy: Enemy): void { private returnEnemyToPool(enemy: Enemy): void {
console.log("Return to enemy pool");
enemy.DeathEvent.off(this.returnEnemyToPool); enemy.DeathEvent.off(this.returnEnemyToPool);
this.enemyPool.return(enemy); this.enemyPool.return(enemy);
this.enemyMover.removeEnemy(enemy);
} }
} }

View File

@ -1,10 +1,13 @@
import { Component, _decorator } from "cc"; import { Component, KeyCode, _decorator } from "cc";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem"; import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem"; import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { EnemySpawner } from "./Enemy/EnemySpawner"; import { EnemySpawner } from "./Enemy/EnemySpawner";
import { MultiInput } from "./Input/MultiInput";
import { VirtualJoystic } from "./Input/VirtualJoystic"; import { VirtualJoystic } from "./Input/VirtualJoystic";
import { KeyboardInput } from "./Input/KeyboardInput";
import { Player } from "./Player/Player"; import { Player } from "./Player/Player";
import { Weapon } from "./Weapon"; import { Weapon } from "./Weapon";
import { EnemyMover } from "./Enemy/EnemyMover";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper") @ccclass("GameBootstrapper")
@ -20,8 +23,13 @@ export class GameBootstrapper extends Component {
public start(): void { public start(): void {
this.virtualJoystic.init(); this.virtualJoystic.init();
this.weapon.init(this.strikeDelay); this.weapon.init(this.strikeDelay);
this.player.init(this.virtualJoystic, this.weapon, 50); const wasd = new KeyboardInput(KeyCode.KEY_W, KeyCode.KEY_S, KeyCode.KEY_A, KeyCode.KEY_D);
this.enemySpawner.init(); const arrowKeys = new KeyboardInput(KeyCode.ARROW_UP, KeyCode.ARROW_DOWN, KeyCode.ARROW_LEFT, KeyCode.ARROW_RIGHT);
const dualInput: MultiInput = new MultiInput([this.virtualJoystic, wasd, arrowKeys]);
this.player.init(dualInput, this.weapon, 50);
const enemyMover = new EnemyMover(this.player.node);
this.enemySpawner.init(enemyMover);
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay); this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay);
new WeaponCollisionSystem(this.weapon); new WeaponCollisionSystem(this.weapon);
} }

View File

@ -0,0 +1,5 @@
import { Vec2 } from "cc";
export interface IInput {
getAxis: () => Vec2;
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "b82ab67e-d720-4feb-ada9-07b6704a2b88",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,60 @@
import { EventKeyboard, Input, input, KeyCode, Vec2 } from "cc";
import { IInput } from "./IInput";
export class KeyboardInput implements IInput {
private xAxis = 0;
private yAxis = 0;
private up: KeyCode;
private down: KeyCode;
private left: KeyCode;
private right: KeyCode;
public constructor(up: KeyCode, down: KeyCode, left: KeyCode, right: KeyCode) {
this.up = up;
this.down = down;
this.left = left;
this.right = right;
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
}
public getAxis(): Vec2 {
return new Vec2(this.xAxis, this.yAxis).normalize();
}
private onKeyDown(event: EventKeyboard): void {
switch (event.keyCode) {
case this.up:
this.yAxis += 1;
break;
case this.down:
this.yAxis += -1;
break;
case this.left:
this.xAxis += -1;
break;
case this.right:
this.xAxis += 1;
break;
}
}
private onKeyUp(event: EventKeyboard): void {
switch (event.keyCode) {
case this.up:
this.yAxis -= 1;
break;
case this.down:
this.yAxis -= -1;
break;
case this.left:
this.xAxis -= -1;
break;
case this.right:
this.xAxis -= 1;
break;
}
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "14d4988b-08bc-4a0e-97e7-b06b78b1a180",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,20 @@
import { Vec2 } from "cc";
import { IInput } from "./IInput";
export class MultiInput implements IInput {
private inputs: IInput[];
public constructor(inputs: IInput[]) {
this.inputs = inputs;
}
public getAxis(): Vec2 {
for (let i = 0; i < this.inputs.length; i++) {
if (!this.inputs[i].getAxis().equals(Vec2.ZERO)) {
return this.inputs[i].getAxis();
}
}
return new Vec2();
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "679d7c05-dfb4-43e8-8115-9af235222733",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -1,8 +1,9 @@
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc"; import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc";
import { IInput } from "./IInput";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("VirtualJoystic") @ccclass("VirtualJoystic")
export class VirtualJoystic extends Component { export class VirtualJoystic extends Component implements IInput {
@property(Number) private maxDistance = 10; @property(Number) private maxDistance = 10;
@property(Node) private knob: Node; @property(Node) private knob: Node;

View File

@ -1,8 +1,8 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc"; import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "../Input/VirtualJoystic"; import { IInput } from "../Input/IInput";
import { Weapon } from "../Weapon"; import { Weapon } from "../Weapon";
import { UnitHealth } from "./UnitHealth";
import { PlayerUI } from "./PlayerUI/PlayerUI"; import { PlayerUI } from "./PlayerUI/PlayerUI";
import { UnitHealth } from "./UnitHealth";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("Player") @ccclass("Player")
@ -11,12 +11,12 @@ export class Player extends Component {
@property(BoxCollider2D) private collider: BoxCollider2D; @property(BoxCollider2D) private collider: BoxCollider2D;
@property(PlayerUI) private playerUI: PlayerUI; @property(PlayerUI) private playerUI: PlayerUI;
private virtualJoystic: VirtualJoystic; private input: IInput;
private weapon: Weapon; private weapon: Weapon;
private health: UnitHealth; private health: UnitHealth;
public init(virtualJoystic: VirtualJoystic, weapon: Weapon, maxHp: number): void { public init(input: IInput, weapon: Weapon, maxHp: number): void {
this.virtualJoystic = virtualJoystic; this.input = input;
this.weapon = weapon; this.weapon = weapon;
this.health = new UnitHealth(maxHp); this.health = new UnitHealth(maxHp);
@ -35,7 +35,7 @@ export class Player extends Component {
} }
public gameTick(deltaTime: number): void { public gameTick(deltaTime: number): void {
const movement: Vec2 = this.virtualJoystic.getAxis(); const movement: Vec2 = this.input.getAxis();
movement.x *= deltaTime * this.speed; movement.x *= deltaTime * this.speed;
movement.y *= deltaTime * this.speed; movement.y *= deltaTime * this.speed;

View File

@ -1,5 +1,6 @@
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc"; import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { GameTimer } from "../Services/GameTimer"; import { GameTimer } from "../Services/GameTimer";
import { delay } from "../Services/Utils/AsyncUtils";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("Weapon") @ccclass("Weapon")
@ -8,15 +9,24 @@ export class Weapon extends Component {
@property(BoxCollider2D) private collider: BoxCollider2D; @property(BoxCollider2D) private collider: BoxCollider2D;
private strikeTimer: GameTimer; private strikeTimer: GameTimer;
private lastDirection = new Vec2();
public init(strikeDelay: number): void { public init(strikeDelay: number): void {
this.strikeTimer = new GameTimer(strikeDelay); this.strikeTimer = new GameTimer(strikeDelay);
this.node.active = false;
} }
public gameTick(deltaTime: number, movement: Vec2): void { public gameTick(deltaTime: number, movement: Vec2): void {
let direction: Vec2 = movement.normalize();
if (direction.x == 0 && direction.y == 0) {
direction = this.lastDirection;
} else {
this.lastDirection = direction;
}
this.strikeTimer.gameTick(deltaTime); this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) { if (this.strikeTimer.tryFinishPeriod()) {
this.strike(movement); this.strike(direction);
} }
} }
@ -28,12 +38,16 @@ export class Weapon extends Component {
return 5; return 5;
} }
private strike(movement: Vec2): void { private async strike(direction: Vec2): Promise<void> {
const direction: Vec2 = movement.normalize(); this.node.active = true;
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI;
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI - 45;
this.node.eulerAngles = new Vec3(0, 0, angle); this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4; this.weaponAnimation.getState("WeaponSwing").speed = 4;
this.weaponAnimation.play("WeaponSwing"); this.weaponAnimation.play("WeaponSwing");
await delay(1000);
this.node.active = false;
} }
} }

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface ISignal<T> { export interface ISignal<T> {
on(handler: (data: T) => void, thisArg: any): void; on(handler: (data: T) => void, thisArg: any): void;
off(handler: (data: T) => void): void; off(handler: (data: T) => void): void;

View File

@ -1,3 +1,5 @@
// Need to capture *this*
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ISignal } from "./ISignal"; import { ISignal } from "./ISignal";
export class Signal<T> implements ISignal<T> { export class Signal<T> implements ISignal<T> {
@ -9,14 +11,12 @@ export class Signal<T> implements ISignal<T> {
this.thisArgs.push(thisArg); this.thisArgs.push(thisArg);
} }
public off(handler: (data: T) => void): void { public off(handler: (data: T) => void): void {
console.log("[OFF] " + this.handlers.length); const index: number = this.handlers.indexOf(handler);
this.handlers = this.handlers.filter((h) => h !== handler); this.handlers.splice(index, 1);
console.log("[OFF] >> " + this.handlers.length); this.thisArgs.splice(index, 1);
} }
public trigger(data: T): void { public trigger(data: T): void {
//[...this.handlers].forEach((handler) => handler(data));
for (let i = 0; i < this.handlers.length; i++) { for (let i = 0; i < this.handlers.length; i++) {
this.handlers[i].call(this.thisArgs[i], data); this.handlers[i].call(this.thisArgs[i], data);
} }