Skip to content

Commit

Permalink
Add syncOn and syncOff
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinten committed Aug 21, 2024
1 parent 96e1635 commit c017c97
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ https://en.wikipedia.org/wiki/Web_colors
- 4315 / 13312
- 4313 / 13312
- 4311 / 13312
- 4271 / 13312
6 changes: 4 additions & 2 deletions game/js/canvas/2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import css from '../css.js';
css`body{margin:0;padding:0;overflow:hidden;}canvas{display:block;touch-action:none;user-select:none;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;}`;

import events from '../events.js';
let {on, off, once, emit, last} = events();

let obj = events({}, ['color']);
let {on, emit, last} = obj;

let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
Expand Down Expand Up @@ -73,4 +75,4 @@ let onF = time => {
};
requestAnimationFrame(onF);

export default { on, off, once, emit, last };
export default obj;
9 changes: 9 additions & 0 deletions game/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,14 @@ export default (obj = {}, eTypes = []) => {
};
});

obj.syncOn = other => {
eTypes.forEach(
eType => other.on(eType, obj[eType]));
};

obj.syncOff = other => eTypes.forEach(
eType => other.off(eType, obj[eType])
);

return obj;
};
31 changes: 5 additions & 26 deletions game/js/statemachine/machine.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import canvas from '../canvas/2d.js';
import createState from './state.js';

let eTypes = ['tap', 'resize', 'step', 'draw'];
let oTypes = ['color'];

let states = {};

let add = (name, state) => {
Expand All @@ -13,16 +10,6 @@ let add = (name, state) => {
if (!state) {
state = createState();
}
eTypes.forEach(eType => {
state[eType] = e => {
state.emit(eType, e);
};
});
oTypes.forEach(oType => {
canvas[oType] = e => {
canvas.emit(oType, e);
};
});
states[name] = state;
if (Object.keys(states).length === 1) {
start(name);
Expand All @@ -38,22 +25,14 @@ let start = (name) => {
Object.values(states).forEach((s) => {
if (s.active) {
s.active = false;
eTypes.forEach(eType => {
canvas.off(eType, s[eType]);
});
oTypes.forEach(oType => {
s.off(oType, canvas[oType]);
});
s.syncOff(canvas);
canvas.syncOff(s);
}
});
let n = states[name];
eTypes.forEach(eType => {
canvas.on(eType, n[eType]);
});
oTypes.forEach(oType => {
n.on(oType, canvas[oType]);
});
n['resize'](canvas.last('resize'));
n.syncOn(canvas);
canvas.syncOn(n);
n.resize(canvas.last('resize'));
n.active = true;
};

Expand Down
2 changes: 1 addition & 1 deletion game/js/statemachine/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default () => {
}
}
};
events(state, ['start', 'stop']);
events(state, ['start', 'stop','tap', 'resize', 'step', 'draw']);
return state;
};

0 comments on commit c017c97

Please sign in to comment.