This commit is contained in:
Martin
2022-11-16 12:26:20 +01:00
parent 3dd10f13ef
commit 0eb9cc907f
23 changed files with 1312 additions and 485 deletions

View File

@@ -3,6 +3,7 @@ import { GroupType } from "../GroupType";
import { Player } from "../Player/Player";
import { GameTimer } from "../../Services/GameTimer";
import { Enemy } from "../Enemy/Enemy";
import { XP } from "../XP/XP";
export class PlayerCollisionSystem {
private playerContacts: Collider2D[] = [];
@@ -20,6 +21,7 @@ export class PlayerCollisionSystem {
this.collisionTimer = new GameTimer(collisionDelay);
this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this));
this.groupToResolver.set(GroupType.XP, this.resolveXpContact.bind(this));
}
public gameTick(deltaTime: number): void {
@@ -60,4 +62,12 @@ export class PlayerCollisionSystem {
console.log("Collided with enemy: Damage: " + damage);
this.player.Health.damage(damage);
}
private resolveXpContact(xpCollider: Collider2D): void {
const xp: XP = xpCollider.node.getComponent(XP);
this.player.addXp(xp.Value);
xp.pickup();
console.log("Collided with xp: " + xp);
}
}

View File

@@ -1,4 +1,4 @@
import { BoxCollider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { BoxCollider2D, Component, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
import { UnitHealth } from "../Player/UnitHealth";
@@ -10,10 +10,13 @@ export class Enemy extends Component implements IDamageDealing {
private health: UnitHealth = new UnitHealth(1);
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
private speed: number;
public setup(): void {
this.node.active = true;
public setup(position: Vec3): void {
this.health = new UnitHealth(1);
this.speed = randomRange(0.5, 1);
this.node.setWorldPosition(position);
this.node.active = true;
}
public get Collider(): BoxCollider2D {
@@ -41,8 +44,8 @@ export class Enemy extends Component implements IDamageDealing {
public moveBy(move: Vec3): void {
const newPosition: Vec3 = this.node.worldPosition;
newPosition.x += move.x;
newPosition.y += move.y;
newPosition.x += move.x * this.speed;
newPosition.y += move.y * this.speed;
this.node.setWorldPosition(newPosition);
}

View File

@@ -0,0 +1,41 @@
import { Component, Node, _decorator } from "cc";
import { XPSpawner } from "../XP/XPSpawner";
import { Enemy } from "./Enemy";
import { EnemyMover } from "./EnemyMover";
import { EnemySpawner } from "./EnemySpawner";
const { ccclass, property } = _decorator;
@ccclass("EnemyManager")
export class EnemyManager extends Component {
@property(EnemySpawner) private enemySpawner: EnemySpawner;
@property(XPSpawner) private xpSpawner: XPSpawner;
private enemyMover: EnemyMover;
public init(targetNode: Node): void {
this.enemyMover = new EnemyMover(targetNode);
this.enemySpawner.init();
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
this.xpSpawner.init();
}
public gameTick(deltaTime: number): void {
this.enemySpawner.gameTick(deltaTime);
this.enemyMover.gameTick(deltaTime);
}
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
this.enemyMover.addEnemy(enemy);
}
private onEnemyDied(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
this.xpSpawner.spawnXp(enemy.node.worldPosition, 1);
this.enemyMover.removeEnemy(enemy);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "c5416aed-64b1-426e-959b-bc7d5c745e76",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -21,7 +21,7 @@ export class EnemyMover {
public gameTick(deltaTime: number): void {
this.enemies.forEach((enemy) => {
let direction: Vec3 = new Vec3();
direction = Vec3.subtract(direction, this.targetNode.position, enemy.node.position);
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
enemy.moveBy(direction.multiplyScalar(deltaTime).normalize());
});
}

View File

@@ -1,22 +1,23 @@
import { Component, Prefab, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
import { GameTimer } from "../../Services/GameTimer";
import { ObjectPool } from "../../Services/ObjectPool";
import { Enemy } from "./Enemy";
import { EnemyMover } from "./EnemyMover";
const { ccclass, property } = _decorator;
@ccclass("EnemySpawner")
export class EnemySpawner extends Component {
@property(Prefab) private enemies: Prefab[] = [];
public enemyAddedEvent: Signal<Enemy> = new Signal<Enemy>();
private enemyPool: ObjectPool<Enemy>;
private spawnTimer: GameTimer;
private enemyMover: EnemyMover;
public init(enemyMover: EnemyMover): void {
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, Enemy);
public init(): void {
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, "Enemy");
this.spawnTimer = new GameTimer(1);
this.enemyMover = enemyMover;
}
public gameTick(deltaTime: number): void {
@@ -24,26 +25,23 @@ export class EnemySpawner extends Component {
if (this.spawnTimer.tryFinishPeriod()) {
this.spawnNewEnemy();
}
}
this.enemyMover.gameTick(deltaTime);
public get EnemyAddedEvent(): ISignal<Enemy> {
return this.enemyAddedEvent;
}
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.setup(new Vec3(randomRange(0, 300), randomRange(0, 800)));
enemy.DeathEvent.on(this.returnEnemyToPool, this);
this.enemyMover.addEnemy(enemy);
this.enemyAddedEvent.trigger(enemy);
}
private returnEnemyToPool(enemy: Enemy): void {
enemy.DeathEvent.off(this.returnEnemyToPool);
this.enemyPool.return(enemy);
this.enemyMover.removeEnemy(enemy);
}
}

View File

@@ -1,13 +1,12 @@
import { Component, KeyCode, _decorator } from "cc";
import { Camera, CCFloat, Component, KeyCode, _decorator } from "cc";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { EnemySpawner } from "./Enemy/EnemySpawner";
import { EnemyManager } from "./Enemy/EnemyManager";
import { KeyboardInput } from "./Input/KeyboardInput";
import { MultiInput } from "./Input/MultiInput";
import { VirtualJoystic } from "./Input/VirtualJoystic";
import { KeyboardInput } from "./Input/KeyboardInput";
import { Player } from "./Player/Player";
import { Weapon } from "./Weapon";
import { EnemyMover } from "./Enemy/EnemyMover";
const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper")
@@ -15,9 +14,10 @@ export class GameBootstrapper extends Component {
@property(VirtualJoystic) private virtualJoystic: VirtualJoystic;
@property(Player) private player: Player;
@property(Weapon) private weapon: Weapon;
@property(EnemySpawner) private enemySpawner: EnemySpawner;
@property(Number) private strikeDelay = 0;
@property(Number) private collisionDelay = 0;
@property(EnemyManager) private enemyManager: EnemyManager;
@property(CCFloat) private strikeDelay = 0;
@property(CCFloat) private collisionDelay = 0;
@property(Camera) private camera: Camera;
private playerCollisionSystem: PlayerCollisionSystem;
public start(): void {
@@ -28,15 +28,17 @@ export class GameBootstrapper extends Component {
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);
new WeaponCollisionSystem(this.weapon);
this.enemyManager.init(this.player.node);
}
public update(deltaTime: number): void {
this.player.gameTick(deltaTime);
this.playerCollisionSystem.gameTick(deltaTime);
this.enemySpawner.gameTick(deltaTime);
this.enemyManager.gameTick(deltaTime);
this.camera.node.worldPosition = this.player.node.worldPosition;
}
}

View File

@@ -4,5 +4,6 @@ export enum GroupType {
DEFAULT = 1 << 0,
PLAYER = 1 << 1,
ENEMY = 1 << 2,
WEAPON = 1 << 3
WEAPON = 1 << 3,
XP = 1 << 4
}

View File

@@ -1,10 +1,10 @@
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc";
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch, CCFloat } from "cc";
import { IInput } from "./IInput";
const { ccclass, property } = _decorator;
@ccclass("VirtualJoystic")
export class VirtualJoystic extends Component implements IInput {
@property(Number) private maxDistance = 10;
@property(CCFloat) private maxDistance = 10;
@property(Node) private knob: Node;
#isUsingJoystic = false;
@@ -35,6 +35,7 @@ export class VirtualJoystic extends Component implements IInput {
}
private activateMouseJoystic(e: EventMouse): void {
console.log(e.getUILocation());
this.activateJoystic(e.getUILocation());
}
@@ -43,7 +44,7 @@ export class VirtualJoystic extends Component implements IInput {
this.node.active = true;
this.#defaultPosition = location;
this.node.setWorldPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0));
this.node.setPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0));
this.knob.position = new Vec3();
}

View File

@@ -1,4 +1,4 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { BoxCollider2D, Collider2D, Component, PointToPointConstraint, Vec2, Vec3, _decorator } from "cc";
import { IInput } from "../Input/IInput";
import { Weapon } from "../Weapon";
import { PlayerUI } from "./PlayerUI/PlayerUI";
@@ -15,6 +15,8 @@ export class Player extends Component {
private weapon: Weapon;
private health: UnitHealth;
private xp: number;
public init(input: IInput, weapon: Weapon, maxHp: number): void {
this.input = input;
this.weapon = weapon;
@@ -34,6 +36,10 @@ export class Player extends Component {
return this.collider;
}
public addXp(points: number): void {
this.xp += points;
}
public gameTick(deltaTime: number): void {
const movement: Vec2 = this.input.getAxis();
movement.x *= deltaTime * this.speed;
@@ -45,6 +51,6 @@ export class Player extends Component {
this.node.setWorldPosition(newPosition);
this.weapon.gameTick(deltaTime, movement);
this.weapon.gameTick(deltaTime);
}
}

View File

@@ -1,6 +1,5 @@
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { Animation, AnimationState, BoxCollider2D, Collider2D, Component, _decorator } from "cc";
import { GameTimer } from "../Services/GameTimer";
import { delay } from "../Services/Utils/AsyncUtils";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
@@ -9,24 +8,21 @@ export class Weapon extends Component {
@property(BoxCollider2D) private collider: BoxCollider2D;
private strikeTimer: GameTimer;
private lastDirection = new Vec2();
private strikeState: AnimationState;
public init(strikeDelay: number): void {
this.strikeTimer = new GameTimer(strikeDelay);
this.node.active = false;
this.weaponAnimation.on(Animation.EventType.FINISHED, this.endStrike, this);
this.strikeState = this.weaponAnimation.getState(this.weaponAnimation.clips[0].name);
this.strikeState.speed = 1;
}
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;
}
public gameTick(deltaTime: number): void {
this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) {
this.strike(direction);
this.strike();
}
}
@@ -38,16 +34,12 @@ export class Weapon extends Component {
return 5;
}
private async strike(direction: Vec2): Promise<void> {
private strike(): void {
this.node.active = true;
this.weaponAnimation.play(this.strikeState.name);
}
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI - 45;
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
this.weaponAnimation.play("WeaponSwing");
await delay(1000);
private endStrike(): void {
this.node.active = false;
}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "1e85dd0e-3cbc-4870-9a6a-50e0c29cfeb7",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,29 @@
import { Component, Vec3, _decorator } from "cc";
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
const { ccclass, property } = _decorator;
@ccclass("XP")
export class XP extends Component {
private pickUpEvent: Signal<XP> = new Signal<XP>();
private value = 2;
public setup(position: Vec3, value: number): void {
this.node.setWorldPosition(position);
this.value = value;
this.node.active = true;
}
public get Value(): number {
return this.value;
}
public get PickupEvent(): ISignal<XP> {
return this.pickUpEvent;
}
public pickup(): void {
this.pickUpEvent.trigger(this);
this.node.active = false;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "7a5361b6-3ae7-45b6-94ec-a05f322d7896",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,25 @@
import { Component, Prefab, Vec3, _decorator } from "cc";
import { ObjectPool } from "../../Services/ObjectPool";
import { XP } from "./XP";
const { ccclass, property } = _decorator;
@ccclass("XPSpawner")
export class XPSpawner extends Component {
@property(Prefab) public xpPrefab: Prefab;
private xpPool: ObjectPool<XP>;
public init(): void {
this.xpPool = new ObjectPool<XP>(this.xpPrefab, this.node, 5, "XP");
}
public spawnXp(position: Vec3, value: number): void {
const xp: XP = this.xpPool.borrow();
xp.setup(position, value);
xp.PickupEvent.on(this.returnXp, this);
}
private returnXp(xp: XP): void {
xp.PickupEvent.off(this.returnXp);
this.xpPool.return(xp);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ab9e206b-f11a-417c-814b-07d29c59ed6e",
"files": [],
"subMetas": {},
"userData": {}
}