document.addEventListener("keydown",keyDown);
gameObjects = [];
const boxStyles = getComputedStyle(document.getElementById("box"))
// This accomplishes what your code was doing, but in two steps
// Placing a + before a string converts it to a positive Number value
const SCREEN_WIDTH = +boxStyles.getPropertyValue("width").replace('px',"");
const SCREEN_HEIGHT = +boxStyles.getPropertyValue("height").replace('px',"");
function createGameObject(id, x, y) {
const retVal = {
"id": id,
"element": document.getElementById(id),
"x": x,
"y": y,
"width": document.getElementById(id).offsetWidth,
"height": document.getElementById(id).offsetHeight,
"setScreenPosition": function(){this.element.style.left = this.x + "px"; this.element.style.top = this.y + "px";},
"moveLeft": function() {this.x -= this.width; this.setScreenPosition();},
"moveRight": function() {this.x += this.width; this.setScreenPosition();},
"moveUp": function() {this.y -= this.height; this.setScreenPosition();},
"moveDown": function() {this.y += this.height; this.setScreenPosition();},
"setXY": function(x, y) {this.x = x; this.y = y; this.setScreenPosition();}
}
retVal.setScreenPosition();
gameObjects.push(retVal);
return retVal;
}
const myPlayer = createGameObject("player", 0, 0);
const myBrick = createGameObject("brick", 128, 32);
const myWall = createGameObject("wall", 256, 128);
function keyDown(e) {
switch(e.keyCode) {
case 37:
moveLeft(myPlayer);
break;
case 38:
moveUp(myPlayer);
break;
case 39:
moveRight(myPlayer);
break;
case 40:
moveDown(myPlayer);
break;
}
}
function moveLeft(gameObject) {
let newX = gameObject.x - gameObject.width;
if (newX < 0){
console.log("Off the screen to the left.");
return
}
// The following three lines perform these actions:
// Check if the gameObj[brick] will touch gameObject[Player] (x)
// Check if the brick is in the way in the other dimension (y)
// Check that the object we're comparing against isn't itself
// I'm using filter to create an array of all objects in the path and then checking the length of that array. An empty array means nothing in the way
if (gameObjects.filter(gameObj=>gameObj.x <= newX && gameObj.x + gameObj.width >= gameObject.x &&
gameObject.y >= gameObj.y && gameObject.y + gameObject.height <= gameObj.y + gameObj.height &&
gameObj.id !== gameObject.id).length) {
console.log("Hitting something on the left.");
return;
}
gameObject.moveLeft();
}
function moveRight(gameObject) {
let newX = gameObject.x + gameObject.width;
if (newX >= SCREEN_WIDTH) {
console.log("Off the screen to the right.");
return;
}
if (gameObjects.filter(gameObj=>gameObj.x === newX && gameObject.y >= gameObj.y &&
gameObject.y + gameObject.height <= gameObj.y + gameObj.height &&
gameObj.id !== gameObject.id).length) {
console.log("Hitting something on the right.")
return;
}
gameObject.moveRight();
}
function moveUp(gameObject) {
let newY = gameObject.y - gameObject.height;
if (newY < 0) {
console.log("Off the screen to the top.");
return;
}
if (gameObjects.filter(gameObj=>gameObj.y <= newY && gameObj.y + gameObj.height >= gameObject.y &&
gameObject.x >= gameObj.x && gameObject.x + gameObject.width <= gameObj.x + gameObj.width &&
gameObj.id !== gameObject.id).length) {
console.log("Hitting something above.");
return;
}
gameObject.moveUp();
}
function moveDown(gameObject) {
let newY = gameObject.y + gameObject.height;
if (newY >= SCREEN_HEIGHT) {
console.log("Off the screen to the bottom.");
return;
}
if (gameObjects.filter(gameObj=>gameObj.y === newY && gameObject.x >= gameObj.x &&
gameObject.x + gameObject.width <= gameObj.x + gameObj.width &&
gameObj.id !== gameObject.id).length) {
console.log("Hitting something below.");
return;
}
gameObject.moveDown();
}
#box {
position:absolute
top:0px;
left:0px;
background-color:blue;
width:512px;
height:256px;
}
#player {
position:absolute;
background-color:red;
width:8px;
height:8px;
z-index:2;
}
#world {
position:relative;
bottom:0px;
left:0px;
width:100%;
height:100%;
z-index:1;
}
#brick {
position:absolute;
background-color:black;
width:16px;
height:16px;
z-index:1;
}
#wall {
position:absolute;
background-color:yellow;
width:16px;
height:48px;
z-index:1;
}
<div id="box">
<div id="world">
<div id="brick"></div>
<div id="wall"/></div>
<div id="player"/></div>
</div>
</div>