Skip to content

Commit

Permalink
Rename some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Cociancich committed Sep 11, 2023
1 parent a7da6ac commit d94f787
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const level = document.getElementById("level");
function saveSettings() {
console.info('saveSettings', { skinmenu: skinmenu.value, lelvel: level.value });
console.info('saveSettings', { skinmenu: skinmenu.value, level: level.value });
localStorage.setItem("skin", skinmenu.value);
localStorage.setItem("level", level.value);
}
function loadSettings() {
level.value = localStorage.getItem("level");
skinmenu.value = localStorage.getItem("skin");
console.info('loadSettings', { skinmenu: skinmenu.value, lelvel: level.value });
console.info('loadSettings', { skinmenu: skinmenu.value, level: level.value });
}
const skincss = document.getElementById("skincss");
const skinmenu = document.getElementById("skinmenu");
Expand Down
34 changes: 18 additions & 16 deletions app/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Game {
room: this.room,
sensor: this.sensor,
});
this.input = new Input({ robot: this.robot });
this.input = new KeyboardInput({ robot: this.robot });
this.ui = new Ui({
robot: this.robot,
room: this.room,
Expand All @@ -65,27 +65,33 @@ class Game {
this.running = false;
}
}
class Input {
var Motion;
(function (Motion) {
Motion[Motion["LEFT"] = -100] = "LEFT";
Motion[Motion["FORWARD"] = 1] = "FORWARD";
Motion[Motion["RIGHT"] = 100] = "RIGHT";
Motion[Motion["BACK"] = -1] = "BACK";
})(Motion || (Motion = {}));
class KeyboardInput {
constructor(ctx) {
this.onKeyDown = (event) => {
switch (event.key) {
case "ArrowRight":
this.input = Input.RIGHT;
this.input = Motion.RIGHT;
break;
case "ArrowLeft":
this.input = Input.LEFT;
this.input = Motion.LEFT;
break;
case "ArrowUp":
this.input = Input.FORWARD;
this.input = Motion.FORWARD;
break;
case "ArrowDown":
this.input = Input.BACK;
this.input = Motion.BACK;
break;
default:
this.input = undefined;
return;
}
//this.keyUp = false;
event.preventDefault();
};
Object.assign(this, ctx);
Expand All @@ -99,21 +105,17 @@ class Input {
if (!dir)
return;
switch (dir) {
case Input.FORWARD:
case Input.BACK:
case Motion.FORWARD:
case Motion.BACK:
this.robot.move(dir);
break;
case Input.LEFT:
case Input.RIGHT:
case Motion.LEFT:
case Motion.RIGHT:
this.robot.rotate(dir);
break;
}
}
}
Input.LEFT = -100;
Input.FORWARD = +1;
Input.RIGHT = 100;
Input.BACK = -1;
class Ram {
constructor(props) {
this.size = 8;
Expand Down Expand Up @@ -210,7 +212,7 @@ class Robot {
look() {
this.sensor.detect();
}
move(amount = Input.FORWARD) {
move(amount = Motion.FORWARD) {
this.moving = true;
this.battery.use();
const newPosition = [...this.position];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"license": "MIT",
"private": true,
"scripts": {
"start": "serve app/"
"start": "serve app/",
"build": "lerna run build"
},
"devDependencies": {
"lerna": "^6.6.2",
Expand Down
4 changes: 2 additions & 2 deletions src/play/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var GameConfiguration = {
var game: Game;

class Game {
private input: Input;
private input: KeyboardInput;
private room = { floorPlan: undefined };
private ui: Ui;

Expand Down Expand Up @@ -49,7 +49,7 @@ class Game {
sensor: this.sensor,
});

this.input = new Input({ robot: this.robot });
this.input = new KeyboardInput({ robot: this.robot });

this.ui = new Ui({
robot: this.robot,
Expand Down
29 changes: 15 additions & 14 deletions src/play/input.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Input implements Component {
static LEFT = -100;
static FORWARD = +1;
static RIGHT = 100;
static BACK = -1;
enum Motion {
LEFT = -100,
FORWARD = +1,
RIGHT = 100,
BACK = -1,
}

class KeyboardInput implements Component {
robot: Robot;

private input?: number;
Expand All @@ -19,12 +21,12 @@ class Input implements Component {
this.input = undefined;
if (!dir) return;
switch (dir) {
case Input.FORWARD:
case Input.BACK:
case Motion.FORWARD:
case Motion.BACK:
this.robot.move(dir);
break;
case Input.LEFT:
case Input.RIGHT:
case Motion.LEFT:
case Motion.RIGHT:
this.robot.rotate(dir);
break;
}
Expand All @@ -33,22 +35,21 @@ class Input implements Component {
private onKeyDown = (event: KeyboardEvent) => {
switch (event.key) {
case "ArrowRight":
this.input = Input.RIGHT;
this.input = Motion.RIGHT;
break;
case "ArrowLeft":
this.input = Input.LEFT;
this.input = Motion.LEFT;
break;
case "ArrowUp":
this.input = Input.FORWARD;
this.input = Motion.FORWARD;
break;
case "ArrowDown":
this.input = Input.BACK;
this.input = Motion.BACK;
break;
default:
this.input = undefined;
return;
}
//this.keyUp = false;
event.preventDefault();
};
}
2 changes: 1 addition & 1 deletion src/play/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Robot {
this.sensor.detect();
}

move(amount = Input.FORWARD) {
move(amount = Motion.FORWARD) {
this.moving = true;
this.battery.use();
const newPosition = [...this.position];
Expand Down

0 comments on commit d94f787

Please sign in to comment.