-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a new component called "Velocity" to generalize things that move in a specific velocity every frame, and create a system that applies that velocity to an entity's position in an isolated, testable, and framerate-independant way. Also remove the speed/velocity calculations from the UserProjectile component so we can use the new Velocity component instead.
- Loading branch information
1 parent
5119c03
commit e6c3c1b
Showing
8 changed files
with
125 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
#ifndef GL_ADAGIO_USERPROJECTILE_H | ||
#define GL_ADAGIO_USERPROJECTILE_H | ||
|
||
struct UserProjectile { | ||
float speed; | ||
}; | ||
struct UserProjectile {}; | ||
|
||
#endif // GL_ADAGIO_USERPROJECTILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef GL_ADAGIO_VELOCITY_H | ||
#define GL_ADAGIO_VELOCITY_H | ||
|
||
struct Velocity { | ||
double direction; | ||
double speed; | ||
}; | ||
|
||
#endif // GL_ADAGIO_VELOCITY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "ApplyVelocity.h" | ||
#include "../components/Position.h" | ||
#include "../components/Velocity.h" | ||
#include <cmath> | ||
|
||
void ApplyVelocity(entt::registry ®istry, Adagio::GameStats &stats, | ||
Adagio::StateMachine *state) { | ||
auto view = registry.view<Position, Velocity>(); | ||
for (auto [entity, pos, velocity] : view.each()) { | ||
const double delta = | ||
stats.getFrameDelta() * 60; // Speed is in "1 pixel per 1/60 of a frame" | ||
pos.position.x += delta * velocity.speed * std::cos(velocity.direction); | ||
pos.position.y += delta * velocity.speed * std::sin(velocity.direction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef GL_ADAGIO_APPLYVELOCITY_H | ||
#define GL_ADAGIO_APPLYVELOCITY_H | ||
|
||
#include "../../state/GameStats.h" | ||
#include "../../state/StateMachine.h" | ||
#include "entt/entt.hpp" | ||
|
||
void ApplyVelocity(entt::registry ®istry, Adagio::GameStats &stats, | ||
Adagio::StateMachine *state); | ||
|
||
#endif // GL_ADAGIO_APPLYVELOCITY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "../../../src/game/systems/ApplyVelocity.h" | ||
#include "../../../src/game/components/Position.h" | ||
#include "../../../src/game/components/Velocity.h" | ||
#include "../EcsTestingHarness.h" | ||
#include <catch2/catch.hpp> | ||
#include <cmath> | ||
|
||
static EcsTestingHarness harness; | ||
static Position &setUpComponents(double direction, double speed); | ||
static bool doubleEquals(double a, double b); | ||
|
||
TEST_CASE("ApplyVelocity: No components defined", "[system][ApplyVelocity]") { | ||
harness.reset(); | ||
REQUIRE_NOTHROW( | ||
ApplyVelocity(harness.registry, harness.stats, harness.stateMachine)); | ||
} | ||
|
||
TEST_CASE("ApplyVelocity: Does nothing with 0 speed", | ||
"[system][ApplyVelocity]") { | ||
Position &pos = setUpComponents(0, 0); | ||
|
||
harness.stats.advanceTime(1); | ||
harness.testSystemFrame(ApplyVelocity); | ||
|
||
REQUIRE(pos.position.x == 0); | ||
REQUIRE(pos.position.y == 0); | ||
} | ||
|
||
TEST_CASE("ApplyVelocity moves in direction 0", "[system][ApplyVelocity]") { | ||
Position &pos = setUpComponents(0, 1); | ||
|
||
harness.stats.advanceTime(1.0 / 60); | ||
harness.testSystemFrame(ApplyVelocity); | ||
|
||
REQUIRE(pos.position.x == 1); | ||
REQUIRE(pos.position.y == 0); | ||
} | ||
|
||
TEST_CASE("ApplyVelocity moves in direction PI/2", "[system][ApplyVelocity]") { | ||
Position &pos = setUpComponents(M_PI_2, 1); | ||
|
||
harness.stats.advanceTime(1.0 / 60); | ||
harness.testSystemFrame(ApplyVelocity); | ||
|
||
REQUIRE(doubleEquals(pos.position.x, 0)); | ||
REQUIRE(doubleEquals(pos.position.y, 1)); | ||
} | ||
|
||
TEST_CASE("ApplyVelocity moves in direction PI/4", "[system][ApplyVelocity]") { | ||
Position &pos = setUpComponents(M_PI_4, 1); | ||
|
||
harness.stats.advanceTime(1.0 / 60); | ||
harness.testSystemFrame(ApplyVelocity); | ||
|
||
REQUIRE(doubleEquals(pos.position.x, 1.0 / std::sqrt(2))); | ||
REQUIRE(doubleEquals(pos.position.y, 1.0 / std::sqrt(2))); | ||
} | ||
|
||
TEST_CASE("ApplyVelocity applies speed every 1/60th of a second", | ||
"[system][ApplyVelocity]") { | ||
Position &pos = setUpComponents(0, 1); | ||
|
||
harness.stats.advanceTime(1); | ||
harness.testSystemFrame(ApplyVelocity); | ||
|
||
REQUIRE(doubleEquals(pos.position.x, 60)); | ||
REQUIRE(doubleEquals(pos.position.y, 0)); | ||
} | ||
|
||
static Position &setUpComponents(double direction, double speed) { | ||
harness.reset(); | ||
auto entity = harness.registry.create(); | ||
Position &pos = | ||
harness.registry.emplace<Position>(entity, Adagio::Vector2d{0, 0}); | ||
harness.registry.emplace<Velocity>(entity, direction, speed); | ||
|
||
return pos; | ||
} | ||
|
||
static bool doubleEquals(double a, double b) { | ||
const double delta = 0.0005; | ||
return std::abs(a - b) < delta; | ||
} |