about summary refs log tree commit diff stats
path: root/html/rogue/js/player.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/rogue/js/player.js')
-rw-r--r--html/rogue/js/player.js188
1 files changed, 132 insertions, 56 deletions
diff --git a/html/rogue/js/player.js b/html/rogue/js/player.js
index 270b26f..5b4a875 100644
--- a/html/rogue/js/player.js
+++ b/html/rogue/js/player.js
@@ -1,64 +1,140 @@
-const createPlayer = (x, y) => ({
-    x,
-    y,
-    width: 32,
-    height: 32,
-    velocityX: 0,
-    velocityY: 0,
-    speed: 300,
-    jumping: false
-});
+// Player state and controls
+const player = {
+    position: { q: 0, r: 0 },    // Current hex position
+    target: null,                 // Target hex to move to
+    path: [],                     // Array of hex coordinates to follow
+    movementProgress: 0,          // Progress of current movement (0 to 1)
+    moveSpeed: 0.1,              // Movement speed (0 to 1 per frame)
+    
+    // Initialize player
+    init() {
+        this.position = { q: 0, r: 0 };
+        this.target = null;
+        this.path = [];
+        return this;
+    },
 
-const updatePlayer = (player, deltaTime) => {
-    const keys = getKeys();
-    const seconds = deltaTime / 1000;
+    // Get neighbors that share an edge with the given hex
+    getEdgeNeighbors(hex) {
+        const directions = [
+            {q: 1, r: 0},   // East
+            {q: 0, r: 1},   // Southeast
+            {q: -1, r: 1},  // Southwest
+            {q: -1, r: 0},  // West
+            {q: 0, r: -1},  // Northwest
+            {q: 1, r: -1}   // Northeast
+        ];
+        
+        return directions.map(dir => ({
+            q: hex.q + dir.q,
+            r: hex.r + dir.r
+        }));
+    },
 
-    let velocityX = 0;
-    let velocityY = player.velocityY;
+    // Find path from current position to target
+    findPath(targetHex) {
+        const start = this.position;
+        const goal = targetHex;
+        
+        // Simple breadth-first search
+        const queue = [[start]];
+        const visited = new Set();
+        const key = hex => `${hex.q},${hex.r}`;
+        visited.add(key(start));
+        
+        while (queue.length > 0) {
+            const path = queue.shift();
+            const current = path[path.length - 1];
+            
+            if (current.q === goal.q && current.r === goal.r) {
+                return path;
+            }
+            
+            const neighbors = this.getEdgeNeighbors(current);
+            for (const neighbor of neighbors) {
+                const neighborKey = key(neighbor);
+                if (!visited.has(neighborKey)) {
+                    visited.add(neighborKey);
+                    queue.push([...path, neighbor]);
+                }
+            }
+        }
+        
+        return null; // No path found
+    },
 
-    // Horizontal movement
-    if (keys.ArrowLeft) velocityX -= player.speed;
-    if (keys.ArrowRight) velocityX += player.speed;
+    // Start moving to a target hex
+    moveTo(targetHex) {
+        if (!this.target) {
+            const path = this.findPath(targetHex);
+            if (path) {
+                this.path = path.slice(1); // Remove starting position
+                if (this.path.length > 0) {
+                    this.target = this.path.shift();
+                    this.movementProgress = 0;
+                }
+            }
+        }
+    },
 
-    // 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
-    };
+    // Update player position
+    update() {
+        if (this.target) {
+            this.movementProgress += this.moveSpeed;
+            
+            if (this.movementProgress >= 1) {
+                // Movement to current target complete
+                this.position = this.target;
+                this.target = null;
+                this.movementProgress = 0;
+                
+                // If there are more points in the path, move to the next one
+                if (this.path.length > 0) {
+                    this.target = this.path.shift();
+                    this.movementProgress = 0;
+                }
+            }
+        }
+    },
 
-    // Handle collisions with the world
-    return handleWorldCollisions(updatedPlayer, window.gameState.world);
-};
+    // Get current interpolated position
+    getCurrentPosition() {
+        if (!this.target) {
+            return this.position;
+        }
 
-const renderPlayer = (ctx, player) => {
-    ctx.fillStyle = '#f00';
-    ctx.fillRect(player.x, player.y, player.width, player.height);
-};
+        // Interpolate between current position and target
+        return {
+            q: this.position.q + (this.target.q - this.position.q) * this.movementProgress,
+            r: this.position.r + (this.target.r - this.position.r) * this.movementProgress
+        };
+    },
 
-// Key handling
-const keys = {};
+    // Draw the player
+    draw(ctx, hexToPixel, camera, HEX_SIZE) {
+        const currentPos = this.getCurrentPosition();
+        const pixelPos = hexToPixel(currentPos);
+        const screenX = pixelPos.x - camera.x;
+        const screenY = pixelPos.y - camera.y;
 
-window.addEventListener('keydown', (e) => {
-    keys[e.key] = true;
-});
-
-window.addEventListener('keyup', (e) => {
-    keys[e.key] = false;
-});
-
-const getKeys = () => ({...keys});
+        ctx.fillStyle = 'red';
+        ctx.beginPath();
+        ctx.arc(screenX, screenY, HEX_SIZE/3, 0, Math.PI * 2);
+        ctx.fill();
+        
+        // Optionally, draw the remaining path
+        if (this.path.length > 0) {
+            ctx.strokeStyle = 'rgba(255,0,0,0.3)';
+            ctx.beginPath();
+            let lastPos = this.target || this.position;
+            this.path.forEach(point => {
+                const from = hexToPixel(lastPos);
+                const to = hexToPixel(point);
+                ctx.moveTo(from.x - camera.x, from.y - camera.y);
+                ctx.lineTo(to.x - camera.x, to.y - camera.y);
+                lastPos = point;
+            });
+            ctx.stroke();
+        }
+    }
+}; 
\ No newline at end of file