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:
@@ -1,7 +1,11 @@
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
|
||||
abstract class EngineAudio {
|
||||
WolfensteinData? activeGame;
|
||||
void playMenuMusic();
|
||||
void playLevelMusic(WolfLevel level);
|
||||
void stopMusic();
|
||||
// You can easily add things like void playSoundEffect(SoundId id); later!
|
||||
void playSoundEffect(int sfxId);
|
||||
Future<void> init();
|
||||
void dispose();
|
||||
}
|
||||
|
||||
22
packages/wolf_3d_engine/lib/src/engine_input.dart
Normal file
22
packages/wolf_3d_engine/lib/src/engine_input.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:wolf_3d_entities/wolf_3d_entities.dart';
|
||||
|
||||
/// A pure, framework-agnostic snapshot of the player's intended actions for a single frame.
|
||||
class EngineInput {
|
||||
final bool isMovingForward;
|
||||
final bool isMovingBackward;
|
||||
final bool isTurningLeft;
|
||||
final bool isTurningRight;
|
||||
final bool isFiring;
|
||||
final bool isInteracting;
|
||||
final WeaponType? requestedWeapon;
|
||||
|
||||
const EngineInput({
|
||||
this.isMovingForward = false,
|
||||
this.isMovingBackward = false,
|
||||
this.isTurningLeft = false,
|
||||
this.isTurningRight = false,
|
||||
this.isFiring = false,
|
||||
this.isInteracting = false,
|
||||
this.requestedWeapon,
|
||||
});
|
||||
}
|
||||
@@ -7,13 +7,17 @@ class DoorManager {
|
||||
// Key is '$x,$y'
|
||||
final Map<String, Door> doors = {};
|
||||
|
||||
// Callback to play sounds without tightly coupling to the audio engine
|
||||
final void Function(int sfxId) onPlaySound;
|
||||
|
||||
DoorManager({required this.onPlaySound});
|
||||
|
||||
void initDoors(Sprite wallGrid) {
|
||||
doors.clear();
|
||||
for (int y = 0; y < wallGrid.length; y++) {
|
||||
for (int x = 0; x < wallGrid[y].length; x++) {
|
||||
int id = wallGrid[y][x];
|
||||
if (id >= 90) {
|
||||
// Assuming 90+ are doors based on your previous code
|
||||
doors['$x,$y'] = Door(x: x, y: y, mapId: id);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +26,12 @@ class DoorManager {
|
||||
|
||||
void update(Duration elapsed) {
|
||||
for (final door in doors.values) {
|
||||
door.update(elapsed.inMilliseconds);
|
||||
final newState = door.update(elapsed.inMilliseconds);
|
||||
|
||||
// The Manager decides: "If a door just started closing, play the close sound."
|
||||
if (newState == DoorState.closing) {
|
||||
onPlaySound(WolfSound.closeDoor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +41,19 @@ class DoorManager {
|
||||
|
||||
String key = '$targetX,$targetY';
|
||||
if (doors.containsKey(key)) {
|
||||
doors[key]!.interact();
|
||||
if (doors[key]!.interact()) {
|
||||
// The Manager decides: "Player successfully opened a door, play the sound."
|
||||
onPlaySound(WolfSound.openDoor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tryOpenDoor(int x, int y) {
|
||||
String key = '$x,$y';
|
||||
if (doors.containsKey(key) && doors[key]!.offset == 0.0) {
|
||||
if (doors[key]!.interact()) {
|
||||
onPlaySound(WolfSound.openDoor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,16 +68,6 @@ class DoorManager {
|
||||
return offsets;
|
||||
}
|
||||
|
||||
void tryOpenDoor(int x, int y) {
|
||||
String key = '$x,$y';
|
||||
if (doors.containsKey(key)) {
|
||||
// If it's closed or closing, interact() will usually start it opening
|
||||
if (doors[key]!.offset == 0.0) {
|
||||
doors[key]!.interact();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool isDoorOpenEnough(int x, int y) {
|
||||
String key = '$x,$y';
|
||||
if (doors.containsKey(key)) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:wolf_3d_data/wolf_3d_data.dart';
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
|
||||
import 'package:wolf_3d_entities/wolf_3d_entities.dart';
|
||||
@@ -12,7 +11,9 @@ class WolfEngine {
|
||||
required this.startingEpisode,
|
||||
required this.onGameWon,
|
||||
required this.audio,
|
||||
});
|
||||
}) : doorManager = DoorManager(
|
||||
onPlaySound: (sfxId) => audio.playSoundEffect(sfxId),
|
||||
);
|
||||
|
||||
final WolfensteinData data;
|
||||
final Difficulty difficulty;
|
||||
@@ -24,7 +25,8 @@ class WolfEngine {
|
||||
final void Function() onGameWon;
|
||||
|
||||
// Managers
|
||||
final DoorManager doorManager = DoorManager();
|
||||
final DoorManager doorManager;
|
||||
|
||||
final PushwallManager pushwallManager = PushwallManager();
|
||||
|
||||
// State
|
||||
@@ -94,6 +96,7 @@ class WolfEngine {
|
||||
final Level objectLevel = activeLevel.objectGrid;
|
||||
|
||||
doorManager.initDoors(currentLevel);
|
||||
|
||||
pushwallManager.initPushwalls(currentLevel, objectLevel);
|
||||
|
||||
audio.playLevelMusic(activeLevel);
|
||||
|
||||
Reference in New Issue
Block a user