summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-12-20 18:57:23 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-12-20 18:57:23 -0500
commit62069b31c7d23055f999c70a58ccf7d58acd333f (patch)
tree7ea4c57e74d978a297d34cf61d9c85db846e8900 /src
parent90aadf3844386824140a20d7fbb847bc16009a94 (diff)
downloadtherapy-62069b31c7d23055f999c70a58ccf7d58acd333f.tar.gz
therapy-62069b31c7d23055f999c70a58ccf7d58acd333f.tar.bz2
therapy-62069b31c7d23055f999c70a58ccf7d58acd333f.zip
Added target velocity HEAD master
The acceleration of a Ponderable entity is now only really a magnitude. The direction of acceleration is such that the velocity goes toward the target velocity. If accelerating would cause the velocity to pass the target velocity, it is instead set to the target.

This is currently being used to 1) generalize terminal velocity due to gravity, and 2) allow an Orientable entity to accelerate quickly to a walking speed (and to a halt) rather than instantly achieve it.
Diffstat (limited to 'src')
-rw-r--r--src/components/ponderable.h5
-rw-r--r--src/systems/orienting.cpp32
-rw-r--r--src/systems/playing.cpp5
-rw-r--r--src/systems/pondering.cpp47
4 files changed, 61 insertions, 28 deletions
diff --git a/src/components/ponderable.h b/src/components/ponderable.h index 221d267..cc42048 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h
@@ -56,6 +56,11 @@ public:
56 vec2d accel = { 0.0, 0.0 }; 56 vec2d accel = { 0.0, 0.0 };
57 57
58 /** 58 /**
59 * The target velocity of the body.
60 */
61 vec2d targetVel = { 0.0, 0.0 };
62
63 /**
59 * The type of physical body that the entity is meant to assume. The body will 64 * The type of physical body that the entity is meant to assume. The body will
60 * be acted upon differently based on this. See the enumeration above for more 65 * be acted upon differently based on this. See the enumeration above for more
61 * details. 66 * details.
diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp index d73ddd2..a2be34f 100644 --- a/src/systems/orienting.cpp +++ b/src/systems/orienting.cpp
@@ -20,30 +20,6 @@ void OrientingSystem::tick(double)
20 auto& ponderable = game_.getEntityManager(). 20 auto& ponderable = game_.getEntityManager().
21 getComponent<PonderableComponent>(entity); 21 getComponent<PonderableComponent>(entity);
22 22
23 switch (orientable.getWalkState())
24 {
25 case OrientableComponent::WalkState::still:
26 {
27 ponderable.vel.x() = 0.0;
28
29 break;
30 }
31
32 case OrientableComponent::WalkState::left:
33 {
34 ponderable.vel.x() = -WALK_SPEED;
35
36 break;
37 }
38
39 case OrientableComponent::WalkState::right:
40 {
41 ponderable.vel.x() = WALK_SPEED;
42
43 break;
44 }
45 }
46
47 if (orientable.isJumping() && (ponderable.vel.y() > 0)) 23 if (orientable.isJumping() && (ponderable.vel.y() > 0))
48 { 24 {
49 orientable.setJumping(false); 25 orientable.setJumping(false);
@@ -62,6 +38,8 @@ void OrientingSystem::moveLeft(id_type entity)
62 orientable.setFacingRight(false); 38 orientable.setFacingRight(false);
63 orientable.setWalkState(OrientableComponent::WalkState::left); 39 orientable.setWalkState(OrientableComponent::WalkState::left);
64 40
41 ponderable.targetVel.x() = -WALK_SPEED;
42
65 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 43 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
66 if (ponderable.grounded) 44 if (ponderable.grounded)
67 { 45 {
@@ -82,6 +60,8 @@ void OrientingSystem::moveRight(id_type entity)
82 orientable.setFacingRight(true); 60 orientable.setFacingRight(true);
83 orientable.setWalkState(OrientableComponent::WalkState::right); 61 orientable.setWalkState(OrientableComponent::WalkState::right);
84 62
63 ponderable.targetVel.x() = WALK_SPEED;
64
85 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 65 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
86 if (ponderable.grounded) 66 if (ponderable.grounded)
87 { 67 {
@@ -93,10 +73,14 @@ void OrientingSystem::moveRight(id_type entity)
93 73
94void OrientingSystem::stopWalking(id_type entity) 74void OrientingSystem::stopWalking(id_type entity)
95{ 75{
76 auto& ponderable = game_.getEntityManager().
77 getComponent<PonderableComponent>(entity);
78
96 auto& orientable = game_.getEntityManager(). 79 auto& orientable = game_.getEntityManager().
97 getComponent<OrientableComponent>(entity); 80 getComponent<OrientableComponent>(entity);
98 81
99 orientable.setWalkState(OrientableComponent::WalkState::still); 82 orientable.setWalkState(OrientableComponent::WalkState::still);
83 ponderable.targetVel.x() = 0;
100 84
101 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>(); 85 auto& animating = game_.getSystemManager().getSystem<AnimatingSystem>();
102 86
diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp index 6652099..83f65d6 100644 --- a/src/systems/playing.cpp +++ b/src/systems/playing.cpp
@@ -5,6 +5,7 @@
5#include "components/playable.h" 5#include "components/playable.h"
6#include "components/controllable.h" 6#include "components/controllable.h"
7#include "components/orientable.h" 7#include "components/orientable.h"
8#include "components/ponderable.h"
8#include "systems/mapping.h" 9#include "systems/mapping.h"
9#include "systems/pondering.h" 10#include "systems/pondering.h"
10#include "systems/orienting.h" 11#include "systems/orienting.h"
@@ -46,6 +47,10 @@ void PlayingSystem::initPlayer()
46 player, 47 player,
47 PonderableComponent::Type::freefalling); 48 PonderableComponent::Type::freefalling);
48 49
50 auto& ponderable = game_.getEntityManager().
51 getComponent<PonderableComponent>(player);
52 ponderable.accel.x() = 720;
53
49 game_.getEntityManager().emplaceComponent<ControllableComponent>(player); 54 game_.getEntityManager().emplaceComponent<ControllableComponent>(player);
50 game_.getEntityManager().emplaceComponent<OrientableComponent>(player); 55 game_.getEntityManager().emplaceComponent<OrientableComponent>(player);
51 56
diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index d841679..649a03a 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp
@@ -44,6 +44,7 @@ void PonderingSystem::initializeBody(
44 44
45 if (type == PonderableComponent::Type::freefalling) 45 if (type == PonderableComponent::Type::freefalling)
46 { 46 {
47 ponderable.targetVel.y() = TERMINAL_VELOCITY;
47 ponderable.accel.y() = NORMAL_GRAVITY; 48 ponderable.accel.y() = NORMAL_GRAVITY;
48 } 49 }
49} 50}
@@ -57,6 +58,8 @@ void PonderingSystem::initPrototype(id_type prototype)
57 ponderable.vel.y() = 0.0; 58 ponderable.vel.y() = 0.0;
58 ponderable.accel.x() = 0.0; 59 ponderable.accel.x() = 0.0;
59 ponderable.accel.y() = 0.0; 60 ponderable.accel.y() = 0.0;
61 ponderable.targetVel.x() = 0.0;
62 ponderable.targetVel.y() = 0.0;
60 ponderable.grounded = false; 63 ponderable.grounded = false;
61 ponderable.frozen = false; 64 ponderable.frozen = false;
62 ponderable.collidable = true; 65 ponderable.collidable = true;
@@ -98,12 +101,48 @@ void PonderingSystem::tickBody(
98 // Accelerate 101 // Accelerate
99 if (!ponderable.frozen) 102 if (!ponderable.frozen)
100 { 103 {
101 ponderable.vel += ponderable.accel * dt; 104 // Determine the effective acceleration, which should be in the direction of
105 // the target velocity.
106 vec2d effAcc = ponderable.accel;
102 107
103 if ((ponderable.type == PonderableComponent::Type::freefalling) 108 if (ponderable.vel.x() == ponderable.targetVel.x())
104 && (ponderable.vel.y() > TERMINAL_VELOCITY))
105 { 109 {
106 ponderable.vel.y() = TERMINAL_VELOCITY; 110 effAcc.x() = 0.0;
111 }
112 else if ((ponderable.accel.x() > 0 &&
113 ponderable.targetVel.x() < ponderable.vel.x()) ||
114 (ponderable.accel.x() < 0 &&
115 ponderable.targetVel.x() > ponderable.vel.x()))
116 {
117 effAcc.x() = -effAcc.x();
118 }
119
120 if (ponderable.vel.y() == ponderable.targetVel.y())
121 {
122 effAcc.y() = 0.0;
123 }
124 else if ((ponderable.accel.y() > 0 &&
125 ponderable.targetVel.y() < ponderable.vel.y()) ||
126 (ponderable.accel.y() < 0 &&
127 ponderable.targetVel.y() > ponderable.vel.y()))
128 {
129 effAcc.y() = -effAcc.y();
130 }
131
132 // Accelerate
133 ponderable.vel += effAcc * dt;
134
135 // If the velocity crossed the target velocity, set it to the target
136 if ((effAcc.x() > 0 && ponderable.vel.x() > ponderable.targetVel.x()) ||
137 (effAcc.x() < 0 && ponderable.vel.x() < ponderable.targetVel.x()))
138 {
139 ponderable.vel.x() = ponderable.targetVel.x();
140 }
141
142 if ((effAcc.y() > 0 && ponderable.vel.y() > ponderable.targetVel.y()) ||
143 (effAcc.y() < 0 && ponderable.vel.y() < ponderable.targetVel.y()))
144 {
145 ponderable.vel.y() = ponderable.targetVel.y();
107 } 146 }
108 } 147 }
109 148