added config manager (closes #18) and fixed crash at win

This commit is contained in:
Nathan DECHER
2020-04-06 10:58:44 +02:00
parent a7e2d1c201
commit d339dd0a06
8 changed files with 331 additions and 120 deletions
+10 -8
View File
@@ -104,7 +104,7 @@ class SnekGame {
draw() {
const assets=require('assets');
const config=this.config;
const config=require('config');
// clear the canvas, because it's easier than having to deal with everything
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
@@ -125,7 +125,7 @@ class SnekGame {
this.ctx.fillRect(0, offsetY+cellSize*this.dimensions[1], this.canvas.width, offsetY);
// draw a grid/checkerboard if requested
if(config.appearance.grid=='grid') {
if(config.get('appearance.grid')=='grid') {
this.ctx.strokeStyle='rgba(0, 0, 0, 50%)';
this.ctx.lineCap='square';
this.ctx.lineWidth=1;
@@ -139,7 +139,7 @@ class SnekGame {
this.ctx.lineTo(this.canvas.width-offsetX, offsetY+y*cellSize);
}
this.ctx.stroke();
} else if(config.appearance.grid=='checkerboard') {
} else if(config.get('appearance.grid')=='checkerboard') {
this.ctx.fillStyle='rgba(0, 0, 0, 10%)';
for(let x=0; x<this.dimensions[0]; x++) {
for(let y=(x+1)%2; y<this.dimensions[1]; y+=2) {
@@ -400,6 +400,8 @@ class SnekGame {
}
handleInputs(inputs) {
const config=require('config');
// change direction if the input is valid
const trySet=(dir) => {
if(!dir.every((e, i) => e==this.lastDirection[i] || e==-this.lastDirection[i])) {
@@ -420,13 +422,13 @@ class SnekGame {
});
// try all inputs in order and unbuffer them if valid
if(inputs.left && trySet([-1, 0])) return delete inputs.left;
else if(inputs.right && trySet([ 1, 0])) return delete inputs.right;
else if(inputs.up && trySet([ 0,-1])) return delete inputs.up;
else if(inputs.down && trySet([ 0, 1])) return delete inputs.down;
if(inputs.left && trySet([-1, 0])) delete inputs.left;
else if(inputs.right && trySet([ 1, 0])) delete inputs.right;
else if(inputs.up && trySet([ 0,-1])) delete inputs.up;
else if(inputs.down && trySet([ 0, 1])) delete inputs.down;
// buffering might be disabled
if(inputs.clearBuffer) {
if(!config.getB('input.buffer')) {
Object
.keys(inputs)
.forEach(k => delete inputs[k]);