From 171db0a705e3633527775d399c826292c61a8d79 Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Sat, 14 Mar 2026 00:11:30 +0100 Subject: [PATCH] Clean up _tick Signed-off-by: Hans Kokx --- lib/features/renderer/renderer.dart | 160 +++++++++++++--------------- 1 file changed, 74 insertions(+), 86 deletions(-) diff --git a/lib/features/renderer/renderer.dart b/lib/features/renderer/renderer.dart index 09d5958..6a4661d 100644 --- a/lib/features/renderer/renderer.dart +++ b/lib/features/renderer/renderer.dart @@ -192,49 +192,16 @@ class _WolfRendererState extends State } void _tick(Duration elapsed) { - // * Process all inputs, first. - inputManager.update(); + // 1. Process all hardware inputs and player intentions + _processInputs(elapsed); - const double moveSpeed = 0.12; - const double turnSpeed = 0.08; - - // Reset steps each tick before calculation - moveStepX = 0; - moveStepY = 0; - - // Handle Manual Weapon Switching - if (inputManager.requestedWeapon != null) { - player.requestWeaponSwitch(inputManager.requestedWeapon!); - } - - // Handle Movement using InputManager - if (inputManager.isMovingForward) { - moveStepX += math.cos(player.angle) * moveSpeed; - moveStepY += math.sin(player.angle) * moveSpeed; - } - if (inputManager.isMovingBackward) { - moveStepX -= math.cos(player.angle) * moveSpeed; - moveStepY -= math.sin(player.angle) * moveSpeed; - } - if (inputManager.isTurningLeft) { - player.angle -= turnSpeed; - } - if (inputManager.isTurningRight) { - player.angle += turnSpeed; - } - - // Keep angle wrapped cleanly - if (player.angle < 0) player.angle += 2 * math.pi; - if (player.angle > 2 * math.pi) player.angle -= 2 * math.pi; - - // UPDATE WEAPON ANIMATION (Lowering/Raising) + // 2. Animate weapon lowering/raising player.updateWeaponSwitch(); - // 1. ANIMATE DOORS + // 3. Animate doors sliding doorStates.forEach((key, state) { if (state == 1) { - // If opening - doorOffsets[key] = (doorOffsets[key] ?? 0.0) + 0.02; // Slide speed + doorOffsets[key] = (doorOffsets[key] ?? 0.0) + 0.02; if (doorOffsets[key]! >= 1.0) { doorOffsets[key] = 1.0; doorStates[key] = 2; // Mark as fully open @@ -242,13 +209,13 @@ class _WolfRendererState extends State } }); - // 2. UPDATED WALL COLLISION + // 4. Process Wall Collision for Movement const double margin = 0.3; + double newX = player.x + moveStepX; int checkX = (moveStepX > 0) ? (newX + margin).toInt() : (newX - margin).toInt(); - if (_isWalkable(checkX, player.y.toInt())) { player.x = newX; } @@ -257,32 +224,12 @@ class _WolfRendererState extends State int checkY = (moveStepY > 0) ? (newY + margin).toInt() : (newY - margin).toInt(); - if (_isWalkable(player.x.toInt(), checkY)) { player.y = newY; } - // 3. UPDATED DOOR INTERACTION (Using InputManager) - if (inputManager.isInteracting) { - int targetX = (player.x + math.cos(player.angle)).toInt(); - int targetY = (player.y + math.sin(player.angle)).toInt(); - - if (targetY > 0 && - targetY < currentLevel.length && - targetX > 0 && - targetX < currentLevel[0].length) { - if (currentLevel[targetY][targetX] >= 90) { - String key = '$targetX,$targetY'; - // Start the animation if it isn't already opening! - if (!doorStates.containsKey(key) || doorStates[key] == 0) { - doorStates[key] = 1; - } - } - } - } - - // --- 4. UPDATE ENTITY LOGIC --- - List itemsToRemove = []; // Collect items to delete after the loop + // 5. Update Entity Logic + List itemsToRemove = []; for (Entity entity in entities) { if (entity is Enemy) { @@ -292,42 +239,25 @@ class _WolfRendererState extends State isWalkable: _isWalkable, onDamagePlayer: _takeDamage, ); - } - // Collectible Interaction Logic - else if (entity is Collectible) { + } else if (entity is Collectible) { double dx = player.x - entity.x; double dy = player.y - entity.y; - double dist = math.sqrt(dx * dx + dy * dy); - - // If player is close enough to the item - if (dist < 0.5) { - if (player.tryPickup(entity)) { - itemsToRemove.add(entity); - } + if (math.sqrt(dx * dx + dy * dy) < 0.5) { + if (player.tryPickup(entity)) itemsToRemove.add(entity); } } } - // Remove the items that were successfully picked up if (itemsToRemove.isNotEmpty) { - setState(() { - entities.removeWhere((e) => itemsToRemove.contains(e)); - }); + entities.removeWhere((e) => itemsToRemove.contains(e)); } - // 5. Weapon Raycast & Firing - bool shouldCheckHit = player.updateWeapon(elapsed.inMilliseconds); - - if (shouldCheckHit) { + // 6. Weapon Raycast & Attacking + if (player.updateWeapon(elapsed.inMilliseconds)) { _performRaycastAttack(elapsed); } - // Input to trigger firing (Using InputManager) - if (inputManager.isFiring) { - player.fire(elapsed.inMilliseconds); - } - - // Fade out the damage flash smoothly + // 7. Screen Effects if (damageFlashOpacity > 0) { damageFlashOpacity = math.max(0.0, damageFlashOpacity - 0.05); } @@ -403,6 +333,64 @@ class _WolfRendererState extends State return true; } + void _processInputs(Duration elapsed) { + inputManager.update(); + + const double moveSpeed = 0.16; + const double turnSpeed = 0.12; + + // Reset steps each tick + moveStepX = 0; + moveStepY = 0; + + // 1. Weapon Switching + if (inputManager.requestedWeapon != null) { + player.requestWeaponSwitch(inputManager.requestedWeapon!); + } + + // 2. Firing + if (inputManager.isFiring) { + player.fire(elapsed.inMilliseconds); + } + + // 3. Movement + if (inputManager.isMovingForward) { + moveStepX += math.cos(player.angle) * moveSpeed; + moveStepY += math.sin(player.angle) * moveSpeed; + } + if (inputManager.isMovingBackward) { + moveStepX -= math.cos(player.angle) * moveSpeed; + moveStepY -= math.sin(player.angle) * moveSpeed; + } + + // 4. Turning + if (inputManager.isTurningLeft) player.angle -= turnSpeed; + if (inputManager.isTurningRight) player.angle += turnSpeed; + + // Keep angle wrapped cleanly + if (player.angle < 0) player.angle += 2 * math.pi; + if (player.angle > 2 * math.pi) player.angle -= 2 * math.pi; + + // 5. Interaction (Doors) + if (inputManager.isInteracting) { + int targetX = (player.x + math.cos(player.angle)).toInt(); + int targetY = (player.y + math.sin(player.angle)).toInt(); + + if (targetY > 0 && + targetY < currentLevel.length && + targetX > 0 && + targetX < currentLevel[0].length) { + if (currentLevel[targetY][targetX] >= 90) { + String key = '$targetX,$targetY'; + // Start the animation if it isn't already opening! + if (!doorStates.containsKey(key) || doorStates[key] == 0) { + doorStates[key] = 1; + } + } + } + } + } + @override Widget build(BuildContext context) { if (_isLoading) {