VIPRA Documentation
Loading...
Searching...
No Matches
event.hpp
1#pragma once
2
3#include <optional>
4
5#include "vipra/logging/logging.hpp"
6#include "vipra/types/idx.hpp"
7
8#include "vipra/vipra_behaviors/conditions/condition.hpp"
9#include "vipra/vipra_behaviors/events/event_status.hpp"
10#include "vipra/vipra_behaviors/util/bool_latch.hpp"
11#include "vipra/vipra_behaviors/util/timed_latch.hpp"
12
13namespace VIPRA::Behaviors {
14
19class Event {
20 DEFAULT_CONSTRUCTIBLE(Event)
21 COPYABLE(Event)
22 MOVEABLE(Event)
23
24 public:
25 explicit Event(std::string name) : _name(std::move(name)) {}
26
27 void evaluate(auto pack);
28
29 void set_start_condition(Condition const& condition) { _startCondition = condition; }
30 void set_end_condition(Condition const& condition) { _endCondition = condition; }
31
32 [[nodiscard]] auto is_occurring() const -> bool
33 {
34 return _status == EventStatus::OCCURRING || _status == EventStatus::STARTING;
35 }
36 [[nodiscard]] auto has_occurred() const -> bool { return _occurred; }
37 [[nodiscard]] auto is_starting() const -> bool
38 {
39 return _status == EventStatus::STARTING;
40 }
41 [[nodiscard]] auto is_ending() const -> bool { return _status == EventStatus::ENDING; }
42
43 void set_status(EventStatus status) { _status = status; }
44 [[nodiscard]] auto get_status() const -> EventStatus const& { return _status; }
45
46 [[nodiscard]] auto get_name() const -> std::string const& { return _name; }
47
48 private:
49 std::string _name;
50 EventStatus _status{EventStatus::NOT_OCCURRING};
51 bool _occurred = false;
52
53 Latch _latch;
54 Condition _startCondition;
55 std::optional<Condition> _endCondition;
56};
57
58void Event::evaluate(auto pack)
59{
60 if ( _status == EventStatus::ENDING ) {
61 _status = EventStatus::NOT_OCCURRING;
62 }
63 if ( _status == EventStatus::STARTING ) {
64 _status = EventStatus::OCCURRING;
65 }
66
67 std::vector<VIPRA::idx> peds{0};
68 std::vector<bool> met{false};
69 std::optional<TimedLatchCollection> temp;
70
71 // TODO (rolland) : These might need target selectors?
72 if ( _status == EventStatus::OCCURRING ) {
73 if ( _endCondition ) {
74 _endCondition.value().evaluate(pack, peds, met, {}, temp);
75 if ( met[0] ) {
76 VIPRA::Log::debug("Event {} is Ending", _name);
77 _status = EventStatus::ENDING;
78 }
79 }
80
81 return;
82 }
83
84 _startCondition.evaluate(pack, peds, met, {}, temp);
85 if ( met[0] ) {
86 VIPRA::Log::debug("Event {} is Starting", _name);
87 _occurred = true;
88 _status = EventStatus::STARTING;
89 }
90}
91} // namespace VIPRA::Behaviors
Definition condition.hpp:14
Literally a boolean with extra syntax.
Definition bool_latch.hpp:10
static VIPRA_INLINE void debug(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:85