Added object pool, event system collision system

This commit is contained in:
Martin
2022-11-08 11:42:14 +01:00
parent f250c24f90
commit 5b098af31d
21 changed files with 757 additions and 243 deletions

View File

@@ -0,0 +1,22 @@
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

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

19
assets/Scripts/Enemy.ts Normal file
View File

@@ -0,0 +1,19 @@
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;
}

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

@@ -1,20 +1,51 @@
import { Component, _decorator } from "cc";
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.player.init(this.virtualJoystic, this.strikeDelay);
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

@@ -1,18 +1,26 @@
import { Component, Vec2, Vec3, _decorator } from "cc";
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
import { VirtualJoystic } from "./VirtualJoystic";
import { Weapon } from "./Weapon";
const { ccclass, property } = _decorator;
@ccclass("Player")
export class Player extends Component {
private virtualJoystic: VirtualJoystic;
@property private speed = 0;
@property(BoxCollider2D) private collider: BoxCollider2D;
@property(Weapon) private weapon: Weapon;
private virtualJoystic: VirtualJoystic;
private weapon: Weapon;
public init(virtualJoystic: VirtualJoystic, strikeDelay: number): void {
public init(virtualJoystic: VirtualJoystic, weapon: Weapon): void {
this.virtualJoystic = virtualJoystic;
this.weapon.init(strikeDelay);
this.weapon = weapon;
this.weapon.node.parent = this.node;
this.weapon.node.setPosition(new Vec3());
}
public get Collider(): Collider2D {
return this.collider;
}
public gameTick(deltaTime: number): void {

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "cebf49b9-55bd-4829-bb97-cb8bd1378a21",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -0,0 +1,7 @@
import { Collider2D, IPhysics2DContact } from "cc";
export type ContactParams = {
selfCollider: Collider2D;
otherCollider: Collider2D;
contact: IPhysics2DContact | null;
};

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ee9d41d4-91f4-4d8f-a4ef-7e0df147fb35",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "53ace3f5-848d-4d10-a12a-9f5a5ae681a2",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

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

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "626b0371-bbf5-4b70-988e-48e9e3ebb171",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,16 @@
import { ISignal } from "./ISignal";
export class Signal<T> implements ISignal<T> {
private handlers: ((data: T) => void)[] = [];
public on(handler: (data: T) => void): void {
this.handlers.push(handler);
}
public off(handler: (data: T) => void): void {
this.handlers = this.handlers.filter((h) => h !== handler);
}
public trigger(data: T): void {
[...this.handlers].forEach((handler) => handler(data));
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "421ff10c-7e13-4fa7-844a-003fd7e738ca",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,85 @@
import { Component, instantiate, Node, Prefab } from "cc";
export class ObjectPool<T extends Component> {
private prefab: Prefab;
private parent: Node;
private pooledObjects: PooledObject<T>[] = [];
private componentType: { new (): T };
public constructor(prefab: Prefab, parent: Node, defaultPoolCount: number, componentType: { new (): T }) {
this.prefab = prefab;
this.parent = parent;
this.componentType = componentType;
for (let i = 0; i < defaultPoolCount; i++) {
this.pooledObjects.push(this.createNew());
}
}
public borrow(): T {
const objectToBorrow: PooledObject<T> | null = this.pooledObjects.find((o) => !o.IsBorrowed);
if (objectToBorrow != null) {
return objectToBorrow.borrow();
}
return this.createNew().borrow();
}
public return(object: T): void {
const objectToReturn: PooledObject<T> | null = this.pooledObjects.find((o) => o.Equals(object));
if (objectToReturn == null) {
throw new Error("Object " + this.prefab.name + " is not a member of the pool");
}
objectToReturn.return();
}
private createNew(): PooledObject<T> {
const newPooledObject: PooledObject<T> = new PooledObject(this.prefab, this.parent, this.componentType);
this.pooledObjects.push(newPooledObject);
return newPooledObject;
}
}
class PooledObject<T extends Component> {
private isBorrowed = false;
private defaultParent: Node;
private instancedNode: Node;
private instancedComponent: T;
public constructor(prefab: Prefab, defaultParent: Node, componentType: { new (): T }) {
this.defaultParent = defaultParent;
this.instancedNode = instantiate(prefab);
this.instancedComponent = <T>this.instancedNode.getComponent(componentType.name);
if (this.instancedComponent == null) {
throw new Error("Object " + prefab.name + " does not have component " + componentType.name);
}
this.clear();
}
public get IsBorrowed(): boolean {
return this.isBorrowed;
}
public Equals(component: T): boolean {
return this.instancedComponent == component;
}
public borrow(): T {
this.isBorrowed = true;
return this.instancedComponent;
}
public return(): void {
this.clear();
}
private clear(): void {
this.instancedNode.active = false;
this.instancedNode.parent = this.defaultParent;
this.isBorrowed = false;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "a4ed3b4c-8989-4f08-9303-4d7a3ae90703",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -1,35 +1,36 @@
import { Animation, BoxCollider2D, Component, Contact2DType, Vec2, Vec3, _decorator } from "cc";
import { Animation, BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("Weapon")
export class Weapon extends Component {
@property(Animation) private weaponAnimation: Animation;
@property(BoxCollider2D) private weaponCollider: BoxCollider2D;
@property(BoxCollider2D) private collider: BoxCollider2D;
private strikeDelay: number;
private currentDelay = 0;
public init(strikeDelay: number): void {
this.strikeDelay = strikeDelay;
this.weaponCollider.on(Contact2DType.BEGIN_CONTACT, () => {
console.log("Begin Contact!");
});
}
public gameTick(deltaTime: number, movement: Vec2): void {
this.currentDelay += deltaTime;
if (this.strikeDelay <= this.currentDelay) {
if (this.strikeDelay / 4 <= this.currentDelay) {
this.currentDelay = 0;
this.strike(movement);
}
}
public get Collider(): Collider2D {
return this.collider;
}
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");
}
}