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

@ -147,7 +147,7 @@
"__id__": 7
},
"tag": 0,
"_group": 1,
"_group": 4,
"_density": 1,
"_sensor": false,
"_friction": 0.2,
@ -179,6 +179,9 @@
"__prefab": {
"__id__": 9
},
"collider": {
"__id__": 6
},
"_id": ""
},
{

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +0,0 @@
import { Collider2D, Contact2DType } from "cc";
import { Enemy, IDamageDealing } from "./Enemy";
import { Player } from "./Player";
import { Weapon } from "./Weapon";
export class CollisionSystem {
public constructor(player: Player, weapon: Weapon) {
player.Collider.on(Contact2DType.BEGIN_CONTACT, this.onPlayerContactBegin, this);
weapon.Collider.on(Contact2DType.BEGIN_CONTACT, this.onWeaponContactBegin, this);
}
private onWeaponContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
console.log("Weapon contact! " + otherCollider.node.name);
}
private onPlayerContactBegin(_selfCollider: Collider2D, otherCollider: Collider2D): void {
console.log("Player contact! " + otherCollider.node.name);
const damageDealing: IDamageDealing = otherCollider.node.getComponent(Enemy);
console.log("DAMAGE: " + damageDealing.Damage);
}
}

View File

@ -1,19 +0,0 @@
import { BoxCollider2D, Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("Enemy")
export class Enemy extends Component implements IDamageDealing {
@property(BoxCollider2D) public collider: BoxCollider2D;
public get Collider(): BoxCollider2D {
return this.collider;
}
public get Damage(): number {
return 3;
}
}
export interface IDamageDealing {
Damage: number;
}

12
assets/Scripts/Game.meta Normal file
View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "e0f54aa0-97e2-4780-9d6e-c51245df0815",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

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,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,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,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,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "3697ba4f-16f0-4a2d-9330-47c472df973a",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -1,22 +1,33 @@
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "./VirtualJoystic";
import { Weapon } from "./Weapon";
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): void {
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 {

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

@ -1,4 +1,5 @@
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { GameTimer } from "../Services/GameTimer";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
@ -6,17 +7,15 @@ export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private collider: BoxCollider2D;
private strikeDelay: number;
private currentDelay = 0;
private strikeTimer: GameTimer;
public init(strikeDelay: number): void {
this.strikeDelay = strikeDelay;
this.strikeTimer = new GameTimer(strikeDelay);
}
public gameTick(deltaTime: number, movement: Vec2): void {
this.currentDelay += deltaTime;
if (this.strikeDelay / 4 <= this.currentDelay) {
this.currentDelay = 0;
this.strikeTimer.gameTick(deltaTime);
if (this.strikeTimer.tryFinishPeriod()) {
this.strike(movement);
}
}
@ -25,6 +24,10 @@ export class Weapon extends Component {
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;

View File

@ -1,51 +0,0 @@
import { Component, instantiate, Prefab, random, randomRange, Vec3, _decorator } from "cc";
import { CollisionSystem } from "./CollisionSystem";
import { Enemy } from "./Enemy";
import { Player } from "./Player";
import { ObjectPool } from "./Services/ObjectPool";
import { VirtualJoystic } from "./VirtualJoystic";
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(Prefab) private enemy: Prefab;
@property(Number) private strikeDelay = 0;
public start(): void {
this.virtualJoystic.init();
this.weapon.init(this.strikeDelay);
this.player.init(this.virtualJoystic, this.weapon);
new CollisionSystem(this.player, this.weapon);
const op: ObjectPool<Player> = new ObjectPool(this.enemy, this.node, 10, Player);
const borrowed: Player[] = [];
for (let index = 0; index < 7; index++) {
const enemy: Player = op.borrow();
enemy.node.parent = this.node;
enemy.node.active = true;
enemy.node.setPosition(new Vec3(randomRange(-200, 200)));
if (index < 5) borrowed.push(enemy);
}
borrowed.forEach((borrowedEnemy) => {
op.return(borrowedEnemy);
});
}
public update(deltaTime: number): void {
this.player.gameTick(deltaTime);
}
public getEnemy<T extends Component>(): T {
const i = instantiate(this.enemy);
i.parent = this.node;
return <T>i.getComponent(Enemy.name);
}
}

View File

@ -2,7 +2,7 @@
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "cebf49b9-55bd-4829-bb97-cb8bd1378a21",
"uuid": "43b42e2e-f3c3-4174-bd1f-d268a2e8c53a",
"files": [],
"subMetas": {},
"userData": {

View File

@ -1,4 +1,4 @@
export interface ISignal<T> {
on(handler: (data: T) => void): void;
on(handler: (data: T) => void, thisArg: any): void;
off(handler: (data: T) => void): void;
}

View File

@ -2,15 +2,23 @@ import { ISignal } from "./ISignal";
export class Signal<T> implements ISignal<T> {
private handlers: ((data: T) => void)[] = [];
private thisArgs: any[] = [];
public on(handler: (data: T) => void): void {
public on(handler: (data: T) => void, thisArg: any): void {
this.handlers.push(handler);
this.thisArgs.push(thisArg);
}
public off(handler: (data: T) => void): void {
console.log("[OFF] " + this.handlers.length);
this.handlers = this.handlers.filter((h) => h !== handler);
console.log("[OFF] >> " + this.handlers.length);
}
public trigger(data: T): void {
[...this.handlers].forEach((handler) => handler(data));
//[...this.handlers].forEach((handler) => handler(data));
for (let i = 0; i < this.handlers.length; i++) {
this.handlers[i].call(this.thisArgs[i], data);
}
}
}

View File

@ -0,0 +1,21 @@
export class GameTimer {
private targetDelay: number;
private currentDelay = 0;
public constructor(targetDelay: number) {
this.targetDelay = targetDelay;
}
public gameTick(deltaTime: number): void {
this.currentDelay += deltaTime;
}
public tryFinishPeriod(): boolean {
if (this.targetDelay <= this.currentDelay) {
this.currentDelay = 0;
return true;
} else {
return false;
}
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "930242d8-1406-4fe0-9dd7-6b700ecff471",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "de96cc65-e1ab-47ec-b1c5-74cd116d4dc4",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "9f621aea-cba9-4ef2-82af-6fae7a3a0fb7",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,3 @@
export async function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a920e5e1-f469-4f4b-ad81-fa27d49f0389",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -0,0 +1,24 @@
{
"physics": {
"collisionGroups": [
{
"index": 1,
"name": "PLAYER"
},
{
"index": 2,
"name": "ENEMY"
},
{
"index": 3,
"name": "WEAPON"
}
],
"collisionMatrix": {
"0": 0,
"1": 4,
"2": 10,
"3": 4
}
}
}