]> git.tdb.fi Git - libs/game.git/commitdiff
Convert components to buffered where appropriate
authorMikko Rasa <tdb@tdb.fi>
Sun, 4 Dec 2022 15:12:59 +0000 (17:12 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 4 Dec 2022 20:20:09 +0000 (22:20 +0200)
13 files changed:
examples/bassteroids/source/damagedealer.cpp
examples/bassteroids/source/hitpoints.cpp
examples/bassteroids/source/hitpoints.h
examples/bassteroids/source/physics.cpp
examples/bassteroids/source/playercontroller.cpp
examples/bassteroids/source/rigidbody.cpp
examples/bassteroids/source/rigidbody.h
source/game/camera.cpp
source/game/camera.h
source/game/transform.cpp
source/game/transform.h
source/game/transformpropagator.cpp
source/gameview/renderer.cpp

index 37be786fc1d5aceb7924ec4dfaa2f28da2319624..7c8e873049d9415ceee3318cd8f01db96860f0dc 100644 (file)
@@ -9,6 +9,9 @@ DamageDealer::DamageDealer(Game::Stage &s):
        System(s),
        observer(stage.get_event_bus())
 {
+       declare_dependency<HitPoints>(UPDATE);
+       declare_dependency<DamageSource>(READ_OLD);
+
        observer.observe<Events::Collision>([this](auto &e){ collision(e); });
 }
 
index ccd0441b45eccdaf4d4787e799353eae760ac3f3..c7ece0d1b00eec13794add4268c92b772bdccf84 100644 (file)
@@ -3,15 +3,17 @@
 using namespace Msp;
 
 HitPoints::HitPoints(Game::Handle<Game::Entity> e, const Setup &s):
-       Component(e),
-       setup(s),
-       remaining_hits(setup.max_hits)
-{ }
+       BufferedComponent<HitPointsData>(e),
+       setup(s)
+{
+       write().remaining_hits = setup.max_hits;
+}
 
 void HitPoints::take_damage(unsigned amount, unsigned type)
 {
        if(!(setup.vulnerable_to&(1<<type)))
                return;
 
-       remaining_hits = (amount<remaining_hits ? remaining_hits-amount : 0);
+       Data &d = write();
+       d.remaining_hits = (amount<d.remaining_hits ? d.remaining_hits-amount : 0);
 }
index a11d961783674e1b13b5043ca74c840cf4003525..f6000d7abab3c6dc54782d4fc2c413817c8263e8 100644 (file)
@@ -13,21 +13,25 @@ struct HitPointsSetup
        unsigned vulnerable_to = ~0U;
 };
 
-class HitPoints: public Msp::Game::Component
+struct HitPointsData
+{
+       unsigned remaining_hits;
+};
+
+class HitPoints: public Msp::Game::BufferedComponent<HitPointsData>
 {
 public:
        using Setup = HitPointsSetup;
 
 private:
        const Setup &setup;
-       unsigned remaining_hits;
 
 public:
        HitPoints(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
 
        void take_damage(unsigned, unsigned);
 
-       bool is_alive() const { return remaining_hits; }
+       bool is_alive() const { return read().remaining_hits; }
 };
 
 #endif
index 73d858295db7df316f55a7e8396c9312e09c5c81..158b745ca83f6e0af19597e7fc7e4df9876e8365 100644 (file)
@@ -11,6 +11,10 @@ Physics::Physics(Game::Stage &s):
        event_source(stage.get_event_bus()),
        observer(stage.get_event_bus())
 {
+       declare_dependency<Game::Transform>(UPDATE);
+       declare_dependency<RigidBody>(UPDATE);
+       declare_dependency<Collider>(READ_OLD);
+
        observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_added(e); });
        observer.observe<Game::Events::EntityDestroyed>([this](auto &e){ entity_removed(e); });
 
index cd0c62bad8594b77d0557a299cb964e71b3ef514..b90992ed6239b191645476f48414e63ec7785b3e 100644 (file)
@@ -14,7 +14,10 @@ PlayerController::PlayerController(Game::Stage &s):
        bullet_setup{ .physical={ .body={ .mass=0.05f, .moment_of_inertia=0.04f }, .collider={ .type=ColliderType::CIRCLE, .radius=0.2f }},
                .hittable={ .damaging=true, .hits={ .max_hits=1 }, .damage={ .amount=1, .type=0 }},
                .mesh={ .object_name="Quaver.object" }}
-{ }
+{
+       declare_dependency<Game::Transform>(READ_OLD);
+       declare_dependency<RigidBody>(UPDATE);
+}
 
 void PlayerController::set_controls(Controls *c)
 {
index 391707a574ee40b2e8b36f8fd098961aab95ab4f..d677fb7ab778c0964cd777d593d58e179ce5987f 100644 (file)
@@ -5,39 +5,41 @@
 using namespace Msp;
 
 RigidBody::RigidBody(Game::Handle<Game::Entity> e, const Setup &s):
-       Component(e),
+       BufferedComponent<RigidBodyData>(e),
        setup(s)
 { }
 
 void RigidBody::set_velocity(const LinAl::Vector<float, 2> &v)
 {
-       velocity = v;
+       write().velocity = v;
 }
 
 void RigidBody::set_angular_velocity(Geometry::Angle<float> as)
 {
-       angular_velocity = as;
+       write().angular_velocity = as;
 }
 
 void RigidBody::add_force(const LinAl::Vector<float, 2> &f)
 {
-       force += f;
+       write().force += f;
 }
 
 void RigidBody::add_force(const LinAl::Vector<float, 2> &f, const LinAl::Vector<float, 2> &p)
 {
-       force += f;
+       Data &d = write();
+       d.force += f;
        LinAl::Vector<float, 2> r = p-entity->get_transform()->get_position().slice<2>(0);
-       torque += r.x*f.y-r.y*f.x;
+       d.torque += r.x*f.y-r.y*f.x;
 }
 
 void RigidBody::add_torque(float t)
 {
-       torque += t;
+       write().torque += t;
 }
 
 void RigidBody::clear_forces()
 {
-       force = LinAl::Vector<float, 2>();
-       torque = 0.0f;
+       Data &d = write();
+       d.force = LinAl::Vector<float, 2>();
+       d.torque = 0.0f;
 }
index aaffdc03c19eec0f18d131d88dd773d6aa8633d6..b75d034ab98e03b42a4beb81cb08d21532d1f4b4 100644 (file)
@@ -11,17 +11,21 @@ struct RigidBodySetup
        float moment_of_inertia = 0.5f;
 };
 
-class RigidBody: public Msp::Game::Component
+struct RigidBodyData
+{
+       Msp::LinAl::Vector<float, 2> velocity;
+       Msp::Geometry::Angle<float> angular_velocity;
+       Msp::LinAl::Vector<float, 2> force;
+       float torque = 0.0f;
+};
+
+class RigidBody: public Msp::Game::BufferedComponent<RigidBodyData>
 {
 public:
        using Setup = RigidBodySetup;
 
 private:
        const Setup &setup;
-       Msp::LinAl::Vector<float, 2> velocity;
-       Msp::Geometry::Angle<float> angular_velocity;
-       Msp::LinAl::Vector<float, 2> force;
-       float torque = 0.0f;
 
 public:
        RigidBody(Msp::Game::Handle<Msp::Game::Entity>, const Setup &);
@@ -34,10 +38,10 @@ public:
        void add_force(const Msp::LinAl::Vector<float, 2> &, const Msp::LinAl::Vector<float, 2> &);
        void add_torque(float);
        void clear_forces();
-       const Msp::LinAl::Vector<float, 2> &get_velocity() const { return velocity; }
-       Msp::Geometry::Angle<float> get_angular_velocity() const { return angular_velocity; }
-       const Msp::LinAl::Vector<float, 2> &get_force() const { return force; }
-       float get_torque() const { return torque; }
+       const Msp::LinAl::Vector<float, 2> &get_velocity() const { return read().velocity; }
+       Msp::Geometry::Angle<float> get_angular_velocity() const { return read().angular_velocity; }
+       const Msp::LinAl::Vector<float, 2> &get_force() const { return read().force; }
+       float get_torque() const { return read().torque; }
 };
 
 #endif
index 37c0f4db466e62610dc0d3ec7930705ec7fc8207..02880cec8d3a223691f785b02aa183ec96f77c3c 100644 (file)
@@ -5,32 +5,36 @@ using namespace std;
 namespace Msp::Game {
 
 Camera::Camera(Handle<Entity> e, const CameraSetup &s):
-       Component(e),
-       setup(s),
-       fov_y(setup.field_of_view_y),
-       size(setup.size),
-       near_clip(setup.near_clip),
-       far_clip(setup.far_clip)
+       BufferedComponent<CameraData>(e),
+       setup(s)
 {
-       if(!is_orthographic())
-               size = { get_aspect(), 1.0f };
+       CameraData &d = write();
+       d.fov_y = setup.field_of_view_y;
+       d.size = setup.size;
+       d.near_clip = setup.near_clip;
+       d.far_clip = setup.far_clip;
+
+       if(!d.is_orthographic())
+               d.size = { d.get_aspect(), 1.0f };
 }
 
 void Camera::set_field_of_view(Geometry::Angle<float> f, float a)
 {
-       if(is_orthographic())
+       Data &d = write();
+       if(d.is_orthographic())
                throw logic_error("Camera is not perspective");
 
-       fov_y = f;
-       size = { a, 1.0f };
+       d.fov_y = f;
+       d.size = { a, 1.0f };
 }
 
 void Camera::set_size(const LinAl::Vector<float, 2> &s)
 {
-       if(!is_orthographic())
+       Data &d = write();
+       if(!d.is_orthographic())
                throw logic_error("Camera is not orthographic");
 
-       size = s;
+       d.size = s;
 }
 
 } // namespace Msp::Game
index 1e8efdd5e85527b536e6d9d87bbcca1f8afedf59..f6df873c1120586c04e6ce6fb3cfb80d55e15159 100644 (file)
@@ -25,31 +25,39 @@ struct CameraSetup
        std::string sequence_name;
 };
 
-class Camera: public Component
+struct CameraData
 {
-public:
-       using Setup = CameraSetup;
-
-private:
-       const Setup &setup;
        Geometry::Angle<float> fov_y;
        LinAl::Vector<float, 2> size;
        float height;
        float near_clip;
        float far_clip;
 
+       bool is_orthographic() const { return fov_y==Geometry::Angle<float>::zero(); }
+       Geometry::Angle<float> get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; }
+       float get_aspect() const { return size.x/size.y; }
+};
+
+class Camera: public BufferedComponent<CameraData>
+{
+public:
+       using Setup = CameraSetup;
+
+private:
+       const Setup &setup;
+
 public:
        Camera(Handle<Entity>, const Setup &);
 
        void set_field_of_view(Geometry::Angle<float>, float);
        void set_size(const LinAl::Vector<float, 2> &);
-       bool is_orthographic() const { return fov_y==Geometry::Angle<float>::zero(); }
-       Geometry::Angle<float> get_fov_vertical() const { return fov_y; }
-       Geometry::Angle<float> get_fov_horizontal() const { return Geometry::atan(tan(fov_y/2.0f)*get_aspect())*2.0f; }
-       const LinAl::Vector<float, 2> &get_size() const { return size; }
-       float get_aspect() const { return size.x/size.y; }
-       float get_near_clip() const { return near_clip; }
-       float get_far_clip() const { return far_clip; }
+       bool is_orthographic() const { return read().is_orthographic(); }
+       Geometry::Angle<float> get_fov_vertical() const { return read().fov_y; }
+       Geometry::Angle<float> get_fov_horizontal() const { return read().get_fov_horizontal(); }
+       const LinAl::Vector<float, 2> &get_size() const { return read().size; }
+       float get_aspect() const { return read().get_aspect(); }
+       float get_near_clip() const { return read().near_clip; }
+       float get_far_clip() const { return read().far_clip; }
        CameraScaling get_scaling() const { return setup.scaling; }
        const std::string &get_sequence_name() const { return setup.sequence_name; }
 };
index e5655f929765554f87be052a9d23647051e1b617..5b55ab2dc4f05c0833196d549f77405e7f43d26b 100644 (file)
@@ -4,39 +4,40 @@
 namespace Msp::Game {
 
 Transform::Transform(Handle<Entity> e):
-       Component(e)
+       BufferedComponent<TransformData>(e)
 { }
 
 void Transform::set_values(const TransformValues &v)
 {
-       values = v;
+       write().values = v;
 }
 
 void Transform::set_position(const LinAl::Vector<float, 3> &p)
 {
-       values.position = p;
+       write().values.position = p;
 }
 
 void Transform::set_rotation(const Geometry::Quaternion<float> &r)
 {
-       values.rotation = normalize(r);
+       write().values.rotation = normalize(r);
 }
 
 void Transform::set_scale(const LinAl::Vector<float, 3> &s)
 {
-       values.scale = s;
+       write().values.scale = s;
 }
 
 void Transform::update_world_matrix(const Transform *parent)
 {
        using Affine = Geometry::AffineTransform<float, 3>;
 
-       local_matrix = Affine::translation(values.position)*
-               Affine::rotation(values.rotation)*Affine::scaling(values.scale);
+       Data &d = write();
+       d.local_matrix = Affine::translation(d.values.position)*
+               Affine::rotation(d.values.rotation)*Affine::scaling(d.values.scale);
        if(parent)
-               world_matrix = parent->get_world_matrix()*local_matrix;
+               d.world_matrix = parent->get_world_matrix()*d.local_matrix;
        else
-               world_matrix = local_matrix;
+               d.world_matrix = d.local_matrix;
 }
 
 } // namespace Msp::Game
index b1766aeb993292d576acb3abf23e7be9ec68fcee..1239546be803dec1c89f23a63e4f19bb6a252cf1 100644 (file)
@@ -22,13 +22,15 @@ struct TransformValues
        { }
 };
 
-class Transform: public Component
+struct TransformData
 {
-private:
        TransformValues values;
        LinAl::Matrix<float, 4, 4> local_matrix = LinAl::Matrix<float, 4, 4>::identity();
        LinAl::Matrix<float, 4, 4> world_matrix = LinAl::Matrix<float, 4, 4>::identity();
+};
 
+class Transform: public BufferedComponent<TransformData>
+{
 public:
        Transform(Handle<Entity>);
 
@@ -36,11 +38,11 @@ public:
        void set_position(const LinAl::Vector<float, 3> &);
        void set_rotation(const Geometry::Quaternion<float> &);
        void set_scale(const LinAl::Vector<float, 3> &);
-       const TransformValues &get_values() const { return values; }
-       const LinAl::Vector<float, 3> &get_position() const { return values.position; }
-       const Geometry::Quaternion<float> &get_rotation() const { return values.rotation; }
-       const LinAl::Vector<float, 3> &get_scale() const { return values.scale; }
-       const LinAl::Matrix<float, 4, 4> &get_world_matrix() const { return world_matrix; }
+       const TransformValues &get_values() const { return read().values; }
+       const LinAl::Vector<float, 3> &get_position() const { return read().values.position; }
+       const Geometry::Quaternion<float> &get_rotation() const { return read().values.rotation; }
+       const LinAl::Vector<float, 3> &get_scale() const { return read().values.scale; }
+       const LinAl::Matrix<float, 4, 4> &get_world_matrix() const { return read().world_matrix; }
 
        void update_world_matrix(const Transform *);
 };
index 631e5292bb62eaae215d55f3dd0af183f05c3348..cadac04eee8133778ad2055c0422bf19d4b71d9c 100644 (file)
@@ -12,6 +12,8 @@ TransformPropagator::TransformPropagator(Stage &s):
        System(s),
        observer(s.get_event_bus())
 {
+       declare_dependency<Transform>(CHAINED_UPDATE);
+
        observer.observe<Events::EntityCreated>([this](auto &){ transforms_dirty = true; });
        observer.observe<Events::EntityDestroyed>([this](auto &){ transforms_dirty = true; });
 }
index ca4e8a91323af48ed16ba2fc2ef2ac0cab9c1e62..5ddfec18fa3b3ae5d136e1def3c0dd5a092d2035 100644 (file)
@@ -15,6 +15,10 @@ Renderer::Renderer(Game::Stage &s, GL::View &v):
        view(v),
        event_observer(s.get_event_bus())
 {
+       declare_dependency<Game::Transform>(READ_FRESH);
+       declare_dependency<Game::Camera>(READ_FRESH);
+       declare_dependency<MeshRenderer>(WRITE);
+
        event_observer.observe<Game::Events::EntityCreated>([this](auto &e){ entity_created(e); });
        event_observer.observe<Game::Events::EntityDestroyed>([this](auto &e){ entity_destroyed(e); });
        event_observer.observe<Game::Events::CameraChanged>([this](auto &e){ camera_changed(e); });