Fixed pistol and knife sprites for retail. Added firing mechanism.

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 20:53:00 +01:00
parent 029e90ea9d
commit d4d5a84bc4
7 changed files with 83 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ class DifficultyScreen extends StatelessWidget {
MaterialPageRoute(
builder: (_) => const WolfRenderer(
difficulty: Difficulty.bringEmOn,
// isDemo: true,
isDemo: false,
showSpriteGallery: true,
),
),

View File

@@ -120,14 +120,11 @@ class Player {
}
void fire(int currentTime) {
// Only spend ammo if the weapon isn't a knife
bool shotFired = currentWeapon.fire(currentTime, currentAmmo: ammo);
// If it was a gun (not a knife) and it fired, consume ammo
if (shotFired && currentWeapon is! Knife) {
ammo--;
}
// TODO: We'll add Raycast hit detection here next!
}
void updateWeapon(int currentTime) {

View File

@@ -16,16 +16,13 @@ abstract class Weapon {
required this.idleSprite,
required this.fireFrames,
required this.damage,
this.msPerFrame = 100, // Speed of animation
this.msPerFrame = 100,
});
int get currentSprite =>
state == WeaponState.idle ? idleSprite : fireFrames[frameIndex];
bool get isFiring => state == WeaponState.firing;
/// The main entry point for the player to use the gun.
/// Returns true if a bullet was actually spent.
/// Core firing logic. Returns true if a bullet was spent.
bool fire(int currentTime, {required int currentAmmo}) {
if (state == WeaponState.idle && currentAmmo > 0) {
state = WeaponState.firing;

View File

@@ -5,14 +5,13 @@ class Knife extends Weapon {
: super(
name: "Knife",
idleSprite: 416,
fireFrames: [417, 418, 419],
fireFrames: [417, 418, 419, 420],
damage: 15,
msPerFrame: 120,
);
@override
bool fire(int currentTime, {required int currentAmmo}) {
// Knife doesn't need ammo!
if (state == WeaponState.idle) {
state = WeaponState.firing;
frameIndex = 0;

View File

@@ -4,8 +4,8 @@ class Pistol extends Weapon {
Pistol()
: super(
name: "Pistol",
idleSprite: 408,
fireFrames: [409, 410, 411, 412],
idleSprite: 421,
fireFrames: [422, 423, 424, 425],
damage: 20,
);
}

View File

@@ -13,6 +13,7 @@ import 'package:wolf_dart/features/entities/entity_registry.dart';
import 'package:wolf_dart/features/map/wolf_map.dart';
import 'package:wolf_dart/features/player/player.dart';
import 'package:wolf_dart/features/renderer/raycast_painter.dart';
import 'package:wolf_dart/features/renderer/weapon_painter.dart';
import 'package:wolf_dart/features/ui/hud.dart';
import 'package:wolf_dart/sprite_gallery.dart';
@@ -55,6 +56,9 @@ class _WolfRendererState extends State<WolfRenderer>
Map<String, double> doorOffsets = {};
Map<String, int> doorStates = {}; // 1 = opening, 2 = fully open
double moveStepX = 0;
double moveStepY = 0;
@override
void initState() {
super.initState();
@@ -191,6 +195,10 @@ class _WolfRendererState extends State<WolfRenderer>
const double moveSpeed = 0.12;
const double turnSpeed = 0.08;
// Reset steps each tick before calculation
moveStepX = 0;
moveStepY = 0;
// 1. ANIMATE DOORS
doorStates.forEach((key, state) {
if (state == 1) {
@@ -203,8 +211,6 @@ class _WolfRendererState extends State<WolfRenderer>
}
});
double moveStepX = 0;
double moveStepY = 0;
final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
if (pressedKeys.contains(LogicalKeyboardKey.keyW)) {
@@ -300,12 +306,12 @@ class _WolfRendererState extends State<WolfRenderer>
});
}
// 5. Fire weapon
bool isCtrlPressed = pressedKeys.contains(LogicalKeyboardKey.controlLeft);
if (isCtrlPressed) {
// 5. Weapon
player.currentWeapon.update(elapsed.inMilliseconds);
if (pressedKeys.contains(LogicalKeyboardKey.controlLeft)) {
player.fire(elapsed.inMilliseconds);
}
player.updateWeapon(elapsed.inMilliseconds);
// Fade out the damage flash smoothly
if (damageFlashOpacity > 0) {
@@ -357,6 +363,40 @@ class _WolfRendererState extends State<WolfRenderer>
sprites: gameMap.sprites,
),
),
// Weapon Viewmodel
Positioned(
bottom: -20,
left: 0,
right: 0,
child: Center(
child: Transform.translate(
offset: Offset(
0,
// Bobbing math: only moves if velocity is > 0
(moveStepX.abs() + moveStepY.abs()) > 0
? math.sin(
DateTime.now()
.millisecondsSinceEpoch /
100,
) *
12
: 0,
),
child: SizedBox(
width: 500,
height: 500,
child: CustomPaint(
painter: WeaponPainter(
sprite:
gameMap.sprites[player
.currentWeapon
.currentSprite],
),
),
),
),
),
),
if (damageFlashOpacity > 0)
Positioned.fill(
child: Container(
@@ -370,6 +410,7 @@ class _WolfRendererState extends State<WolfRenderer>
},
),
),
// HUD
Hud(player: player),
],

View File

@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
import 'package:wolf_dart/features/renderer/color_palette.dart';
class WeaponPainter extends CustomPainter {
final List<List<int>> sprite;
WeaponPainter({required this.sprite});
@override
void paint(Canvas canvas, Size size) {
double pixelSize = size.width / 64;
final paint = Paint();
for (int x = 0; x < 64; x++) {
for (int y = 0; y < 64; y++) {
int colorByte = sprite[x][y];
if (colorByte != 255) {
// Transparency check
paint.color = ColorPalette.vga[colorByte];
canvas.drawRect(
Rect.fromLTWH(x * pixelSize, y * pixelSize, pixelSize, pixelSize),
paint,
);
}
}
}
}
@override
bool shouldRepaint(covariant WeaponPainter oldDelegate) => true;
}