Skip to content

Commit

Permalink
Added actions to add a static ball to the page
Browse files Browse the repository at this point in the history
  • Loading branch information
devpaul committed Nov 14, 2017
1 parent fb98200 commit afcec7e
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 58 deletions.
18 changes: 16 additions & 2 deletions src/commands/randomizeEncounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import OutsideContext from '../context/OutsideContext';

export type RandomizeEncounterAction = Action<undefined, OutsideContext>;

export default function randomizeEncounter({ state: outsideContext }: RandomizeEncounterAction) {
outsideContext.randomizeEncounter();
export default function randomizeEncounter({ state: outside }: RandomizeEncounterAction) {
const monsters = outside.getMonsterDefinitions();
const num = Math.floor(Math.random() * monsters.length);
const monster = monsters[num];
const {
heights,
name
} = monster;
const distance = Math.random() * 8 + 2;
const height = Math.random() * (heights.max - heights.min) + heights.min;

outside.setMonster({
name,
height,
distance
});
};
8 changes: 8 additions & 0 deletions src/commands/removeDerpyball.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Action } from '../framework/Executor';
import OutsideContext from '../context/OutsideContext';

export type RemoveDerpyballAction = Action<undefined, OutsideContext>;

export default function removeDerpyball({ state: outside }: RemoveDerpyballAction) {
outside.removeBall();
}
10 changes: 10 additions & 0 deletions src/commands/throwDerpyball.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Action } from '../framework/Executor';
import OutsideContext, { Throw } from '../context/OutsideContext';

export type ThrowDerpyballAction = Action<Throw, OutsideContext>;

export default function throwDerpyball({ payload, state: outside }: ThrowDerpyballAction) {
if (payload) {
outside.throwBall(payload)
}
}
26 changes: 26 additions & 0 deletions src/containers/ControlsContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Container from '../framework/Container';
import { ActionType, State } from '../initialize';
import Controls, { ControlsProperties } from '../widgets/Controls';
import { Throw } from '../context/OutsideContext';

const ControlsContainer = Container(Controls, [ State.App, State.Executor ], {
getProperties([ app, executor]): ControlsProperties {
return {
onActionButtonPressed() {
console.log('pressed', app.state);
const derpyball: Throw = {
direction: [ 1, 0, 0],
initialTime: performance.now(),
position: [ 0, 3, -4],
speed: 1
};
executor.execute({ type: ActionType.ThrowDerpyball, payload: derpyball });
},
onActionButtonReleased() {
console.log('released', app.state);
}
}
}
});

export default ControlsContainer;
17 changes: 12 additions & 5 deletions src/containers/OutsideContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import Outside, { OutsideProperties } from '../widgets/Outside';
import Container from '../framework/Container';
import { throws } from '../util/properties';
import OutsideContext from '../context/OutsideContext';
import { State } from '../initialize';
import { ActionType, State } from '../initialize';
import AssetContext from '../context/AssetContext';
import Executor from '../framework/Executor';

const OutsideContainer = Container(Outside, [ State.Outside, State.Asset ], {
getProperties(payload: [ OutsideContext, AssetContext ]): OutsideProperties {
const OutsideContainer = Container(Outside, [ State.Outside, State.Asset, State.Executor ], {
getProperties(payload: [ OutsideContext, AssetContext, Executor ]): OutsideProperties {
const [
outside = throws(),
appContext = throws()
appContext = throws(),
executor = throws()
] = payload;
let monster: OutsideProperties['monster'];
const monsterInfo = outside.monster;
Expand All @@ -27,7 +29,12 @@ const OutsideContainer = Container(Outside, [ State.Outside, State.Asset ], {
}
return {
environment: outside.environment,
monster
derpyball: outside.derpyball,
monster,

removeDerpyball() {
executor.execute(ActionType.RemoveDerpyball);
}
}
}
});
Expand Down
64 changes: 36 additions & 28 deletions src/context/OutsideContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InjectorBase } from '../framework/InjectorBase';
import { assign } from '@dojo/core/lang';

interface Monster {
distance: number;
Expand All @@ -12,6 +13,16 @@ export const enum Environment {
Forest = 'forest'
}

declare type Dimension3 = [ number, number, number ];

export interface Throw {
direction: Dimension3;
initialTime: number;
position: Dimension3;
speed: number;
thrownTime?: number;
}

/**
* Defines a monster's traits
*/
Expand All @@ -28,12 +39,14 @@ export interface MonsterDefinition {
}

export default class OutsideContext extends InjectorBase {
private _environment: Environment = Environment.Forest;
environment: Environment = Environment.Forest;

private _derpyball?: Throw;
private _monster?: Monster;
private _monsterDefinitions: Map<string, MonsterDefinition> = new Map();

get environment(): Environment {
return this._environment;
get derpyball(): Throw | undefined {
return this._derpyball;
}

get monster(): Monster | undefined {
Expand All @@ -44,39 +57,34 @@ export default class OutsideContext extends InjectorBase {
this._monsterDefinitions.set(definitions.name, definitions);
}

randomizeEncounter() {
const {
heights,
name,
} = this.randomMonster();
const distance = Math.random() * 8 + 2;
const height = Math.random() * (heights.max - heights.min) + heights.min;
ballThrown() {
if (this._derpyball && !this._derpyball.thrownTime) {
this._derpyball.thrownTime = performance.now();
}
}

this.setMonster({
name,
height,
distance
});
getMonsterDefinitions(): MonsterDefinition[] {
return Array.from(this._monsterDefinitions.values());
}

setEnvironment(name: Environment) {
if (this._environment !== name) {
this._environment = name;
this.emitInvalidate();
removeBall() {
if (this._derpyball) {
this._derpyball = undefined;
}
}

setMonster(monster: Monster | undefined) {
if (this._monster !== monster) {
this._monster = monster;
this.emitInvalidate();
throwBall(value: Throw) {
if (!this._derpyball && value) {
this._derpyball = {
direction: <Dimension3> Array.from(value.direction),
initialTime: value.initialTime || performance.now(),
position: <Dimension3> Array.from(value.position),
speed: value.speed
}
}
}

private randomMonster() {
const max = this._monsterDefinitions.size;
const num = Math.floor(Math.random() * max);
const definitions = Array.from(this._monsterDefinitions.values());
return definitions[num];
setMonster(monster: Monster | undefined) {
this._monster = assign({}, monster);
}
}
2 changes: 1 addition & 1 deletion src/framework/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Command<T extends Action = Action> {

export interface Action<T = any, U = any> {
type: string;
payload?: T;
payload: T;
state: U;
}

Expand Down
14 changes: 10 additions & 4 deletions src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import randomizeEncounter from './commands/randomizeEncounter';
import registerMonsters from './commands/registerMonsters';
import AssetContext from './context/AssetContext';
import loadedMonsters from './commands/loadedMonsters';
import removeDerpyball from './commands/removeDerpyball';
import throwDerpyball from './commands/throwDerpyball';

// Require globals
require('aframe');
Expand All @@ -23,20 +25,24 @@ export const enum State {

export const enum ActionType {
Initialize = 'initialize',
LoadMonsters = 'loadMonsters',
LoadedMonsters = 'loadedMonsters',
LoadMonsters = 'loadMonsters',
RandomizeEncounter = 'randomizeEncounter',
RegisterMonsters = 'registerMonsters'
RegisterMonsters = 'registerMonsters',
RemoveDerpyball = 'removeDerpyball',
ThrowDerpyball = 'throwDerpyball'
}

export default function initialize() {
const registry = new Registry();
const executor = new Executor(registry, [
{ type: ActionType.Initialize, handler: initializeApp, state: [ State.App, State.Executor ] },
{ type: ActionType.LoadMonsters, handler: loadMonsters, state: State.Executor },
{ type: ActionType.LoadedMonsters, handler: loadedMonsters, state: [ State.App, State.Executor ] },
{ type: ActionType.LoadMonsters, handler: loadMonsters, state: State.Executor },
{ type: ActionType.RandomizeEncounter, handler: randomizeEncounter, state: State.Outside },
{ type: ActionType.RegisterMonsters, handler: registerMonsters, state: [ State.Asset, State.Outside ] }
{ type: ActionType.RegisterMonsters, handler: registerMonsters, state: [ State.Asset, State.Outside ] },
{ type: ActionType.RemoveDerpyball, handler: removeDerpyball, state: State.Outside },
{ type: ActionType.ThrowDerpyball, handler: throwDerpyball, state: State.Outside }
]);
const appContext = new AppContext();
const assetContext = new AssetContext();
Expand Down
29 changes: 28 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProjectorMixin } from '@dojo/widget-core/mixins/Projector';
import initialize from './initialize';
import initialize, { ActionType } from './initialize';
import AppContainer from './containers/AppContainer';
import Executor from './framework/Executor';

const root = document.querySelector('go-derpy') || undefined;

Expand All @@ -14,5 +15,31 @@ const projector = new Projector();
projector.setProperties({ registry });
projector.append(root);

// TODO move this into a listener
document.addEventListener('keydown', (event: KeyboardEvent) => {
if (event.keyCode === 32) {
const executor = registry.getInjector<Executor>('executor');
if (!executor) {
console.warn('missing executor');
return;
}
const ball = {
direction: [ 1, 0, 0],
initialTime: performance.now(),
position: [ 0, 3, -4],
speed: 1
};
executor.execute(ActionType.ThrowDerpyball, ball);
}
else if (event.key === '-') {
const executor = registry.getInjector<Executor>('executor');
if (!executor) {
console.warn('missing executor');
return;
}
executor.execute(ActionType.RemoveDerpyball);
}
});

(<any> root.ownerDocument).registry = registry;
(<any> root.ownerDocument).projector = projector;
6 changes: 0 additions & 6 deletions src/widgets/Controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ export interface ControlsProperties {
onActionButtonReleased?: EventHandler
}

function logEvent(name: string) {
return (event: Event) => {
console.log(name, event);
}
}

export default class Controls extends WidgetBase<ControlsProperties> {
protected render(): DNode[] {
return [
Expand Down
Loading

0 comments on commit afcec7e

Please sign in to comment.