Skip to content

Commit

Permalink
Adds remove listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp committed Apr 7, 2019
1 parent 60a2bd5 commit 52b75e6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ npm i foodinvasion
### 2: Add the package in your main.js

```javascript
import startPlaying from 'foodinvasion';
import { startPlaying, removeGame } from 'foodinvasion';

...
startPlaying();

//after game is done and you need to remove the listeners
removeGame();
```


Expand Down Expand Up @@ -61,7 +64,7 @@ Example in Vue:
</template>
<script>
import startPlaying from 'foodinvasion';
import { startPlaying, removeGame } from 'foodinvasion';
export default {
data() {
Expand All @@ -82,6 +85,9 @@ export default {
this.showGame = true;
},
},
beforeDestroy() {
removeGame();
}
};
</script>
```
Expand Down
43 changes: 31 additions & 12 deletions src/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { enemies, Enemy, OBJECT_ENEMY } from './game';

export const Game = new function () {
const boards = [];
let stopGame = false;

// Game Initialization
this.initialize = function (canvasElementId, spriteData, callback) {
Expand Down Expand Up @@ -43,26 +44,33 @@ export const Game = new function () {
this.keys = {};

this.setupInput = function () {
window.addEventListener('keydown', (e) => {
if (KEY_CODES[e.keyCode]) {
Game.keys[KEY_CODES[e.keyCode]] = true;
e.preventDefault();
}
}, false);
window.addEventListener('keydown', this.keyDown, false);

window.addEventListener('keyup', (e) => {
if (KEY_CODES[e.keyCode]) {
Game.keys[KEY_CODES[e.keyCode]] = false;
e.preventDefault();
}
}, false);
window.addEventListener('keyup', this.keyUp, false);
};

this.keyDown = function (e){
if (KEY_CODES[e.keyCode]) {
Game.keys[KEY_CODES[e.keyCode]] = true;
e.preventDefault();
}
}

this.keyUp = function (e){
if (KEY_CODES[e.keyCode]) {
Game.keys[KEY_CODES[e.keyCode]] = false;
e.preventDefault();
}
}


let lastTime = new Date().getTime();
const maxTime = 1 / 30;
// Game Loop
this.loop = function () {

if(stopGame) return;

const curTime = new Date().getTime();
requestAnimationFrame(Game.loop);
let dt = (curTime - lastTime) / 1000;
Expand Down Expand Up @@ -119,6 +127,17 @@ export const Game = new function () {
this.canvas.style.left = '0px';
this.canvas.style.top = '0px';
};

//after we are done with the game we delete all listeners
this.removeGame = function (){
stopGame = true;

window.removeEventListener('keydown', this.keyDown, false);

window.removeEventListener('keyup', this.keyUp, false);

this.canvas = null;
}
}();


Expand Down
4 changes: 4 additions & 0 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,7 @@ Explosion.prototype.step = function (dt) {
export const startPlaying = function () {
Game.initialize('game', sprites, startGame);
};

export const removeGame = function () {
Game.removeGame();
};
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/* eslint-disable no-alert */
/* eslint-disable func-names */

import { startPlaying } from './game';
import { startPlaying as start, removeGame as remove } from './game';

export default startPlaying;
export const startPlaying = start;

export const removeGame = remove;

0 comments on commit 52b75e6

Please sign in to comment.