]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added physics system that updates entity positions and veocity in its own thread
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 7 Sep 2019 05:12:33 +0000 (01:12 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 7 Sep 2019 05:12:33 +0000 (01:12 -0400)
src/physics.cpp [new file with mode: 0644]
src/physics.hpp [new file with mode: 0644]

diff --git a/src/physics.cpp b/src/physics.cpp
new file mode 100644 (file)
index 0000000..31569ab
--- /dev/null
@@ -0,0 +1,64 @@
+/**
+ * @file physics.cpp
+ *
+ * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "physics.hpp"
+#include "components/Physics.hpp"
+#include "components/Position.hpp"
+#include "components/Velocity.hpp"
+
+void PhysicsSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
+                              [[maybe_unused]]entityx::EventManager& events)
+{
+
+}
+    
+void PhysicsSystem::update([[maybe_unused]]entityx::EntityManager& entities,
+                           [[maybe_unused]]entityx::EventManager& events,
+                           [[maybe_unused]]entityx::TimeDelta dt)
+{
+    entities.each<Position, Velocity>
+        ([dt](entityx::Entity e, Position &pos, Velocity &vel){
+
+        bool has_phys = e.has_component<Physics>();
+
+        pos.x += (vel.x * dt/1000.0);
+        pos.y += (vel.y * dt/1000.0);
+
+        double fallPosition = 0;
+
+        // TODO make this intersect world instead of 0 y
+        if (has_phys) {
+            Physics *p = e.component<Physics>().get();
+            // TODO only make this occur when the entity has a hitbox
+            if (pos.y == fallPosition) {
+                p->standing = true;
+                return;
+            }
+
+            if (pos.y < fallPosition) {
+                pos.y = fallPosition;
+                vel.y = 0;
+                p->standing = true;
+            } else {
+                p->standing = false;
+                vel.y -= 920 * (dt/1000.0f);
+            }
+        }
+    });
+}
diff --git a/src/physics.hpp b/src/physics.hpp
new file mode 100644 (file)
index 0000000..63443c4
--- /dev/null
@@ -0,0 +1,55 @@
+/**
+ * @file position.hpp
+ * Manages all Lua scripts.
+ *
+ * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SYSTEM_PHYSICS_HPP_
+#define SYSTEM_PHYSICS_HPP_
+
+#include <entityx/entityx.h>
+#include <sol/sol.hpp>
+
+/**
+ * @class PhysicsSystem
+ * Handles the position and velocity updating of all entities
+ */
+class PhysicsSystem : public entityx::System<PhysicsSystem>
+{
+private:
+public:
+    PhysicsSystem(void) {}
+
+    ~PhysicsSystem(void) {}
+
+    /**
+     * Prepares the system for running.
+     */
+    void configure(entityx::EntityManager& entities,
+                   entityx::EventManager& events) final;
+    
+    /**
+     * Updates the position of all entites, and if they have a physics component
+     * then we update their velocities accordingly.
+     */
+    void update(entityx::EntityManager& entites,
+                entityx::EventManager& events,
+                entityx::TimeDelta dt) final;
+};
+
+#endif // SYSTEM_PHYSICS_HPP_
+