R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
position_history.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/math.hpp>
5#include <aegis2d/ecs/entity.hpp>
6#include <aegis2d/ecs/registry.hpp>
7
8#include <rtype/tuning.hpp>
9
10#include <cstddef>
11#include <cstdint>
12#include <deque>
13#include <optional>
14#include <utility>
15#include <vector>
16
17namespace rtype {
18
26public:
28 static constexpr std::size_t capacity = tuning::position_history_ticks;
29
37 void record(std::uint64_t tick, aegis2d::ecs::Registry& registry);
38
43 [[nodiscard]] std::optional<aegis2d::Vec2> position(aegis2d::ecs::Entity entity,
44 std::uint64_t tick) const;
45
50 [[nodiscard]] std::vector<std::pair<aegis2d::ecs::Entity, aegis2d::Vec2>>
51 enemies(std::uint64_t tick) const;
52
55 [[nodiscard]] std::optional<std::uint64_t> oldest() const noexcept;
56
59 [[nodiscard]] std::optional<std::uint64_t> newest() const noexcept;
60
63 [[nodiscard]] std::size_t size() const noexcept;
64
66 void clear() noexcept;
67
68private:
69 struct Frame {
70 std::uint64_t tick = 0;
71 // By increasing Entity::id(), for a binary search.
72 std::vector<std::pair<aegis2d::ecs::Entity, aegis2d::Vec2>> enemies;
73 };
74
75 [[nodiscard]] const Frame* frame(std::uint64_t tick) const noexcept;
76
77 std::deque<Frame> frames_;
78};
79
80} // namespace rtype
The positions of the enemies over the last tuning::position_history_ticks ticks.
Definition position_history.hpp:25
std::optional< aegis2d::Vec2 > position(aegis2d::ecs::Entity entity, std::uint64_t tick) const
Where an enemy was at a past tick.
std::vector< std::pair< aegis2d::ecs::Entity, aegis2d::Vec2 > > enemies(std::uint64_t tick) const
The enemies at a past tick.
void record(std::uint64_t tick, aegis2d::ecs::Registry &registry)
Keeps the positions of the enemies after a tick.
std::optional< std::uint64_t > newest() const noexcept
Newest tick kept.
std::optional< std::uint64_t > oldest() const noexcept
Oldest tick kept.
static constexpr std::size_t capacity
Ticks kept: 500 ms at 60 ticks per second.
Definition position_history.hpp:28
void clear() noexcept
Forgets every tick.
std::size_t size() const noexcept
Number of ticks kept.
constexpr std::uint32_t position_history_ticks
Ticks of enemy positions the server keeps, for lag compensation: 500 ms.
Definition tuning.hpp:22
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.