Add collision detection, initial player render

This commit is contained in:
Sky Johnson 2024-10-11 18:27:28 -05:00
parent a89d530cd0
commit 7101277cb8
6 changed files with 17 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@ -35,6 +35,7 @@ body {
text-align: center; text-align: center;
border-radius: 3px; border-radius: 3px;
user-select: none; user-select: none;
-webkit-user-select: none;
text-decoration: none; text-decoration: none;
transition: opacity 0.1s ease, background-color 0.1s ease, color 0.1s ease, background 0.1s ease; transition: opacity 0.1s ease, background-color 0.1s ease, color 0.1s ease, background 0.1s ease;
-webkit-tap-highlight-color: transparent; -webkit-tap-highlight-color: transparent;
@ -529,6 +530,7 @@ body::-webkit-scrollbar-thumb {
height: 440px; height: 440px;
image-rendering: pixelated; image-rendering: pixelated;
image-rendering: crisp-edges; image-rendering: crisp-edges;
image-rendering: -webkit-optimize-contrast;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
} }
} }

View File

@ -275,7 +275,7 @@ function ajax_only()
*/ */
function json_response($data) function json_response($data)
{ {
header('Content-Type: application/json'); header('Content-Type: application/json; charset=utf-8');
echo json_encode($data); echo json_encode($data);
exit; exit;
} }

View File

@ -11,7 +11,7 @@
foreach (links as $link): ?> foreach (links as $link): ?>
<a href="<?= $link[0] ?>" class="<?= ce(($GLOBALS['active_nav_tab'] ?? '') == $link[1], 'active') ?>"> <a href="<?= $link[0] ?>" class="<?= ce(($GLOBALS['active_nav_tab'] ?? '') == $link[1], 'active') ?>">
<img src="/assets/img/icons/<?= $link[2] ?>.png"> <img src="/assets/img/icons/<?= $link[2] ?>.png" title="<?= $link[3] ?>">
<?= $link[3] ?> <?= $link[3] ?>
</a> </a>
<?php endforeach; ?> <?php endforeach; ?>

View File

@ -76,6 +76,18 @@
}[e.key]; }[e.key];
if (direction !== undefined) { if (direction !== undefined) {
// If player would move outside map bounds, return early
if (direction === 0 && player.y === 0) return;
if (direction === 1 && player.y === map.length - 1) return;
if (direction === 2 && player.x === 0) return;
if (direction === 3 && player.x === map[0].length - 1) return;
// Return early if the player is trying to move into a wall
if (direction === 0 && map[player.y - 1][player.x] === 2) return;
if (direction === 1 && map[player.y + 1][player.x] === 2) return;
if (direction === 2 && map[player.y][player.x - 1] === 2) return;
if (direction === 3 && map[player.y][player.x + 1] === 2) return;
// Execute a POST request to /move. If successful, the server will return a new x,y position // Execute a POST request to /move. If successful, the server will return a new x,y position
fetch('/move', { fetch('/move', {
method: 'POST', method: 'POST',
@ -156,6 +168,7 @@
} }
window.addEventListener('load', () => { window.addEventListener('load', () => {
map[player.y][player.x] = 1
updateCanvasSize() updateCanvasSize()
setupEventListeners() setupEventListeners()
render() render()