feat: App

This commit is contained in:
cha0s 2019-04-14 16:33:26 -05:00
parent 2bcb541c99
commit 92e36587d0
4 changed files with 29 additions and 15 deletions

11
client/app.js Normal file
View File

@ -0,0 +1,11 @@
import {compose} from '@avocado/core';
import {EventEmitter, Property} from '@avocado/mixins';
const decorate = compose(
EventEmitter,
Property('selfEntity', {
track: true,
}),
);
export class App extends decorate(class {}) {}

View File

@ -17,8 +17,11 @@ import {clearAnimation, setAnimation, Ticker} from '@avocado/timing';
import {InputPacket, KeysPacket, StatePacket} from '../common/packet';
import {augmentParserWithThroughput} from '../common/parser-throughput';
import {WorldTime} from '../common/world-time';
import {App} from './app';
import Ui from './ui';
import DebugUi from './ui/debug';
// Application.
const app = new App();
// DOM.
const appNode = document.querySelector('.app');
// Graphics stage.
@ -28,10 +31,6 @@ const stage = new Stage(Vector.mul(visibleSize, visibleScale));
stage.scale = visibleScale;
// Handle "own" entity.
let selfEntity;
let selfEntityResolver;
const selfEntityPromise = new Promise((resolve) => {
selfEntityResolver = resolve;
});
function hasSelfEntity() {
return selfEntity && 'string' !== typeof selfEntity;
}
@ -71,7 +70,7 @@ room.on('entityAdded', (entity) => {
if ('string' === typeof selfEntity) {
if (entity === room.findEntity(selfEntity)) {
selfEntity = entity;
selfEntityResolver(entity);
app.selfEntity = entity;
// Add back our self entity traits.
for (const type of selfEntityOnlyTraits) {
entity.addTrait(type);
@ -292,8 +291,8 @@ ReactDOM.render(UiComponent, stage.ui);
const debugUiNode = document.querySelector('.debug-container');
const DebugUiComponent = <DebugUi
actionRegistry={actionRegistry}
app={app}
Parser={AugmentedParser}
selfEntityPromise={selfEntityPromise}
stage={stage}
/>;
ReactDOM.render(DebugUiComponent, debugUiNode);
@ -305,8 +304,8 @@ const evaluationContext = {
selfEntity,
stage,
};
selfEntityPromise.then((entity) => {
evaluationContext.selfEntity = entity;
app.on('selfEntityChanged', (selfEntity) => {
evaluationContext.selfEntity = selfEntity;
});
// Just inject it into global for now.
window.humus = evaluationContext;

View File

@ -16,7 +16,7 @@ const decorate = compose(
const DebugUi = ({
actionRegistry,
selfEntityPromise,
app,
stage,
Parser,
}) => {
@ -76,7 +76,7 @@ const DebugUi = ({
}}
>
<Throughput Parser={Parser} />
<SelfEntity selfEntityPromise={selfEntityPromise} />
<SelfEntity app={app} />
</div>
</div>;
};

View File

@ -39,14 +39,18 @@ const decorate = compose(
`),
);
const SelfEntityComponent = ({selfEntityPromise}) => {
const SelfEntityComponent = ({app}) => {
const [selfEntity, setSelfEntity] = useState();
const [position, setPosition] = useState([0, 0]);
useEffect(() => {
selfEntityPromise.then((entity) => {
setSelfEntity(entity);
setPosition(entity.position);
});
const onSelfEntityChanged = () => {
setSelfEntity(app.selfEntity);
setPosition(app.selfEntity.position);
};
app.on('selfEntityChanged', onSelfEntityChanged);
return () => {
app.off('selfEntityChanged', onSelfEntityChanged);
}
}, [selfEntity]);
useEffect(() => {
if (!selfEntity) {