Skip to content

Commit 686bfed

Browse files
minor unit rendering improvements
1 parent e74d1f2 commit 686bfed

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

src/client/graphics/layers/UnitLayer.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
AlternateViewEvent,
1010
ContextMenuEvent,
1111
MouseUpEvent,
12+
ReplaySpeedChangeEvent,
1213
TouchEvent,
1314
UnitSelectionEvent,
1415
} from "../../InputHandler";
@@ -17,6 +18,10 @@ import { TransformHandler } from "../TransformHandler";
1718
import { Layer } from "./Layer";
1819

1920
import { GameUpdateType } from "../../../core/game/GameUpdates";
21+
import {
22+
defaultReplaySpeedMultiplier,
23+
ReplaySpeedMultiplier,
24+
} from "../../utilities/ReplaySpeedMultiplier";
2025
import {
2126
getColoredSprite,
2227
isSpriteReady,
@@ -62,7 +67,10 @@ export class UnitLayer implements Layer {
6267
UnitType.TransportShip,
6368
UnitType.TradeShip,
6469
];
65-
private readonly tickIntervalMs: number = 100;
70+
private baseTickIntervalMs: number = 100;
71+
private tickIntervalMs: number = 100;
72+
private replaySpeedMultiplier: ReplaySpeedMultiplier =
73+
defaultReplaySpeedMultiplier;
6674
private lastTickTimestamp = 0;
6775

6876
constructor(
@@ -72,6 +80,11 @@ export class UnitLayer implements Layer {
7280
) {
7381
this.theme = game.config().theme();
7482
this.transformHandler = transformHandler;
83+
this.baseTickIntervalMs = this.game
84+
.config()
85+
.serverConfig()
86+
.turnIntervalMs();
87+
this.updateTickInterval();
7588
this.lastTickTimestamp = this.now();
7689
}
7790

@@ -81,6 +94,14 @@ export class UnitLayer implements Layer {
8194

8295
tick() {
8396
this.lastTickTimestamp = this.now();
97+
const configuredInterval = this.game
98+
.config()
99+
.serverConfig()
100+
.turnIntervalMs();
101+
if (configuredInterval !== this.baseTickIntervalMs) {
102+
this.baseTickIntervalMs = configuredInterval;
103+
this.updateTickInterval();
104+
}
84105
const unitIds = this.game
85106
.updatesSinceLastTick()
86107
?.[GameUpdateType.Unit]?.map((unit) => unit.id);
@@ -93,6 +114,9 @@ export class UnitLayer implements Layer {
93114
this.eventBus.on(MouseUpEvent, (e) => this.onMouseUp(e));
94115
this.eventBus.on(TouchEvent, (e) => this.onTouch(e));
95116
this.eventBus.on(UnitSelectionEvent, (e) => this.onUnitSelectionChange(e));
117+
this.eventBus.on(ReplaySpeedChangeEvent, (e) =>
118+
this.onReplaySpeedChange(e.replaySpeedMultiplier),
119+
);
96120
this.redraw();
97121

98122
loadAllSprites();
@@ -567,8 +591,8 @@ export class UnitLayer implements Layer {
567591
this.interpolationContext.clearRect(
568592
0,
569593
0,
570-
this.game.width(),
571-
this.game.height(),
594+
this.interpolationCanvas.width,
595+
this.interpolationCanvas.height,
572596
);
573597

574598
const alpha = this.computeTickAlpha();
@@ -586,18 +610,18 @@ export class UnitLayer implements Layer {
586610
case UnitType.MIRVWarhead:
587611
this.renderWarhead(unit, position);
588612
continue;
613+
default:
614+
if (!isSpriteReady(unit)) {
615+
continue;
616+
}
617+
this.drawSpriteAtPosition(
618+
unit,
619+
position,
620+
this.getInterpolatedSpriteColor(unit),
621+
this.interpolationContext,
622+
false,
623+
);
589624
}
590-
if (!isSpriteReady(unit)) {
591-
continue;
592-
}
593-
const customColor = this.getInterpolatedSpriteColor(unit);
594-
this.drawSpriteAtPosition(
595-
unit,
596-
position,
597-
customColor,
598-
this.interpolationContext,
599-
false,
600-
);
601625
}
602626
}
603627

@@ -708,6 +732,21 @@ export class UnitLayer implements Layer {
708732
return Math.max(0, elapsed / this.tickIntervalMs);
709733
}
710734

735+
private onReplaySpeedChange(multiplier: ReplaySpeedMultiplier) {
736+
this.replaySpeedMultiplier = multiplier;
737+
this.updateTickInterval();
738+
this.lastTickTimestamp = this.now();
739+
}
740+
741+
private updateTickInterval() {
742+
const baseInterval = this.baseTickIntervalMs;
743+
if (baseInterval <= 0) {
744+
this.tickIntervalMs = 0;
745+
return;
746+
}
747+
this.tickIntervalMs = baseInterval * this.replaySpeedMultiplier;
748+
}
749+
711750
private now(): number {
712751
if (typeof performance !== "undefined" && performance.now) {
713752
return performance.now();

0 commit comments

Comments
 (0)