Skip to content

Commit

Permalink
chore: reorg shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
eordano committed Apr 11, 2020
1 parent d88304f commit 42ddb0f
Show file tree
Hide file tree
Showing 36 changed files with 783 additions and 720 deletions.
31 changes: 17 additions & 14 deletions gamekit/GamekitScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BuildDCLInterface } from './DCLInterface/BuildDCLInterface'
import { BuildECSInterface } from './DCLInterface/BuildECSInterface'
import { customEval, getES5Context } from './sandbox'


const LOADING = 'loading'
const AWAKE = 'awake'
const RUNNING = 'running'
Expand All @@ -18,7 +17,6 @@ const RUNNING = 'running'
* This is the class that runs all the magic (see `runFirstRound`)
*/
export abstract class GamekitScene implements ISceneRunningScript {

rendererInterface!: IRendererParcelSceneToScript

devTools: any
Expand All @@ -37,7 +35,6 @@ export abstract class GamekitScene implements ISceneRunningScript {
this.setupDevtools()

this.status = AWAKE

;(this.rendererInterface as any).on('sceneStart')

try {
Expand All @@ -47,10 +44,14 @@ export abstract class GamekitScene implements ISceneRunningScript {
}
}

/**
* Merge together the core `dcl` functions (like `loadModule` to send messages to the main thread) and the more
* GameKit specific functions (like `addEntity`)
*/
protected setupDCLInterface() {
Object.defineProperty(this, 'engine', {
get: () => this.rendererInterface,
enumerable: false
enumerable: false,
})
this.dcl = { ...BuildDCLInterface(this), ...BuildECSInterface(this.outboundEventQueue) }
this.fixupDeprecations()
Expand All @@ -69,14 +70,16 @@ export abstract class GamekitScene implements ISceneRunningScript {
abstract getSource(): Promise<string | void>

runFirstRound() {
return this.getSource().then(source => {
if (!source) {
throw new Error('Could not load source')
}
return customEval(source, getES5Context({ dcl: this.dcl }))
}).catch(error => {
console.log(error)
})
return this.getSource()
.then((source) => {
if (!source) {
throw new Error('Could not load source')
}
return customEval(source, getES5Context({ dcl: this.dcl }))
})
.catch((error) => {
console.log(error)
})
}

runStartFunctions() {
Expand Down Expand Up @@ -187,11 +190,11 @@ export abstract class GamekitScene implements ISceneRunningScript {
Object.defineProperty(this, 'manualUpdate', {
get: () => this.managedUpdateCalls,
set: (value: boolean) => (this.managedUpdateCalls = value),
enumerable: false
enumerable: false,
})
Object.defineProperty(this, 'events', {
get: () => this.outboundEventQueue,
enumerable: false
enumerable: false,
})
}

Expand Down
40 changes: 20 additions & 20 deletions scene-api/ecs/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class DisposableComponentUpdated {
* @public
*/
export function Component(componentName: string, classId?: number) {
return function<TFunction extends ComponentConstructor<any>>(target: TFunction): TFunction | void {
return function <TFunction extends ComponentConstructor<any>>(target: TFunction): TFunction | void {
if (target.isComponent) {
throw new TypeError(
`You cannot extend a component. Trying to extend ${target.originalClassName} with: ${componentName}`
Expand All @@ -108,15 +108,15 @@ export function Component(componentName: string, classId?: number) {
enumerable: false,
writable: false,
configurable: false,
value: componentName
value: componentName,
})

if (classId !== undefined) {
Object.defineProperty(ret, componentClassIdSymbol, {
enumerable: false,
writable: false,
configurable: false,
value: classId
value: classId,
})
}

Expand All @@ -143,7 +143,7 @@ export function Component(componentName: string, classId?: number) {
*/

export function DisposableComponent(componentName: string, classId: number) {
return function<TFunction extends DisposableComponentConstructor<any>>(target: TFunction): TFunction | void {
return function <TFunction extends DisposableComponentConstructor<any>>(target: TFunction): TFunction | void {
if (target.isComponent) {
throw new TypeError(
`You cannot extend a component. Trying to extend ${target.originalClassName} with: ${componentName}`
Expand All @@ -169,22 +169,22 @@ export function DisposableComponent(componentName: string, classId: number) {
enumerable: false,
writable: false,
configurable: false,
value: componentName
value: componentName,
})

Object.defineProperty(ret, componentIdSymbol, {
enumerable: false,
writable: false,
configurable: false,
value: id
value: id,
})

if ((classId as any) !== undefined) {
Object.defineProperty(ret, componentClassIdSymbol, {
enumerable: false,
writable: false,
configurable: false,
value: classId
value: classId,
})
}

Expand Down Expand Up @@ -281,14 +281,14 @@ export class ObservableComponent {

Object.defineProperty(target, componentSymbol, {
...Object.getOwnPropertyDescriptor(target, componentSymbol),
enumerable: false
enumerable: false,
})

Object.defineProperty(target, propertyKey.toString(), {
get: function() {
get: function () {
return this[componentSymbol]
},
set: function(value) {
set: function (value) {
const oldValue = this[componentSymbol]

if (value) {
Expand All @@ -307,18 +307,18 @@ export class ObservableComponent {
}
}
},
enumerable: true
enumerable: true,
})
}
}

static field(target: ObservableComponent, propertyKey: string) {
if (delete (target as any)[propertyKey]) {
Object.defineProperty(target, propertyKey.toString(), {
get: function(this: ObservableComponent) {
get: function (this: ObservableComponent) {
return this.data[propertyKey]
},
set: function(this: ObservableComponent, value) {
set: function (this: ObservableComponent, value) {
const oldValue = this.data[propertyKey]
this.data[propertyKey] = value

Expand All @@ -330,18 +330,18 @@ export class ObservableComponent {
}
}
},
enumerable: true
enumerable: true,
})
}
}

static uiValue(target: ObservableComponent, propertyKey: string) {
if (delete (target as any)[propertyKey]) {
Object.defineProperty(target, propertyKey.toString(), {
get: function(this: ObservableComponent): string | number {
get: function (this: ObservableComponent): string | number {
return this.data[propertyKey].toString()
},
set: function(this: ObservableComponent, value: string | number) {
set: function (this: ObservableComponent, value: string | number) {
const oldValue = this.data[propertyKey]

const finalValue = new UIValue(value)
Expand All @@ -356,29 +356,29 @@ export class ObservableComponent {
}
}
},
enumerable: true
enumerable: true,
})
}
}

static readonly(target: ObservableComponent, propertyKey: string) {
if (delete (target as any)[propertyKey]) {
Object.defineProperty(target, propertyKey.toString(), {
get: function(this: ObservableComponent) {
get: function (this: ObservableComponent) {
if (propertyKey in this.data === false) {
throw new Error(`The field ${propertyKey} is uninitialized`)
}
return this.data[propertyKey]
},
set: function(this: ObservableComponent, value) {
set: function (this: ObservableComponent, value) {
if (propertyKey in this.data) {
throw new Error(`The field ${propertyKey} is readonly`)
}
this.data[propertyKey] = value
this.dirty = true
},
enumerable: true,
configurable: false
configurable: false,
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions scene-api/ecs/ComponentGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export class ComponentGroup {
}

Object.defineProperty(this, 'requires', {
get: function() {
get: function () {
return requires.slice()
}
},
})

Object.defineProperty(this, 'requiresNames', {
get: function() {
get: function () {
return this._requiresNames.slice()
}
},
})

for (let ix = 0; ix < requires.length; ix++) {
Expand All @@ -50,7 +50,7 @@ export class ComponentGroup {
)
}

if (this._requiresNames.some($ => $ === name)) {
if (this._requiresNames.some(($) => $ === name)) {
throw new Error(`ComponentGroup: the required component list has a repeated name ${name}`)
}

Expand Down
2 changes: 1 addition & 1 deletion scene-api/ecs/ECSEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ComponentConstructor,
DisposableComponentCreated,
DisposableComponentRemoved,
getComponentClassId
getComponentClassId,
} from './Component'
import { EventManager } from './EventManager'
import { ComponentGroup } from './ComponentGroup'
Expand Down
2 changes: 1 addition & 1 deletion scene-api/ecs/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class EventManager {

listeners.push({
listener,
fn: listenerFunction
fn: listenerFunction,
})

return this
Expand Down
2 changes: 1 addition & 1 deletion scene-api/ecs/UIValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
export enum UIValueType {
PERCENT = 0,
PIXELS = 1
PIXELS = 1,
}

/**
Expand Down
37 changes: 37 additions & 0 deletions scene-api/engine/Animator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Component, ObservableComponent } from '../ecs/Component'
import { AnimationState } from './animation/AnimationState'
import { Shape } from './Shape'
import { CLASS_ID } from './Components'
/**
* @public
*/
@Component('engine.animator', CLASS_ID.ANIMATION)
export class Animator extends Shape {
@ObservableComponent.readonly
private states: AnimationState[] = []
/**
* Adds an AnimationState to the animation lists.
*/
addClip(clip: AnimationState) {
this.states.push(clip)
clip.onChange(() => {
this.dirty = true
})
return this
}
/**
* Gets the animation clip instance for the specified clip name.
* If the clip doesn't exist a new one will be created.
*/
getClip(clipName: string): AnimationState {
for (let i = 0; i < this.states.length; i++) {
const clip = this.states[i]
if (clip.clip === clipName) {
return clip
}
}
const newClip = new AnimationState(clipName)
this.addClip(newClip)
return newClip
}
}
21 changes: 21 additions & 0 deletions scene-api/engine/Billboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component, ObservableComponent } from '../ecs/Component'
import { CLASS_ID } from './Components'
/**
* Billboard defines a behavior that makes the entity face the camera in any moment.
* @public
*/
@Component('engine.billboard', CLASS_ID.BILLBOARD)
export class Billboard extends ObservableComponent {
@ObservableComponent.field
x: boolean = true
@ObservableComponent.field
y: boolean = true
@ObservableComponent.field
z: boolean = true
constructor(x: boolean = true, y: boolean = true, z: boolean = true) {
super()
this.x = x
this.y = y
this.z = z
}
}
Loading

0 comments on commit 42ddb0f

Please sign in to comment.