Further clean up _tick
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -192,76 +192,22 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _tick(Duration elapsed) {
|
void _tick(Duration elapsed) {
|
||||||
// 1. Process all hardware inputs and player intentions
|
// 1. Inputs & Intentions
|
||||||
_processInputs(elapsed);
|
_processInputs(elapsed);
|
||||||
|
|
||||||
// 2. Animate weapon lowering/raising
|
// 2. State Updates
|
||||||
player.updateWeaponSwitch();
|
player.updateWeaponSwitch();
|
||||||
|
_updateDoors();
|
||||||
|
_applyMovementAndCollision();
|
||||||
|
_updateEntities(elapsed);
|
||||||
|
_updateScreenEffects();
|
||||||
|
|
||||||
// 3. Animate doors sliding
|
// 3. Combat
|
||||||
doorStates.forEach((key, state) {
|
|
||||||
if (state == 1) {
|
|
||||||
doorOffsets[key] = (doorOffsets[key] ?? 0.0) + 0.02;
|
|
||||||
if (doorOffsets[key]! >= 1.0) {
|
|
||||||
doorOffsets[key] = 1.0;
|
|
||||||
doorStates[key] = 2; // Mark as fully open
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 4. Process Wall Collision for Movement
|
|
||||||
const double margin = 0.3;
|
|
||||||
|
|
||||||
double newX = player.x + moveStepX;
|
|
||||||
int checkX = (moveStepX > 0)
|
|
||||||
? (newX + margin).toInt()
|
|
||||||
: (newX - margin).toInt();
|
|
||||||
if (_isWalkable(checkX, player.y.toInt())) {
|
|
||||||
player.x = newX;
|
|
||||||
}
|
|
||||||
|
|
||||||
double newY = player.y + moveStepY;
|
|
||||||
int checkY = (moveStepY > 0)
|
|
||||||
? (newY + margin).toInt()
|
|
||||||
: (newY - margin).toInt();
|
|
||||||
if (_isWalkable(player.x.toInt(), checkY)) {
|
|
||||||
player.y = newY;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 5. Update Entity Logic
|
|
||||||
List<Entity> itemsToRemove = [];
|
|
||||||
|
|
||||||
for (Entity entity in entities) {
|
|
||||||
if (entity is Enemy) {
|
|
||||||
entity.update(
|
|
||||||
elapsedMs: elapsed.inMilliseconds,
|
|
||||||
player: player.position,
|
|
||||||
isWalkable: _isWalkable,
|
|
||||||
onDamagePlayer: _takeDamage,
|
|
||||||
);
|
|
||||||
} else if (entity is Collectible) {
|
|
||||||
double dx = player.x - entity.x;
|
|
||||||
double dy = player.y - entity.y;
|
|
||||||
if (math.sqrt(dx * dx + dy * dy) < 0.5) {
|
|
||||||
if (player.tryPickup(entity)) itemsToRemove.add(entity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (itemsToRemove.isNotEmpty) {
|
|
||||||
entities.removeWhere((e) => itemsToRemove.contains(e));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. Weapon Raycast & Attacking
|
|
||||||
if (player.updateWeapon(elapsed.inMilliseconds)) {
|
if (player.updateWeapon(elapsed.inMilliseconds)) {
|
||||||
_performRaycastAttack(elapsed);
|
_performRaycastAttack(elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Screen Effects
|
// 4. Render
|
||||||
if (damageFlashOpacity > 0) {
|
|
||||||
damageFlashOpacity = math.max(0.0, damageFlashOpacity - 0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(() {});
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,6 +337,83 @@ class _WolfRendererState extends State<WolfRenderer>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _updateDoors() {
|
||||||
|
doorStates.forEach((key, state) {
|
||||||
|
if (state == 1) {
|
||||||
|
// 1 = opening
|
||||||
|
doorOffsets[key] = (doorOffsets[key] ?? 0.0) + 0.02; // Slide speed
|
||||||
|
if (doorOffsets[key]! >= 1.0) {
|
||||||
|
doorOffsets[key] = 1.0;
|
||||||
|
doorStates[key] = 2; // Mark as fully open
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _applyMovementAndCollision() {
|
||||||
|
const double margin = 0.3;
|
||||||
|
|
||||||
|
// X-axis collision
|
||||||
|
double newX = player.x + moveStepX;
|
||||||
|
int checkX = (moveStepX > 0)
|
||||||
|
? (newX + margin).toInt()
|
||||||
|
: (newX - margin).toInt();
|
||||||
|
|
||||||
|
if (_isWalkable(checkX, player.y.toInt())) {
|
||||||
|
player.x = newX;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Y-axis collision
|
||||||
|
double newY = player.y + moveStepY;
|
||||||
|
int checkY = (moveStepY > 0)
|
||||||
|
? (newY + margin).toInt()
|
||||||
|
: (newY - margin).toInt();
|
||||||
|
|
||||||
|
if (_isWalkable(player.x.toInt(), checkY)) {
|
||||||
|
player.y = newY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _updateEntities(Duration elapsed) {
|
||||||
|
List<Entity> itemsToRemove = [];
|
||||||
|
|
||||||
|
for (Entity entity in entities) {
|
||||||
|
if (entity is Enemy) {
|
||||||
|
entity.update(
|
||||||
|
elapsedMs: elapsed.inMilliseconds,
|
||||||
|
player: player.position,
|
||||||
|
isWalkable: _isWalkable,
|
||||||
|
onDamagePlayer: _takeDamage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Collectible Interaction Logic
|
||||||
|
else if (entity is Collectible) {
|
||||||
|
double dx = player.x - entity.x;
|
||||||
|
double dy = player.y - entity.y;
|
||||||
|
double dist = math.sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
|
// If player is close enough to the item
|
||||||
|
if (dist < 0.5) {
|
||||||
|
if (player.tryPickup(entity)) {
|
||||||
|
itemsToRemove.add(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the items that were successfully picked up
|
||||||
|
if (itemsToRemove.isNotEmpty) {
|
||||||
|
entities.removeWhere((e) => itemsToRemove.contains(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _updateScreenEffects() {
|
||||||
|
// Fade out the damage flash smoothly
|
||||||
|
if (damageFlashOpacity > 0) {
|
||||||
|
damageFlashOpacity = math.max(0.0, damageFlashOpacity - 0.05);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (_isLoading) {
|
if (_isLoading) {
|
||||||
|
|||||||
Reference in New Issue
Block a user