-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.cpp
84 lines (67 loc) · 2.13 KB
/
time.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "time.h"
#include "config.h"
#include "common.h"
void TimeSystem::activate()
{
auto& cal = AccessWorld_UseUnique<Calendar>::access_unique();
cal.day = 1;
cal.hour = 7;
cal.minute = 0;
ActionCompleteSignal signal{ 0 };
react_to_event(signal);
}
void TimeSystem::react_to_event(ActionCompleteSignal& signal)
{
auto& q = AccessWorld_UseUnique<TurnOrderQueue>::access_unique();
auto& current_in_order = AccessWorld_UseUnique<CurrentInTurn>::access_unique();
auto current_entity = current_in_order.current;
if (!AccessWorld_QueryComponent<ActionPoints>::has_component(current_entity)
|| !AccessWorld_QueryComponent<Speed>::has_component(current_entity))
{
current_entity = entt::null;
while (!q.order.empty())
{
q.order.pop();
}
}
if (current_entity != entt::null)
{
auto& points = AccessWorld_QueryComponent<ActionPoints>::get_component(current_entity);
auto& speed = AccessWorld_QueryComponent<Speed>::get_component(current_entity);
points.ap -= signal.cost;
if (points.ap > 0)
{
q.order.emplace(std::tuple<ActionPoints, Speed, Entity>(points, speed, current_entity));
}
}
if (q.order.empty())
{
for (auto&& [e, ap, s] : AccessWorld_QueryAllEntitiesWith<ActionPoints, Speed>::query().each())
{
ap.ap += ACTION_POINTS_PER_TURN + (AccessWorld_QueryComponent<Player>::has_component(e) ? ACTION_POINTS_PLAYER_BONUS : 0);
q.order.emplace(std::tuple<ActionPoints, Speed, Entity>{ ap, s, e });
}
AccessEvents_Emit<CalendarUpdateSignal>::emit_event();
}
const auto top = q.order.top();
current_in_order.current = std::get<2>(top);
q.order.pop();
AccessEvents_Emit<AwaitingActionSignal>::emit_event(AwaitingActionSignal{ current_in_order.current });
}
void TimeSystem::react_to_event(CalendarUpdateSignal&)
{
auto& cal = AccessWorld_UseUnique<Calendar>::access_unique();
cal.minute++;
if (cal.minute > 60)
{
cal.minute = 0;
cal.hour++;
AccessEvents_Emit<HourPassedSignal>::emit_event();
if (cal.hour > 24)
{
cal.hour = 0;
cal.day++;
AccessEvents_Emit<DayPassedSignal>::emit_event();
}
}
}