mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 08:36:14 +00:00
Added object pool, event system collision system
This commit is contained in:
7
assets/Scripts/Services/ContactParams.ts
Normal file
7
assets/Scripts/Services/ContactParams.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Collider2D, IPhysics2DContact } from "cc";
|
||||
|
||||
export type ContactParams = {
|
||||
selfCollider: Collider2D;
|
||||
otherCollider: Collider2D;
|
||||
contact: IPhysics2DContact | null;
|
||||
};
|
9
assets/Scripts/Services/ContactParams.ts.meta
Normal file
9
assets/Scripts/Services/ContactParams.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ee9d41d4-91f4-4d8f-a4ef-7e0df147fb35",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Services/EventSystem.meta
Normal file
12
assets/Scripts/Services/EventSystem.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "53ace3f5-848d-4d10-a12a-9f5a5ae681a2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
4
assets/Scripts/Services/EventSystem/ISignal.ts
Normal file
4
assets/Scripts/Services/EventSystem/ISignal.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface ISignal<T> {
|
||||
on(handler: (data: T) => void): void;
|
||||
off(handler: (data: T) => void): void;
|
||||
}
|
9
assets/Scripts/Services/EventSystem/ISignal.ts.meta
Normal file
9
assets/Scripts/Services/EventSystem/ISignal.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "626b0371-bbf5-4b70-988e-48e9e3ebb171",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
16
assets/Scripts/Services/EventSystem/Signal.ts
Normal file
16
assets/Scripts/Services/EventSystem/Signal.ts
Normal 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));
|
||||
}
|
||||
}
|
9
assets/Scripts/Services/EventSystem/Signal.ts.meta
Normal file
9
assets/Scripts/Services/EventSystem/Signal.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "421ff10c-7e13-4fa7-844a-003fd7e738ca",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
85
assets/Scripts/Services/ObjectPool.ts
Normal file
85
assets/Scripts/Services/ObjectPool.ts
Normal 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;
|
||||
}
|
||||
}
|
9
assets/Scripts/Services/ObjectPool.ts.meta
Normal file
9
assets/Scripts/Services/ObjectPool.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a4ed3b4c-8989-4f08-9303-4d7a3ae90703",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user