1 #ifndef MSP_GAME_COMPONENT_H_
2 #define MSP_GAME_COMPONENT_H_
4 #include <msp/time/timedelta.h>
5 #include "accessguard.h"
7 #include "mspgame_api.h"
13 class MSPGAME_API Component
16 Handle<Entity> entity;
18 Component(Handle<Entity>);
20 virtual ~Component() = default;
22 Handle<Entity> get_entity() const { return entity; }
26 class BufferedComponent: public Component
33 uint8_t read_index = 0;
34 uint8_t write_index = 0;
37 BufferedComponent(Handle<Entity> e): Component(e) { }
39 const T &read() const;
43 virtual void prepare_tick() { write_index = 1-read_index; written = false; }
44 virtual void commit_tick() { if(written) read_index = write_index; }
48 const T &BufferedComponent<T>::read() const
51 AccessGuard::get_instance().check<AccessGuard::Read<T>>();
53 return data[read_index];
57 T &BufferedComponent<T>::write()
60 AccessGuard::get_instance().check<AccessGuard::Write<T>>();
62 if(!written && write_index!=read_index)
64 data[write_index] = data[read_index];
67 return data[write_index];
70 } // namespace Msp::Game