Unified game screen and abstracted input
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -53,6 +53,7 @@ void main() async {
|
|||||||
difficulty: Difficulty.bringEmOn,
|
difficulty: Difficulty.bringEmOn,
|
||||||
startingEpisode: 0,
|
startingEpisode: 0,
|
||||||
audio: cliAudio,
|
audio: cliAudio,
|
||||||
|
input: input,
|
||||||
onGameWon: () {
|
onGameWon: () {
|
||||||
exitCleanly(0);
|
exitCleanly(0);
|
||||||
print("YOU WON!");
|
print("YOU WON!");
|
||||||
@@ -108,7 +109,7 @@ void main() async {
|
|||||||
|
|
||||||
input.update();
|
input.update();
|
||||||
|
|
||||||
engine.tick(elapsed, input.currentInput);
|
engine.tick(elapsed);
|
||||||
rasterizer.render(engine, buffer);
|
rasterizer.render(engine, buffer);
|
||||||
rasterizer.finalizeFrame();
|
rasterizer.finalizeFrame();
|
||||||
|
|
||||||
|
|||||||
@@ -32,12 +32,6 @@ class _DifficultyScreenState extends State<DifficultyScreen> {
|
|||||||
startingEpisode: Wolf3d.I.activeEpisode,
|
startingEpisode: Wolf3d.I.activeEpisode,
|
||||||
audio: Wolf3d.I.audio,
|
audio: Wolf3d.I.audio,
|
||||||
),
|
),
|
||||||
// builder: (_) => WolfFlutterRenderer(
|
|
||||||
// Wolf3d.I.activeGame,
|
|
||||||
// difficulty: difficulty,
|
|
||||||
// startingEpisode: Wolf3d.I.activeEpisode,
|
|
||||||
// audio: Wolf3d.I.audio,
|
|
||||||
// ),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,19 +27,22 @@ class GameScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class _GameScreenState extends State<GameScreen> {
|
class _GameScreenState extends State<GameScreen> {
|
||||||
late final WolfEngine _engine;
|
late final WolfEngine _engine;
|
||||||
final Wolf3dInput _inputManager = Wolf3dFlutterInput();
|
late final Wolf3dInput _input;
|
||||||
bool _useAsciiMode = false;
|
bool _useAsciiMode = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
|
_input = Wolf3dFlutterInput();
|
||||||
|
|
||||||
// The Host Screen owns the engine lifecycle
|
// The Host Screen owns the engine lifecycle
|
||||||
_engine = WolfEngine(
|
_engine = WolfEngine(
|
||||||
data: widget.data,
|
data: widget.data,
|
||||||
difficulty: widget.difficulty,
|
difficulty: widget.difficulty,
|
||||||
startingEpisode: widget.startingEpisode,
|
startingEpisode: widget.startingEpisode,
|
||||||
audio: widget.audio,
|
audio: widget.audio,
|
||||||
|
input: _input,
|
||||||
onGameWon: () => Navigator.of(context).pop(),
|
onGameWon: () => Navigator.of(context).pop(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -53,14 +56,8 @@ class _GameScreenState extends State<GameScreen> {
|
|||||||
children: [
|
children: [
|
||||||
// Render the active view
|
// Render the active view
|
||||||
_useAsciiMode
|
_useAsciiMode
|
||||||
? WolfAsciiRenderer(
|
? WolfAsciiRenderer(engine: _engine)
|
||||||
engine: _engine,
|
: WolfFlutterRenderer(engine: _engine),
|
||||||
inputManager: _inputManager,
|
|
||||||
)
|
|
||||||
: WolfFlutterRenderer(
|
|
||||||
engine: _engine,
|
|
||||||
inputManager: _inputManager,
|
|
||||||
),
|
|
||||||
|
|
||||||
// Invisible listener to trap the 'Tab' key and swap renderers
|
// Invisible listener to trap the 'Tab' key and swap renderers
|
||||||
Focus(
|
Focus(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'dart:math' as math;
|
|||||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.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_engine/wolf_3d_engine.dart';
|
||||||
import 'package:wolf_3d_entities/wolf_3d_entities.dart';
|
import 'package:wolf_3d_entities/wolf_3d_entities.dart';
|
||||||
|
import 'package:wolf_3d_input/wolf_3d_input.dart';
|
||||||
|
|
||||||
class WolfEngine {
|
class WolfEngine {
|
||||||
WolfEngine({
|
WolfEngine({
|
||||||
@@ -11,6 +12,7 @@ class WolfEngine {
|
|||||||
required this.startingEpisode,
|
required this.startingEpisode,
|
||||||
required this.onGameWon,
|
required this.onGameWon,
|
||||||
required this.audio,
|
required this.audio,
|
||||||
|
required this.input,
|
||||||
}) : doorManager = DoorManager(
|
}) : doorManager = DoorManager(
|
||||||
onPlaySound: (sfxId) => audio.playSoundEffect(sfxId),
|
onPlaySound: (sfxId) => audio.playSoundEffect(sfxId),
|
||||||
);
|
);
|
||||||
@@ -28,6 +30,7 @@ class WolfEngine {
|
|||||||
|
|
||||||
// Managers
|
// Managers
|
||||||
final DoorManager doorManager;
|
final DoorManager doorManager;
|
||||||
|
final Wolf3dInput input;
|
||||||
|
|
||||||
final PushwallManager pushwallManager = PushwallManager();
|
final PushwallManager pushwallManager = PushwallManager();
|
||||||
|
|
||||||
@@ -51,12 +54,15 @@ class WolfEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Expect standard Dart Duration. The host app is responsible for the loop.
|
// Expect standard Dart Duration. The host app is responsible for the loop.
|
||||||
void tick(Duration elapsed, EngineInput input) {
|
void tick(Duration elapsed) {
|
||||||
if (!isInitialized) return;
|
if (!isInitialized) return;
|
||||||
|
|
||||||
_timeAliveMs += elapsed.inMilliseconds;
|
_timeAliveMs += elapsed.inMilliseconds;
|
||||||
|
|
||||||
final inputResult = _processInputs(elapsed, input);
|
input.update();
|
||||||
|
final currentInput = input.currentInput;
|
||||||
|
|
||||||
|
final inputResult = _processInputs(elapsed, currentInput);
|
||||||
|
|
||||||
doorManager.update(elapsed);
|
doorManager.update(elapsed);
|
||||||
pushwallManager.update(elapsed, currentLevel);
|
pushwallManager.update(elapsed, currentLevel);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ dependencies:
|
|||||||
wolf_3d_data_types: any
|
wolf_3d_data_types: any
|
||||||
wolf_3d_entities: any
|
wolf_3d_entities: any
|
||||||
wolf_3d_data: any
|
wolf_3d_data: any
|
||||||
|
wolf_3d_input: any
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
lints: ^6.0.0
|
lints: ^6.0.0
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/scheduler.dart';
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
|
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
|
||||||
import 'package:wolf_3d_input/wolf_3d_input.dart';
|
|
||||||
|
|
||||||
// 1. The widget now only requires the engine!
|
// 1. The widget now only requires the engine!
|
||||||
abstract class BaseWolfRenderer extends StatefulWidget {
|
abstract class BaseWolfRenderer extends StatefulWidget {
|
||||||
final WolfEngine engine;
|
final WolfEngine engine;
|
||||||
final Wolf3dInput inputManager;
|
|
||||||
|
|
||||||
const BaseWolfRenderer({
|
const BaseWolfRenderer({
|
||||||
required this.engine,
|
required this.engine,
|
||||||
required this.inputManager,
|
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -37,9 +34,8 @@ abstract class BaseWolfRendererState<T extends BaseWolfRenderer>
|
|||||||
Duration delta = elapsed - _lastTick;
|
Duration delta = elapsed - _lastTick;
|
||||||
_lastTick = elapsed;
|
_lastTick = elapsed;
|
||||||
|
|
||||||
widget.inputManager.update();
|
|
||||||
// Tick the shared engine
|
// Tick the shared engine
|
||||||
widget.engine.tick(delta, widget.inputManager.currentInput);
|
widget.engine.tick(delta);
|
||||||
|
|
||||||
performRender();
|
performRender();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,11 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/scheduler.dart';
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.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_engine/wolf_3d_engine.dart';
|
||||||
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
|
||||||
import 'package:wolf_3d_input/wolf_3d_input.dart';
|
|
||||||
import 'package:wolf_3d_renderer/base_renderer.dart';
|
import 'package:wolf_3d_renderer/base_renderer.dart';
|
||||||
|
|
||||||
class WolfAsciiRenderer extends BaseWolfRenderer {
|
class WolfAsciiRenderer extends BaseWolfRenderer {
|
||||||
const WolfAsciiRenderer({
|
const WolfAsciiRenderer({
|
||||||
required super.engine,
|
required super.engine,
|
||||||
required super.inputManager,
|
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -19,7 +16,6 @@ class WolfAsciiRenderer extends BaseWolfRenderer {
|
|||||||
|
|
||||||
class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
|
class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
|
||||||
with SingleTickerProviderStateMixin {
|
with SingleTickerProviderStateMixin {
|
||||||
final Wolf3dInput inputManager = Wolf3dFlutterInput();
|
|
||||||
late final WolfEngine engine;
|
late final WolfEngine engine;
|
||||||
late Ticker _gameLoop;
|
late Ticker _gameLoop;
|
||||||
final FocusNode _focusNode = FocusNode();
|
final FocusNode _focusNode = FocusNode();
|
||||||
@@ -39,6 +35,7 @@ class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
|
|||||||
difficulty: widget.engine.difficulty,
|
difficulty: widget.engine.difficulty,
|
||||||
startingEpisode: widget.engine.startingEpisode,
|
startingEpisode: widget.engine.startingEpisode,
|
||||||
audio: widget.engine.audio,
|
audio: widget.engine.audio,
|
||||||
|
input: widget.engine.input,
|
||||||
onGameWon: () {
|
onGameWon: () {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
@@ -56,8 +53,7 @@ class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
|
|||||||
Duration delta = elapsed - _lastTick;
|
Duration delta = elapsed - _lastTick;
|
||||||
_lastTick = elapsed;
|
_lastTick = elapsed;
|
||||||
|
|
||||||
inputManager.update();
|
engine.tick(delta);
|
||||||
engine.tick(delta, inputManager.currentInput);
|
|
||||||
|
|
||||||
// Calculate frame synchronously and trigger UI rebuild
|
// Calculate frame synchronously and trigger UI rebuild
|
||||||
setState(() {
|
setState(() {
|
||||||
|
|||||||
@@ -4,14 +4,11 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/scheduler.dart';
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.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_engine/wolf_3d_engine.dart';
|
||||||
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
|
|
||||||
import 'package:wolf_3d_input/wolf_3d_input.dart';
|
|
||||||
import 'package:wolf_3d_renderer/base_renderer.dart';
|
import 'package:wolf_3d_renderer/base_renderer.dart';
|
||||||
|
|
||||||
class WolfFlutterRenderer extends BaseWolfRenderer {
|
class WolfFlutterRenderer extends BaseWolfRenderer {
|
||||||
const WolfFlutterRenderer({
|
const WolfFlutterRenderer({
|
||||||
required super.engine,
|
required super.engine,
|
||||||
required super.inputManager,
|
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -21,7 +18,6 @@ class WolfFlutterRenderer extends BaseWolfRenderer {
|
|||||||
|
|
||||||
class _WolfFlutterRendererState extends State<WolfFlutterRenderer>
|
class _WolfFlutterRendererState extends State<WolfFlutterRenderer>
|
||||||
with SingleTickerProviderStateMixin {
|
with SingleTickerProviderStateMixin {
|
||||||
final Wolf3dInput inputManager = Wolf3dFlutterInput();
|
|
||||||
late final WolfEngine engine;
|
late final WolfEngine engine;
|
||||||
late Ticker _gameLoop;
|
late Ticker _gameLoop;
|
||||||
final FocusNode _focusNode = FocusNode();
|
final FocusNode _focusNode = FocusNode();
|
||||||
@@ -45,6 +41,7 @@ class _WolfFlutterRendererState extends State<WolfFlutterRenderer>
|
|||||||
difficulty: widget.engine.difficulty,
|
difficulty: widget.engine.difficulty,
|
||||||
startingEpisode: widget.engine.startingEpisode,
|
startingEpisode: widget.engine.startingEpisode,
|
||||||
audio: widget.engine.audio,
|
audio: widget.engine.audio,
|
||||||
|
input: widget.engine.input,
|
||||||
onGameWon: () {
|
onGameWon: () {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
@@ -62,8 +59,7 @@ class _WolfFlutterRendererState extends State<WolfFlutterRenderer>
|
|||||||
Duration delta = elapsed - _lastTick;
|
Duration delta = elapsed - _lastTick;
|
||||||
_lastTick = elapsed;
|
_lastTick = elapsed;
|
||||||
|
|
||||||
inputManager.update();
|
engine.tick(delta);
|
||||||
engine.tick(delta, inputManager.currentInput);
|
|
||||||
|
|
||||||
// Only start rendering a new frame if the previous one is finished.
|
// Only start rendering a new frame if the previous one is finished.
|
||||||
// This prevents memory leaks and stuttering on lower-end hardware!
|
// This prevents memory leaks and stuttering on lower-end hardware!
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ dependencies:
|
|||||||
wolf_3d_data_types: any
|
wolf_3d_data_types: any
|
||||||
wolf_3d_engine: any
|
wolf_3d_engine: any
|
||||||
wolf_3d_entities: any
|
wolf_3d_entities: any
|
||||||
wolf_3d_input: any
|
|
||||||
wolf_3d_flutter: any
|
wolf_3d_flutter: any
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
Reference in New Issue
Block a user