Skip to content

Commit

Permalink
Fix physics interpolation. Implement new power up types
Browse files Browse the repository at this point in the history
  • Loading branch information
justindriggers committed Jan 28, 2024
1 parent d9c198a commit 89ac3b7
Show file tree
Hide file tree
Showing 29 changed files with 404 additions and 96 deletions.
1 change: 1 addition & 0 deletions linguine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(linguine
src/data/spells/actions/Explosion.cpp
src/data/spells/actions/MassHeal.cpp
src/data/spells/actions/ModifyHealth.cpp
src/data/spells/actions/Revive.cpp
src/data/spells/effects/Effect.cpp
src/data/spells/effects/ModifyHealthOverTime.cpp
src/entity/archetype/Archetype.cpp
Expand Down
8 changes: 7 additions & 1 deletion linguine/include/TimeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ class TimeManager {
}

inline void pause() {
_pausedTimeScale = _timeScale;
setTimeScale(0.0f);
}

inline void resume() {
setTimeScale(1.0f);
setTimeScale(_pausedTimeScale);
_lastTickTime = currentTime();
_currentTickTime = _lastTickTime;
}
Expand Down Expand Up @@ -72,11 +73,16 @@ class TimeManager {
_timeScale = timeScale;
}

[[nodiscard]] inline float getAccumulatorProgress() const {
return _accumulator / _fixedDeltaTime;
}

private:
constexpr static float _fixedDeltaTime = 0.02f;

float _accumulator = _fixedDeltaTime;
float _timeScale = 1.0f;
float _pausedTimeScale = 1.0f;

time_t _startTime;
time_t _lastTickTime;
Expand Down
11 changes: 11 additions & 0 deletions linguine/src/components/Boost.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace linguine {

struct Boost {
float magnitude = 0.0f;
float duration = 1.0f;
float elapsed = 0.0f;
};

} // namespace linguine
1 change: 1 addition & 0 deletions linguine/src/components/CameraFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct CameraFixture {
float size = 15.0f;
Measurement type = Measurement::Height;
float speed = 1.0f;
bool shake = false;
Camera* camera;
};

Expand Down
9 changes: 6 additions & 3 deletions linguine/src/components/PowerUp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace linguine {

struct PowerUp {
enum Type {
MassHeal
enum class Type {
MassHeal,
Revive,
SpeedBoost,
TimeWarp
};

Type type = MassHeal;
Type type = Type::SpeedBoost;
};

} // namespace linguine
7 changes: 5 additions & 2 deletions linguine/src/components/Shake.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace linguine {

struct Shake {
float duration = 0.0f;
Shake(float magnitude, float duration)
: magnitude(magnitude), duration(duration) {}

const float magnitude = 0.0f;
const float duration = 0.0f;
float elapsed = 0.0f;
float magnitude = 0.0f;
};

} // namespace linguine
1 change: 1 addition & 0 deletions linguine/src/components/SpawnPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct SpawnPoint {
float spawnChance = 1.0f;
float powerUpInterval = 15.0f;
float powerUpElapsed = 0.0f;
std::unordered_map<uint64_t, float> powerUpCooldowns{};
};

} // namespace linguine
14 changes: 14 additions & 0 deletions linguine/src/components/TimeWarp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace linguine {

struct TimeWarp {
TimeWarp(float factor, float duration)
: factor(factor), duration(duration) {}

const float factor = 1.0f;
const float duration = 1.0f;
float elapsed = 0.0f;
};

} // namespace linguine
6 changes: 4 additions & 2 deletions linguine/src/data/spells/SpellDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "actions/MassHeal.h"
#include "actions/ModifyHealth.h"
#include "data/spells/actions/Explosion.h"
#include "data/spells/actions/Revive.h"
#include "effects/ModifyHealthOverTime.h"

namespace linguine {
Expand All @@ -17,8 +18,9 @@ class SpellDatabase {
EntityManager& entityManager)
: _spells {
{ 1, std::make_shared<Spell>("Base", 0.0f, 1.5f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "",std::make_unique<ModifyHealth>(entityManager, 250)) },
{ 2, std::make_shared<Spell>("AoE", 0.0f, 6.0f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "",std::make_unique<MassHeal>(entityManager, 200)) },
{ 3, std::make_shared<Spell>("Bomb", 0.0f, 6.0f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "",std::make_unique<Explosion>(entityManager, 2000)) }
{ 2, std::make_shared<Spell>("AoE", 0.0f, 6.0f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "",std::make_unique<MassHeal>(entityManager, 0.25f)) },
{ 3, std::make_shared<Spell>("Bomb", 0.0f, 6.0f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "",std::make_unique<Explosion>(entityManager, 2000)) },
{ 4, std::make_shared<Spell>("Revive", 0.0f, 60.0f, glm::vec3(0.12477f, 0.56471f, 0.07421f), "", std::make_unique<Revive>(entityManager)) }
} {}

Spell& getSpellById(uint64_t id) {
Expand Down
2 changes: 1 addition & 1 deletion linguine/src/data/spells/actions/MassHeal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace linguine {
void MassHeal::execute(Entity& target) {
_entityManager.find<Health, Alive>()->each([this](const Entity& entity) {
auto health = entity.get<Health>();
health->current = glm::clamp(health->current + _power, 0, health->max);
health->current = glm::clamp(health->current + static_cast<int32_t>(glm::round(static_cast<float>(health->max) * _power)), 0, health->max);
});
}

Expand Down
4 changes: 2 additions & 2 deletions linguine/src/data/spells/actions/MassHeal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace linguine {

class MassHeal : public Action {
public:
MassHeal(EntityManager& entityManager, int32_t power)
MassHeal(EntityManager& entityManager, float power)
: _entityManager(entityManager), _power(power) {}

~MassHeal() override = default;
Expand All @@ -17,7 +17,7 @@ class MassHeal : public Action {

private:
EntityManager& _entityManager;
int32_t _power;
float _power;
};

} // namespace linguine
30 changes: 30 additions & 0 deletions linguine/src/data/spells/actions/Revive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "Revive.h"

#include <glm/common.hpp>

#include "components/Alive.h"
#include "components/Health.h"

namespace linguine {

void Revive::execute(linguine::Entity& target) {
auto dead = std::vector<uint64_t>();

_entityManager.find<Health>()->each([&dead](const Entity& entity) {
if (!entity.has<Alive>()) {
dead.push_back(entity.getId());
}
});

if (!dead.empty()) {
auto randomEntity = std::uniform_int_distribution(0, static_cast<int>(dead.size() - 1));
auto i = randomEntity(_random);

auto entity = _entityManager.getById(dead[i]);
auto health = entity->get<Health>();
health->current = static_cast<int32_t>(glm::round(static_cast<float>(health->max) * 0.25f));
entity->add<Alive>();
}
}

} // namespace linguine
23 changes: 23 additions & 0 deletions linguine/src/data/spells/actions/Revive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Action.h"

#include <random>

namespace linguine {

class Revive : public Action {
public:
explicit Revive(EntityManager& entityManager)
: _entityManager(entityManager) {}

~Revive() override = default;

void execute(Entity& target) override;

private:
std::random_device _random;
EntityManager& _entityManager;
};

} // namespace linguine
3 changes: 1 addition & 2 deletions linguine/src/scenes/GameOverScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "components/CameraFixture.h"
#include "components/PhysicalState.h"
#include "components/Progressable.h"
#include "components/Shake.h"
#include "components/Text.h"
#include "components/Toast.h"
#include "components/Transform.h"
Expand Down Expand Up @@ -48,12 +47,12 @@ void GameOverScene::init() {

{
auto uiCameraEntity = createEntity();
uiCameraEntity->add<Shake>();
uiCameraEntity->add<Transform>();
uiCameraEntity->add<PhysicalState>();

auto fixture = uiCameraEntity->add<CameraFixture>();
fixture->size = 240.0f;
fixture->shake = true;
fixture->type = CameraFixture::Measurement::Width;
fixture->camera = renderer.createCamera();
fixture->camera->clearColor = Palette::Blue;
Expand Down
39 changes: 26 additions & 13 deletions linguine/src/scenes/InfiniteRunnerScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "components/Ability.h"
#include "components/Alive.h"
#include "components/Attachment.h"
#include "components/Boost.h"
#include "components/CameraFixture.h"
#include "components/Cast.h"
#include "components/Circle.h"
Expand All @@ -20,7 +21,6 @@
#include "components/Player.h"
#include "components/Progressable.h"
#include "components/Score.h"
#include "components/Shake.h"
#include "components/ShipPart.h"
#include "components/SpawnPoint.h"
#include "components/StarSpawnPoint.h"
Expand Down Expand Up @@ -61,11 +61,11 @@ namespace linguine {
void InfiniteRunnerScene::init() {
registerSystem(std::make_unique<FpsSystem>(getEntityManager(), get<Logger>()));
registerSystem(std::make_unique<GestureRecognitionSystem>(getEntityManager(), get<InputManager>(), get<Renderer>(), get<TimeManager>()));
registerSystem(std::make_unique<PhysicsInterpolationSystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<VelocitySystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<PlayerControllerSystem>(getEntityManager(), get<InputManager>(), get<AudioManager>()));
registerSystem(std::make_unique<VelocitySystem>(getEntityManager()));
registerSystem(std::make_unique<CameraFollowSystem>(getEntityManager()));
registerSystem(std::make_unique<AttachmentSystem>(getEntityManager()));
registerSystem(std::make_unique<PhysicsInterpolationSystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<CollisionSystem>(getEntityManager()));
registerSystem(std::make_unique<EffectSystem>(getEntityManager(), *_spellDatabase));
registerSystem(std::make_unique<HudSystem>(getEntityManager(), get<Renderer>(), get<SaveManager>()));
Expand All @@ -79,7 +79,7 @@ void InfiniteRunnerScene::init() {
registerSystem(std::make_unique<ShakeSystem>(getEntityManager(), get<SaveManager>()));

// Scene-specific
registerSystem(std::make_unique<SpawnSystem>(getEntityManager(), get<Renderer>()));
registerSystem(std::make_unique<SpawnSystem>(getEntityManager(), get<Renderer>(), *_spellDatabase));
registerSystem(std::make_unique<ScoringSystem>(getEntityManager(), *_spellDatabase, get<Renderer>(), get<AudioManager>(), get<SaveManager>()));
registerSystem(std::make_unique<TutorialSystem>(getEntityManager()));

Expand All @@ -96,7 +96,6 @@ void InfiniteRunnerScene::init() {

{
auto cameraEntity = createEntity();
cameraEntity->add<Shake>();

auto follow = cameraEntity->add<Follow>();

Expand All @@ -118,6 +117,7 @@ void InfiniteRunnerScene::init() {

auto fixture = cameraEntity->add<CameraFixture>();
fixture->size = 15.2f;
fixture->shake = true;
fixture->type = CameraFixture::Measurement::Width;
fixture->camera = renderer.createCamera();
fixture->camera->clearColor = { 0.007f, 0.01521f, 0.04667f };
Expand Down Expand Up @@ -172,7 +172,7 @@ void InfiniteRunnerScene::init() {
auto player = playerEntity->add<Player>();
player->speed = 3.0f + 1.0f * static_cast<float>(_upgradeDatabase.getRankByLevel(2, level));
player->acceleration = 0.05f + 0.02f * static_cast<float>(_upgradeDatabase.getRankByLevel(3, level));
player->maxSpeed = 20.0f;
player->maxSpeed = 30.0f;

auto transform = playerEntity->add<Transform>();
transform->scale = glm::vec3(2.5f);
Expand Down Expand Up @@ -284,30 +284,43 @@ void InfiniteRunnerScene::init() {
auto particleEntity = createEntity();

auto particle = particleEntity->add<Particle>();
particle->scalePerSecond = -0.35f;
particle->duration = 5.0f;

auto playerTransform = playerEntity->get<Transform>();

auto randomScale = std::uniform_real_distribution(0.125f * playerTransform->scale.x, 0.25f * playerTransform->scale.x);
auto randomX = std::uniform_real_distribution(-0.2f * playerTransform->scale.x, 0.2f * playerTransform->scale.y);

auto transform = particleEntity->add<Transform>();
transform->scale = glm::vec3(randomScale(_random));
transform->position = { playerTransform->position.x + randomX(_random), playerTransform->position.y - 0.45f * playerTransform->scale.y, 2.0f };

particleEntity->add<PhysicalState>(transform->position, 0.0f);

auto velocity = particleEntity->add<Velocity>();
velocity->velocity = { 0.0f, -3.0f };

auto randomColor = std::uniform_int_distribution(0, 1);

auto circle = particleEntity->add<Circle>();
circle->feature = new CircleFeature();

if (randomColor(_random) > 0) {
circle->feature->color = { 0.78354f, 0.78354f, 0.78354f };
if (playerEntity->has<Boost>()) {
auto boost = playerEntity->get<Boost>();

particle->scalePerSecond = -3.0f * (boost->elapsed / boost->duration) - 1.0f;

auto randomScale = std::uniform_real_distribution((0.125f + 0.075f * (1.0f - boost->elapsed / boost->duration)) * playerTransform->scale.x, (0.25f + 0.075f * (1.0f - boost->elapsed / boost->duration)) * playerTransform->scale.x);
transform->scale = glm::vec3(randomScale(_random));

circle->feature->color = Palette::Orange;
} else {
particle->scalePerSecond = -0.35f;

auto randomScale = std::uniform_real_distribution(0.125f * playerTransform->scale.x, 0.25f * playerTransform->scale.x);
transform->scale = glm::vec3(randomScale(_random));

auto randomColor = std::uniform_int_distribution(0, 1);

if (randomColor(_random) > 0) {
circle->feature->color = Palette::Gray;
}
}

circle->renderable = renderer.create(std::unique_ptr<CircleFeature>(circle->feature));
Expand Down
6 changes: 3 additions & 3 deletions linguine/src/scenes/TitleScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ namespace linguine {
void TitleScene::init() {
registerSystem(std::make_unique<FpsSystem>(getEntityManager(), get<Logger>()));
registerSystem(std::make_unique<GestureRecognitionSystem>(getEntityManager(), get<InputManager>(), get<Renderer>(), get<TimeManager>()));
registerSystem(std::make_unique<VelocitySystem>(getEntityManager()));
registerSystem(std::make_unique<PhysicsInterpolationSystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<VelocitySystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<CameraFollowSystem>(getEntityManager()));
registerSystem(std::make_unique<DialogSystem>(getEntityManager()));
registerSystem(std::make_unique<ButtonSystem>(getEntityManager(), get<Renderer>(), get<AudioManager>()));
registerSystem(std::make_unique<FooterSystem>(getEntityManager(), get<Renderer>()));
registerSystem(std::make_unique<AttachmentSystem>(getEntityManager()));
registerSystem(std::make_unique<PhysicsInterpolationSystem>(getEntityManager(), get<TimeManager>()));
registerSystem(std::make_unique<CollisionSystem>(getEntityManager()));
registerSystem(std::make_unique<ParticleSystem>(getEntityManager()));
registerSystem(std::make_unique<FireSystem>(getEntityManager()));

registerSystem(std::make_unique<SpawnSystem>(getEntityManager(), get<Renderer>()));
registerSystem(std::make_unique<SpawnSystem>(getEntityManager(), get<Renderer>(), *_spellDatabase));

registerSystem(std::make_unique<TransformationSystem>(getEntityManager()));
registerSystem(std::make_unique<CameraSystem>(getEntityManager(), get<Renderer>()));
Expand Down
5 changes: 4 additions & 1 deletion linguine/src/scenes/TitleScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
#include <random>

#include "ServiceLocator.h"
#include "data/spells/SpellDatabase.h"
#include "data/upgrades/UpgradeDatabase.h"

namespace linguine {

class TitleScene : public Scene {
public:
explicit TitleScene(ServiceLocator& serviceLocator)
: Scene(serviceLocator) {}
: Scene(serviceLocator),
_spellDatabase(std::make_unique<SpellDatabase>(serviceLocator, getEntityManager())) {}

void init() override;

private:
std::random_device _random;
std::unique_ptr<SpellDatabase> _spellDatabase;
UpgradeDatabase _upgradeDatabase;
};

Expand Down

0 comments on commit 89ac3b7

Please sign in to comment.