11#include "../common/Logger/Logger.hpp"
18 int health,
const std::string ¤tAnimation,
int srcX,
int srcY,
19 int srcW,
int srcH,
float velocityX,
float velocityY, uint32_t serverTick) {
27 it->second.type = type;
28 it->second.health = health;
36 float errorX = x - it->second.x;
37 float errorY = y - it->second.y;
38 float errorDistance = std::sqrt(errorX * errorX + errorY * errorY);
45 if (errorDistance < jitterThreshold) {
50 it->second.prevX = it->second.x;
51 it->second.prevY = it->second.y;
52 it->second.targetX = x;
53 it->second.targetY = y;
54 it->second.interpolationFactor = 0.5f;
62 it->second.prevX = it->second.x;
63 it->second.prevY = it->second.y;
64 it->second.targetX = x;
65 it->second.targetY = y;
66 it->second.interpolationFactor = 0.0f;
81 it->second.snapshots.push_back(snapshot);
84 while (it->second.snapshots.size() > 3) {
85 it->second.snapshots.pop_front();
89 it->second.prevX = it->second.x;
90 it->second.prevY = it->second.y;
91 it->second.targetX = x;
92 it->second.targetY = y;
93 it->second.interpolationFactor = 0.0f;
101 it->second.currentAnimation = currentAnimation;
102 it->second.startPixelX = srcX;
103 it->second.startPixelY = srcY;
104 it->second.spriteSizeX = srcW;
105 it->second.spriteSizeY = srcH;
110 newEntity.
type = type;
113 newEntity.
health = health;
127 newEntity.
scale = 3.0f;
142 LOG_DEBUG(
"Entity created: ID=",
id,
" Type=",
static_cast<int>(type),
" at (", x,
",", y,
")");
160 float scrollSpeed,
float parallaxSpeedFactor) {
168 if (!mainBackground.empty()) {
176 std::string fullPath =
"assets/" + mainBackground;
184 LOG_WARNING(
"Failed to load main background: ", fullPath,
" - using black background");
187 LOG_INFO(
"No main background defined - using black background");
191 if (!parallaxBackground.empty()) {
199 std::string fullPath =
"assets/" + parallaxBackground;
207 LOG_WARNING(
"Failed to load parallax background: ", fullPath,
" - no parallax layer");
226 LOG_DEBUG(
"Background system deactivated");
275 int tilesNeeded =
static_cast<int>(std::ceil(
static_cast<float>(screenWidth) / scaledWidth)) + 2;
279 if (wrappedOffset < 0) {
280 wrappedOffset += scaledWidth;
283 for (
int i = 0; i < tilesNeeded; i++) {
284 float drawX = (
static_cast<float>(i) * scaledWidth) - wrappedOffset;
289 static_cast<float>(screenHeight), 0xFFFFFFFF);
301 int tilesNeeded =
static_cast<int>(std::ceil(
static_cast<float>(screenWidth) / scaledWidth)) + 2;
304 if (wrappedOffset < 0) {
305 wrappedOffset += scaledWidth;
308 for (
int i = 0; i < tilesNeeded; i++) {
309 float drawX = (
static_cast<float>(i) * scaledWidth) - wrappedOffset;
313 drawX, 0.0f, scaledWidth,
static_cast<float>(screenHeight), 0xFFFFFFFF);
320 LOG_INFO(
"Local player entity ID set to: ",
id);
333 static int frameCount = 0;
334 if (++frameCount % 60 == 0) {
335 int wallCount = 0, playerCount = 0, enemyCount = 0;
336 for (
const auto &[
id, entity] :
_entities) {
344 LOG_INFO(
"EntityRenderer: Rendering ",
_entities.size(),
" entities - Players:", playerCount,
345 " Enemies:", enemyCount,
" Walls:", wallCount);
351 for (
const auto &[
id, entity] :
_entities) {
352 switch (entity.type) {
375 LOG_WARNING(
"Unknown entity type: ",
static_cast<int>(entity.type));
395 float scale = entity.
scale > 0.0f ? entity.
scale : 3.0f;
398 uint32_t tint = isLocalPlayer ? 0xFF008000 : 0xFFFFFFFF;
401 entity.
x - (srcWidth * scale / 2), entity.
y - (srcHeight * scale / 2), 0.0f,
411 _graphics.
DrawText(-1,
"YOU",
static_cast<int>(entity.
x - 15),
static_cast<int>(entity.
y - 50), 14,
428 uint32_t color = 0xFF0000FF;
429 float halfSize = 12.0f;
450 static int debugCount = 0;
451 if (debugCount < 10) {
452 LOG_DEBUG(
"Projectile ", entity.
entityId,
": sprite(", srcX,
",", srcY,
",", srcWidth,
",", srcHeight,
458 float scale = entity.
scale > 0.0f ? entity.
scale : 2.0f;
461 uint32_t tint = 0xFFFFFFFF;
467 entity.
y - (srcHeight * scale / 2), 0.0f, scale, tint);
472 if (maxHealth <= 0 || health < 0) {
477 float barWidth = 30.0f;
478 float barHeight = 4.0f;
479 float healthRatio =
static_cast<float>(health) /
static_cast<float>(maxHealth);
482 if (healthRatio > 1.0f)
484 if (healthRatio < 0.0f)
488 float barX = x - barWidth / 2.0f;
492 static_cast<int>(barHeight), 0xFF0000FF);
496 static_cast<int>(barWidth * healthRatio),
static_cast<int>(barHeight),
509 float x = entity.
x - width / 2.0f;
510 float y = entity.
y - height / 2.0f;
513 uint32_t tint = 0xFFFFFFFF;
516 float healthRatio = entity.
health / 100.0f;
518 uint8_t green =
static_cast<uint8_t
>(255 * healthRatio);
519 uint8_t blue =
static_cast<uint8_t
>(255 * healthRatio);
521 tint = 0xFF000000 | (blue << 16) | (green << 8) | red;
527 uint32_t wallColor = 0xFF13458B;
533 static_cast<int>(height), wallColor);
537 static_cast<int>(height), 0xFF000000);
555 float scale = entity.
scale > 0.0f ? entity.
scale : 2.0f;
558 uint32_t tint = 0xFFFFFFFF;
561 entity.
x - (srcWidth * scale / 2), entity.
y - (srcHeight * scale / 2), 0.0f,
572 std::string idText =
"ID:" + std::to_string(entity.
entityId);
573 float textX = entity.
x - 15.0f;
574 float textY = entity.
y - 45.0f;
576 _graphics.
DrawText(-1, idText.c_str(),
static_cast<int>(textX),
static_cast<int>(textY), 10, 0xFFFFFFFF);
580 std::string hpText =
"HP:" + std::to_string(entity.
health);
581 _graphics.
DrawText(-1, hpText.c_str(),
static_cast<int>(textX),
static_cast<int>(textY - 12), 10,
586 std::string typeText =
"Type:" + std::to_string(
static_cast<int>(entity.
type));
587 _graphics.
DrawText(-1, typeText.c_str(),
static_cast<int>(textX),
static_cast<int>(textY - 24), 10,
605 if (entity.interpolationFactor >= 1.0f) {
611 entity.interpolationFactor =
clamp(entity.interpolationFactor, 0.0f, 1.0f);
614 entity.x =
lerp(entity.prevX, entity.targetX, entity.interpolationFactor);
615 entity.y =
lerp(entity.prevY, entity.targetY, entity.interpolationFactor);
626 it->second.x += deltaX;
627 it->second.y += deltaY;
629 it->second.targetX += deltaX;
630 it->second.targetY += deltaY;
631 it->second.prevX += deltaX;
632 it->second.prevY += deltaY;
636 return start + ((end - start) * t);
650 auto now = std::chrono::steady_clock::now();
651 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
652 return static_cast<uint64_t
>(duration.count());
void moveEntityLocally(uint32_t entityId, float deltaX, float deltaY)
Move an entity locally (client-side prediction)
float _interpolationSpeed
std::unordered_map< uint32_t, RenderableEntity > _entities
Entity cache: maps entity ID to its renderable state.
void renderHealthBar(float x, float y, int health, int maxHealth)
Render a health bar above an entity.
void setMyEntityId(uint32_t id)
Set the local player's entity ID for visual differentiation.
void renderOrbitalModule(const RenderableEntity &entity)
Render an orbital module (drone)
void renderBackground()
Render scrolling background layers.
BackgroundConfig _mainBackground
Main background layer (scrolls at map's scroll speed)
void removeEntity(uint32_t id)
Remove an entity from the rendering cache.
Graphics::RaylibGraphics & _graphics
Reference to graphics subsystem for drawing operations.
void renderProjectile(const RenderableEntity &entity)
Render a projectile (player or enemy bullet)
void renderWall(const RenderableEntity &entity)
Render a wall/obstacle.
void renderDebugInfo(const RenderableEntity &entity)
Render debug information for an entity.
uint64_t getCurrentTimeMs() const
Get current time in milliseconds.
EntityRenderer(Graphics::RaylibGraphics &graphics)
Constructor.
void clearBackground()
Clear background configuration.
bool _localPlayerIsMoving
Track whether local player is currently moving (used for reconciliation logic)
void updateBackground(float deltaTime)
Update background scroll positions.
float _reconciliationThreshold
void setBackground(const std::string &mainBackground, const std::string ¶llaxBackground, float scrollSpeed, float parallaxSpeedFactor)
Set up background layers for parallax scrolling.
uint32_t _myEntityId
Local player's entity ID (for visual differentiation)
bool _clientSidePredictionEnabled
Client-side prediction enabled flag (for local player only)
void renderPlayer(const RenderableEntity &entity, bool isLocalPlayer)
Render a player entity.
void updateEntity(uint32_t id, RType::Messages::Shared::EntityType type, float x, float y, int health, const std::string ¤tAnimation, int srcX, int srcY, int srcW, int srcH, float velocityX=0.0f, float velocityY=0.0f, uint32_t serverTick=0)
Update or create an entity in the local cache.
float lerp(float start, float end, float t) const
Linear interpolation between two values.
float clamp(float value, float min, float max) const
Clamp a value between min and max.
bool _interpolationEnabled
Interpolation enabled flag.
void updateInterpolation(float deltaTime)
Update interpolation for all entities.
void clearAllEntities()
Clear all entities from the cache.
bool _backgroundActive
Whether backgrounds are configured and active.
void render()
Render all cached entities.
void renderEnemy(const RenderableEntity &entity)
Render an enemy entity.
bool _showDebugInfo
Debug mode: show entity IDs and health bars (toggle with F3)
BackgroundConfig _parallaxBackground
Parallax background layer (rendered on top, scrolls slower for depth effect)
Raylib implementation of the IGraphics interface.
void DrawRectFilled(int x, int y, int width, int height, unsigned int color) override
Draw a filled rectangle.
void DrawTextureEx(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX, float destY, float rotation, float scale, unsigned int tint) override
Draw a texture with advanced parameters (rotation, scale, source rectangle)
bool GetTextureSize(const char *textureName, int &width, int &height) const override
Get the dimensions of a loaded texture.
void UnloadTexture(const char *textureName) override
Unload a previously loaded texture.
void DrawTexturePro(const char *textureName, int srcX, int srcY, int srcW, int srcH, float destX, float destY, float destW, float destH, unsigned int tint) override
Draw a texture with separate width/height scaling (for non-uniform scaling)
void DrawRectangleLines(int x, int y, int width, int height, unsigned int color) override
Draw a rectangle outline (alias for DrawRect)
int GetWindowHeight() const override
Get current window height in pixels.
void DrawText(int fontHandle, const char *text, int x, int y, int fontSize, unsigned int color) override
Draw text using a loaded font.
int GetWindowWidth() const override
Get current window width in pixels.
int LoadTexture(const char *name, const char *filepath) override
Load a texture from an image file.
EntityType
Entity type enum - matches Cap'n Proto enum.
Configuration for scrolling parallax backgrounds.
int textureHeight
Texture height for tiling.
float scrollOffset
Current scroll offset (updates each frame)
bool loaded
Whether texture is loaded.
std::string textureName
Texture name (loaded texture)
int textureWidth
Texture width for tiling.
std::string texturePath
Asset path for texture loading.
float scrollSpeed
Scroll speed in pixels/second.
A single state snapshot with timestamp for time-based interpolation.
uint64_t timestamp
Local timestamp when received (milliseconds)
uint32_t serverTick
Server tick number.
float velocityX
Velocity X (for extrapolation)
float velocityY
Velocity Y (for extrapolation)
Cached entity state for rendering.
float interpolationFactor
Progress from 0.0 (prev) to 1.0 (target) (DEPRECATED)
int spriteSizeY
Sprite sheet size Y.
uint32_t entityId
Unique entity identifier.
int startPixelX
Sprite sheet start pixel X.
int offsetY
Sprite offset Y for rendering.
float prevY
Previous position Y (DEPRECATED)
float scale
Sprite scale multiplier.
float targetX
Target position X (DEPRECATED)
float targetY
Target position Y (DEPRECATED)
int offsetX
Sprite offset X for rendering.
float prevX
Previous position X (DEPRECATED)
float x
Current rendered position X.
std::string currentAnimation
Current animation name from server.
bool extrapolationEnabled
Allow extrapolation beyond last snapshot.
int health
Current health (-1 for entities without health)
int spriteSizeX
Sprite sheet size X.
std::deque< Snapshot > snapshots
Recent snapshots (max 3)
RType::Messages::Shared::EntityType type
Entity type (Player, Enemy, Bullet, etc.)
int currentFrame
Current animation frame.
uint64_t interpolationDelay
Time to look back for interpolation (ms)
int startPixelY
Sprite sheet start pixel Y.
float y
Current rendered position Y.