Break out spawnable entities and use a registry to spawn them.
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
12
lib/features/difficulty/difficulty.dart
Normal file
12
lib/features/difficulty/difficulty.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
enum Difficulty {
|
||||
canIPlayDaddy(0, "Can I play, Daddy?"),
|
||||
dontHurtMe(1, "Don't hurt me."),
|
||||
bringEmOn(2, "Bring em' on!"),
|
||||
iAmDeathIncarnate(3, "I am Death incarnate!"),
|
||||
;
|
||||
|
||||
final String title;
|
||||
final int level;
|
||||
|
||||
const Difficulty(this.level, this.title);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_dart/classes/difficulty.dart';
|
||||
import 'package:wolf_dart/features/difficulty/difficulty.dart';
|
||||
import 'package:wolf_dart/features/renderer/renderer.dart';
|
||||
|
||||
class DifficultyScreen extends StatelessWidget {
|
||||
|
||||
42
lib/features/entities/decorative.dart
Normal file
42
lib/features/entities/decorative.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
|
||||
class Decorative extends Entity {
|
||||
Decorative({
|
||||
required super.x,
|
||||
required super.y,
|
||||
required super.spriteIndex,
|
||||
required super.mapId,
|
||||
super.state = EntityState.staticObj, // Defaults to static
|
||||
});
|
||||
|
||||
// Checks if the Map ID belongs to a standard decoration
|
||||
static bool isDecoration(int objId) {
|
||||
return (objId >= 23 && objId <= 70) || objId == 124;
|
||||
}
|
||||
|
||||
// Safely calculates the VSWAP sprite index for standard decorations
|
||||
static int getSpriteIndex(int objId) {
|
||||
if (objId == 124) return 95; // Dead guard
|
||||
return objId - 21;
|
||||
}
|
||||
|
||||
static Decorative? trySpawn(
|
||||
int objId,
|
||||
double x,
|
||||
double y,
|
||||
int difficultyLevel,
|
||||
) {
|
||||
if (isDecoration(objId)) {
|
||||
return Decorative(
|
||||
x: x,
|
||||
y: y,
|
||||
spriteIndex: getSpriteIndex(objId),
|
||||
mapId: objId,
|
||||
state: objId == 124 ? EntityState.dead : EntityState.staticObj,
|
||||
);
|
||||
}
|
||||
|
||||
// Not a decoration!
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:wolf_dart/classes/enemy.dart';
|
||||
import 'package:wolf_dart/classes/entity.dart';
|
||||
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 BrownGuard extends Enemy {
|
||||
static const double speed = 0.03;
|
||||
@@ -14,24 +14,42 @@ class BrownGuard extends Enemy {
|
||||
required super.angle,
|
||||
required super.mapId,
|
||||
}) : super(
|
||||
spriteIndex: 50, // Default front-facing idle
|
||||
// Default front-facing idle
|
||||
spriteIndex: 50,
|
||||
state: EntityState.idle,
|
||||
);
|
||||
|
||||
// Checks if a Map ID is a valid Brown Guard for the selected difficulty
|
||||
static bool isSpawnableForDifficulty(int objId, int difficultyLevel) {
|
||||
static BrownGuard? trySpawn(
|
||||
int objId,
|
||||
double x,
|
||||
double y,
|
||||
int difficultyLevel,
|
||||
) {
|
||||
bool canSpawn = false;
|
||||
switch (difficultyLevel) {
|
||||
case 0:
|
||||
return objId >= 108 && objId <= 115;
|
||||
canSpawn = objId >= 108 && objId <= 115;
|
||||
break;
|
||||
case 1:
|
||||
return objId >= 144 && objId <= 151;
|
||||
canSpawn = objId >= 144 && objId <= 151;
|
||||
break;
|
||||
case 2:
|
||||
return objId >= 180 && objId <= 187;
|
||||
canSpawn = objId >= 180 && objId <= 187;
|
||||
break;
|
||||
case 3:
|
||||
return objId >= 216 && objId <= 223;
|
||||
default:
|
||||
return false;
|
||||
canSpawn = objId >= 216 && objId <= 223;
|
||||
break;
|
||||
}
|
||||
|
||||
if (canSpawn) {
|
||||
return BrownGuard(
|
||||
x: x,
|
||||
y: y,
|
||||
angle: Enemy.getInitialAngle(objId),
|
||||
mapId: objId,
|
||||
);
|
||||
}
|
||||
return null; // Not a Brown Guard!
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -64,8 +82,12 @@ class BrownGuard extends Enemy {
|
||||
}
|
||||
|
||||
double diff = angle - angleToPlayer;
|
||||
while (diff <= -math.pi) diff += 2 * math.pi;
|
||||
while (diff > math.pi) diff -= 2 * math.pi;
|
||||
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;
|
||||
79
lib/features/entities/enemies/enemy.dart
Normal file
79
lib/features/entities/enemies/enemy.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:wolf_dart/classes/linear_coordinates.dart';
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
|
||||
abstract class Enemy extends Entity {
|
||||
Enemy({
|
||||
required super.x,
|
||||
required super.y,
|
||||
required super.spriteIndex,
|
||||
super.angle,
|
||||
super.state,
|
||||
super.mapId,
|
||||
super.lastActionTime,
|
||||
});
|
||||
|
||||
// Decodes the Map ID to figure out which way the enemy is facing
|
||||
static double getInitialAngle(int objId) {
|
||||
int normalizedId = (objId - 108) % 36;
|
||||
int direction = normalizedId % 4; // 0=East, 1=North, 2=West, 3=South
|
||||
|
||||
switch (direction) {
|
||||
case 0:
|
||||
return 0.0;
|
||||
case 1:
|
||||
return 3 * math.pi / 2;
|
||||
case 2:
|
||||
return math.pi;
|
||||
case 3:
|
||||
return math.pi / 2;
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
// The enemy can now check its own line of sight!
|
||||
bool hasLineOfSight(
|
||||
LinearCoordinates player,
|
||||
bool Function(int x, int y) isWalkable,
|
||||
) {
|
||||
double dx = player.x - x;
|
||||
double dy = player.y - y;
|
||||
double distance = math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
// 1. FOV Check
|
||||
double angleToPlayer = math.atan2(dy, dx);
|
||||
double diff = angle - angleToPlayer;
|
||||
|
||||
while (diff <= -math.pi) {
|
||||
diff += 2 * math.pi;
|
||||
}
|
||||
while (diff > math.pi) {
|
||||
diff -= 2 * math.pi;
|
||||
}
|
||||
|
||||
if (diff.abs() > math.pi / 2) return false;
|
||||
|
||||
// 2. Map Check
|
||||
double dirX = dx / distance;
|
||||
double dirY = dy / distance;
|
||||
double stepSize = 0.2;
|
||||
for (double i = 0; i < distance; i += stepSize) {
|
||||
int checkX = (x + dirX * i).toInt();
|
||||
int checkY = (y + dirY * i).toInt();
|
||||
|
||||
if (!isWalkable(checkX, checkY)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Update signature is cleaner now
|
||||
void update({
|
||||
required int elapsedMs,
|
||||
required LinearCoordinates player,
|
||||
required bool Function(int x, int y) isWalkable,
|
||||
required void Function(int damage) onDamagePlayer,
|
||||
});
|
||||
}
|
||||
21
lib/features/entities/entity.dart
Normal file
21
lib/features/entities/entity.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
enum EntityState { staticObj, idle, patrolling, shooting, pain, dead }
|
||||
|
||||
abstract class Entity<T> {
|
||||
double x;
|
||||
double y;
|
||||
int spriteIndex;
|
||||
double angle;
|
||||
EntityState state;
|
||||
int mapId;
|
||||
int lastActionTime;
|
||||
|
||||
Entity({
|
||||
required this.x,
|
||||
required this.y,
|
||||
required this.spriteIndex,
|
||||
this.angle = 0.0,
|
||||
this.state = EntityState.staticObj,
|
||||
this.mapId = 0,
|
||||
this.lastActionTime = 0,
|
||||
});
|
||||
}
|
||||
35
lib/features/entities/entity_registry.dart
Normal file
35
lib/features/entities/entity_registry.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:wolf_dart/features/entities/decorative.dart';
|
||||
import 'package:wolf_dart/features/entities/enemies/brown_guard.dart';
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
|
||||
typedef EntitySpawner =
|
||||
Entity? Function(int objId, double x, double y, int difficultyLevel);
|
||||
|
||||
abstract class EntityRegistry {
|
||||
// Add future enemies (SSGuard, Dog, etc.) to this list!
|
||||
static final List<EntitySpawner> _spawners = [
|
||||
Decorative.trySpawn,
|
||||
BrownGuard.trySpawn,
|
||||
];
|
||||
|
||||
static Entity? spawn(
|
||||
int objId,
|
||||
double x,
|
||||
double y,
|
||||
int difficultyLevel,
|
||||
int maxSprites,
|
||||
) {
|
||||
for (final spawner in _spawners) {
|
||||
Entity? entity = spawner(objId, x, y, difficultyLevel);
|
||||
|
||||
if (entity != null) {
|
||||
// Safety bounds check for the VSWAP array
|
||||
if (entity.spriteIndex >= 0 && entity.spriteIndex < maxSprites) {
|
||||
return entity;
|
||||
}
|
||||
return null; // VSWAP doesn't have this sprite!
|
||||
}
|
||||
}
|
||||
return null; // No class claimed this Map ID
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_dart/classes/entity.dart';
|
||||
import 'package:wolf_dart/classes/linear_coordinates.dart';
|
||||
import 'package:wolf_dart/classes/matrix.dart';
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
import 'package:wolf_dart/features/renderer/color_palette.dart';
|
||||
|
||||
class RaycasterPainter extends CustomPainter {
|
||||
|
||||
@@ -3,13 +3,12 @@ import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:wolf_dart/classes/decorative.dart';
|
||||
import 'package:wolf_dart/classes/difficulty.dart';
|
||||
import 'package:wolf_dart/classes/enemy.dart';
|
||||
import 'package:wolf_dart/classes/entity.dart';
|
||||
import 'package:wolf_dart/classes/linear_coordinates.dart';
|
||||
import 'package:wolf_dart/classes/matrix.dart';
|
||||
import 'package:wolf_dart/features/enemies/brown_guard.dart';
|
||||
import 'package:wolf_dart/features/difficulty/difficulty.dart';
|
||||
import 'package:wolf_dart/features/entities/enemies/enemy.dart';
|
||||
import 'package:wolf_dart/features/entities/entity.dart';
|
||||
import 'package:wolf_dart/features/entities/entity_registry.dart';
|
||||
import 'package:wolf_dart/features/map/wolf_map.dart';
|
||||
import 'package:wolf_dart/features/renderer/raycast_painter.dart';
|
||||
import 'package:wolf_dart/sprite_gallery.dart';
|
||||
@@ -88,34 +87,17 @@ class _WolfRendererState extends State<WolfRenderer>
|
||||
case 22:
|
||||
playerAngle = math.pi;
|
||||
}
|
||||
} // 1. POPULATE DECORATIONS & DEAD BODIES
|
||||
else if (Decorative.isDecoration(objId)) {
|
||||
int spriteIdx = Decorative.getSpriteIndex(objId);
|
||||
if (spriteIdx >= 0 && spriteIdx < gameMap.sprites.length) {
|
||||
entities.add(
|
||||
Decorative(
|
||||
x: x + 0.5,
|
||||
y: y + 0.5,
|
||||
spriteIndex: spriteIdx,
|
||||
mapId: objId,
|
||||
// NEW: Dynamically assign the state!
|
||||
state: objId == 124 ? EntityState.dead : EntityState.staticObj,
|
||||
),
|
||||
);
|
||||
}
|
||||
} else if (BrownGuard.isSpawnableForDifficulty(
|
||||
objId,
|
||||
widget.difficulty.level,
|
||||
)) {
|
||||
if (50 < gameMap.sprites.length) {
|
||||
entities.add(
|
||||
BrownGuard(
|
||||
x: x + 0.5,
|
||||
y: y + 0.5,
|
||||
angle: Enemy.getInitialAngle(objId),
|
||||
mapId: objId,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
Entity? newEntity = EntityRegistry.spawn(
|
||||
objId,
|
||||
x + 0.5,
|
||||
y + 0.5,
|
||||
widget.difficulty.level,
|
||||
gameMap.sprites.length,
|
||||
);
|
||||
|
||||
if (newEntity != null) {
|
||||
entities.add(newEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user