Play (wrong) door sound effect when the door opens or closes

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-15 21:13:41 +01:00
parent b3b909a9b6
commit b0852543b0
18 changed files with 268 additions and 485 deletions

View File

@@ -3,54 +3,47 @@ enum DoorState { closed, opening, open, closing }
class Door {
final int x;
final int y;
final int mapId; // To differentiate between regular doors and elevator doors
final int mapId;
DoorState state = DoorState.closed;
double offset = 0.0;
int openTime = 0; // When did the door fully open?
// How long a door stays open before auto-closing
int openTime = 0;
static const int openDurationMs = 3000;
Door({
required this.x,
required this.y,
required this.mapId,
});
// Returns true if the door state changed this frame (useful for playing sounds later)
bool update(int currentTimeMs) {
bool stateChanged = false;
Door({required this.x, required this.y, required this.mapId});
/// Updates animation. Returns the NEW state if it changed this frame, else null.
DoorState? update(int currentTimeMs) {
if (state == DoorState.opening) {
offset += 0.02; // Slide speed
offset += 0.02;
if (offset >= 1.0) {
offset = 1.0;
state = DoorState.open;
openTime = currentTimeMs;
stateChanged = true;
return DoorState.open;
}
} else if (state == DoorState.open) {
if (currentTimeMs - openTime > openDurationMs) {
state = DoorState.closing;
stateChanged = true;
return DoorState.closing;
}
} else if (state == DoorState.closing) {
// Note: We don't check for entities blocking the door yet!
offset -= 0.02;
if (offset <= 0.0) {
offset = 0.0;
state = DoorState.closed;
stateChanged = true;
return DoorState.closed;
}
}
return stateChanged;
return null;
}
void interact() {
/// Triggers the opening process. Returns true if it successfully started opening.
bool interact() {
if (state == DoorState.closed || state == DoorState.closing) {
state = DoorState.opening;
return true;
}
return false;
}
}