]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Physics and world collide
authorAndy <drumsetmonkey@gmail.com>
Wed, 26 Oct 2016 13:27:40 +0000 (09:27 -0400)
committerAndy <drumsetmonkey@gmail.com>
Wed, 26 Oct 2016 13:27:40 +0000 (09:27 -0400)
src/player.cpp
src/world.cpp
xml/entities.xml

index 59274b37f677ea789c6a161976d0788783d47b5f..37a958da1bbc7d0808cc4ede26d4241586014979 100644 (file)
@@ -12,7 +12,9 @@ void PlayerSystem::create(void)
        player = game::entities.create();
        player.assign<Position>(0.0f, 100.0f);
        player.assign<Direction>(0.0f, 0.0f);
-       player.assign<Physics>(-0.001f);
+       // The new g value is a multiplier for the gravity constant. This allows for air resistance simulation.
+       //player.assign<Physics>(-0.001f);
+       player.assign<Physics>(1);
        player.assign<Visible>(-0.2f);
 
        auto sprite = player.assign<Sprite>();
index 365345db90b7579fe51d34dbcfcc1ae9b1bd5a89..8e2d60b8f61e3c2290e57a649930ba3e14d7af02 100644 (file)
@@ -370,6 +370,31 @@ void WorldSystem::load(const std::string& file)
                                                
                                                float cdat[2] = {dim.x, dim.y};
                                                entity.assign<Solid>(cdat[0], cdat[1]);
+                                       } else if (tname == "Direction") {
+                                               vec2 dir;
+
+                                               if (wxml->Attribute("direction") != nullptr) {
+                                                       dir = str2coord(wxml->StrAttribute("direction"));
+                                               } else if (wxml->Attribute("value") != nullptr) {
+                                                       dir = str2coord(wxml->StrAttribute("value"));   
+                                               } else {
+                                                       dir = vec2(0,0);
+                                               }
+
+                                               float cdat[2] = {dir.x, dir.y};
+                                               entity.assign<Direction>(cdat[0], cdat[1]);
+                                       } else if (tname == "Physics") {
+                                               float g;
+
+                                               if (wxml->Attribute("gravity") != nullptr) {
+                                                       g = wxml->FloatAttribute("gravity");
+                                               } else if (wxml->Attribute("value") != nullptr) {
+                                                       g = wxml->FloatAttribute("value");      
+                                               } else {
+                                                       g = 1.0f;
+                                               }
+                                               
+                                               entity.assign<Physics>(g);
                                        }
 
                                        abcd = abcd->NextSiblingElement();
@@ -1013,10 +1038,18 @@ void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
 
 void WorldSystem::detect(entityx::TimeDelta dt)
 {
+       game::entities.each<Direction, Physics>(
+               [&](entityx::Entity e, Direction &vel, Physics &phys) {
+                       (void)e;
+                       // handle gravity
+               if (vel.y > -2.0f) {
+                               vel.y -= (GRAVITY_CONSTANT * phys.g) * dt;
+                       }
+               });
+
        game::entities.each<Position, Direction, Solid>(
            [&](entityx::Entity e, Position &loc, Direction &vel, Solid &dim) {
                (void)e;
-
                //if (health.health <= 0)
                //      UserError("die mofo");
 
@@ -1039,11 +1072,7 @@ void WorldSystem::detect(entityx::TimeDelta dt)
                                // TODO ground flag
                        }
                }
-
-        // handle gravity
-        else if (vel.y > -2.0f) {
-                       vel.y -= GRAVITY_CONSTANT * dt;
-               }
+        
 
                // insure that the entity doesn't fall off either edge of the world.
         if (loc.x < world.startX) {
index 306085b5b1f21c77ad4a5a0fd8190b9a65aa51a4..cf6dbf3554cb0eab131690a51771edbe55c90e54 100644 (file)
@@ -4,7 +4,9 @@
        <Position value="0.0,100.0" />
        <Visible value="0.2" />
        <Sprite image="assets/NPC.png" />
+       <Direction />
        <Solid />
+       <Physics />
 </npc>
 
 <structure>