better spawn delay for ESP32

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-26 21:12:08 +02:00
parent ab14765750
commit f97f67422a
2 changed files with 64 additions and 17 deletions

View File

@@ -68,9 +68,11 @@ private:
static constexpr int MAX_LEFT_VEHICLES = 2; static constexpr int MAX_LEFT_VEHICLES = 2;
static constexpr int MAX_RIGHT_VEHICLES = 2; static constexpr int MAX_RIGHT_VEHICLES = 2;
static constexpr int MAX_VEHICLES = MAX_LEFT_VEHICLES + MAX_RIGHT_VEHICLES; static constexpr int MAX_VEHICLES = MAX_LEFT_VEHICLES + MAX_RIGHT_VEHICLES;
static constexpr int VEHICLE_SPAWN_DELAY = 1000; // milliseconds static constexpr int VEHICLE_SPAWN_DELAY = 2500; // milliseconds
static constexpr float MIN_SPEED = 1.0f; static constexpr float MIN_SPEED = 1.0f;
static constexpr float MAX_SPEED = 2.0f; static constexpr float MAX_SPEED = 2.0f;
static constexpr int MIN_SAME_DIRECTION_DISTANCE = 48; // 32 + 16 pixels
static constexpr int MAX_SAME_DIRECTION_DISTANCE = 64; // 32 + 32 pixels
menu_options_t *m_options; menu_options_t *m_options;
uint64_t m_animationCounter; uint64_t m_animationCounter;
@@ -130,4 +132,11 @@ private:
* @return Pointer to bitmap data * @return Pointer to bitmap data
*/ */
static const unsigned char *getVehicleBitmap(VehicleType type, Direction direction, int &width, int &height); static const unsigned char *getVehicleBitmap(VehicleType type, Direction direction, int &width, int &height);
/**
* @brief Check if there's enough distance to spawn a vehicle in a specific direction
* @param direction Direction to check
* @return true if spawning is allowed
*/
bool canSpawnInDirection(Direction direction) const;
}; };

View File

@@ -1,14 +1,10 @@
#include "ui/ScreenSaver.h" #include "ui/ScreenSaver.h"
#include <cstdlib>
#include "data/roads.h" #include "data/roads.h"
#include "data/vehicles.h" #include "data/vehicles.h"
#include <cstdlib>
ScreenSaver::ScreenSaver(menu_options_t *options) ScreenSaver::ScreenSaver(menu_options_t *options)
: Widget(options->u8g2), : Widget(options->u8g2), m_options(options), m_animationCounter(0), m_lastSpawnTime(0), m_leftVehicleCount(0),
m_options(options),
m_animationCounter(0),
m_lastSpawnTime(0),
m_leftVehicleCount(0),
m_rightVehicleCount(0) m_rightVehicleCount(0)
{ {
initVehicles(); initVehicles();
@@ -82,6 +78,43 @@ void ScreenSaver::update(const uint64_t dt)
} }
} }
bool ScreenSaver::canSpawnInDirection(Direction direction) const
{
// Minimalen Abstand zwischen 48 und 64 Pixel zufällig wählen
int requiredDistance =
MIN_SAME_DIRECTION_DISTANCE + (random() % (MAX_SAME_DIRECTION_DISTANCE - MIN_SAME_DIRECTION_DISTANCE + 1));
for (const auto &vehicle : m_vehicles)
{
if (!vehicle.active || vehicle.direction != direction)
continue;
// Abstand zum nächsten Fahrzeug in gleicher Richtung prüfen
if (direction == Direction::LEFT)
{
// Fahrzeuge fahren von rechts nach links
// Neues Fahrzeug würde bei u8g2->width + 16 starten
int newVehicleX = u8g2->width + 16;
// Prüfen ob genug Abstand zum existierenden Fahrzeug
if (newVehicleX - vehicle.x < requiredDistance)
return false;
}
else // Direction::RIGHT
{
// Fahrzeuge fahren von links nach rechts
// Neues Fahrzeug würde bei -32 starten
int newVehicleX = -32;
// Prüfen ob genug Abstand zum existierenden Fahrzeug
if (vehicle.x - newVehicleX < requiredDistance)
return false;
}
}
return true;
}
void ScreenSaver::trySpawnVehicle() void ScreenSaver::trySpawnVehicle()
{ {
// Check if we can spawn a new vehicle // Check if we can spawn a new vehicle
@@ -115,6 +148,11 @@ void ScreenSaver::trySpawnVehicle()
return; return;
} }
if (!canSpawnInDirection(direction))
{
return;
}
// Create new vehicle // Create new vehicle
Vehicle &newVehicle = m_vehicles[availableSlot]; Vehicle &newVehicle = m_vehicles[availableSlot];
newVehicle.type = getRandomVehicleType(); newVehicle.type = getRandomVehicleType();
@@ -234,8 +272,8 @@ void ScreenSaver::drawTransparentBitmap(const int x, const int y, const int widt
const int screenX = x + px; const int screenX = x + px;
// Bounds checking // Bounds checking
if (const int screenY = y + py; screenX >= 0 && screenX < u8g2->width && if (const int screenY = y + py;
screenY >= 0 && screenY < u8g2->height) screenX >= 0 && screenX < u8g2->width && screenY >= 0 && screenY < u8g2->height)
{ {
u8g2_DrawPixel(u8g2, screenX, screenY); u8g2_DrawPixel(u8g2, screenX, screenY);
} }