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

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