feat: Implement player block breaking with 10-pixel range and inventory collection

This commit is contained in:
Kacper Kostka (aider)
2025-04-05 19:12:11 +02:00
parent d765defa9d
commit 80c243ebf8
3 changed files with 80 additions and 35 deletions

View File

@@ -29,7 +29,7 @@ class Player extends Entity {
this.maxHealth = 100;
this.health = 100;
this.breakingPower = 1;
this.breakingRange = 3;
this.breakingRange = 10; // Increased from 3 to 10 pixels
this.isBreaking = false;
this.breakingCooldown = 0;
this.breakingCooldownMax = 10;
@@ -252,30 +252,38 @@ class Player extends Entity {
}
breakBlock() {
// Calculate the position in front of the player
const halfWidth = this.width / 2;
const breakX = Math.floor(this.x + (this.direction * this.breakingRange));
const breakY = Math.floor(this.y);
// Get mouse position in world coordinates
const worldX = Math.floor(currentMouseX / PIXEL_SIZE) + worldOffsetX;
const worldY = Math.floor(currentMouseY / PIXEL_SIZE) + worldOffsetY;
// Get the block type at that position
const blockType = getPixel(breakX, breakY);
// Calculate distance from player to target block
const distance = Math.sqrt(
Math.pow(worldX - this.x, 2) +
Math.pow(worldY - this.y, 2)
);
// Only break non-empty blocks that aren't special entities
if (blockType !== EMPTY &&
blockType !== WATER &&
blockType !== FIRE &&
blockType !== SQUARE &&
blockType !== CIRCLE &&
blockType !== TRIANGLE) {
// Only break blocks within range
if (distance <= this.breakingRange) {
// Get the block type at that position
const blockType = getPixel(worldX, worldY);
// Add to inventory based on block type
this.addToInventory(blockType);
// Replace with empty space
setPixel(breakX, breakY, EMPTY);
// Create a breaking effect (particles)
this.createBreakingEffect(breakX, breakY, blockType);
// Only break non-empty blocks that aren't special entities
if (blockType !== EMPTY &&
blockType !== WATER &&
blockType !== FIRE &&
blockType !== SQUARE &&
blockType !== CIRCLE &&
blockType !== TRIANGLE) {
// Add to inventory based on block type
this.addToInventory(blockType);
// Replace with empty space
setPixel(worldX, worldY, EMPTY);
// Create a breaking effect (particles)
this.createBreakingEffect(worldX, worldY, blockType);
}
}
}