summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-11-17 23:42:16 +0100
committerAki <please@ignore.pl>2022-11-17 23:50:47 +0100
commit5059c1888b7a78bdbf2347baf4ec6372c1e6236f (patch)
tree642f1d7dc432160c877d6bd3c2469e3c455df240
parent068d56e95ab3a96c25ec3a2d2e8fff74e527a6eb (diff)
downloadkurator-5059c1888b7a78bdbf2347baf4ec6372c1e6236f.zip
kurator-5059c1888b7a78bdbf2347baf4ec6372c1e6236f.tar.gz
kurator-5059c1888b7a78bdbf2347baf4ec6372c1e6236f.tar.bz2
Implemented naive hit detection events
-rw-r--r--battles/include/kurator/battles/Battle.h2
-rw-r--r--battles/src/BaseBattle.cpp13
-rw-r--r--battles/src/BaseBattle.h3
-rw-r--r--kurator/src/Battle.cpp40
-rw-r--r--kurator/src/Battle.h3
-rw-r--r--kurator/src/DamagePop.h15
6 files changed, 73 insertions, 3 deletions
diff --git a/battles/include/kurator/battles/Battle.h b/battles/include/kurator/battles/Battle.h
index 82a5da5..1542597 100644
--- a/battles/include/kurator/battles/Battle.h
+++ b/battles/include/kurator/battles/Battle.h
@@ -3,6 +3,7 @@
#include <memory>
#include <entt/entity/registry.hpp>
+#include <entt/signal/dispatcher.hpp>
#include "Scenario.h"
@@ -18,6 +19,7 @@ class Battle
public:
virtual ~Battle() = default;
virtual entt::registry& registry() = 0;
+ virtual entt::dispatcher& dispatcher() = 0;
virtual void update(float dt) = 0;
};
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp
index e7d70c2..4088936 100644
--- a/battles/src/BaseBattle.cpp
+++ b/battles/src/BaseBattle.cpp
@@ -1,8 +1,10 @@
#include "BaseBattle.h"
#include <entt/entity/registry.hpp>
+#include <entt/signal/dispatcher.hpp>
#include <kurator/battles/components.h>
+#include <kurator/battles/events.h>
#include <kurator/battles/Scenario.h>
#include <kurator/universe.h>
@@ -37,6 +39,13 @@ BaseBattle::registry()
}
+entt::dispatcher&
+BaseBattle::dispatcher()
+{
+ return _dispatcher;
+}
+
+
void
BaseBattle::update(const float dt)
{
@@ -108,8 +117,10 @@ BaseBattle::turrets(const float dt)
auto& target_points = _registry.get<HitPoints>(state.target);
const auto& target = _registry.get<Transform>(state.target);
const auto distance = transform.position - target.position;
- if (def.range > distance.magnitude())
+ if (def.range > distance.magnitude()) {
target_points.health -= def.base_damage;
+ _dispatcher.trigger(Hit{def.base_damage, entity, state.target});
+ }
control.reload = def.rate_of_fire;
}
}
diff --git a/battles/src/BaseBattle.h b/battles/src/BaseBattle.h
index 02cc024..8240fd2 100644
--- a/battles/src/BaseBattle.h
+++ b/battles/src/BaseBattle.h
@@ -1,6 +1,7 @@
#pragma once
#include <entt/entity/registry.hpp>
+#include <entt/signal/dispatcher.hpp>
#include <kurator/battles/Battle.h>
#include <kurator/battles/Scenario.h>
@@ -20,9 +21,11 @@ class BaseBattle : public Battle
public:
BaseBattle(const Scenario& scenario);
entt::registry& registry() override;
+ entt::dispatcher& dispatcher() override;
void update(float dt) override;
private:
entt::registry _registry;
+ entt::dispatcher _dispatcher;
RandomSpawner spawner;
TeamManager manager;
void pick_random_targets();
diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp
index f78675c..ffddc92 100644
--- a/kurator/src/Battle.cpp
+++ b/kurator/src/Battle.cpp
@@ -8,10 +8,12 @@
#include <raylib.h>
#include <kurator/battles/components.h>
+#include <kurator/battles/events.h>
#include <kurator/battles/Battle.h>
#include <kurator/battles/scenarios.h>
#include <kurator/universe/ShipType.h>
+#include "DamagePop.h"
#include "Session.h"
#include "Title.h"
@@ -24,6 +26,13 @@ Battle::Battle(std::shared_ptr<Session> _session) :
session {std::move(_session)},
battle {battles::prepare(battles::scenarios::example())}
{
+ battle->dispatcher().sink<battles::Hit>().connect<&Battle::receive>(*this);
+}
+
+
+Battle::~Battle()
+{
+ battle->dispatcher().sink<battles::Hit>().disconnect(*this);
}
@@ -31,6 +40,13 @@ void
Battle::update(const float dt)
{
battle->update(dt);
+ auto& registry = battle->registry();
+ auto pops = registry.view<DamagePop>();
+ for (auto&& [entity, pop] : pops.each()) {
+ pop.timer -= dt;
+ if (pop.timer < 0.0)
+ registry.destroy(entity);
+ }
if (IsKeyPressed(KEY_SPACE))
session->set(std::make_shared<Title>(session));
}
@@ -43,9 +59,9 @@ Battle::draw() const
const int width = GetScreenWidth();
const int height = GetScreenHeight();
const double scale = std::min(width/10.0, height/10.0);
- auto view = battle->registry().view<const universe::ShipType, const battles::Team, const battles::Transform>();
+ auto& registry = battle->registry();
+ auto view = registry.view<const universe::ShipType, const battles::Team, const battles::Transform>();
for (auto [entity, ship_type, team, transform] : view.each()) {
- (void) entity;
const auto color = team.id == 1 ? RED : GREEN;
const int x = width/2 + transform.position.x*scale;
const int y = height/2 + transform.position.y*scale;
@@ -53,6 +69,26 @@ Battle::draw() const
DrawLine(x, y, x + 6 * std::cos(transform.angle), y + 6 * std::sin(transform.angle), WHITE);
DrawText(ship_type.name.c_str(), x+10, y-10, 20.0f, GRAY);
}
+ auto pops = registry.view<DamagePop, const battles::Transform>();
+ for (auto [entity, pop, transform] : pops.each()) {
+ const int x = width/2 + transform.position.x*scale;
+ const int y = height/2 + transform.position.y*scale;
+ const int text = MeasureText(pop.text, 10);
+ DrawText(pop.text, x-text/2, y-5-22*(0.4-pop.timer), 10, RED);
+ }
+}
+
+
+void
+Battle::receive(const battles::Hit& hit)
+{
+ auto& registry = battle->registry();
+ if (!registry.valid(hit.victim))
+ return;
+ const auto entity = registry.create();
+ const auto& transform = registry.get<battles::Transform>(hit.victim);
+ registry.emplace<DamagePop>(entity, 0.4, TextFormat("%.1f", hit.damage));
+ registry.emplace<battles::Transform>(entity, transform.position, 0.0);
}
diff --git a/kurator/src/Battle.h b/kurator/src/Battle.h
index 64cdb47..d59f024 100644
--- a/kurator/src/Battle.h
+++ b/kurator/src/Battle.h
@@ -3,6 +3,7 @@
#include <memory>
#include <kurator/battles/Battle.h>
+#include <kurator/battles/events.h>
#include "Scene.h"
#include "Session.h"
@@ -16,8 +17,10 @@ class Battle : public Scene
{
public:
explicit Battle(std::shared_ptr<Session> _session);
+ virtual ~Battle();
void update(float dt) override;
void draw() const override;
+ void receive(const battles::Hit& hit);
private:
std::shared_ptr<Session> session;
std::unique_ptr<battles::Battle> battle;
diff --git a/kurator/src/DamagePop.h b/kurator/src/DamagePop.h
new file mode 100644
index 0000000..dc4c19a
--- /dev/null
+++ b/kurator/src/DamagePop.h
@@ -0,0 +1,15 @@
+#pragma once
+
+
+namespace kurator
+{
+
+
+struct DamagePop
+{
+ double timer;
+ const char* text;
+};
+
+
+} // namespace kurator