about summary refs log tree commit diff stats
path: root/html/rogue/js/player.js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-12-24 14:36:12 -0500
committerelioat <elioat@tilde.institute>2024-12-24 14:36:12 -0500
commit1bbd9f9fe02e388f135f07d3cf9e66a5dcbbadfb (patch)
tree76656e73d8c7748a934f27abf908d282fa8bad51 /html/rogue/js/player.js
parent23a9df9793a3f1cc0dbdfc677254df56c33501f4 (diff)
downloadtour-1bbd9f9fe02e388f135f07d3cf9e66a5dcbbadfb.tar.gz
*
Diffstat (limited to 'html/rogue/js/player.js')
-rw-r--r--html/rogue/js/player.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/html/rogue/js/player.js b/html/rogue/js/player.js
index e69de29..270b26f 100644
--- a/html/rogue/js/player.js
+++ b/html/rogue/js/player.js
@@ -0,0 +1,64 @@
+const createPlayer = (x, y) => ({
+    x,
+    y,
+    width: 32,
+    height: 32,
+    velocityX: 0,
+    velocityY: 0,
+    speed: 300,
+    jumping: false
+});
+
+const updatePlayer = (player, deltaTime) => {
+    const keys = getKeys();
+    const seconds = deltaTime / 1000;
+
+    let velocityX = 0;
+    let velocityY = player.velocityY;
+
+    // Horizontal movement
+    if (keys.ArrowLeft) velocityX -= player.speed;
+    if (keys.ArrowRight) velocityX += player.speed;
+
+    // Simple jumping (can be improved)
+    if (keys.ArrowUp && !player.jumping) {
+        velocityY = -500;
+    }
+
+    // Apply gravity
+    velocityY += 980 * seconds; // 980 pixels/secondĀ²
+
+    // Update position
+    const x = player.x + velocityX * seconds;
+    const y = player.y + velocityY * seconds;
+
+    // Create updated player state
+    let updatedPlayer = {
+        ...player,
+        x,
+        y,
+        velocityX,
+        velocityY
+    };
+
+    // Handle collisions with the world
+    return handleWorldCollisions(updatedPlayer, window.gameState.world);
+};
+
+const renderPlayer = (ctx, player) => {
+    ctx.fillStyle = '#f00';
+    ctx.fillRect(player.x, player.y, player.width, player.height);
+};
+
+// Key handling
+const keys = {};
+
+window.addEventListener('keydown', (e) => {
+    keys[e.key] = true;
+});
+
+window.addEventListener('keyup', (e) => {
+    keys[e.key] = false;
+});
+
+const getKeys = () => ({...keys});