Skip to content
Open
2 changes: 1 addition & 1 deletion src/client/graphics/layers/RadialMenuElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export const deleteUnitElement: MenuElement = {
.units()
.filter(
(unit) =>
unit.constructionType() === undefined &&
!unit.isUnderConstruction() &&
unit.markedForDeletion() === false &&
params.game.manhattanDist(unit.tile(), params.tile) <=
DELETE_SELECTION_RADIUS,
Expand Down
11 changes: 2 additions & 9 deletions src/client/graphics/layers/StructureDrawingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,12 @@ export class SpriteFactory {
const screenPos = this.transformHandler.worldToScreenCoordinates(worldPos);

const isMarkedForDeletion = unit.markedForDeletion() !== false;
const isConstruction = unit.type() === UnitType.Construction;
const constructionType = unit.constructionType();
const structureType = isConstruction ? constructionType! : unit.type();
const isConstruction = unit.isUnderConstruction();
const structureType = unit.type();
const { type, stage } = options;
const { scale } = this.transformHandler;

if (type === "icon" || type === "dot") {
if (isConstruction && constructionType === undefined) {
console.warn(
`Unit ${unit.id()} is a construction but has no construction type.`,
);
return parentContainer;
}
const texture = this.createTexture(
structureType,
unit.owner(),
Expand Down
22 changes: 5 additions & 17 deletions src/client/graphics/layers/StructureIconsLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,7 @@ export class StructureIconsLayer implements Layer {
this.checkForOwnershipChange(render, unitView);
this.checkForLevelChange(render, unitView);
}
} else if (
this.structures.has(unitView.type()) ||
unitView.type() === UnitType.Construction
) {
} else if (this.structures.has(unitView.type())) {
this.addNewStructure(unitView);
}
}
Expand All @@ -437,10 +434,7 @@ export class StructureIconsLayer implements Layer {
}

private modifyVisibility(render: StructureRenderInfo) {
const structureType =
render.unit.type() === UnitType.Construction
? render.unit.constructionType()!
: render.unit.type();
const structureType = render.unit.type();
const structureInfos = this.structures.get(structureType);

let focusStructure = false;
Expand Down Expand Up @@ -481,10 +475,7 @@ export class StructureIconsLayer implements Layer {
render: StructureRenderInfo,
unit: UnitView,
) {
if (
render.underConstruction &&
render.unit.type() !== UnitType.Construction
) {
if (render.underConstruction && !unit.isUnderConstruction()) {
render.underConstruction = false;
render.iconContainer?.destroy();
render.dotContainer?.destroy();
Expand Down Expand Up @@ -532,10 +523,7 @@ export class StructureIconsLayer implements Layer {
: screenPos.y,
);

const type =
render.unit.type() === UnitType.Construction
? render.unit.constructionType()
: render.unit.type();
const type = render.unit.type();
const margin =
type !== undefined && STRUCTURE_SHAPES[type] !== undefined
? ICON_SIZE[STRUCTURE_SHAPES[type]]
Expand Down Expand Up @@ -589,7 +577,7 @@ export class StructureIconsLayer implements Layer {
this.createLevelSprite(unitView),
this.createDotSprite(unitView),
unitView.level(),
unitView.type() === UnitType.Construction,
unitView.isUnderConstruction(),
);
this.renders.push(render);
this.computeNewLocation(render);
Expand Down
8 changes: 4 additions & 4 deletions src/client/graphics/layers/StructureLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class StructureLayer implements Layer {
)) {
this.paintCell(
new Cell(this.game.x(tile), this.game.y(tile)),
unit.type() === UnitType.Construction
unit.isUnderConstruction()
? underConstructionColor
: unit.owner().territoryColor(),
130,
Expand All @@ -199,7 +199,7 @@ export class StructureLayer implements Layer {
}

private handleUnitRendering(unit: UnitView) {
const unitType = unit.constructionType() ?? unit.type();
const unitType = unit.type();
const iconType = unitType;
if (!this.isUnitTypeSupported(unitType)) return;

Expand All @@ -208,7 +208,7 @@ export class StructureLayer implements Layer {
let borderColor = unit.owner().borderColor();

// Handle cooldown states and special icons
if (unit.type() === UnitType.Construction) {
if (unit.isUnderConstruction()) {
icon = this.unitIcons.get(iconType);
borderColor = underConstructionColor;
} else {
Expand Down Expand Up @@ -247,7 +247,7 @@ export class StructureLayer implements Layer {
unit: UnitView,
) {
let color = unit.owner().borderColor();
if (unit.type() === UnitType.Construction) {
if (unit.isUnderConstruction()) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
color = underConstructionColor;
}
Expand Down
48 changes: 24 additions & 24 deletions src/client/graphics/layers/UILayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,15 @@ export class UILayer implements Layer {
}

onUnitEvent(unit: UnitView) {
const underConst =
(
unit as unknown as { isUnderConstruction?: () => boolean }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to do this? why not just unit.isUnderConstruction?

).isUnderConstruction?.() ?? false;
if (underConst) {
this.createLoadingBar(unit);
return;
}
switch (unit.type()) {
case UnitType.Construction: {
const constructionType = unit.constructionType();
if (constructionType === undefined) {
// Skip units without construction type
return;
}
this.createLoadingBar(unit);
break;
}
case UnitType.Warship: {
this.drawHealthBar(unit);
break;
Expand Down Expand Up @@ -318,22 +317,23 @@ export class UILayer implements Layer {
if (!unit.isActive()) {
return 1;
}
switch (unit.type()) {
case UnitType.Construction: {
const constructionType = unit.constructionType();
if (constructionType === undefined) {
return 1;
}
const constDuration =
this.game.unitInfo(constructionType).constructionDuration;
if (constDuration === undefined) {
throw new Error("unit does not have constructionTime");
}
return (
(this.game.ticks() - unit.createdAt()) /
(constDuration === 0 ? 1 : constDuration)
);
const underConst =
(
unit as unknown as { isUnderConstruction?: () => boolean }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

).isUnderConstruction?.() ?? false;
if (underConst) {
const constDuration = this.game.unitInfo(
unit.type(),
).constructionDuration;
if (constDuration === undefined) {
throw new Error("unit does not have constructionTime");
}
return (
(this.game.ticks() - unit.createdAt()) /
(constDuration === 0 ? 1 : constDuration)
);
}
switch (unit.type()) {
case UnitType.MissileSilo:
case UnitType.SAMLauncher:
return !unit.markedForDeletion()
Expand Down
5 changes: 0 additions & 5 deletions src/core/configuration/DefaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,6 @@ export class DefaultConfig implements Config {
experimental: true,
upgradable: true,
};
case UnitType.Construction:
return {
cost: () => 0n,
territoryBound: true,
};
case UnitType.Train:
return {
cost: () => 0n,
Expand Down
42 changes: 28 additions & 14 deletions src/core/execution/CityExecution.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
import { Execution, Game, Player, Unit, UnitType, isUnit } from "../game/Game";
import { TileRef } from "../game/GameMap";
import { TrainStationExecution } from "./TrainStationExecution";

Expand All @@ -7,33 +7,47 @@ export class CityExecution implements Execution {
private city: Unit | null = null;
private active: boolean = true;

constructor(playerOrUnit: Unit);
constructor(playerOrUnit: Player, tile: TileRef);

constructor(
private player: Player,
private tile: TileRef,
) {}
private playerOrUnit: Player | Unit,
private tile?: TileRef,
) {
if (!isUnit(playerOrUnit) && tile === undefined) {
throw new Error("tile is required when playerOrUnit is a Player");
}
}

init(mg: Game, ticks: number): void {
this.mg = mg;
}

tick(ticks: number): void {
if (this.city === null) {
const spawnTile = this.player.canBuild(UnitType.City, this.tile);
if (spawnTile === false) {
console.warn("cannot build city");
this.active = false;
return;
if (!this.city) {
if (isUnit(this.playerOrUnit)) {
this.city = this.playerOrUnit;
this.createStation();
} else {
const spawnTile = this.playerOrUnit.canBuild(UnitType.City, this.tile!);
if (spawnTile === false) {
console.warn("cannot build city");
this.active = false;
return;
}
this.city = this.playerOrUnit.buildUnit(UnitType.City, spawnTile, {});
this.createStation();
}
this.city = this.player.buildUnit(UnitType.City, spawnTile, {});
this.createStation();
}
if (!this.city.isActive()) {
this.active = false;
return;
}

if (this.player !== this.city.owner()) {
this.player = this.city.owner();
if (!isUnit(this.playerOrUnit)) {
if (this.playerOrUnit !== this.city.owner()) {
this.playerOrUnit = this.city.owner();
}
}
}

Expand Down
Loading
Loading