Improved weapon switching

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 00:05:32 +01:00
parent 895a997604
commit cdd676233f
8 changed files with 61 additions and 54 deletions

View File

@@ -1,4 +1,5 @@
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:wolf_dart/features/weapon/weapon.dart';
class InputManager { class InputManager {
Set<LogicalKeyboardKey> _previousKeys = {}; Set<LogicalKeyboardKey> _previousKeys = {};
@@ -14,7 +15,7 @@ class InputManager {
// Continuous // Continuous
bool isFiring = false; bool isFiring = false;
int? requestedWeaponIndex; WeaponType? requestedWeapon;
void update() { void update() {
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed; final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
@@ -39,17 +40,19 @@ class InputManager {
!pressedKeys.contains(LogicalKeyboardKey.space); !pressedKeys.contains(LogicalKeyboardKey.space);
// * Manual Weapon Switching // * Manual Weapon Switching
requestedWeaponIndex = null; requestedWeapon = null;
// Iterate through newly pressed keys and switch on them // Iterate through newly pressed keys and switch on them
for (final LogicalKeyboardKey key in newlyPressedKeys) { for (final LogicalKeyboardKey key in newlyPressedKeys) {
switch (key) { switch (key) {
case LogicalKeyboardKey.digit1: case LogicalKeyboardKey.digit1:
requestedWeaponIndex = 0; // Knife requestedWeapon = WeaponType.knife;
case LogicalKeyboardKey.digit2: case LogicalKeyboardKey.digit2:
requestedWeaponIndex = 1; // Pistol requestedWeapon = WeaponType.pistol;
case LogicalKeyboardKey.digit3: case LogicalKeyboardKey.digit3:
requestedWeaponIndex = 2; // Machine Gun requestedWeapon = WeaponType.machineGun;
case LogicalKeyboardKey.digit4:
requestedWeapon = WeaponType.chainGun;
} }
} }

View File

@@ -3,6 +3,7 @@ import 'dart:math' as math;
import 'package:wolf_dart/classes/linear_coordinates.dart'; import 'package:wolf_dart/classes/linear_coordinates.dart';
import 'package:wolf_dart/features/entities/collectible.dart'; import 'package:wolf_dart/features/entities/collectible.dart';
import 'package:wolf_dart/features/weapon/weapon.dart'; import 'package:wolf_dart/features/weapon/weapon.dart';
import 'package:wolf_dart/features/weapon/weapons/chain_gun.dart';
import 'package:wolf_dart/features/weapon/weapons/knife.dart'; import 'package:wolf_dart/features/weapon/weapons/knife.dart';
import 'package:wolf_dart/features/weapon/weapons/machine_gun.dart'; import 'package:wolf_dart/features/weapon/weapons/machine_gun.dart';
import 'package:wolf_dart/features/weapon/weapons/pistol.dart'; import 'package:wolf_dart/features/weapon/weapons/pistol.dart';
@@ -28,13 +29,15 @@ class Player {
// Weapon System // Weapon System
late Weapon currentWeapon; late Weapon currentWeapon;
int currentWeaponIndex = 1; // Starts with Pistol (Index 1) final Map<WeaponType, Weapon?> weapons = {
WeaponType.knife: Knife(),
// Fixed indices: 0 = Knife, 1 = Pistol, 2 = Machine Gun, 3 = Chain Gun WeaponType.pistol: Pistol(),
final List<Weapon?> availableWeapons = [null, null, null, null]; WeaponType.machineGun: null,
WeaponType.chainGun: null,
};
WeaponSwitchState switchState = WeaponSwitchState.idle; WeaponSwitchState switchState = WeaponSwitchState.idle;
int? pendingWeaponIndex; WeaponType? pendingWeaponType;
// 0.0 is resting, 500.0 is fully off-screen // 0.0 is resting, 500.0 is fully off-screen
double weaponAnimOffset = 0.0; double weaponAnimOffset = 0.0;
@@ -47,10 +50,7 @@ class Player {
required this.y, required this.y,
required this.angle, required this.angle,
}) { }) {
// Start with Knife and Pistol currentWeapon = weapons[WeaponType.pistol]!;
availableWeapons[0] = Knife();
availableWeapons[1] = Pistol();
currentWeapon = availableWeapons[1]!;
} }
// Helper getter to interface with the RaycasterPainter // Helper getter to interface with the RaycasterPainter
@@ -63,8 +63,10 @@ class Player {
weaponAnimOffset += switchSpeed; weaponAnimOffset += switchSpeed;
if (weaponAnimOffset >= 500.0) { if (weaponAnimOffset >= 500.0) {
weaponAnimOffset = 500.0; weaponAnimOffset = 500.0;
currentWeaponIndex = pendingWeaponIndex!;
currentWeapon = availableWeapons[currentWeaponIndex]!; // Grab the pending weapon from inventory, default to pistol if something goes wrong
currentWeapon = weapons[pendingWeaponType ?? WeaponType.pistol]!;
switchState = WeaponSwitchState.raising; switchState = WeaponSwitchState.raising;
} }
} else if (switchState == WeaponSwitchState.raising) { } else if (switchState == WeaponSwitchState.raising) {
@@ -76,18 +78,14 @@ class Player {
} }
} }
void requestWeaponSwitch(int index) { void requestWeaponSwitch(WeaponType weaponType) {
// Prevent switching if animating, firing, picking the same gun, or if slot is empty
if (switchState != WeaponSwitchState.idle) return; if (switchState != WeaponSwitchState.idle) return;
if (currentWeapon.state != WeaponState.idle) return; if (currentWeapon.state != WeaponState.idle) return;
if (index == currentWeaponIndex) return; if (weaponType == currentWeapon.type) return;
if (index < 0 || index >= availableWeapons.length) return; if (!weapons.containsKey(weaponType)) return;
if (availableWeapons[index] == null) return; if (weaponType == WeaponType.knife && ammo <= 0) return;
// Don't switch to a firearm if out of ammo pendingWeaponType = weaponType;
if (index > 0 && ammo <= 0) return;
pendingWeaponIndex = index;
switchState = WeaponSwitchState.lowering; switchState = WeaponSwitchState.lowering;
} }
@@ -138,8 +136,8 @@ class Player {
addAmmo(8); addAmmo(8);
// Auto-switch back to Pistol if holding Knife and just got ammo // Auto-switch back to Pistol if holding Knife and just got ammo
if (currentWeaponIndex == 0 && previousAmmo <= 0) { if (currentWeapon is Knife && previousAmmo <= 0) {
requestWeaponSwitch(1); requestWeaponSwitch(WeaponType.pistol);
} }
pickedUp = true; pickedUp = true;
break; break;
@@ -150,7 +148,6 @@ class Player {
if (item.mapId == 54) score += 1000; if (item.mapId == 54) score += 1000;
if (item.mapId == 55) score += 5000; if (item.mapId == 55) score += 5000;
if (item.mapId == 56) { if (item.mapId == 56) {
// 1-Up
heal(100); heal(100);
addAmmo(25); addAmmo(25);
} }
@@ -159,20 +156,17 @@ class Player {
case CollectibleType.weapon: case CollectibleType.weapon:
if (item.mapId == 50) { if (item.mapId == 50) {
if (!hasMachineGun) { if (!weapons.containsKey(WeaponType.machineGun)) {
hasMachineGun = true; weapons[WeaponType.machineGun] = MachineGun();
availableWeapons[2] = MachineGun();
} }
requestWeaponSwitch(2); requestWeaponSwitch(WeaponType.machineGun);
pickedUp = true; pickedUp = true;
} }
// Assuming mapId 51 is Chain Gun for later
if (item.mapId == 51) { if (item.mapId == 51) {
if (!hasChainGun) { if (!weapons.containsKey(WeaponType.chainGun)) {
hasChainGun = true; weapons[WeaponType.chainGun] = ChainGun();
// availableWeapons[3] = ChainGun(); // Uncomment when you add the class
} }
requestWeaponSwitch(3); requestWeaponSwitch(WeaponType.chainGun);
pickedUp = true; pickedUp = true;
} }
break; break;
@@ -187,17 +181,14 @@ class Player {
} }
void fire(int currentTime) { void fire(int currentTime) {
if (switchState != WeaponSwitchState.idle) { if (switchState != WeaponSwitchState.idle) return;
return; // No shooting while switching
}
bool shotFired = currentWeapon.fire(currentTime, currentAmmo: ammo); bool shotFired = currentWeapon.fire(currentTime, currentAmmo: ammo);
if (shotFired && currentWeaponIndex > 0) { // Check currentWeapon.type
// If it's a gun if (shotFired && currentWeapon.type != WeaponType.knife) {
ammo--; ammo--;
if (ammo <= 0) { if (ammo <= 0) {
// Auto-switch to knife when out of bullets requestWeaponSwitch(WeaponType.knife);
requestWeaponSwitch(0);
} }
} }
} }

View File

@@ -46,7 +46,6 @@ class _WolfRendererState extends State<WolfRenderer>
late Player player; late Player player;
bool _isLoading = true; bool _isLoading = true;
final bool _spaceWasPressed = false;
double damageFlashOpacity = 0.0; double damageFlashOpacity = 0.0;
@@ -204,8 +203,8 @@ class _WolfRendererState extends State<WolfRenderer>
moveStepY = 0; moveStepY = 0;
// Handle Manual Weapon Switching // Handle Manual Weapon Switching
if (inputManager.requestedWeaponIndex != null) { if (inputManager.requestedWeapon != null) {
player.requestWeaponSwitch(inputManager.requestedWeaponIndex!); player.requestWeaponSwitch(inputManager.requestedWeapon!);
} }
// Handle Movement using InputManager // Handle Movement using InputManager

View File

@@ -1,7 +1,9 @@
enum WeaponState { idle, firing } enum WeaponState { idle, firing }
enum WeaponType { knife, pistol, machineGun, chainGun }
abstract class Weapon { abstract class Weapon {
final String name; final WeaponType type;
final int idleSprite; final int idleSprite;
final List<int> fireFrames; final List<int> fireFrames;
final int damage; final int damage;
@@ -12,7 +14,7 @@ abstract class Weapon {
int lastFrameTime = 0; int lastFrameTime = 0;
Weapon({ Weapon({
required this.name, required this.type,
required this.idleSprite, required this.idleSprite,
required this.fireFrames, required this.fireFrames,
required this.damage, required this.damage,

View File

@@ -0,0 +1,12 @@
import 'package:wolf_dart/features/weapon/weapon.dart';
class ChainGun extends Weapon {
ChainGun()
: super(
type: WeaponType.chainGun,
idleSprite: 432,
fireFrames: [433, 434],
damage: 40,
msPerFrame: 30,
);
}

View File

@@ -3,7 +3,7 @@ import 'package:wolf_dart/features/weapon/weapon.dart';
class Knife extends Weapon { class Knife extends Weapon {
Knife() Knife()
: super( : super(
name: "Knife", type: WeaponType.knife,
idleSprite: 416, idleSprite: 416,
fireFrames: [417, 418, 419, 420], fireFrames: [417, 418, 419, 420],
damage: 15, damage: 15,

View File

@@ -3,9 +3,9 @@ import 'package:wolf_dart/features/weapon/weapon.dart';
class MachineGun extends Weapon { class MachineGun extends Weapon {
MachineGun() MachineGun()
: super( : super(
name: "Machine Gun", type: WeaponType.machineGun,
idleSprite: 413, idleSprite: 427,
fireFrames: [414, 415], fireFrames: [428, 429, 430],
damage: 20, damage: 20,
msPerFrame: 80, msPerFrame: 80,
); );

View File

@@ -3,7 +3,7 @@ import 'package:wolf_dart/features/weapon/weapon.dart';
class Pistol extends Weapon { class Pistol extends Weapon {
Pistol() Pistol()
: super( : super(
name: "Pistol", type: WeaponType.pistol,
idleSprite: 421, idleSprite: 421,
fireFrames: [422, 423, 424, 425], fireFrames: [422, 423, 424, 425],
damage: 20, damage: 20,