// Player Entity // Igralec z WASD kontrolami in isometrično podporo class Player { constructor(scene, gridX = 50, gridY = 50, offsetX = 0, offsetY = 0) { this.scene = scene; this.gridX = gridX; this.gridY = gridY; // Terrain offset (za sinhronizacijo s terrain containerjem) this.offsetX = offsetX; this.offsetY = offsetY; this.iso = new IsometricUtils(48, 24); // Hitrostgibanja this.moveSpeed = 150; // px/s this.gridMoveTime = 200; // ms za premik na eno kocko // Stanje this.isMoving = false; this.direction = 'down'; // Kreira sprite this.createSprite(); // Setup kontrole this.setupControls(); // Začetna pozicija this.updatePosition(); } createSprite() { // Use custom sprite if available, otherwise procedural let texKey = 'player'; if (this.scene.textures.exists('player_sprite')) { texKey = 'player_sprite'; } else if (!this.scene.textures.exists(texKey)) { TextureGenerator.createPlayerSprite(this.scene, texKey); } // Kreira sprite const screenPos = this.iso.toScreen(this.gridX, this.gridY); this.sprite = this.scene.add.sprite( screenPos.x + this.offsetX, screenPos.y + this.offsetY, texKey ); this.sprite.setOrigin(0.5, 1); this.sprite.setScale(0.3); // Optimized for visibility // Depth sorting this.updateDepth(); } setupControls() { // WASD kontrole this.keys = this.scene.input.keyboard.addKeys({ up: Phaser.Input.Keyboard.KeyCodes.W, down: Phaser.Input.Keyboard.KeyCodes.S, left: Phaser.Input.Keyboard.KeyCodes.A, right: Phaser.Input.Keyboard.KeyCodes.D }); } update(delta) { if (!this.isMoving) { this.handleInput(); } } handleInput() { let targetX = this.gridX; let targetY = this.gridY; let moved = false; // WASD za isometric movement if (this.keys.up.isDown) { // W = North-West v isometric view targetX--; moved = true; this.direction = 'up'; } else if (this.keys.down.isDown) { // S = South-East v isometric view targetX++; moved = true; this.direction = 'down'; } if (this.keys.left.isDown) { // A = South-West v isometric view targetY--; moved = true; this.direction = 'left'; } else if (this.keys.right.isDown) { // D = North-East v isometric view targetY++; moved = true; this.direction = 'right'; } // Preveri kolizijo z robovi mape const terrainSystem = this.scene.terrainSystem; if (moved && terrainSystem) { if (this.iso.isInBounds(targetX, targetY, terrainSystem.width, terrainSystem.height)) { this.moveToGrid(targetX, targetY); } } } moveToGrid(targetX, targetY) { this.isMoving = true; this.gridX = targetX; this.gridY = targetY; const targetScreen = this.iso.toScreen(targetX, targetY); // Tween za smooth gibanje (brez animacije za zdaj) this.scene.tweens.add({ targets: this.sprite, x: targetScreen.x + this.offsetX, y: targetScreen.y + this.offsetY, duration: this.gridMoveTime, ease: 'Linear', onComplete: () => { this.isMoving = false; } }); // Posodobi depth this.updateDepth(); } updatePosition() { const screenPos = this.iso.toScreen(this.gridX, this.gridY); this.sprite.setPosition( screenPos.x + this.offsetX, screenPos.y + this.offsetY ); this.updateDepth(); } updateDepth() { const depth = this.iso.getDepth(this.gridX, this.gridY); this.sprite.setDepth(depth + 1000); // +1000 da je nad terenom } getPosition() { return { x: this.gridX, y: this.gridY }; } getScreenPosition() { return { x: this.sprite.x, y: this.sprite.y }; } destroy() { if (this.sprite) { this.sprite.destroy(); } } }