Skip to content

Commit

Permalink
use constants for event emitter names
Browse files Browse the repository at this point in the history
  • Loading branch information
Even Alander committed Oct 1, 2020
1 parent 622801d commit 53ad8cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export const ENDED = "ENDED";
export const COOLDOWN = 250;
export const SCORE_CAP = 150;
export const SCORE_INCREASE = 10;

export const EMIT_MOVE = "EMIT_MOVE";
export const EMIT_NEW_GAME = "EMIT_NEW_GAME";
14 changes: 7 additions & 7 deletions src/controls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import { UP, RIGHT, DOWN, LEFT } from "./constants";
import { UP, RIGHT, DOWN, LEFT, EMIT_MOVE, EMIT_NEW_GAME } from "./constants";

class EventEmitter {
constructor() {
Expand All @@ -23,20 +23,20 @@ const events = new EventEmitter();
import Mousetrap from "mousetrap";

Mousetrap.bind("up", () => {
events.emit("move", { direction: UP });
events.emit(EMIT_MOVE, { direction: UP });
});
Mousetrap.bind("right", () => {
events.emit("move", { direction: RIGHT });
events.emit(EMIT_MOVE, { direction: RIGHT });
});
Mousetrap.bind("down", () => {
events.emit("move", { direction: DOWN });
events.emit(EMIT_MOVE, { direction: DOWN });
});
Mousetrap.bind("left", () => {
events.emit("move", { direction: LEFT });
events.emit(EMIT_MOVE, { direction: LEFT });
});
Mousetrap.bind("enter", () => {
events.emit("new_game");
events.emit(EMIT_NEW_GAME);
});
Mousetrap.bind("space", () => events.emit("new_game"));
Mousetrap.bind("space", () => events.emit(EMIT_NEW_GAME));

export default events;
6 changes: 3 additions & 3 deletions src/game.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import { IDLE, ENDED } from "./constants";
import { IDLE, ENDED, EMIT_MOVE, EMIT_NEW_GAME } from "./constants";

import canvas from "./html.js";
import Snake from "./snake";
Expand Down Expand Up @@ -78,10 +78,10 @@ requestAnimationFrame(render);

import controls from "./controls";

controls.on("move", ({ direction }) => {
controls.on(EMIT_MOVE, ({ direction }) => {
s.setDir(direction);
});
controls.on("new_game", () => {
controls.on(EMIT_NEW_GAME, () => {
s.newGame();
requestAnimationFrame(render);
});

0 comments on commit 53ad8cb

Please sign in to comment.