feat: Implement player block breaking with 10-pixel range and inventory collection
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user