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

Expose decals to scripting #1512

Merged
merged 9 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
56 changes: 46 additions & 10 deletions src/object/decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "object/decal.hpp"
#include "scripting/decal.hpp"

#include "sprite/sprite_manager.hpp"
#include "util/reader.hpp"
#include "util/reader_mapping.hpp"

Decal::Decal(const ReaderMapping& reader) :
MovingSprite(reader, "images/decal/explanations/billboard-bigtux.png", LAYER_OBJECTS, COLGROUP_DISABLED),
default_action("default"),
solid(),
flip(NO_FLIP)
ExposedObject<Decal, scripting::Decal>(this),
m_default_action("default"),
m_solid(),
m_flip(NO_FLIP),
m_fade_sprite(m_sprite.get()->clone())
{
m_layer = reader_get_layer(reader, LAYER_OBJECTS);

reader.get("solid", solid, false);
if (solid)
reader.get("solid", m_solid, false);
if (m_solid)
set_group(COLGROUP_STATIC);
if (reader.get("action", default_action))
set_action(default_action, -1);
if (reader.get("action", m_default_action))
set_action(m_default_action, -1);
}

ObjectSettings
Expand All @@ -40,8 +44,8 @@ Decal::get_settings()
ObjectSettings result = MovingSprite::get_settings();

result.add_int(_("Z-pos"), &m_layer, "z-pos", LAYER_OBJECTS);
result.add_bool(_("Solid"), &solid, "solid", false);
result.add_text(_("Action"), &default_action, "action", std::string("default"));
result.add_bool(_("Solid"), &m_solid, "solid", false);
result.add_text(_("Action"), &m_default_action, "action", std::string("default"));

result.reorder({"z-pos", "sprite", "x", "y"});

Expand All @@ -55,8 +59,40 @@ Decal::~Decal()
void
Decal::draw(DrawingContext& context)
{
m_sprite->draw(context.color(), get_pos(), m_layer, flip);
m_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
if(m_fade_timer.started()) m_fade_sprite->draw(context.color(), get_pos(), m_layer, m_flip);
}

void
Decal::fade(std::string new_sprite, float fade_time)
{
m_fade_sprite = SpriteManager::current()->create(new_sprite);
m_sprite.swap(m_fade_sprite);
// From now on flip_sprite == the old one

m_sprite.get()->set_alpha(0);
m_fade_timer.start(fade_time);
}

void
Decal::update(float)
{
if (m_fade_timer.started())
{
if (m_fade_timer.check())
{
m_sprite.get()->set_alpha(1.0f);
m_fade_sprite.get()->set_alpha(0.0f);
m_fade_timer.stop();
}
else
{
float new_alpha = sqrtf(m_fade_timer.get_timegone() / m_fade_timer.get_period());
float old_alpha = sqrtf(m_fade_timer.get_timeleft() / m_fade_timer.get_period());
m_sprite.get()->set_alpha(new_alpha);
m_fade_sprite.get()->set_alpha(old_alpha);
}
}
}

/* EOF */
17 changes: 13 additions & 4 deletions src/object/decal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
#define HEADER_SUPERTUX_OBJECT_DECAL_HPP

#include "object/moving_sprite.hpp"
#include "scripting/decal.hpp"
#include "squirrel/exposed_object.hpp"
#include "supertux/timer.hpp"

class ReaderMapping;

/** A decorative image, perhaps part of the terrain */
class Decal final : public MovingSprite
class Decal final : public MovingSprite,
public ExposedObject<Decal, scripting::Decal>
{
friend class FlipLevelTransformer;

Expand All @@ -38,11 +42,16 @@ class Decal final : public MovingSprite
virtual ObjectSettings get_settings() override;

virtual void draw(DrawingContext& context) override;
virtual void update(float dt_sec) override;

void fade(const std::string new_sprite, float fade_time);

private:
std::string default_action;
bool solid;
Flip flip;
std::string m_default_action;
bool m_solid;
Flip m_flip;
SpritePtr m_fade_sprite;
Timer m_fade_timer;

private:
Decal(const Decal&) = delete;
Expand Down
38 changes: 38 additions & 0 deletions src/scripting/decal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SuperTux
// Copyright (C) 2020 Grzegorz Przybylski <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "object/decal.hpp"
#include "scripting/decal.hpp"

namespace scripting {

void
Decal::change_sprite(const std::string& new_sprite_name)
{
SCRIPT_GUARD_VOID;
object.change_sprite(new_sprite_name);
}

void
Decal::fade(const std::string& new_sprite_name, float fade_time)
{
SCRIPT_GUARD_VOID;
object.fade(new_sprite_name, fade_time);
}

} // namespace scripting

/* EOF */
50 changes: 50 additions & 0 deletions src/scripting/decal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SuperTux
// Copyright (C) 2020 Grzegorz Przybylski <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_SCRIPTING_DECAL_HPP
#define HEADER_SUPERTUX_SCRIPTING_DECAL_HPP

#ifndef SCRIPTING_API
#include "scripting/game_object.hpp"

class Decal;
#endif

namespace scripting {

class Decal final
#ifndef SCRIPTING_API
: public GameObject<::Decal>
#endif
{
#ifndef SCRIPTING_API
public:
using GameObject::GameObject;
private:
Decal(const Decal&) = delete;
Decal& operator=(const Decal&) = delete;
#endif

public:
void change_sprite(const std::string& new_sprite_name);
void fade(const std::string& new_sprite_name, float fade_time);
Zwatotem marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace scripting

#endif

/* EOF */
Loading