udomacenje zombija in uboj\
This commit is contained in:
@@ -6,19 +6,20 @@ class Player {
|
||||
this.gridX = gridX;
|
||||
this.gridY = gridY;
|
||||
|
||||
// Terrain offset (za sinhronizacijo s terrain containerjem)
|
||||
// Terrain offset
|
||||
this.offsetX = offsetX;
|
||||
this.offsetY = offsetY;
|
||||
|
||||
this.iso = new IsometricUtils(48, 24);
|
||||
|
||||
// Hitrostgibanja
|
||||
// Hitrost gibanja
|
||||
this.moveSpeed = 150; // px/s
|
||||
this.gridMoveTime = 200; // ms za premik na eno kocko
|
||||
|
||||
// Stanje
|
||||
this.isMoving = false;
|
||||
this.direction = 'down';
|
||||
this.lastDir = { x: 0, y: 1 }; // Default south
|
||||
|
||||
// Kreira sprite
|
||||
this.createSprite();
|
||||
@@ -26,6 +27,11 @@ class Player {
|
||||
// Setup kontrole
|
||||
this.setupControls();
|
||||
|
||||
// Space za napad
|
||||
this.scene.input.keyboard.on('keydown-SPACE', () => {
|
||||
this.attack();
|
||||
});
|
||||
|
||||
// Začetna pozicija
|
||||
this.updatePosition();
|
||||
}
|
||||
@@ -53,7 +59,7 @@ class Player {
|
||||
|
||||
// Scale logic
|
||||
if (isAnimated) {
|
||||
this.sprite.setScale(1.5); // 64px frame -> looks good around 96px total height relative to 48px tile
|
||||
this.sprite.setScale(1.5);
|
||||
} else {
|
||||
this.sprite.setScale(0.3);
|
||||
}
|
||||
@@ -61,58 +67,17 @@ class Player {
|
||||
// --- HAND / HELD ITEM SPRITE ---
|
||||
this.handSprite = this.scene.add.sprite(
|
||||
screenPos.x + this.offsetX + 10,
|
||||
screenPos.y + this.offsetY - 25, // Adjusted for new height
|
||||
screenPos.y + this.offsetY - 25,
|
||||
'item_axe'
|
||||
);
|
||||
this.handSprite.setOrigin(0.5, 0.5);
|
||||
this.handSprite.setScale(0.25);
|
||||
this.handSprite.setVisible(false);
|
||||
|
||||
// Depth sorting
|
||||
this.updateDepth();
|
||||
}
|
||||
|
||||
// ... setupControls ...
|
||||
|
||||
// ... update ...
|
||||
|
||||
moveToGrid(targetX, targetY) {
|
||||
this.isMoving = true;
|
||||
this.gridX = targetX;
|
||||
this.gridY = targetY;
|
||||
|
||||
const targetScreen = this.iso.toScreen(targetX, targetY);
|
||||
|
||||
// Play Animation
|
||||
if (this.sprite.texture.key === 'player_walk') {
|
||||
this.sprite.play('player_walk_anim', true);
|
||||
}
|
||||
|
||||
// Tween za smooth gibanje
|
||||
this.scene.tweens.add({
|
||||
targets: [this.sprite, this.handSprite], // Move both
|
||||
x: '+=' + (targetScreen.x + this.offsetX - this.sprite.x),
|
||||
y: '+=' + (targetScreen.y + this.offsetY - this.sprite.y),
|
||||
duration: this.gridMoveTime,
|
||||
ease: 'Linear',
|
||||
onComplete: () => {
|
||||
this.isMoving = false;
|
||||
this.updatePosition();
|
||||
|
||||
// Stop Animation
|
||||
if (this.sprite.texture.key === 'player_walk') {
|
||||
this.sprite.stop();
|
||||
this.sprite.setFrame(0); // Idle frame
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Posodobi depth
|
||||
this.updateDepth();
|
||||
}
|
||||
|
||||
setupControls() {
|
||||
// WASD kontrole
|
||||
this.keys = this.scene.input.keyboard.addKeys({
|
||||
up: Phaser.Input.Keyboard.KeyCodes.W,
|
||||
down: Phaser.Input.Keyboard.KeyCodes.S,
|
||||
@@ -121,14 +86,43 @@ class Player {
|
||||
});
|
||||
}
|
||||
|
||||
attack() {
|
||||
console.log('⚔️ Player Attack!');
|
||||
if (this.scene.interactionSystem) {
|
||||
const targetX = this.gridX + this.lastDir.x;
|
||||
const targetY = this.gridY + this.lastDir.y;
|
||||
this.scene.interactionSystem.handleInteraction(targetX, targetY, true); // true = attackMode
|
||||
}
|
||||
|
||||
// Animation
|
||||
this.scene.tweens.add({
|
||||
targets: this.handSprite,
|
||||
angle: 45, // Swing
|
||||
yoyo: true,
|
||||
duration: 100
|
||||
});
|
||||
|
||||
// Player lunge
|
||||
const lungeX = this.sprite.x + (this.lastDir.x * 10);
|
||||
const lungeY = this.sprite.y + (this.lastDir.y * 5);
|
||||
this.scene.tweens.add({
|
||||
targets: this.sprite,
|
||||
x: lungeX,
|
||||
y: lungeY,
|
||||
yoyo: true,
|
||||
duration: 50
|
||||
});
|
||||
}
|
||||
|
||||
update(delta) {
|
||||
this.updateDepth();
|
||||
if (this.isMoving) {
|
||||
this.updateDepth();
|
||||
}
|
||||
|
||||
if (!this.isMoving) {
|
||||
this.handleInput();
|
||||
}
|
||||
|
||||
// Sync Held Item with Inventory
|
||||
this.updateHeldItem();
|
||||
}
|
||||
|
||||
@@ -158,47 +152,110 @@ class Player {
|
||||
let targetX = this.gridX;
|
||||
let targetY = this.gridY;
|
||||
let moved = false;
|
||||
let facingRight = !this.sprite.flipX; // Keep current
|
||||
let facingRight = !this.sprite.flipX;
|
||||
|
||||
// WASD za isometric movement
|
||||
if (this.keys.up.isDown) { // North-West
|
||||
targetX--;
|
||||
// WASD
|
||||
let dx = 0;
|
||||
let dy = 0;
|
||||
|
||||
if (this.keys.up.isDown) {
|
||||
dx = -1; dy = 0;
|
||||
moved = true;
|
||||
facingRight = false; // Left-ish
|
||||
} else if (this.keys.down.isDown) { // South-East
|
||||
targetX++;
|
||||
facingRight = false;
|
||||
} else if (this.keys.down.isDown) {
|
||||
dx = 1; dy = 0;
|
||||
moved = true;
|
||||
facingRight = true; // Right-ish
|
||||
facingRight = true;
|
||||
}
|
||||
|
||||
if (this.keys.left.isDown) { // South-West
|
||||
targetY++; // SWAPPED: Was --
|
||||
if (this.keys.left.isDown) {
|
||||
dx = 0; dy = 1;
|
||||
moved = true;
|
||||
facingRight = false; // Left-ish
|
||||
} else if (this.keys.right.isDown) { // North-East
|
||||
targetY--; // SWAPPED: Was ++
|
||||
facingRight = false;
|
||||
} else if (this.keys.right.isDown) {
|
||||
dx = 0; dy = -1;
|
||||
moved = true;
|
||||
facingRight = true; // Right-ish
|
||||
facingRight = true;
|
||||
}
|
||||
|
||||
// Apply Facing
|
||||
this.sprite.setFlipX(!facingRight);
|
||||
// Update target
|
||||
targetX = this.gridX + dx;
|
||||
targetY = this.gridY + dy;
|
||||
|
||||
// Update Hand Position based on facing
|
||||
const handOffset = facingRight ? 10 : -10;
|
||||
this.handSprite.setX(this.sprite.x + handOffset);
|
||||
this.handSprite.setFlipX(!facingRight);
|
||||
// Update Facing Direction and Last Dir
|
||||
if (moved) {
|
||||
// Keep diagonal input clean or prioritize one axis?
|
||||
// Just use the calculated dx/dy.
|
||||
// Note: If both UP and LEFT pressed, logic above overwrites dx/dy.
|
||||
// Let's refine to allow diagonal accumulation if needed, but existing logic prioritized axis.
|
||||
// Current logic: RIGHT/LEFT overwrites UP/DOWN. This is fine for now.
|
||||
|
||||
// Preveri kolizijo z robovi mape
|
||||
this.lastDir = { x: dx, y: dy };
|
||||
this.sprite.setFlipX(!facingRight);
|
||||
|
||||
// Hand offset
|
||||
const handOffset = facingRight ? 10 : -10;
|
||||
this.handSprite.setX(this.sprite.x + handOffset);
|
||||
this.handSprite.setFlipX(!facingRight);
|
||||
}
|
||||
|
||||
// Collision Check
|
||||
const terrainSystem = this.scene.terrainSystem;
|
||||
if (moved && terrainSystem) {
|
||||
if (this.iso.isInBounds(targetX, targetY, terrainSystem.width, terrainSystem.height)) {
|
||||
this.moveToGrid(targetX, targetY);
|
||||
|
||||
const tile = terrainSystem.tiles[targetY][targetX];
|
||||
let isPassable = true;
|
||||
|
||||
if (tile.type.name === 'water') isPassable = false;
|
||||
|
||||
const key = `${targetX},${targetY}`;
|
||||
if (terrainSystem.decorationsMap.has(key)) {
|
||||
const decor = terrainSystem.decorationsMap.get(key);
|
||||
const solidTypes = ['tree', 'stone', 'bush', 'wall', 'ruin', 'fence', 'house', 'gravestone'];
|
||||
if (solidTypes.includes(decor.type)) {
|
||||
console.log('⛔ Blocked by:', decor.type);
|
||||
isPassable = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (isPassable) {
|
||||
this.moveToGrid(targetX, targetY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
moveToGrid(targetX, targetY) {
|
||||
this.isMoving = true;
|
||||
this.gridX = targetX;
|
||||
this.gridY = targetY;
|
||||
|
||||
const targetScreen = this.iso.toScreen(targetX, targetY);
|
||||
|
||||
if (this.sprite.texture.key === 'player_walk') {
|
||||
this.sprite.play('player_walk_anim', true);
|
||||
}
|
||||
|
||||
this.scene.tweens.add({
|
||||
targets: [this.sprite, this.handSprite],
|
||||
x: '+=' + (targetScreen.x + this.offsetX - this.sprite.x),
|
||||
y: '+=' + (targetScreen.y + this.offsetY - this.sprite.y),
|
||||
duration: this.gridMoveTime,
|
||||
ease: 'Linear',
|
||||
onComplete: () => {
|
||||
this.isMoving = false;
|
||||
this.updatePosition();
|
||||
|
||||
if (this.sprite.texture.key === 'player_walk') {
|
||||
this.sprite.stop();
|
||||
this.sprite.setFrame(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.updateDepth();
|
||||
}
|
||||
|
||||
updatePosition() {
|
||||
const screenPos = this.iso.toScreen(this.gridX, this.gridY);
|
||||
@@ -207,7 +264,6 @@ class Player {
|
||||
screenPos.y + this.offsetY
|
||||
);
|
||||
|
||||
// Update hand
|
||||
const facingRight = !this.sprite.flipX;
|
||||
const handOffset = facingRight ? 10 : -10;
|
||||
this.handSprite.setPosition(
|
||||
@@ -219,7 +275,6 @@ class Player {
|
||||
}
|
||||
|
||||
updateDepth() {
|
||||
// Pixel-perfect depth sorting based on screen Y
|
||||
if (this.sprite) {
|
||||
this.sprite.setDepth(this.sprite.y);
|
||||
if (this.handSprite) this.handSprite.setDepth(this.sprite.y + 1);
|
||||
@@ -235,9 +290,7 @@ class Player {
|
||||
}
|
||||
|
||||
destroy() {
|
||||
if (this.sprite) {
|
||||
this.sprite.destroy();
|
||||
}
|
||||
if (this.sprite) this.sprite.destroy();
|
||||
}
|
||||
|
||||
dieAnimation() {
|
||||
|
||||
Reference in New Issue
Block a user