This commit is contained in:
Nikita Lisitsa 2024-08-19 18:05:03 +03:00
parent b51f8ee2dd
commit f44df139b1

View file

@ -11,6 +11,7 @@
#include <psemek/geom/box.hpp>
#include <psemek/geom/camera.hpp>
#include <psemek/geom/contains.hpp>
#include <psemek/geom/gradient.hpp>
#include <psemek/ecs/container.hpp>
#include <psemek/ecs/declare_uuid.hpp>
@ -248,6 +249,7 @@ namespace gmtk
psemek_ecs_declare_uuid("source")
resource_type type;
float animate = 0.f;
};
struct transformer
@ -255,6 +257,7 @@ namespace gmtk
psemek_ecs_declare_uuid("transformer")
transformer_type type;
float animate = 0.f;
};
struct crossing
@ -352,6 +355,9 @@ namespace gmtk
struct lab
{
psemek_ecs_declare_uuid("lab")
float animate = 0.f;
float animate_error = 0.f;
};
struct map
@ -743,6 +749,20 @@ namespace gmtk
});
}
float animation_factor(float animate)
{
static geom::gradient<float> const g
{
std::pair{0.5f, 0.f},
geom::easing_type::cubic,
std::pair{0.875f, 1.f},
geom::easing_type::cubic,
std::pair{1.f, 0.f},
};
return g(animate);
}
void draw(map & map, gfx::painter & painter, float pixel_size)
{
map.world.apply<vertex const, crossing const>([&](vertex const & v, crossing const & c)
@ -796,19 +816,35 @@ namespace gmtk
map.world.apply<vertex const, source const>([&](vertex const & v, source const & s)
{
painter.rect(v.location.bbox(-0.2f), gfx::black);
painter.rect(v.location.bbox(-0.225f), color_of(s.type));
geom::vector shift{0.f, 0.f};
if (v.location.coords[0] < 0)
shift = {1.f, 0.f};
else if (v.location.coords[0] >= 3)
shift = {-1.f, 0.f};
else
shift = {0.f, -1.f};
shift *= v.location.bbox()[0].length() * animation_factor(s.animate) / 12.f;
painter.rect(shift + v.location.bbox(-0.2f), gfx::black);
painter.rect(shift + v.location.bbox(-0.225f), color_of(s.type));
});
map.world.apply<vertex const, transformer const>([&](vertex const & v, transformer const & t)
{
draw_structure(v.location.bbox(), t, painter);
auto box = v.location.bbox();
box = geom::expand(box, 0.125f * animation_factor(t.animate));
draw_structure(box, t, painter);
});
map.world.apply<vertex const, lab const>([&](vertex const & v, lab const &)
map.world.apply<vertex const, lab const>([&](vertex const & v, lab const & l)
{
painter.rect(v.location.bbox(-0.2f), {0, 0, 0, 255});
painter.rect(v.location.bbox(-0.225f), {192, 192, 192, 255});
geom::vector shift{1.f, -1.f};
shift[0] *= v.location.bbox()[0].length() * geom::sqr(std::sin(l.animate_error * geom::pi)) * std::sin(l.animate_error * geom::pi * 5.f) / 12.f;
shift[1] *= v.location.bbox()[0].length() * animation_factor(l.animate) / 12.f;
painter.rect(shift + v.location.bbox(-0.2f), {0, 0, 0, 255});
painter.rect(shift + v.location.bbox(-0.225f), {192, 192, 192, 255});
auto pen = v.location.bbox(-0.4f).corner(0.5f, 1.f);
@ -1099,8 +1135,8 @@ namespace gmtk
{
map_.spawn_timer -= 1.f;
map_.world.apply<vertex const, source const>(
[&](vertex const & v, source const & s)
map_.world.apply<vertex const, source>(
[&](vertex const & v, source & s)
{
auto p = v.location.down();
@ -1108,6 +1144,9 @@ namespace gmtk
if (!map_.world.get(t).contains<occupied>())
{
if (!map_.world.get(t).get<path_vertex>().belts_to.empty())
s.animate += 1.f;
auto i = map_.world.create(
item{s.type, p}
);
@ -1156,7 +1195,7 @@ namespace gmtk
tutorial_state_ = 3;
}
map_.world.apply<vertex const, transformer const>([&](vertex const & v, transformer const & t)
map_.world.apply<vertex const, transformer>([&](vertex const & v, transformer & t)
{
boost::container::flat_map<resource_type, location> has_inputs;
@ -1209,6 +1248,9 @@ namespace gmtk
break;
}
if (crafted)
t.animate += 1.f;
if (!crafted && !has_inputs.empty())
{
return;
@ -1254,16 +1296,18 @@ namespace gmtk
if (auto cell = map_.world.index<index>().find(i.start.up()))
{
if (map_.world.get(*cell).get_if<lab>())
if (auto l = map_.world.get(*cell).get_if<lab>())
{
if (i.type == stages[map_.stage].type)
{
map_.resource_count += 1;
l->animate += 1.f;
map_.world.destroy(entity);
}
else
{
map_.world.attach(map_.world.index<path_index>().get(i.start), occupied{entity});
l->animate_error += 1.f;
map_.world.destroy(entity);
}
return;
}
@ -1323,6 +1367,22 @@ namespace gmtk
map_.world.attach(map_.world.index<path_index>().get(i.start), occupied{entity});
});
map_.world.apply<source>([&](source & s)
{
s.animate = std::max(0.f, s.animate - 3.f * dt);
});
map_.world.apply<transformer>([&](transformer & t)
{
t.animate = std::max(0.f, t.animate - 3.f * dt);
});
map_.world.apply<lab>([&](lab & l)
{
l.animate = std::max(0.f, l.animate - 3.f * dt);
l.animate_error = std::max(0.f, l.animate_error - 3.f * dt);
});
float aspect_ratio = (screen_size_[0] * 1.f) / screen_size_[1];
if (view_transition_)