R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
EntityRenderer.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** EntityRenderer.hpp - Specialized renderer for game entities
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <deque>
12#include <string>
13#include <unordered_map>
14#include <vector>
17
40 public:
54 struct Snapshot {
55 float x;
56 float y;
57 float velocityX;
58 float velocityY;
59 uint64_t timestamp;
60 uint32_t serverTick;
61 };
62
63 uint32_t entityId;
65 float x;
66 float y;
67 int health;
68
69 // Snapshot buffer for time-based interpolation (ring buffer of last 3 snapshots)
70 std::deque<Snapshot> snapshots;
73
74 // Legacy fields for backward compatibility (remove after migration)
75 float prevX;
76 float prevY;
77 float targetX;
78 float targetY;
80
85 int offsetX;
86 int offsetY;
87 float scale;
88 std::string currentAnimation;
89 std::vector<int>
92 };
93
107
115 explicit EntityRenderer(Graphics::RaylibGraphics &graphics);
116
120 ~EntityRenderer() = default;
121
144 void updateEntity(uint32_t id, RType::Messages::Shared::EntityType type, float x, float y, int health,
145 const std::string &currentAnimation, int srcX, int srcY, int srcW, int srcH,
146 float velocityX = 0.0f, float velocityY = 0.0f, uint32_t serverTick = 0);
147
155 void removeEntity(uint32_t id);
156
162 void clearAllEntities();
163
174 void setBackground(const std::string &mainBackground, const std::string &parallaxBackground,
175 float scrollSpeed, float parallaxSpeedFactor);
176
182 void clearBackground();
183
190 void updateBackground(float deltaTime);
191
202 void setMyEntityId(uint32_t id);
203
213 void render();
214
219 size_t getEntityCount() const { return _entities.size(); }
220
226 bool hasEntity(uint32_t id) const { return _entities.count(id) > 0; }
227
232 void setDebugMode(bool enabled) { _showDebugInfo = enabled; }
233
241 void setInterpolationEnabled(bool enabled) { _interpolationEnabled = enabled; }
242
250 void setInterpolationSpeed(float speed) { _interpolationSpeed = speed; }
251
260
276 void setReconciliationThreshold(float threshold) { _reconciliationThreshold = threshold; }
277
282 [[nodiscard]] float getReconciliationThreshold() const { return _reconciliationThreshold; }
283
291 void updateInterpolation(float deltaTime);
292
305 void moveEntityLocally(uint32_t entityId, float deltaX, float deltaY);
306
315 void setLocalPlayerMoving(bool moving) { _localPlayerIsMoving = moving; }
316
317 private:
326 void renderPlayer(const RenderableEntity &entity, bool isLocalPlayer);
327
332 void renderEnemy(const RenderableEntity &entity);
333
338 void renderProjectile(const RenderableEntity &entity);
339
344 void renderWall(const RenderableEntity &entity);
345
350 void renderOrbitalModule(const RenderableEntity &entity);
351
362 void renderHealthBar(float x, float y, int health, int maxHealth);
363
371 void renderDebugInfo(const RenderableEntity &entity);
372
379 void renderBackground();
380
388 [[nodiscard]] float lerp(float start, float end, float t) const;
389
397 [[nodiscard]] float clamp(float value, float min, float max) const;
398
403 [[nodiscard]] uint64_t getCurrentTimeMs() const;
404
406 std::unordered_map<uint32_t, RenderableEntity> _entities;
407
409 uint32_t _myEntityId = 0;
410
413
415 bool _showDebugInfo = true;
416
419
423 float _interpolationSpeed = 10.0f;
424
427
433
436
437 // ===== Background configuration =====
438
441
444
446 bool _backgroundActive = false;
447};
Specialized renderer for game entities with client-side interpolation.
void moveEntityLocally(uint32_t entityId, float deltaX, float deltaY)
Move an entity locally (client-side prediction)
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 setInterpolationSpeed(float speed)
Set the interpolation speed multiplier.
void setLocalPlayerMoving(bool moving)
Set whether the local player is currently moving.
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.
void clearBackground()
Clear background configuration.
~EntityRenderer()=default
Destructor.
bool _localPlayerIsMoving
Track whether local player is currently moving (used for reconciliation logic)
void updateBackground(float deltaTime)
Update background scroll positions.
void setBackground(const std::string &mainBackground, const std::string &parallaxBackground, float scrollSpeed, float parallaxSpeedFactor)
Set up background layers for parallax scrolling.
void setInterpolationEnabled(bool enabled)
Enable or disable interpolation for smooth movement.
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 &currentAnimation, 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.
void setDebugMode(bool enabled)
Toggle debug information overlay.
void setClientSidePredictionEnabled(bool enabled)
Enable or disable client-side prediction for local player.
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.
size_t getEntityCount() const
Get the number of entities currently cached.
bool _interpolationEnabled
Interpolation enabled flag.
void updateInterpolation(float deltaTime)
Update interpolation for all entities.
bool hasEntity(uint32_t id) const
Check if an entity exists in the cache.
void clearAllEntities()
Clear all entities from the cache.
bool _backgroundActive
Whether backgrounds are configured and active.
float getReconciliationThreshold() const
Get the current reconciliation threshold.
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)
void setReconciliationThreshold(float threshold)
Set the reconciliation threshold for client-side prediction.
BackgroundConfig _parallaxBackground
Parallax background layer (rendered on top, scrolls slower for depth effect)
Raylib implementation of the IGraphics interface.
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)
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.
std::vector< int > animationFrameIndices
Animation frame sequence (sprite indices to allow freedom of picking frames manually)
uint64_t interpolationDelay
Time to look back for interpolation (ms)
int startPixelY
Sprite sheet start pixel Y.
float y
Current rendered position Y.