Improve rendering

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 23:27:40 +01:00
parent a6a5cb705d
commit 895a997604
5 changed files with 227 additions and 93 deletions

View File

@@ -7,6 +7,8 @@ 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';
enum WeaponSwitchState { idle, lowering, raising }
class Player {
// Spatial
double x;
@@ -24,9 +26,21 @@ class Player {
bool hasMachineGun = false;
bool hasChainGun = false;
// Weapon
// Weapon System
late Weapon currentWeapon;
final List<Weapon> availableWeapons = [];
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];
WeaponSwitchState switchState = WeaponSwitchState.idle;
int? pendingWeaponIndex;
// 0.0 is resting, 500.0 is fully off-screen
double weaponAnimOffset = 0.0;
// How fast the weapon drops/raises per tick
final double switchSpeed = 30.0;
Player({
required this.x,
@@ -34,18 +48,53 @@ class Player {
required this.angle,
}) {
// Start with Knife and Pistol
availableWeapons.add(Knife());
availableWeapons.add(Pistol());
currentWeapon = availableWeapons[1];
availableWeapons[0] = Knife();
availableWeapons[1] = Pistol();
currentWeapon = availableWeapons[1]!;
}
// Helper getter to interface with the RaycasterPainter
LinearCoordinates get position => (x: x, y: y);
// Helper methods to keep state manipulation safe
// --- Weapon Switching & Animation Logic ---
void updateWeaponSwitch() {
if (switchState == WeaponSwitchState.lowering) {
weaponAnimOffset += switchSpeed;
if (weaponAnimOffset >= 500.0) {
weaponAnimOffset = 500.0;
currentWeaponIndex = pendingWeaponIndex!;
currentWeapon = availableWeapons[currentWeaponIndex]!;
switchState = WeaponSwitchState.raising;
}
} else if (switchState == WeaponSwitchState.raising) {
weaponAnimOffset -= switchSpeed;
if (weaponAnimOffset <= 0) {
weaponAnimOffset = 0.0;
switchState = WeaponSwitchState.idle;
}
}
}
void requestWeaponSwitch(int index) {
// Prevent switching if animating, firing, picking the same gun, or if slot is empty
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;
// Don't switch to a firearm if out of ammo
if (index > 0 && ammo <= 0) return;
pendingWeaponIndex = index;
switchState = WeaponSwitchState.lowering;
}
// --- Health & Damage ---
void takeDamage(int damage) {
health = math.max(0, health - damage);
if (health <= 0) {
print("YOU DIED!");
} else {
@@ -55,24 +104,22 @@ class Player {
void heal(int amount) {
final int newHealth = math.min(100, health + amount);
if (health < 100) {
print("Feelin' better. ($newHealth)");
}
health = newHealth;
}
void addAmmo(int amount) {
final int newAmmo = math.min(99, ammo + amount);
if (ammo < 99) {
print("Hell yeah. ($newAmmo)");
}
ammo = newAmmo;
}
// --- Interaction & Firing ---
bool tryPickup(Collectible item) {
bool pickedUp = false;
@@ -86,12 +133,18 @@ class Player {
case CollectibleType.ammo:
if (ammo >= 99) return false;
int previousAmmo = ammo;
addAmmo(8);
// Auto-switch back to Pistol if holding Knife and just got ammo
if (currentWeaponIndex == 0 && previousAmmo <= 0) {
requestWeaponSwitch(1);
}
pickedUp = true;
break;
case CollectibleType.treasure:
// Score values for Cross (52), Chalice (53), Chest (54), Crown (55)
if (item.mapId == 52) score += 100;
if (item.mapId == 53) score += 500;
if (item.mapId == 54) score += 1000;
@@ -105,9 +158,23 @@ class Player {
break;
case CollectibleType.weapon:
if (item.mapId == 50) hasMachineGun = true;
if (item.mapId == 51) hasChainGun = true;
pickedUp = true;
if (item.mapId == 50) {
if (!hasMachineGun) {
hasMachineGun = true;
availableWeapons[2] = MachineGun();
}
requestWeaponSwitch(2);
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
}
requestWeaponSwitch(3);
pickedUp = true;
}
break;
case CollectibleType.key:
@@ -120,10 +187,18 @@ class Player {
}
void fire(int currentTime) {
// Only spend ammo if the weapon isn't a knife
if (switchState != WeaponSwitchState.idle) {
return; // No shooting while switching
}
bool shotFired = currentWeapon.fire(currentTime, currentAmmo: ammo);
if (shotFired && currentWeapon is! Knife) {
if (shotFired && currentWeaponIndex > 0) {
// If it's a gun
ammo--;
if (ammo <= 0) {
// Auto-switch to knife when out of bullets
requestWeaponSwitch(0);
}
}
}
@@ -132,8 +207,6 @@ class Player {
int oldFrame = currentWeapon.frameIndex;
currentWeapon.update(currentTime);
// In your Pistol (Indices 212-215), Index 213 is the flash.
// This translates to frameIndex == 1 in our fireFrames list.
if (currentWeapon.state == WeaponState.firing &&
oldFrame == 0 &&
currentWeapon.frameIndex == 1) {
@@ -141,13 +214,4 @@ class Player {
}
return false;
}
// Logic to switch weapons (e.g., picking up the Machine Gun)
void equipBestWeapon() {
if (hasChainGun) {
/* set chain gun */
} else if (hasMachineGun) {
currentWeapon = MachineGun();
}
}
}