Can now pick up items

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 20:27:38 +01:00
parent bca8d10964
commit 026db920b3
7 changed files with 358 additions and 40 deletions

View File

@@ -0,0 +1,47 @@
import 'package:wolf_dart/features/entities/entity.dart';
enum CollectibleType { ammo, health, treasure, weapon, key }
class Collectible extends Entity {
final CollectibleType type;
Collectible({
required super.x,
required super.y,
required super.spriteIndex,
required super.mapId,
required this.type,
}) : super(state: EntityState.staticObj);
// Define which Map IDs are actually items you can pick up
static bool isCollectible(int objId) {
return (objId >= 43 && objId <= 44) || // Keys
(objId >= 47 && objId <= 56); // Health, Ammo, Weapons, Treasure, 1-Up
}
static CollectibleType _getType(int objId) {
if (objId == 43 || objId == 44) return CollectibleType.key;
if (objId == 47 || objId == 48) return CollectibleType.health;
if (objId == 49) return CollectibleType.ammo;
if (objId == 50 || objId == 51) return CollectibleType.weapon;
return CollectibleType.treasure; // 52-56
}
static Collectible? trySpawn(
int objId,
double x,
double y,
int difficultyLevel,
) {
if (isCollectible(objId)) {
return Collectible(
x: x,
y: y,
spriteIndex: objId - 21, // Same VSWAP math as decorations!
mapId: objId,
type: _getType(objId),
);
}
return null;
}
}

View File

@@ -11,7 +11,15 @@ class Decorative extends Entity {
// Checks if the Map ID belongs to a standard decoration
static bool isDecoration(int objId) {
return (objId >= 23 && objId <= 70) || objId == 124;
if (objId == 124) return true; // Dead guard
if (objId >= 23 && objId <= 70) {
// Exclude the collectibles!
if ((objId >= 43 && objId <= 44) || (objId >= 47 && objId <= 56)) {
return false;
}
return true;
}
return false;
}
// Safely calculates the VSWAP sprite index for standard decorations

View File

@@ -0,0 +1,125 @@
import 'dart:math' as math;
import 'package:wolf_dart/classes/linear_coordinates.dart';
import 'package:wolf_dart/features/entities/enemies/enemy.dart';
import 'package:wolf_dart/features/entities/entity.dart';
class Dog extends Enemy {
static const double speed = 0.05; // Dogs are much faster than guards!
bool _hasBittenThisCycle = false;
Dog({
required super.x,
required super.y,
required super.angle,
required super.mapId,
}) : super(
spriteIndex: 99, // Dogs start at index 99 in VSWAP
state: EntityState.idle,
);
static Dog? trySpawn(int objId, double x, double y, int difficultyLevel) {
bool canSpawn = false;
switch (difficultyLevel) {
case 0:
canSpawn = objId >= 116 && objId <= 119;
break;
case 1:
canSpawn = objId >= 152 && objId <= 155;
break;
case 2:
canSpawn = objId >= 188 && objId <= 191;
break;
case 3:
canSpawn = objId >= 224 && objId <= 227;
break;
}
if (canSpawn) {
return Dog(
x: x,
y: y,
angle: Enemy.getInitialAngle(objId),
mapId: objId,
);
}
return null;
}
@override
void update({
required int elapsedMs,
required LinearCoordinates player,
required bool Function(int x, int y) isWalkable,
required void Function(int damage) onDamagePlayer,
}) {
if (state == EntityState.idle) {
if (hasLineOfSight(player, isWalkable)) {
state = EntityState.patrolling;
lastActionTime = elapsedMs;
}
}
if (state == EntityState.idle ||
state == EntityState.patrolling ||
state == EntityState.shooting) {
// "Shooting" here means biting
double dx = player.x - x;
double dy = player.y - y;
double distance = math.sqrt(dx * dx + dy * dy);
double angleToPlayer = math.atan2(dy, dx);
if (state == EntityState.patrolling || state == EntityState.shooting) {
angle = angleToPlayer;
}
double diff = angle - angleToPlayer;
while (diff <= -math.pi) {
diff += 2 * math.pi;
}
while (diff > math.pi) {
diff -= 2 * math.pi;
}
int octant = ((diff + (math.pi / 8)) / (math.pi / 4)).floor() % 8;
if (octant < 0) octant += 8;
if (state == EntityState.idle) {
spriteIndex = 99 + octant; // Base dog standing sprite
} else if (state == EntityState.patrolling) {
if (distance > 0.8) {
double moveX = x + math.cos(angle) * speed;
double moveY = y + math.sin(angle) * speed;
if (isWalkable(moveX.toInt(), y.toInt())) x = moveX;
if (isWalkable(x.toInt(), moveY.toInt())) y = moveY;
}
// Dogs only have 4 walking angles, alternating legs
int walkFrame = (elapsedMs ~/ 100) % 4;
spriteIndex = 107 + (walkFrame * 8) + octant;
// Dog Bite Attack (Must be practically touching the player)
if (distance < 1.0 && elapsedMs - lastActionTime > 1000) {
state = EntityState.shooting;
lastActionTime = elapsedMs;
_hasBittenThisCycle = false;
}
} else if (state == EntityState.shooting) {
int timeAttacking = elapsedMs - lastActionTime;
if (timeAttacking < 200) {
spriteIndex = 139; // Jumping/Biting frame
if (!_hasBittenThisCycle) {
onDamagePlayer(5); // Dogs do less damage than guards
_hasBittenThisCycle = true;
}
} else {
state = EntityState.patrolling;
lastActionTime = elapsedMs;
}
}
}
}
}

View File

@@ -1,5 +1,7 @@
import 'package:wolf_dart/features/entities/collectible.dart';
import 'package:wolf_dart/features/entities/decorative.dart';
import 'package:wolf_dart/features/entities/enemies/brown_guard.dart';
import 'package:wolf_dart/features/entities/enemies/dog.dart';
import 'package:wolf_dart/features/entities/entity.dart';
typedef EntitySpawner =
@@ -8,8 +10,10 @@ typedef EntitySpawner =
abstract class EntityRegistry {
// Add future enemies (SSGuard, Dog, etc.) to this list!
static final List<EntitySpawner> _spawners = [
Decorative.trySpawn,
BrownGuard.trySpawn,
Collectible.trySpawn, // Check collectibles
Decorative.trySpawn, // Then check decorations
BrownGuard.trySpawn, // Then check guards
Dog.trySpawn, // Then check dogs
];
static Entity? spawn(