posodobitev
This commit is contained in:
226
src/systems/BuildSystem.js
Normal file
226
src/systems/BuildSystem.js
Normal file
@@ -0,0 +1,226 @@
|
||||
class BuildSystem {
|
||||
constructor(scene) {
|
||||
this.scene = scene;
|
||||
this.buildMode = false;
|
||||
this.selectedBuilding = 'fence';
|
||||
this.previewSprite = null;
|
||||
this.placedBuildings = [];
|
||||
|
||||
// Building definitions
|
||||
this.buildings = {
|
||||
'fence': {
|
||||
name: 'Fence (Old)',
|
||||
textureKey: 'fence_isometric',
|
||||
cost: { wood: 2 },
|
||||
collision: false,
|
||||
scale: 0.3
|
||||
},
|
||||
'fence_post': {
|
||||
name: 'Fence Post',
|
||||
textureKey: 'fence_post',
|
||||
cost: { wood: 1 },
|
||||
collision: false,
|
||||
scale: 0.2
|
||||
},
|
||||
'fence_horizontal': {
|
||||
name: 'Fence →',
|
||||
textureKey: 'fence_horizontal',
|
||||
cost: { wood: 2 },
|
||||
collision: false,
|
||||
scale: 0.2
|
||||
},
|
||||
'fence_vertical': {
|
||||
name: 'Fence ↓',
|
||||
textureKey: 'fence_vertical',
|
||||
cost: { wood: 2 },
|
||||
collision: false,
|
||||
scale: 0.2
|
||||
},
|
||||
'fence_corner': {
|
||||
name: 'Fence ⌞',
|
||||
textureKey: 'fence_corner',
|
||||
cost: { wood: 2 },
|
||||
collision: false,
|
||||
scale: 0.2
|
||||
},
|
||||
'barn': {
|
||||
name: 'Barn',
|
||||
textureKey: 'barn_isometric',
|
||||
cost: { wood: 40, stone: 20 },
|
||||
collision: true,
|
||||
scale: 0.5
|
||||
},
|
||||
'grave': {
|
||||
name: 'Grave',
|
||||
textureKey: 'grave_zombie',
|
||||
cost: { stone: 10 },
|
||||
collision: false,
|
||||
scale: 0.3
|
||||
},
|
||||
'farmhouse': {
|
||||
name: 'Farmhouse',
|
||||
textureKey: 'farmhouse_isometric',
|
||||
cost: { wood: 50, stone: 30, gold: 100 },
|
||||
collision: true,
|
||||
scale: 0.5
|
||||
},
|
||||
'blacksmith': {
|
||||
name: 'Blacksmith',
|
||||
textureKey: 'blacksmith_workshop',
|
||||
cost: { wood: 30, stone: 40, gold: 80 },
|
||||
collision: true,
|
||||
scale: 0.45
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
toggleBuildMode() {
|
||||
this.buildMode = !this.buildMode;
|
||||
console.log(`Build Mode: ${this.buildMode ? 'ON' : 'OFF'}`);
|
||||
|
||||
// Notify UI
|
||||
const uiScene = this.scene.scene.get('UIScene');
|
||||
if (uiScene) {
|
||||
uiScene.toggleBuildMenu(this.buildMode);
|
||||
}
|
||||
|
||||
// Show/hide preview
|
||||
if (this.buildMode) {
|
||||
this.createPreview();
|
||||
} else {
|
||||
this.destroyPreview();
|
||||
}
|
||||
|
||||
return this.buildMode;
|
||||
}
|
||||
|
||||
selectBuilding(buildingId) {
|
||||
if (!this.buildings[buildingId]) return;
|
||||
this.selectedBuilding = buildingId;
|
||||
|
||||
// Update UI
|
||||
const uiScene = this.scene.scene.get('UIScene');
|
||||
if (uiScene) {
|
||||
uiScene.updateBuildSelection(this.buildings[buildingId].name);
|
||||
}
|
||||
|
||||
// Refresh preview
|
||||
if (this.buildMode) {
|
||||
this.destroyPreview();
|
||||
this.createPreview();
|
||||
}
|
||||
}
|
||||
|
||||
createPreview() {
|
||||
const building = this.buildings[this.selectedBuilding];
|
||||
if (!this.scene.textures.exists(building.textureKey)) {
|
||||
console.warn(`Texture not found: ${building.textureKey}`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.previewSprite = this.scene.add.sprite(0, 0, building.textureKey);
|
||||
this.previewSprite.setOrigin(0.5, 1);
|
||||
this.previewSprite.setAlpha(0.6);
|
||||
this.previewSprite.setDepth(10000); // Always on top
|
||||
this.previewSprite.setScale(building.scale || 1.0);
|
||||
}
|
||||
|
||||
destroyPreview() {
|
||||
if (this.previewSprite) {
|
||||
this.previewSprite.destroy();
|
||||
this.previewSprite = null;
|
||||
}
|
||||
}
|
||||
|
||||
update() {
|
||||
if (!this.buildMode || !this.previewSprite) return;
|
||||
|
||||
// Follow mouse
|
||||
const pointer = this.scene.input.activePointer;
|
||||
const worldPoint = this.scene.cameras.main.getWorldPoint(pointer.x, pointer.y);
|
||||
const gridPos = this.scene.iso.toGrid(worldPoint.x, worldPoint.y);
|
||||
const screenPos = this.scene.iso.toScreen(gridPos.x, gridPos.y);
|
||||
|
||||
this.previewSprite.setPosition(screenPos.x, screenPos.y);
|
||||
|
||||
// Check if can place
|
||||
const canPlace = this.canPlaceAt(gridPos.x, gridPos.y);
|
||||
this.previewSprite.setTint(canPlace ? 0x00ff00 : 0xff0000);
|
||||
|
||||
// Place on click
|
||||
if (pointer.isDown && canPlace) {
|
||||
this.placeBuilding(gridPos.x, gridPos.y);
|
||||
}
|
||||
}
|
||||
|
||||
canPlaceAt(gridX, gridY) {
|
||||
// Check if already has building
|
||||
const exists = this.placedBuildings.find(b => b.gridX === gridX && b.gridY === gridY);
|
||||
if (exists) return false;
|
||||
|
||||
// Check terrain (only on farm tiles)
|
||||
if (!this.scene.terrainSystem) return false;
|
||||
const tile = this.scene.terrainSystem.getTile(gridX, gridY);
|
||||
if (!tile || tile.type !== 'farm') return false;
|
||||
|
||||
// Check cost
|
||||
const building = this.buildings[this.selectedBuilding];
|
||||
if (!this.hasResources(building.cost)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
hasResources(cost) {
|
||||
const inv = this.scene.inventorySystem;
|
||||
if (!inv) return false;
|
||||
|
||||
for (const [resource, amount] of Object.entries(cost)) {
|
||||
if (resource === 'gold') {
|
||||
if (inv.gold < amount) return false;
|
||||
} else {
|
||||
if (!inv.hasItem(resource, amount)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
placeBuilding(gridX, gridY) {
|
||||
const building = this.buildings[this.selectedBuilding];
|
||||
|
||||
// Consume resources
|
||||
const inv = this.scene.inventorySystem;
|
||||
for (const [resource, amount] of Object.entries(building.cost)) {
|
||||
if (resource === 'gold') {
|
||||
inv.gold -= amount;
|
||||
} else {
|
||||
inv.removeItem(resource, amount);
|
||||
}
|
||||
}
|
||||
|
||||
// Create sprite
|
||||
const screenPos = this.scene.iso.toScreen(gridX, gridY);
|
||||
const sprite = this.scene.add.sprite(screenPos.x, screenPos.y, building.textureKey);
|
||||
sprite.setOrigin(0.5, 1);
|
||||
sprite.setScale(building.scale || 1.0);
|
||||
sprite.setDepth(this.scene.iso.getDepth(gridX, gridY) + 5); // Above terrain
|
||||
|
||||
// Store
|
||||
this.placedBuildings.push({
|
||||
gridX,
|
||||
gridY,
|
||||
type: this.selectedBuilding,
|
||||
sprite,
|
||||
collision: building.collision
|
||||
});
|
||||
|
||||
console.log(`🏗️ Placed ${building.name} at (${gridX}, ${gridY})`);
|
||||
|
||||
// Update UI
|
||||
this.scene.events.emit('update-inventory');
|
||||
}
|
||||
|
||||
isCollisionAt(gridX, gridY) {
|
||||
const building = this.placedBuildings.find(b => b.gridX === gridX && b.gridY === gridY);
|
||||
return building ? building.collision : false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user