new structure, spawner, rebind

This commit is contained in:
Martin
2022-11-08 19:45:57 +01:00
parent 5b098af31d
commit 279218b4c3
45 changed files with 1157 additions and 302 deletions

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "06e5187a-01d6-4e7c-a597-cf6b9be31705",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,63 @@
import { Collider2D, Contact2DType } from "cc";
import { GroupType } from "../GroupType";
import { Player } from "../Player/Player";
import { GameTimer } from "../../Services/GameTimer";
import { Enemy } from "../Enemy/Enemy";
export class PlayerCollisionSystem {
private playerContacts: Collider2D[] = [];
private collisionTimer: GameTimer;
private player: Player;
private groupToResolver: Map<number, (collider: Collider2D) => void> = new Map<number, (collider: Collider2D) => void>();
public constructor(player: Player, collisionDelay: number) {
this.player = player;
player.Collider.on(Contact2DType.BEGIN_CONTACT, this.onPlayerContactBegin, this);
player.Collider.on(Contact2DType.END_CONTACT, this.onPlayerContactEnd, this);
this.collisionTimer = new GameTimer(collisionDelay);
this.groupToResolver.set(GroupType.ENEMY, this.resolveEnemyContact.bind(this));
}
public gameTick(deltaTime: number): void {
this.collisionTimer.gameTick(deltaTime);
if (this.collisionTimer.tryFinishPeriod()) {
this.resolveAllContacts();
}
}
private onPlayerContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
this.playerContacts.push(otherCollider);
this.resolveContact(otherCollider);
}
private onPlayerContactEnd(_selfCollider: Collider2D, otherCollider: Collider2D): void {
const index: number = this.playerContacts.indexOf(otherCollider);
if (index != -1) {
this.playerContacts.splice(index, 1);
}
}
private resolveAllContacts(): void {
for (let i = 0; i < this.playerContacts.length; i++) {
this.resolveContact(this.playerContacts[i]);
}
}
private resolveContact(otherCollider: Collider2D): void {
if (this.groupToResolver.has(otherCollider.group)) {
this.groupToResolver.get(otherCollider.group)(otherCollider);
} else {
console.log("Collided with undefined group: " + otherCollider.group);
}
}
private resolveEnemyContact(enemyCollider: Collider2D): void {
const damage: number = enemyCollider.node.getComponent(Enemy).Damage;
console.log("Collided with enemy: Damage: " + damage);
this.player.Health.damage(damage);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ea4efcef-c9f5-4116-b43d-7cae85bf298f",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,15 @@
import { Collider2D, Contact2DType } from "cc";
import { Enemy } from "../Enemy/Enemy";
import { Weapon } from "../Weapon";
export class WeaponCollisionSystem {
private weapon: Weapon;
public constructor(weapon: Weapon) {
this.weapon = weapon;
weapon.Collider.on(Contact2DType.BEGIN_CONTACT, this.onWeaponContactBegin, this);
}
private onWeaponContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
otherCollider.getComponent(Enemy).dealDamage(this.weapon.Damage);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "eea89f2a-424b-4169-b2b7-7bf5261e9750",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "e71832fc-dfd7-409d-924d-f87037c811e4",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

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

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "e6f969c0-2783-4eb3-b467-356c68db4512",
"files": [],
"subMetas": {},
"userData": {}
}

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

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "8aa2a393-cd51-4bb4-a979-095e6fce95a9",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,34 @@
import { Component, _decorator } from "cc";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { WeaponCollisionSystem } from "./Collision/WeaponCollisionSystem";
import { EnemySpawner } from "./Enemy/EnemySpawner";
import { VirtualJoystic } from "./Input/VirtualJoystic";
import { Player } from "./Player/Player";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ccclass("GameBootstrapper")
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;
private playerCollisionSystem: PlayerCollisionSystem;
public start(): void {
this.virtualJoystic.init();
this.weapon.init(this.strikeDelay);
this.player.init(this.virtualJoystic, this.weapon, 50);
this.enemySpawner.init();
this.playerCollisionSystem = new PlayerCollisionSystem(this.player, this.collisionDelay);
new WeaponCollisionSystem(this.weapon);
}
public update(deltaTime: number): void {
this.player.gameTick(deltaTime);
this.playerCollisionSystem.gameTick(deltaTime);
this.enemySpawner.gameTick(deltaTime);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "5faa9913-e167-4014-92de-7061e103e557",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,8 @@
// Flags, shift must match index in ProjectSettings > Physics
export enum GroupType {
DEFAULT = 1 << 0,
PLAYER = 1 << 1,
ENEMY = 1 << 2,
WEAPON = 1 << 3
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "99f179ea-aaed-4e55-9e65-b4d5e45fc997",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "7a57756d-1dfb-4d05-adfd-1401435d75f6",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,79 @@
import { _decorator, Component, Node, Vec3, input, Input, EventMouse, Vec2, EventTouch } from "cc";
const { ccclass, property } = _decorator;
@ccclass("VirtualJoystic")
export class VirtualJoystic extends Component {
@property(Number) private maxDistance = 10;
@property(Node) private knob: Node;
#isUsingJoystic = false;
#defaultPosition: Vec2 = new Vec2();
public init(): void {
input.on(Input.EventType.MOUSE_DOWN, this.activateMouseJoystic, this);
input.on(Input.EventType.MOUSE_UP, this.deactivateJoystic, this);
input.on(Input.EventType.MOUSE_MOVE, this.moveKnobMouse, this);
input.on(Input.EventType.TOUCH_START, this.activateTouchJoystic, this);
input.on(Input.EventType.TOUCH_END, this.deactivateJoystic, this);
input.on(Input.EventType.TOUCH_MOVE, this.moveKnobTouch, this);
this.deactivateJoystic();
}
public getAxis(): Vec2 {
if (this.#isUsingJoystic) {
return new Vec2(this.knob.position.x / this.maxDistance, this.knob.position.y / this.maxDistance);
} else {
return new Vec2();
}
}
private activateTouchJoystic(e: EventTouch): void {
this.activateJoystic(e.getUILocation());
}
private activateMouseJoystic(e: EventMouse): void {
this.activateJoystic(e.getUILocation());
}
private activateJoystic(location: Vec2): void {
this.#isUsingJoystic = true;
this.node.active = true;
this.#defaultPosition = location;
this.node.setWorldPosition(new Vec3(this.#defaultPosition.x, this.#defaultPosition.y, 0));
this.knob.position = new Vec3();
}
private deactivateJoystic(): void {
this.#isUsingJoystic = false;
this.node.active = false;
}
private moveKnobTouch(e: EventTouch): void {
this.moveKnob(e.getUILocation());
}
private moveKnobMouse(e: EventMouse): void {
this.moveKnob(e.getUILocation());
}
private moveKnob(location: Vec2): void {
if (!this.#isUsingJoystic) return;
const posDelta: Vec2 = location.subtract(this.#defaultPosition);
let x: number = posDelta.x;
let y: number = posDelta.y;
const length: number = Math.sqrt(posDelta.x ** 2 + posDelta.y ** 2);
if (this.maxDistance < length) {
const multiplier: number = this.maxDistance / length;
x *= multiplier;
y *= multiplier;
}
this.knob.position = new Vec3(x, y, 0);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a51b28bc-4ef0-4324-97fc-44f964ce087a",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "3697ba4f-16f0-4a2d-9330-47c472df973a",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,50 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "../Input/VirtualJoystic";
import { Weapon } from "../Weapon";
import { UnitHealth } from "./UnitHealth";
import { PlayerUI } from "./PlayerUI/PlayerUI";
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends Component {
@property private speed = 0;
@property(BoxCollider2D) private collider: BoxCollider2D;
@property(PlayerUI) private playerUI: PlayerUI;
private virtualJoystic: VirtualJoystic;
private weapon: Weapon;
private health: UnitHealth;
public init(virtualJoystic: VirtualJoystic, weapon: Weapon, maxHp: number): void {
this.virtualJoystic = virtualJoystic;
this.weapon = weapon;
this.health = new UnitHealth(maxHp);
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
this.playerUI.init(this.health);
}
public get Health(): UnitHealth {
return this.health;
}
public get Collider(): Collider2D {
return this.collider;
}
public gameTick(deltaTime: number): void {
const movement: Vec2 = this.virtualJoystic.getAxis();
movement.x *= deltaTime * this.speed;
movement.y *= deltaTime * this.speed;
const newPosition: Vec3 = this.node.worldPosition;
newPosition.x += movement.x;
newPosition.y += movement.y;
this.node.setWorldPosition(newPosition);
this.weapon.gameTick(deltaTime, movement);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "b570abb8-1bcd-4ef0-85e8-4d783531001d",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "82d14029-0ea6-4de6-b796-05339d2d19b1",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,19 @@
import { Component, ProgressBar, _decorator } from "cc";
import { UnitHealth } from "../UnitHealth";
const { ccclass, property } = _decorator;
@ccclass("PlayerHealthUI")
export class PlayerHealthUI extends Component {
@property(ProgressBar) public healthBar: ProgressBar;
private health: UnitHealth;
public init(health: UnitHealth): void {
this.healthBar.progress = 1;
this.health = health;
this.health.HealthPointsChangeEvent.on(this.updateHealthBar, this);
}
private updateHealthBar(): void {
this.healthBar.progress = this.health.HealthPoints / this.health.MaxHealthPoints;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "84a1c057-2ef8-4ead-8903-5753f36ac735",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,13 @@
import { Component, _decorator } from "cc";
import { UnitHealth } from "../UnitHealth";
import { PlayerHealthUI } from "./PlayerHealthUI";
const { ccclass, property } = _decorator;
@ccclass("PlayerUI")
export class PlayerUI extends Component {
@property(PlayerHealthUI) private healthUI: PlayerHealthUI;
public init(playerHealth: UnitHealth): void {
this.healthUI.init(playerHealth);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "634f676f-c335-42a2-85e8-f214e9ce5eb7",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,42 @@
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
export class UnitHealth {
private healthPoints: number;
private maxHealthPoints: number;
private healthPointsChangeEvent: Signal<number> = new Signal<number>();
public constructor(maxHealth: number) {
this.maxHealthPoints = maxHealth;
this.healthPoints = maxHealth;
}
public get IsAlive(): boolean {
return 0 < this.healthPoints;
}
public get HealthPoints(): number {
return this.healthPoints;
}
public get MaxHealthPoints(): number {
return this.maxHealthPoints;
}
public get HealthPointsChangeEvent(): ISignal<number> {
return this.healthPointsChangeEvent;
}
public heal(points: number): void {
this.healthPoints = Math.min(this.maxHealthPoints, this.healthPoints + points);
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public damage(points: number): void {
this.healthPoints -= points;
this.healthPointsChangeEvent.trigger(this.healthPoints);
}
public setMaxHealth(maxHealth: number): void {
this.maxHealthPoints = maxHealth;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "8118806a-dc7c-4e94-84c2-b0a95be43209",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,39 @@
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { GameTimer } from "../Services/GameTimer";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private collider: BoxCollider2D;
private strikeTimer: GameTimer;
public init(strikeDelay: number): void {
this.strikeTimer = new GameTimer(strikeDelay);
}
public gameTick(deltaTime: number, movement: Vec2): void {
this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) {
this.strike(movement);
}
}
public get Collider(): Collider2D {
return this.collider;
}
public get Damage(): number {
return 5;
}
private strike(movement: Vec2): void {
const direction: Vec2 = movement.normalize();
const angle: number = (Math.atan2(direction.y, direction.x) * 180) / Math.PI;
this.node.eulerAngles = new Vec3(0, 0, angle);
this.weaponAnimation.getState("WeaponSwing").speed = 4;
this.weaponAnimation.play("WeaponSwing");
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "2391b8b7-f9fa-42a8-b046-55ff57b07d02",
"files": [],
"subMetas": {},
"userData": {}
}