Skip to content

Commit

Permalink
Add trigger direction property to script trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
tobbi committed Aug 9, 2024
1 parent 0928a4f commit 9c17696
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/trigger/scripttrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "trigger/scripttrigger.hpp"

#include "editor/editor.hpp"
#include "object/player.hpp"
#include "supertux/debug.hpp"
#include "supertux/sector.hpp"
#include "util/log.hpp"
Expand All @@ -26,11 +27,16 @@
ScriptTrigger::ScriptTrigger(const ReaderMapping& reader) :
Trigger(reader),
triggerevent(),
m_trigger_direction(Direction::AUTO),
script(),
must_activate(false),
oneshot(false),
runcount(0)
{
std::string trigger_direction;
reader.get("trigger-direction", trigger_direction, dir_to_string(Direction::AUTO).c_str());
m_trigger_direction = string_to_dir(trigger_direction);

reader.get("script", script);
reader.get("button", must_activate);
reader.get("oneshot", oneshot);
Expand All @@ -51,18 +57,39 @@ ScriptTrigger::get_settings()
result.add_script(_("Script"), &script, "script");
result.add_bool(_("Button"), &must_activate, "button");
result.add_bool(_("Oneshot"), &oneshot, "oneshot", false);
result.add_direction(_("Trigger Direction"), &m_trigger_direction,
{ Direction::AUTO, Direction::LEFT, Direction::RIGHT, Direction::UP, Direction::DOWN }, "trigger-direction");

result.reorder({"script", "button", "width", "height", "x", "y"});

return result;
}

void
ScriptTrigger::event(Player& , EventType type)
ScriptTrigger::event(Player& player, EventType type)
{
if (type != triggerevent || (oneshot && runcount >= 1))
return;

if(m_trigger_direction != Direction::AUTO)
{
const auto& pos = get_pos();
const auto& player_pos = player.get_pos();
Direction player_direction = Direction::NONE;

if (player_pos.x < pos.x)
player_direction = Direction::LEFT;
if (player_pos.x + player.get_width() > pos.x + get_width())
player_direction = Direction::RIGHT;
if (player_pos.y < pos.y)
player_direction = Direction::UP;
if (player_pos.y + player.get_height() > pos.y + get_height())
player_direction = Direction::DOWN;

if (player_direction != m_trigger_direction)
return;
}

Sector::get().run_script(script, "ScriptTrigger");
runcount++;
}
Expand Down
1 change: 1 addition & 0 deletions src/trigger/scripttrigger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ScriptTrigger final : public Trigger

private:
EventType triggerevent;
Direction m_trigger_direction;
std::string script;
bool must_activate;
bool oneshot;
Expand Down

0 comments on commit 9c17696

Please sign in to comment.