Skip to content
Snippets Groups Projects
Commit 5992cab7 authored by Greatroot's avatar Greatroot
Browse files

Integrate Wu and my movement logic with Leannas character sprite rendering fix.

parent d86b122d
No related branches found
No related tags found
No related merge requests found
......@@ -193,8 +193,8 @@ function Game() {
}
// Variables to store the current position of the character
let characterX = 0;
let characterY = 0;
let characterX = startingTileIndex.x;
let characterY = startingTileIndex.y;
let lastKeyPressTime = 0;
let keyPressInterval = 200; // Adjust this value to change the movement speed
......@@ -218,21 +218,24 @@ function Game() {
y: mouseY
});
let newCharacterX = characterX;
let newCharacterY = characterY;
// Move the character based on WASD keys
if (keyIsDown(65) && millis() - lastKeyPressTime > keyPressInterval) { // 'A' key
characterX = constrain(characterX - 1, 0, tile_arr_size - 1);
newCharacterX = constrain(characterX - 1, 0, tile_arr_size - 1);
direction = 2; // left
lastKeyPressTime = millis();
} else if (keyIsDown(68) && millis() - lastKeyPressTime > keyPressInterval) { // 'D' key
characterX = constrain(characterX + 1, 0, tile_arr_size - 1);
newCharacterX = constrain(characterX + 1, 0, tile_arr_size - 1);
direction = 3; // right
lastKeyPressTime = millis();
} else if (keyIsDown(87) && millis() - lastKeyPressTime > keyPressInterval) { // 'W' key
characterY = constrain(characterY - 1, 0, tile_arr_size - 1);
newCharacterY = constrain(characterY - 1, 0, tile_arr_size - 1);
direction = 1; // backward
lastKeyPressTime = millis();
} else if (keyIsDown(83) && millis() - lastKeyPressTime > keyPressInterval) { // 'S' key
characterY = constrain(characterY + 1, 0, tile_arr_size - 1);
newCharacterY = constrain(characterY + 1, 0, tile_arr_size - 1);
direction = 0; // forward
lastKeyPressTime = millis();
}
......@@ -251,6 +254,13 @@ function Game() {
}
}
// Check to make sure that the new position of the character does not
// violate game rules (e.g. character cannot go onto broken sidewalk tiles)
if (movementIsValid(characterX, characterY, newCharacterX, newCharacterY)) {
characterX = newCharacterX;
characterY = newCharacterY;
}
// Second loop: display the character
let tsc = tileArr[characterX][characterY];
......@@ -301,6 +311,44 @@ function Game() {
// pop();
}
function movementIsValid(prevX, prevY, nextX, nextY) {
/*
Checks to see if the character moving from (prevX, prevY) to (nextX, nextY)
is valid for the wheelchair character to move to
*/
// Check to make sure that the new coords are valid, if not, then don't
// allow the player to move to that tile
let tileType_playerIsOn = tileArr[prevX][prevY].type;
let tileType_nextTile = tileArr[nextX][nextY].type;
// check redundancy later here
if (tileType_nextTile == "road" && tileType_playerIsOn == "sidewalk_corner_curbRamp") {
return true;
}
if (tileType_nextTile == "sidewalk_corner_curbRamp" && tileType_playerIsOn == "road") {
return true;
}
if (tileType_nextTile == "road" && tileType_playerIsOn == "road") {
return true;
}
if (tileType_nextTile == "sidewalk_corner_curbRamp") {
return true;
}
// I think we only want to walk on sidwalk no?
if (tileType_playerIsOn != "road" &&
(tileType_nextTile == "sidewalk" || tileType_nextTile == "sidewalk_corner")) {
return true;
}
// If we got here, then the movement is not allowed
return false;
}
this.mouseClicked = function() {
let {
x: tgc_x,
......@@ -366,21 +414,22 @@ function Game() {
// this.keyPressed = function() {
// if (key === 'r') {
// tileArr = rotateMatrixClockwise(tileArr);
// }
this.keyPressed = function() {
// FUTURE FEATURE: rotation
// if (key === 'r') {
// tileArr = rotateMatrixClockwise(tileArr);
// }
// if (key === 't') {
// // Cycle to the next tool in our tools array (and wrap back around if
// // we have reached the end of our tools array)
// currentToolIndex = (currentToolIndex + 1) % tools.length;
if (key === 't') {
// Cycle to the next tool in our tools array (and wrap back around if
// we have reached the end of our tools array)
currentToolIndex = (currentToolIndex + 1) % tools.length;
// // TODO: Change the cursor to an image that matches the current tool
// // type can either be predefined image or path to image
// // cursor(type, x, y)
// }
// }
// TODO: Change the cursor to an image that matches the current tool
// type can either be predefined image or path to image
// cursor(type, x, y)
}
}
// this.keyReleased = function() {
......@@ -399,10 +448,10 @@ function Game() {
// playerSprite_newTcgCords_x += 1;
// }
// // Check to make sure that the new coords are valid, if not, then don't
// // allow the player to move to that tile
// let tileType_playerIsOn = tileArr[playerSprite_curTgc.x][playerSprite_curTgc.y].type;
// let tileType_nextTile = tileArr[playerSprite_newTcgCords_x][playerSprite_newTcgCords_y].type;
// // Check to make sure that the new coords are valid, if not, then don't
// // allow the player to move to that tile
// let tileType_playerIsOn = tileArr[playerSprite_curTgc.x][playerSprite_curTgc.y].type;
// let tileType_nextTile = tileArr[playerSprite_newTcgCords_x][playerSprite_newTcgCords_y].type;
// // console.log(`\nCurrent tile: ${tileType_playerIsOn}`);
// // console.log(`Next tile: ${tileType_nextTile}`);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment