Skip to content

Commit afcec7e

Browse files
committed
Added actions to add a static ball to the page
1 parent fb98200 commit afcec7e

File tree

11 files changed

+206
-58
lines changed

11 files changed

+206
-58
lines changed

src/commands/randomizeEncounter.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import OutsideContext from '../context/OutsideContext';
33

44
export type RandomizeEncounterAction = Action<undefined, OutsideContext>;
55

6-
export default function randomizeEncounter({ state: outsideContext }: RandomizeEncounterAction) {
7-
outsideContext.randomizeEncounter();
6+
export default function randomizeEncounter({ state: outside }: RandomizeEncounterAction) {
7+
const monsters = outside.getMonsterDefinitions();
8+
const num = Math.floor(Math.random() * monsters.length);
9+
const monster = monsters[num];
10+
const {
11+
heights,
12+
name
13+
} = monster;
14+
const distance = Math.random() * 8 + 2;
15+
const height = Math.random() * (heights.max - heights.min) + heights.min;
16+
17+
outside.setMonster({
18+
name,
19+
height,
20+
distance
21+
});
822
};

src/commands/removeDerpyball.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Action } from '../framework/Executor';
2+
import OutsideContext from '../context/OutsideContext';
3+
4+
export type RemoveDerpyballAction = Action<undefined, OutsideContext>;
5+
6+
export default function removeDerpyball({ state: outside }: RemoveDerpyballAction) {
7+
outside.removeBall();
8+
}

src/commands/throwDerpyball.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Action } from '../framework/Executor';
2+
import OutsideContext, { Throw } from '../context/OutsideContext';
3+
4+
export type ThrowDerpyballAction = Action<Throw, OutsideContext>;
5+
6+
export default function throwDerpyball({ payload, state: outside }: ThrowDerpyballAction) {
7+
if (payload) {
8+
outside.throwBall(payload)
9+
}
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Container from '../framework/Container';
2+
import { ActionType, State } from '../initialize';
3+
import Controls, { ControlsProperties } from '../widgets/Controls';
4+
import { Throw } from '../context/OutsideContext';
5+
6+
const ControlsContainer = Container(Controls, [ State.App, State.Executor ], {
7+
getProperties([ app, executor]): ControlsProperties {
8+
return {
9+
onActionButtonPressed() {
10+
console.log('pressed', app.state);
11+
const derpyball: Throw = {
12+
direction: [ 1, 0, 0],
13+
initialTime: performance.now(),
14+
position: [ 0, 3, -4],
15+
speed: 1
16+
};
17+
executor.execute({ type: ActionType.ThrowDerpyball, payload: derpyball });
18+
},
19+
onActionButtonReleased() {
20+
console.log('released', app.state);
21+
}
22+
}
23+
}
24+
});
25+
26+
export default ControlsContainer;

src/containers/OutsideContainer.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import Outside, { OutsideProperties } from '../widgets/Outside';
22
import Container from '../framework/Container';
33
import { throws } from '../util/properties';
44
import OutsideContext from '../context/OutsideContext';
5-
import { State } from '../initialize';
5+
import { ActionType, State } from '../initialize';
66
import AssetContext from '../context/AssetContext';
7+
import Executor from '../framework/Executor';
78

8-
const OutsideContainer = Container(Outside, [ State.Outside, State.Asset ], {
9-
getProperties(payload: [ OutsideContext, AssetContext ]): OutsideProperties {
9+
const OutsideContainer = Container(Outside, [ State.Outside, State.Asset, State.Executor ], {
10+
getProperties(payload: [ OutsideContext, AssetContext, Executor ]): OutsideProperties {
1011
const [
1112
outside = throws(),
12-
appContext = throws()
13+
appContext = throws(),
14+
executor = throws()
1315
] = payload;
1416
let monster: OutsideProperties['monster'];
1517
const monsterInfo = outside.monster;
@@ -27,7 +29,12 @@ const OutsideContainer = Container(Outside, [ State.Outside, State.Asset ], {
2729
}
2830
return {
2931
environment: outside.environment,
30-
monster
32+
derpyball: outside.derpyball,
33+
monster,
34+
35+
removeDerpyball() {
36+
executor.execute(ActionType.RemoveDerpyball);
37+
}
3138
}
3239
}
3340
});

src/context/OutsideContext.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InjectorBase } from '../framework/InjectorBase';
2+
import { assign } from '@dojo/core/lang';
23

34
interface Monster {
45
distance: number;
@@ -12,6 +13,16 @@ export const enum Environment {
1213
Forest = 'forest'
1314
}
1415

16+
declare type Dimension3 = [ number, number, number ];
17+
18+
export interface Throw {
19+
direction: Dimension3;
20+
initialTime: number;
21+
position: Dimension3;
22+
speed: number;
23+
thrownTime?: number;
24+
}
25+
1526
/**
1627
* Defines a monster's traits
1728
*/
@@ -28,12 +39,14 @@ export interface MonsterDefinition {
2839
}
2940

3041
export default class OutsideContext extends InjectorBase {
31-
private _environment: Environment = Environment.Forest;
42+
environment: Environment = Environment.Forest;
43+
44+
private _derpyball?: Throw;
3245
private _monster?: Monster;
3346
private _monsterDefinitions: Map<string, MonsterDefinition> = new Map();
3447

35-
get environment(): Environment {
36-
return this._environment;
48+
get derpyball(): Throw | undefined {
49+
return this._derpyball;
3750
}
3851

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

47-
randomizeEncounter() {
48-
const {
49-
heights,
50-
name,
51-
} = this.randomMonster();
52-
const distance = Math.random() * 8 + 2;
53-
const height = Math.random() * (heights.max - heights.min) + heights.min;
60+
ballThrown() {
61+
if (this._derpyball && !this._derpyball.thrownTime) {
62+
this._derpyball.thrownTime = performance.now();
63+
}
64+
}
5465

55-
this.setMonster({
56-
name,
57-
height,
58-
distance
59-
});
66+
getMonsterDefinitions(): MonsterDefinition[] {
67+
return Array.from(this._monsterDefinitions.values());
6068
}
6169

62-
setEnvironment(name: Environment) {
63-
if (this._environment !== name) {
64-
this._environment = name;
65-
this.emitInvalidate();
70+
removeBall() {
71+
if (this._derpyball) {
72+
this._derpyball = undefined;
6673
}
6774
}
6875

69-
setMonster(monster: Monster | undefined) {
70-
if (this._monster !== monster) {
71-
this._monster = monster;
72-
this.emitInvalidate();
76+
throwBall(value: Throw) {
77+
if (!this._derpyball && value) {
78+
this._derpyball = {
79+
direction: <Dimension3> Array.from(value.direction),
80+
initialTime: value.initialTime || performance.now(),
81+
position: <Dimension3> Array.from(value.position),
82+
speed: value.speed
83+
}
7384
}
7485
}
7586

76-
private randomMonster() {
77-
const max = this._monsterDefinitions.size;
78-
const num = Math.floor(Math.random() * max);
79-
const definitions = Array.from(this._monsterDefinitions.values());
80-
return definitions[num];
87+
setMonster(monster: Monster | undefined) {
88+
this._monster = assign({}, monster);
8189
}
8290
}

src/framework/Executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface Command<T extends Action = Action> {
99

1010
export interface Action<T = any, U = any> {
1111
type: string;
12-
payload?: T;
12+
payload: T;
1313
state: U;
1414
}
1515

src/initialize.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import randomizeEncounter from './commands/randomizeEncounter';
88
import registerMonsters from './commands/registerMonsters';
99
import AssetContext from './context/AssetContext';
1010
import loadedMonsters from './commands/loadedMonsters';
11+
import removeDerpyball from './commands/removeDerpyball';
12+
import throwDerpyball from './commands/throwDerpyball';
1113

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

2426
export const enum ActionType {
2527
Initialize = 'initialize',
26-
LoadMonsters = 'loadMonsters',
2728
LoadedMonsters = 'loadedMonsters',
29+
LoadMonsters = 'loadMonsters',
2830
RandomizeEncounter = 'randomizeEncounter',
29-
RegisterMonsters = 'registerMonsters'
31+
RegisterMonsters = 'registerMonsters',
32+
RemoveDerpyball = 'removeDerpyball',
33+
ThrowDerpyball = 'throwDerpyball'
3034
}
3135

3236
export default function initialize() {
3337
const registry = new Registry();
3438
const executor = new Executor(registry, [
3539
{ type: ActionType.Initialize, handler: initializeApp, state: [ State.App, State.Executor ] },
36-
{ type: ActionType.LoadMonsters, handler: loadMonsters, state: State.Executor },
3740
{ type: ActionType.LoadedMonsters, handler: loadedMonsters, state: [ State.App, State.Executor ] },
41+
{ type: ActionType.LoadMonsters, handler: loadMonsters, state: State.Executor },
3842
{ type: ActionType.RandomizeEncounter, handler: randomizeEncounter, state: State.Outside },
39-
{ type: ActionType.RegisterMonsters, handler: registerMonsters, state: [ State.Asset, State.Outside ] }
43+
{ type: ActionType.RegisterMonsters, handler: registerMonsters, state: [ State.Asset, State.Outside ] },
44+
{ type: ActionType.RemoveDerpyball, handler: removeDerpyball, state: State.Outside },
45+
{ type: ActionType.ThrowDerpyball, handler: throwDerpyball, state: State.Outside }
4046
]);
4147
const appContext = new AppContext();
4248
const assetContext = new AssetContext();

src/main.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ProjectorMixin } from '@dojo/widget-core/mixins/Projector';
2-
import initialize from './initialize';
2+
import initialize, { ActionType } from './initialize';
33
import AppContainer from './containers/AppContainer';
4+
import Executor from './framework/Executor';
45

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

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

18+
// TODO move this into a listener
19+
document.addEventListener('keydown', (event: KeyboardEvent) => {
20+
if (event.keyCode === 32) {
21+
const executor = registry.getInjector<Executor>('executor');
22+
if (!executor) {
23+
console.warn('missing executor');
24+
return;
25+
}
26+
const ball = {
27+
direction: [ 1, 0, 0],
28+
initialTime: performance.now(),
29+
position: [ 0, 3, -4],
30+
speed: 1
31+
};
32+
executor.execute(ActionType.ThrowDerpyball, ball);
33+
}
34+
else if (event.key === '-') {
35+
const executor = registry.getInjector<Executor>('executor');
36+
if (!executor) {
37+
console.warn('missing executor');
38+
return;
39+
}
40+
executor.execute(ActionType.RemoveDerpyball);
41+
}
42+
});
43+
1744
(<any> root.ownerDocument).registry = registry;
1845
(<any> root.ownerDocument).projector = projector;

src/widgets/Controls.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ export interface ControlsProperties {
77
onActionButtonReleased?: EventHandler
88
}
99

10-
function logEvent(name: string) {
11-
return (event: Event) => {
12-
console.log(name, event);
13-
}
14-
}
15-
1610
export default class Controls extends WidgetBase<ControlsProperties> {
1711
protected render(): DNode[] {
1812
return [

0 commit comments

Comments
 (0)