Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
namespace TSE {
export class Scene {
private _root: SimObject;
public constructor() {
this._root = new SimObject(0, "__ROOT__", this);
}
public get root(): SimObject { return this._root; }
public get isLoaded(): boolean { return this._root.isLoaded; }
public addObject(object: SimObject): void { this._root.addChild(object); }
public getObjectByName(name: string): SimObject { return this._root.getObjectByName(name); }
public load(): void { this._root.load(); }
public update(time: number): void { this._root.update(time); }
public render(shader: Shader): void { this._root.render(shader); }
}
}

View File

@@ -0,0 +1,107 @@
namespace TSE {
export class SimObject {
public name: string;
public transform: Transform = new Transform();
private _id: number;
private _children: SimObject[] = [];
private _parent: SimObject;
private _scene: Scene;
private _isLoaded: boolean = false;
private _components: BaseComponent[] = [];
private _behaviors: BaseBehavior[] = [];
private _localMatrix: Matrix4x4 = Matrix4x4.identity();
private _worldMatrix: Matrix4x4 = Matrix4x4.identity();
public constructor(id: number, name: string, scene?: Scene) {
this._id = id;
this.name = name;
this._scene = scene;
}
public get id(): number { return this._id; }
public get parent(): SimObject { return this._parent; }
public get worldMatrix(): Matrix4x4 { return this._worldMatrix; }
public get isLoaded(): boolean { return this._isLoaded; }
public addChild(child: SimObject): void {
child._parent = this;
this._children.push(child);
child.load();
child.onAdded(this._scene);
}
public removeChild(child: SimObject): void {
const index = this._children.indexOf(child);
if (index == -1) return;
child._parent = undefined;
this._children.splice(index, 1);
}
public getObjectByName(name: string): SimObject {
if (this.name === name) return this;
for (let child of this._children) {
let result = child.getObjectByName(name);
if (result !== undefined) return result;
}
return undefined;
}
public addComponent(component: BaseComponent): void {
this._components.push(component);
component.owner = this;
}
public addBehavior(behavior: BaseBehavior): void {
this._behaviors.push(behavior);
behavior.owner = this;
}
public load(): void {
this._isLoaded = true;
for (let c of this._components)
c.load();
for (let child of this._children)
child.load();
}
public update(time: number): void {
this._localMatrix = this.transform.transformationMatrix;
this.updateWorldMatrix(this._parent?._worldMatrix);
for (let c of this._components)
c.update(time);
for (let c of this._behaviors)
c.update(time);
for (let child of this._children)
child.update(time);
}
public render(shader: Shader): void {
for (let c of this._components)
c.render(shader);
for (let child of this._children)
child.render(shader);
}
protected onAdded(scene: Scene): void {
this._scene = scene;
}
private updateWorldMatrix(parentWorldMatrix: Matrix4x4): void {
if (parentWorldMatrix !== undefined)
this._worldMatrix = Matrix4x4.multiply(parentWorldMatrix, this._localMatrix);
else
this._worldMatrix.copyFrom(this._localMatrix);
}
}
}

View File

@@ -0,0 +1,86 @@
namespace TSE {
export enum ZoneState {
UNINITIALIZED,
LOADING,
UPDATING
}
export class Zone {
private _name: string;
private _description: string;
private _id: number;
private _scene: Scene;
private _state: ZoneState = ZoneState.UNINITIALIZED;
private _globalId: number = -1;
public constructor(id: number, name: string, descripton?: string) {
this._id = id;
this._name = name;
this._description = descripton;
this._scene = new Scene();
}
public get id(): number { return this._id; }
public get name(): string { return this._name; }
public get description(): string { return this._description; }
public get scene(): Scene { return this._scene; }
public get state(): ZoneState { return this._state; }
public initialize(zoneData: any): void {
if (zoneData.objects === undefined) return;
for (let object of zoneData.objects) this.loadSimObject(object, this._scene.root);
}
public load(): void {
if (this._state !== ZoneState.UNINITIALIZED) return;
this._state = ZoneState.LOADING;
this._scene.load();
this._state = ZoneState.UPDATING;
}
public unload(): void {
if (this._state === ZoneState.UNINITIALIZED) return;
this._state = ZoneState.UNINITIALIZED;
}
public update(time: number): void {
if (this._state === ZoneState.UPDATING)
this._scene.update(time);
}
public render(shader: Shader): void {
if (this._state === ZoneState.UPDATING)
this._scene.render(shader);
}
public onActivated(): void { }
public onDeactivated(): void { }
private loadSimObject(dataSection: any, parent: SimObject): void {
if (dataSection.name === undefined) throw new Error("Invalid Object data!");
let name: string = String(dataSection.name);
this._globalId++;
let simObject = new SimObject(this._globalId, name, this._scene);
if (dataSection.transform !== undefined) simObject.transform.setFromJson(dataSection.transform);
if (dataSection.components !== undefined) {
for (let component of dataSection.components)
simObject.addComponent(ComponentManager.extractComponent(component));
}
if (dataSection.behaviors !== undefined) {
for (let behavior of dataSection.behaviors)
simObject.addBehavior(BehaviorManager.extractComponent(behavior));
}
if (dataSection.children !== undefined) {
for (let object of dataSection.children) this.loadSimObject(object, simObject);
}
parent.addChild(simObject);
}
}
}

View File

@@ -0,0 +1,69 @@
namespace TSE {
export class ZoneManager implements IMessageHanlder{
private static _globalZoneId: number = -1;
//private static _zones: { [id: number]: Zone } = {};
private static _registeredZones: {[id: number]: string} = {};
private static _activeZone: Zone;
private static _instance: ZoneManager;
private constructor() { }
public static initialize(): void {
ZoneManager._instance = new ZoneManager();
ZoneManager._registeredZones[0] = "assets/zones/testZone.json";
}
public static changeZone(id: number): void {
if (ZoneManager._activeZone !== undefined) {
ZoneManager._activeZone.onDeactivated();
ZoneManager._activeZone.unload();
delete ZoneManager._activeZone;
}
let zonePath = ZoneManager._registeredZones[id];
if (zonePath === undefined) throw new Error("Zone Id does not exist!");
if (AssetManager.isAssetLoaded(zonePath)) ZoneManager.loadZone(AssetManager.getAsset(zonePath));
else {
Message.subscribe(MESSAGE_ASSET_LOADER_ASSET_LOADED + zonePath, ZoneManager._instance);
AssetManager.loadAsset(zonePath);
}
}
public static update(time: number): void {
if (ZoneManager._activeZone === undefined) return;
ZoneManager._activeZone.update(time);
}
public static render(shader: Shader): void {
if (ZoneManager._activeZone === undefined) return;
ZoneManager._activeZone.render(shader);
}
private static loadZone(asset: JsonAsset): void {
let zoneData = asset.data;
if (zoneData.id === undefined) throw new Error("Zone file format exception: Zone id not present.")
let zoneId: number = Number(zoneData.id);
let zoneName: string = String(zoneData.name);
let zoneDescription: string = String(zoneData.description);
let zone = new Zone(zoneId, zoneName, zoneDescription);
zone.initialize(zoneData);
ZoneManager._activeZone = zone;
zone.load();
zone.onActivated();
}
public onMessage(message: Message): void {
if (message.code.indexOf(MESSAGE_ASSET_LOADER_ASSET_LOADED) !== -1) {
let asset = message.context as JsonAsset;
ZoneManager.loadZone(asset);
}
}
public static get zone(): Zone { return this._activeZone; }
}
}