FAZA 2: Player entity with WASD movement, walking animation, and camera follow - Ready for testing
This commit is contained in:
163
src/entities/Player.js
Normal file
163
src/entities/Player.js
Normal file
@@ -0,0 +1,163 @@
|
||||
// Player Entity
|
||||
// Igralec z WASD kontrolami in isometrično podporo
|
||||
class Player {
|
||||
constructor(scene, gridX = 50, gridY = 50) {
|
||||
this.scene = scene;
|
||||
this.gridX = gridX;
|
||||
this.gridY = gridY;
|
||||
|
||||
this.iso = new IsometricUtils(48, 24);
|
||||
|
||||
// Hitrost gibanja
|
||||
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() {
|
||||
// Generiraj player teksturo
|
||||
TextureGenerator.createPlayerSprite(this.scene, 'player');
|
||||
TextureGenerator.createPlayerWalkSprite(this.scene, 'player_walk');
|
||||
|
||||
// Kreira sprite
|
||||
const screenPos = this.iso.toScreen(this.gridX, this.gridY);
|
||||
this.sprite = this.scene.add.sprite(screenPos.x, screenPos.y, 'player');
|
||||
this.sprite.setOrigin(0.5, 1); // Anchor na dnu sprite-a
|
||||
|
||||
// Depth sorting
|
||||
this.updateDepth();
|
||||
|
||||
// Dodaj walking animacijo
|
||||
if (!this.scene.anims.exists('player_walk_anim')) {
|
||||
this.scene.anims.create({
|
||||
key: 'player_walk_anim',
|
||||
frames: this.scene.anims.generateFrameNumbers('player_walk', {
|
||||
start: 0,
|
||||
end: 3
|
||||
}),
|
||||
frameRate: 8,
|
||||
repeat: -1
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Animacija hoje
|
||||
this.sprite.play('player_walk_anim', true);
|
||||
|
||||
// Tween za smooth gibanje
|
||||
this.scene.tweens.add({
|
||||
targets: this.sprite,
|
||||
x: targetScreen.x,
|
||||
y: targetScreen.y,
|
||||
duration: this.gridMoveTime,
|
||||
ease: 'Linear',
|
||||
onComplete: () => {
|
||||
this.isMoving = false;
|
||||
this.sprite.stop();
|
||||
this.sprite.setFrame(0); // Idle frame
|
||||
}
|
||||
});
|
||||
|
||||
// Posodobi depth
|
||||
this.updateDepth();
|
||||
}
|
||||
|
||||
updatePosition() {
|
||||
const screenPos = this.iso.toScreen(this.gridX, this.gridY);
|
||||
this.sprite.setPosition(screenPos.x, screenPos.y);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user