From: Andy Date: Wed, 26 Oct 2016 13:27:40 +0000 (-0400) Subject: Physics and world collide X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=0fa2320e978926db6781a2bdcdf5a9b6f0317e93;p=clyne%2Fgamedev.git Physics and world collide --- diff --git a/src/player.cpp b/src/player.cpp index 59274b3..37a958d 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -12,7 +12,9 @@ void PlayerSystem::create(void) player = game::entities.create(); player.assign(0.0f, 100.0f); player.assign(0.0f, 0.0f); - player.assign(-0.001f); + // The new g value is a multiplier for the gravity constant. This allows for air resistance simulation. + //player.assign(-0.001f); + player.assign(1); player.assign(-0.2f); auto sprite = player.assign(); diff --git a/src/world.cpp b/src/world.cpp index 365345d..8e2d60b 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -370,6 +370,31 @@ void WorldSystem::load(const std::string& file) float cdat[2] = {dim.x, dim.y}; entity.assign(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(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(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( + [&](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( [&](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) { diff --git a/xml/entities.xml b/xml/entities.xml index 306085b..cf6dbf3 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -4,7 +4,9 @@ + +