Update framework
This commit is contained in:
5
framework/world/component.d.ts
vendored
Normal file
5
framework/world/component.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export declare class Component<T> {
|
||||
id: number;
|
||||
properties: T;
|
||||
constructor(props: T);
|
||||
}
|
5
framework/world/component.js
Normal file
5
framework/world/component.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export class Component {
|
||||
constructor(props) {
|
||||
this.properties = props;
|
||||
}
|
||||
}
|
24
framework/world/ecs-world.d.ts
vendored
Normal file
24
framework/world/ecs-world.d.ts
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import { World } from '../ecs/index';
|
||||
import { Game } from '../game';
|
||||
import { Component } from '../ecs/component';
|
||||
import { System } from '../ecs/system';
|
||||
import { BaseEntity, Entity } from '../ecs/entity';
|
||||
import { Query } from '../ecs/query';
|
||||
import { World as IWorld } from '.';
|
||||
export declare class ECSWorld implements IWorld {
|
||||
instance: Game;
|
||||
id: string;
|
||||
world: World;
|
||||
running: boolean;
|
||||
constructor(instance: Game);
|
||||
update(): void;
|
||||
updateDraw(): boolean;
|
||||
createEntity(components: Array<Component>): BaseEntity;
|
||||
createComponent(props: any): Component;
|
||||
createSystem(systemExecutor: Function): void;
|
||||
addSystem(system: System): void;
|
||||
addEntity(entity: BaseEntity): void;
|
||||
removeEntity(entity: BaseEntity): void;
|
||||
createQuery(include: Array<Component>, exclude: Array<Component>): Query;
|
||||
extendEntity(entity: Entity, components: Array<Component>): BaseEntity;
|
||||
}
|
40
framework/world/ecs-world.js
Normal file
40
framework/world/ecs-world.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import { World } from '../ecs/index';
|
||||
export class ECSWorld {
|
||||
constructor(instance) {
|
||||
this.instance = instance;
|
||||
this.running = true;
|
||||
this.id = 'ECSScene';
|
||||
this.world = new World();
|
||||
}
|
||||
update() {
|
||||
if (this.running)
|
||||
this.world.run();
|
||||
}
|
||||
updateDraw() {
|
||||
return true;
|
||||
}
|
||||
createEntity(components) {
|
||||
return this.world.createEntity(components);
|
||||
}
|
||||
createComponent(props) {
|
||||
return this.world.createComponent(props);
|
||||
}
|
||||
createSystem(systemExecutor) {
|
||||
return this.world.createSystem(systemExecutor);
|
||||
}
|
||||
addSystem(system) {
|
||||
return this.world.addSystem(system);
|
||||
}
|
||||
addEntity(entity) {
|
||||
this.world.addEntity(entity);
|
||||
}
|
||||
removeEntity(entity) {
|
||||
return this.world.removeEntity(entity);
|
||||
}
|
||||
createQuery(include, exclude) {
|
||||
return this.world.createQuery(include, exclude);
|
||||
}
|
||||
extendEntity(entity, components) {
|
||||
return this.world.extendEntity(entity, components);
|
||||
}
|
||||
}
|
6
framework/world/entity.d.ts
vendored
Normal file
6
framework/world/entity.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Component } from "./component";
|
||||
export declare class Entity {
|
||||
id: number;
|
||||
components: Array<Component<any>>;
|
||||
constructor();
|
||||
}
|
5
framework/world/entity.js
Normal file
5
framework/world/entity.js
Normal file
@@ -0,0 +1,5 @@
|
||||
export class Entity {
|
||||
constructor() {
|
||||
this.components = new Array();
|
||||
}
|
||||
}
|
11
framework/world/event-bus.d.ts
vendored
Normal file
11
framework/world/event-bus.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
export declare class EventBus {
|
||||
private events;
|
||||
constructor();
|
||||
emit(id: string, data: any): void;
|
||||
subscribe(id: string, subscriber: Function): void;
|
||||
}
|
||||
export declare class EventItem {
|
||||
id: string;
|
||||
subscribers: Function[];
|
||||
constructor(id: string);
|
||||
}
|
30
framework/world/event-bus.js
Normal file
30
framework/world/event-bus.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export class EventBus {
|
||||
constructor() {
|
||||
this.events = new Map();
|
||||
}
|
||||
emit(id, data) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
let ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
return;
|
||||
}
|
||||
ev.subscribers.forEach((subscriber) => {
|
||||
subscriber(data);
|
||||
});
|
||||
}
|
||||
subscribe(id, subscriber) {
|
||||
let ev = this.events.get(id);
|
||||
if (!ev) {
|
||||
ev = new EventItem(id);
|
||||
this.events.set(id, ev);
|
||||
}
|
||||
ev.subscribers.push(subscriber);
|
||||
}
|
||||
}
|
||||
export class EventItem {
|
||||
constructor(id) {
|
||||
this.id = id;
|
||||
this.subscribers = [];
|
||||
}
|
||||
}
|
3
framework/world/index.d.ts
vendored
Normal file
3
framework/world/index.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface World {
|
||||
update(dt: number): any;
|
||||
}
|
1
framework/world/index.js
Normal file
1
framework/world/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export {};
|
13
framework/world/query.d.ts
vendored
Normal file
13
framework/world/query.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { World } from ".";
|
||||
import { Component } from "./component";
|
||||
import { Entity } from "./entity";
|
||||
export declare class Query {
|
||||
include: Array<Component<any>>;
|
||||
exclude: Array<Component<any>>;
|
||||
private results;
|
||||
isDirty: boolean;
|
||||
includeComponentIds: number[];
|
||||
excludeComponentIds: number[];
|
||||
constructor(include: Array<Component<any>>, exclude: Array<Component<any>>);
|
||||
execute(world: World): Array<Entity>;
|
||||
}
|
27
framework/world/query.js
Normal file
27
framework/world/query.js
Normal file
@@ -0,0 +1,27 @@
|
||||
export class Query {
|
||||
constructor(include, exclude) {
|
||||
this.include = include;
|
||||
this.exclude = exclude;
|
||||
this.isDirty = true;
|
||||
this.results = new Array();
|
||||
this.includeComponentIds = include.map((component) => component.id);
|
||||
this.excludeComponentIds = exclude.map((component) => component.id);
|
||||
}
|
||||
execute(world) {
|
||||
if (!this.isDirty && this.results) {
|
||||
return this.results;
|
||||
}
|
||||
let filtered;
|
||||
const entities = world.entities.filter(entity => {
|
||||
let ids = entity.components.map(component => component.id);
|
||||
let includes = ids.map(id => this.includeComponentIds.includes(id)).includes(true);
|
||||
let excludes = ids.map(id => this.excludeComponentIds.includes(id)).includes(true);
|
||||
return includes && !excludes;
|
||||
});
|
||||
if (entities.length > 0) {
|
||||
this.isDirty = false;
|
||||
this.results = entities;
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
}
|
6
framework/world/system.d.ts
vendored
Normal file
6
framework/world/system.d.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import { World } from ".";
|
||||
export declare class System {
|
||||
executor: Function;
|
||||
constructor(executor: Function);
|
||||
execute(world: World): void;
|
||||
}
|
8
framework/world/system.js
Normal file
8
framework/world/system.js
Normal file
@@ -0,0 +1,8 @@
|
||||
export class System {
|
||||
constructor(executor) { }
|
||||
execute(world) {
|
||||
if (this.executor) {
|
||||
this.executor(world);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user