1 #ifndef MSP_GAME_COMPONENT_H_
2 #define MSP_GAME_COMPONENT_H_
4 #include <msp/time/timedelta.h>
5 #include "accessguard.h"
15 Handle<Entity> entity;
17 Component(Handle<Entity>);
19 virtual ~Component() = default;
21 Handle<Entity> get_entity() const { return entity; }
25 class BufferedComponent: public Component
32 uint8_t read_index = 0;
33 uint8_t write_index = 0;
36 BufferedComponent(Handle<Entity> e): Component(e) { }
38 const T &read() const;
42 virtual void prepare_tick() { write_index = 1-read_index; written = false; }
43 virtual void commit_tick() { if(written) read_index = write_index; }
47 const T &BufferedComponent<T>::read() const
50 AccessGuard::get_instance().check<AccessGuard::Read<T>>();
52 return data[read_index];
56 T &BufferedComponent<T>::write()
59 AccessGuard::get_instance().check<AccessGuard::Write<T>>();
61 if(!written && write_index!=read_index)
63 data[write_index] = data[read_index];
66 return data[write_index];
69 } // namespace Msp::Game