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

Add trigger direction property to script trigger #3041

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
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
Loading