Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PulsingLight: Move and change colour together with tilemap #3167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/object/pulsing_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
#include <assert.h>
#include <math.h>

#include "object/tilemap.hpp"
#include "math/random.hpp"
#include "math/util.hpp"

PulsingLight::PulsingLight(const Vector& center, float cycle_len_, float min_alpha_, float max_alpha_, const Color& color_) :
PulsingLight::PulsingLight(const Vector& center, float cycle_len_, float min_alpha_, float max_alpha_, const Color& color_, const TileMap* parent_tilemap_) :
Light(center, color_),
min_alpha(min_alpha_),
max_alpha(max_alpha_),
cycle_len(cycle_len_),
parent_tilemap(parent_tilemap_),
rel_position(center),
rel_color(color_),
t(0)
{
assert(cycle_len > 0);
Expand All @@ -50,13 +54,17 @@ PulsingLight::update(float dt_sec)

void
PulsingLight::draw(DrawingContext& context)
{
Color old_color = color;

color.alpha *= min_alpha + ((max_alpha - min_alpha) * cosf(math::TAU * t / cycle_len));
{
const float alpha = min_alpha + ((max_alpha - min_alpha) * cosf(math::TAU * t / cycle_len));
if (parent_tilemap) {
position = rel_position + parent_tilemap->get_offset();
color = (rel_color * parent_tilemap->get_current_tint()).validate();
color.alpha *= alpha * parent_tilemap->get_alpha();
} else {
color.alpha = rel_color.alpha * alpha;
}

Light::draw(context);

color = old_color;
}

/* EOF */
7 changes: 6 additions & 1 deletion src/object/pulsing_light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

#include "object/light.hpp"

class TileMap;

/**
* Light source that changes alpha value to give the impression of a pulsating light
*/
class PulsingLight final : public Light
{
public:
PulsingLight(const Vector& center, float cycle_len = 5.0, float min_alpha = 0.0, float max_alpha = 1.0, const Color& color = Color(1.0, 1.0, 1.0, 1.0));
PulsingLight(const Vector& center, float cycle_len = 5.0, float min_alpha = 0.0, float max_alpha = 1.0, const Color& color = Color(1.0, 1.0, 1.0, 1.0), const TileMap* parent_tilemap = nullptr);
~PulsingLight() override;
virtual GameObjectClasses get_class_types() const override { return Light::get_class_types().add(typeid(PulsingLight)); }

Expand All @@ -36,6 +38,9 @@ class PulsingLight final : public Light
float min_alpha; /**< minimum alpha */
float max_alpha; /**< maximum alpha */
float cycle_len; /**< length in seconds of one cycle */
const TileMap* parent_tilemap; /**< A reference to Tilemap for which the PulsingLight was made for. optional */
Vector rel_position; /**< relative position w.r.t. tilemap */
Color rel_color; /**< relative color w.r.t. tilemap */

float t; /**< local time in seconds */
};
Expand Down
6 changes: 3 additions & 3 deletions src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ Sector::convert_tiles2gameobject()
{
// add lights for fire tiles
uint32_t attributes = tile.get_attributes();
Vector pos = tm.get_tile_position(x, y) + tm_offset;
Vector pos = tm.get_tile_position(x, y);
Vector center = pos + Vector(16, 16);

if (attributes & Tile::FIRE) {
Expand All @@ -844,13 +844,13 @@ Sector::convert_tiles2gameobject()
&& (tm.get_tile(x, y-1).get_attributes() != attributes || y%3 == 0)) {
float pseudo_rnd = static_cast<float>(static_cast<int>(pos.x) % 10) / 10;
add<PulsingLight>(center, 1.0f + pseudo_rnd, 0.8f, 1.0f,
(Color(1.0f, 0.3f, 0.0f, 1.0f) * tm.get_current_tint()).validate());
Color(1.0f, 0.3f, 0.0f, 1.0f), &tm);
}
} else {
// torch
float pseudo_rnd = static_cast<float>(static_cast<int>(pos.x) % 10) / 10;
add<PulsingLight>(center, 1.0f + pseudo_rnd, 0.9f, 1.0f,
(Color(1.0f, 1.0f, 0.6f, 1.0f) * tm.get_current_tint()).validate());
Color(1.0f, 1.0f, 0.6f, 1.0f), &tm);
}
}
}
Expand Down
Loading