Code edit (8 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: unit is not defined' in or related to this line: 'switch (unit.type) {' Line Number: 2176
Code edit (1 edits merged)
Please save this source code
Code edit (14 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: rockIlot2Center is not defined' in or related to this line: 'baseBuilding = {' Line Number: 1740
Code edit (1 edits merged)
Please save this source code
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: LK.getTime is not a function' in or related to this line: 'self.lastHitTime = LK.getTime();' Line Number: 1099
Code edit (2 edits merged)
Please save this source code
User prompt
could you implement findFactoryForUnit
Code edit (1 edits merged)
Please save this source code
User prompt
add a new function findEmplacementForBuilding(building, cellX, cellY) that scans the map around the given cellX,cellY to find a free area of rocks that fits the given building size
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Timeout.tick error: buildable.placeBuilding is not a function' in or related to this line: 'buildable.placeBuilding(placeX, placeY);' Line Number: 2117
Code edit (1 edits merged)
Please save this source code
Code edit (6 edits merged)
Please save this source code
User prompt
in handleDragEnd, don't select buildings if they are under fog
User prompt
don't select ennemy units and building if they are under fog
User prompt
in updateFogOfWar remove the fog in a radius shape instead of a square
Code edit (2 edits merged)
Please save this source code
User prompt
in building growExplosion, remove building from player list of buildings
User prompt
in checkGameEnd, check if player1 of player2 have no more buildings game over if so
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
call updateFogOfWar(); after every building placement
===================================================================
--- original.js
+++ change.js
@@ -100,10 +100,9 @@
return;
}
var progressDisplay = new BuildingProgressDisplay(self);
game.addChild(progressDisplay);
- //enqueueBuildable(self, progressDisplay);
- enqueueBuildable2(self.type, self.sourceFactory, player1, progressDisplay);
+ enqueueBuildable(self.type, self.sourceFactory, player1, progressDisplay);
game.children.forEach(function (child) {
if (child instanceof BuildableItemIcon) {
child.alpha = 0.75;
}
@@ -1333,8 +1332,30 @@
/****
* Game Code
****/
+function findFactoryForUnit(unitType, player) {
+ // Determine the required factory type based on the unit type
+ var requiredFactoryType;
+ switch (unitType) {
+ case 'unitQuad':
+ case 'unitLightTank':
+ requiredFactoryType = 'lightFactory';
+ break;
+ case 'unitHeavyTank':
+ case 'unitHarvester':
+ requiredFactoryType = 'heavyFactory';
+ break;
+ default:
+ console.error('Unknown unit type for factory requirement:', unitType);
+ return null;
+ }
+ // Find the first factory of the required type that belongs to the player
+ var suitableFactory = player.buildings.find(function (building) {
+ return building.type === requiredFactoryType;
+ });
+ return suitableFactory || null; // Return the factory or null if not found
+}
// Global function to calculate the angle between two points
function calculateAngle(element1, element2) {
var dx = element2.cellX - element1.cellX;
var dy = element2.cellY - element1.cellY;
@@ -1670,117 +1691,10 @@
}
}
return true;
}
-function enqueueBuildable(selectedBuildable, progressDisplay) {
- console.log("enqueueBuildable...", selectedBuildable);
- if (!currentSelection || !currentSelection.buildable.includes(selectedBuildable.type)) {
- console.log("Cannot build this item here.");
- return;
- }
- var buildableInfo = buildableRepository.getBuildableInfo(selectedBuildable.type);
- if (buildableInfo && player1.spice < buildableInfo.cost) {
- console.log("Not enough resources to build.");
- progressDisplay.blinkRed();
- return;
- }
- updateBaseInfo();
- var constructionTime = buildableInfo.constructionTime;
- var buildable;
- switch (buildableInfo.className) {
- case 'ConstructionYard':
- buildable = new ConstructionYard(0, 0, player1.playerId);
- break;
- case 'WindTrap':
- buildable = new WindTrap(0, 0, player1.playerId);
- break;
- case 'SpiceRefinery':
- buildable = new SpiceRefinery(0, 0, player1.playerId);
- break;
- case 'LightFactory':
- buildable = new LightFactory(0, 0, player1.playerId);
- break;
- case 'HeavyFactory':
- buildable = new HeavyFactory(0, 0, player1.playerId);
- break;
- case 'UnitQuad':
- buildable = new UnitQuad(0, 0, player1.playerId);
- break;
- case 'UnitLightTank':
- buildable = new UnitLightTank(0, 0, player1.playerId);
- break;
- case 'UnitHeavyTank':
- buildable = new UnitHeavyTank(0, 0, player1.playerId);
- break;
- case 'UnitHarvester':
- buildable = new UnitHarvester(0, 0, player1.playerId);
- break;
- // Add cases for other buildings as needed
- }
- if (!buildable) {
- console.error('Building object is not defined.');
- return;
- }
- buildable.constructionProgress = 0;
- buildable.constructionTimer = LK.setInterval(function () {
- buildable.constructionProgress += 0.1;
- var progressFraction = buildable.constructionProgress / constructionTime;
- buildable.progressDisplay.setProgress(progressFraction);
- // Deduct spice progressively based on construction progress
- var costPerTick = buildableInfo.cost * 0.1 / constructionTime;
- player1.spice -= costPerTick;
- updateBaseInfo();
- // When construction is complete...
- if (buildable.constructionProgress >= constructionTime) {
- LK.clearInterval(buildable.constructionTimer);
- console.log(selectedBuildable.type + " construction completed.");
- buildable.constructionTimer = null;
- buildable.progressDisplay.hide();
- if (buildable.isBuilding) {
- console.log("Building Ready for placement ", buildable);
- if (progressDisplay.parentIcon && typeof progressDisplay.parentIcon.setLabelToPlace === 'function') {
- progressDisplay.parentIcon.setLabelToPlace(); // Change label text to 'Place'
- }
- currentBuildingForPlacement = buildable;
- }
- if (buildable.isUnit) {
- console.log("Unit Ready to spawn ", buildable);
- var spawnCell = {
- cellX: 0,
- cellY: 0
- };
- if (selectedBuildable && selectedBuildable.sourceFactory) {
- console.log("Found source factory ", selectedBuildable.sourceFactory);
- spawnCell.cellX = selectedBuildable.sourceFactory.cellX;
- spawnCell.cellY = selectedBuildable.sourceFactory.cellY;
- } else {
- console.log("No source factory found ", selectedBuildable);
- }
- spawnUnit(buildable.type, spawnCell.cellX, spawnCell.cellY, player1.playerId);
- }
- game.children.forEach(function (child) {
- if (child instanceof BuildableItemIcon) {
- child.asset.interactive = true;
- child.alpha = 1.0;
- }
- });
- }
- }, 100);
- buildable.progressDisplay = progressDisplay;
- currentSelection.buildingQueue.push(buildable);
- game.children.forEach(function (child) {
- if (child instanceof BuildableItemIcon) {
- child.asset.interactive = false;
- }
- });
- // Instantiate and display the CancelActionButton on the command panel for buildings
- if (buildable.isBuilding) {
- cancelActionBtn = new CancelActionButton(commandPanel.x + commandPanel.width - 200, commandPanel.y + 200);
- }
- game.addChild(cancelActionBtn);
-}
-function enqueueBuildable2(buildableType, buildableSourceFactory, currentPlayer, progressDisplay) {
- console.log(currentPlayer.playerId + " - enqueueBuildable2...", buildableType, buildableSourceFactory);
+function enqueueBuildable(buildableType, buildableSourceFactory, currentPlayer, progressDisplay) {
+ console.log(currentPlayer.playerId + " - enqueueBuildable...", buildableType, buildableSourceFactory);
if (currentPlayer.playerId === player1.playerId && !currentSelection || !currentSelection.buildable.includes(buildableType)) {
console.log(currentPlayer.playerId + " - Cannot build this item here.");
return;
}
@@ -2081,22 +1995,25 @@
console.log("aiActing...");
// 1) Meet buildings requirements
console.log("aiNeededBuildingList:", aiNeededBuildingList);
if (Object.keys(aiNeededBuildingList).length > 0 && !player2.isCurrentlyBuilding) {
- aiHandleBuilding(aiNeededBuildingList);
+ aiHandleBuilding(Object.keys(aiNeededBuildingList)[0]);
}
// 2) Meet units requirements
console.log("aiNeededUnitsList:", aiNeededUnitsList);
+ if (Object.keys(aiNeededUnitsList).length > 0 && !player2.isCurrentlyBuildingUnit) {
+ var tempSourceFactory = findFactoryForUnit(Object.keys(aiNeededUnitsList)[0], player2);
+ aiHandleUnitConstruction(Object.keys(aiNeededUnitsList)[0], tempSourceFactory);
+ }
}
-function aiHandleBuilding(buildingList) {
- console.log("aiHandleBuilding...", buildingList);
+function aiHandleBuilding(buildingToBuild) {
+ console.log("aiHandleBuilding...", buildingToBuild);
if (player2.isCurrentlyBuilding) {
- console.log("Already building...");
+ console.log("Already building building...");
return;
}
player2.isCurrentlyBuilding = true;
- // Build first in the list - Later handle priorities
- enqueueBuildable2(Object.keys(buildingList)[0], null, player2);
+ enqueueBuildable(buildingToBuild, null, player2);
}
function aiPlaceBuilding(buildable) {
console.log("aiPlaceBuilding...", buildable);
if (!buildable || !buildable.isBuilding) {
@@ -2115,8 +2032,17 @@
var placeX = freePosition.cellX;
var placeY = freePosition.cellY;
handleBuildingPlacement(buildable, placeX, placeY);
}
+function aiHandleUnitConstruction(unitToBuild, sourceFactory) {
+ console.log("aiHandleUnitConstruction...", unitToBuild);
+ if (player2.isCurrentlyBuildingUnit) {
+ console.log("Already building unit...");
+ return;
+ }
+ player2.isCurrentlyBuildingUnit = true;
+ enqueueBuildable(unitToBuild, sourceFactory, player2);
+}
/* ***************************************************************************************************** */
/* ********************************************* INIT FUNCTIONS ****************************************** */
/* ***************************************************************************************************** */
// Prepare the map
a tileable sand terrain tile.
A square tileable rock terrain tile WITHOUT BBORDER. Single Game Texture. In-Game asset. 2d. No shadows. No Border
Zenith view of Dune's Wind Trap power facility square fence. Ressembles a bit to Sydney's Opera. Zenith view Directly from above.
grey cancel icon. UI
thin white circle empty.
0x5e86ff
Zenith view of a white rectangular Harvester shape of a garbage truck with a triangular head. Harvesting on sand, with flowing spice in the back. inside a square fence. Zenith view. Directly overhead. Plumb view.
Minimal Ui icon of an right sign aside with an icon of a target. sand background
Minimal icon of a home with direction icon pointing to the home. sand background
3 white flat isometric concentric circles like a target.
Remove background
Gray background
Minimal Ui icon of target sign on a fire icon.. sand background
top view of an explosion effect.
Simple heavy army tank factory buiding a tank with a crane. Square fence... Zenith view Directly overhead. Plumb view.
an empty black popup UI. UI