Skip to content

Commit

Permalink
Upgraded PathMove Events to support target sprites.
Browse files Browse the repository at this point in the history
I also removed the error-prone relative path move logic.

And I used all that in the forest entrance introduction.
  • Loading branch information
Yohann Ferreira committed Sep 10, 2012
1 parent 4ad98e2 commit b689201
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
14 changes: 5 additions & 9 deletions dat/maps/layna_forest_entrance.lua
Expand Up @@ -663,16 +663,14 @@ function _CreateEvents()
DialogueManager:AddDialogue(dialogue);
event = hoa_map.DialogueEvent("Kalya talks about the save point - part 4", dialogue);
event:AddEventLinkAtEnd("second_hero:SetNoCollision(true)");
event:AddEventLinkAtEnd("set hero coord to move_back_to_hero_event call");
event:AddEventLinkAtEnd("Set Camera");
EventManager:RegisterEvent(event);

-- TODO: Add support for path move event to VirtualSprite* target - obtaining the destination, only at start call and get rid of this hack.
event = hoa_map.ScriptedEvent("set hero coord to move_back_to_hero_event call", "update_move_to_hero_event_coord", "");
event = hoa_map.ScriptedSpriteEvent("Set Camera", hero, "SetCamera", "");
event:AddEventLinkAtEnd("2nd hero goes back to party");
EventManager:RegisterEvent(event);

-- NOTE: The actual destination is set just before the actual start call
move_back_to_hero_event = hoa_map.PathMoveSpriteEvent("2nd hero goes back to party", kalya_sprite, 0, 0, false);
move_back_to_hero_event = hoa_map.PathMoveSpriteEvent("2nd hero goes back to party", kalya_sprite, hero, false);
move_back_to_hero_event:AddEventLinkAtEnd("Map:Popstate()");
move_back_to_hero_event:AddEventLinkAtEnd("end of save point event");
EventManager:RegisterEvent(move_back_to_hero_event);
Expand Down Expand Up @@ -760,10 +758,8 @@ map_functions = {
move_next_to_hero_event:SetDestination(hero:GetXPosition(), hero:GetYPosition() + 2.0, false);
end,

update_move_to_hero_event_coord = function()
move_back_to_hero_event:SetDestination(hero:GetXPosition(), hero:GetYPosition(), false);
-- Also set back the camera to the visible hero
Map:SetCamera(hero, 800);
SetCamera = function(sprite)
Map:SetCamera(sprite, 800);
end,

Sprite_Collision_on = function(sprite)
Expand Down
45 changes: 31 additions & 14 deletions src/modes/map/map_events.cpp
Expand Up @@ -602,9 +602,9 @@ bool LookAtSpriteEvent::_Update() {
PathMoveSpriteEvent::PathMoveSpriteEvent(const std::string& event_id, uint16 sprite_id,
float x_coord, float y_coord, bool run) :
SpriteEvent(event_id, PATH_MOVE_SPRITE_EVENT, sprite_id),
_relative_destination(false),
_destination_x(x_coord),
_destination_y(y_coord),
_target_sprite(NULL),
_last_x_position(0.0f),
_last_y_position(0.0f),
_current_node_x(0.0f),
Expand All @@ -616,9 +616,9 @@ PathMoveSpriteEvent::PathMoveSpriteEvent(const std::string& event_id, uint16 spr
PathMoveSpriteEvent::PathMoveSpriteEvent(const std::string& event_id, VirtualSprite* sprite,
float x_coord, float y_coord, bool run) :
SpriteEvent(event_id, PATH_MOVE_SPRITE_EVENT, sprite),
_relative_destination(false),
_destination_x(x_coord),
_destination_y(y_coord),
_target_sprite(NULL),
_last_x_position(0.0f),
_last_y_position(0.0f),
_current_node_x(0.0f),
Expand All @@ -627,26 +627,44 @@ PathMoveSpriteEvent::PathMoveSpriteEvent(const std::string& event_id, VirtualSpr
_run(run)
{}

void PathMoveSpriteEvent::SetRelativeDestination(bool relative) {
PathMoveSpriteEvent::PathMoveSpriteEvent(const std::string& event_id, VirtualSprite* sprite,
VirtualSprite* target_sprite, bool run) :
SpriteEvent(event_id, PATH_MOVE_SPRITE_EVENT, sprite),
_destination_x(-1.0f),
_destination_y(-1.0f),
_target_sprite(target_sprite),
_last_x_position(0.0f),
_last_y_position(0.0f),
_current_node_x(0.0f),
_current_node_y(0.0f),
_current_node(0),
_run(run)
{}

void PathMoveSpriteEvent::SetDestination(float x_coord, float y_coord, bool run) {
if (MapMode::CurrentInstance()->GetEventSupervisor()->IsEventActive(GetEventID()) == true) {
IF_PRINT_WARNING(MAP_DEBUG) << "attempted illegal operation while event was active: "
<< GetEventID() << std::endl;
return;
}

_relative_destination = relative;
_destination_x = x_coord;
_destination_y = y_coord;
_target_sprite = NULL;
_path.clear();
_run = run;
}

void PathMoveSpriteEvent::SetDestination(float x_coord, float y_coord, bool run) {
void PathMoveSpriteEvent::SetDestination(VirtualSprite* target_sprite, bool run) {
if (MapMode::CurrentInstance()->GetEventSupervisor()->IsEventActive(GetEventID()) == true) {
IF_PRINT_WARNING(MAP_DEBUG) << "attempted illegal operation while event was active: "
<< GetEventID() << std::endl;
return;
}

_destination_x = x_coord;
_destination_y = y_coord;
_destination_x = -1.0f;
_destination_y = -1.0f;
_target_sprite = target_sprite;
_path.clear();
_run = run;
}
Expand All @@ -659,14 +677,13 @@ void PathMoveSpriteEvent::_Start() {
_last_y_position = _sprite->GetYPosition();
_sprite->is_running = _run;

// Set and check the destination position
if (_relative_destination) {
// Add The integer part of the source node, but keep the destination offset untouched.
_destination_x = hoa_utils::GetFloatInteger(_destination_x)
+ hoa_utils::GetFloatInteger(_last_x_position) + hoa_utils::GetFloatFraction(_destination_x);
_destination_y = hoa_utils::GetFloatInteger(_destination_y)
+ hoa_utils::GetFloatInteger(_last_y_position) + hoa_utils::GetFloatFraction(_destination_y);
// Only set the destination at start call since the target coord may have changed
// between the load time and the event actual start.
if (_target_sprite) {
_destination_x = _target_sprite->GetXPosition();
_destination_y = _target_sprite->GetYPosition();
}

MapPosition dest(_destination_x, _destination_y);

_path = MapMode::CurrentInstance()->GetObjectSupervisor()->FindPath(_sprite, dest);
Expand Down
26 changes: 15 additions & 11 deletions src/modes/map/map_events.h
Expand Up @@ -684,39 +684,43 @@ class PathMoveSpriteEvent : public SpriteEvent {
**/
PathMoveSpriteEvent(const std::string& event_id, VirtualSprite* sprite, float x_coord, float y_coord, bool run);

~PathMoveSpriteEvent()
{}

/** \brief Used to toggle whether or not the destination provided in the constructor is relative or absolute
*** \note Any previous existing paths are cleared when this function is called. If this function is called when
*** the event is active, no change will take place.
/** \param event_id The ID of this event
*** \param sprite A pointer to the sprite to move
*** \param target_sprite The target sprite to move the sprite to
*** \param run whether the character has to go there by walking or running
**/
void SetRelativeDestination(bool relative);
PathMoveSpriteEvent(const std::string& event_id, VirtualSprite* sprite, VirtualSprite* target_sprite, bool run);

~PathMoveSpriteEvent()
{}

/** \brief Used to change the destination coordinates after the class object has been constructed
*** \param x_coord The X coordinate to move the sprite to
*** \param y_coord The Y coordinate to move the sprite to
*** or:
*** \param target_sprite The target sprite to move the sprite to
*** \param run whether the character has to go there by walking or running
*** \note Any previous existing paths are cleared when this function is called. If this function is called when
*** the event is active, no change will take place.
*** This function is especially useful when trying to path move according to another NPC position, wichi may have
*** changed between the event declaration (at map load time) and the event actual start.
**/
void SetDestination(float x_coord, float y_coord, bool run);
void SetDestination(VirtualSprite* target_sprite, bool run);

Path GetPath() const
{ return _path; }
{ return _path; }

//! \brief Stops and frees the sprite from the control_event
void Terminate();

protected:
//! \brief When true, the destination coordinates are relative to the current position of the sprite. Otherwise the destination is absolute.
bool _relative_destination;

//! \brief Stores the destination coordinates for the path movement. These may be either absolute or relative coordinates.
float _destination_x, _destination_y;

//! \brief The destination target, useful when willing to reach a moving point.
VirtualSprite* _target_sprite;

//! \brief Used to store the previous coordinates of the sprite during path movement, so as to set the proper direction of the sprite as it moves
float _last_x_position, _last_y_position;

Expand Down
5 changes: 3 additions & 2 deletions src/modes/mode_bindings.cpp
Expand Up @@ -520,8 +520,9 @@ void BindModeCode() {
luabind::class_<PathMoveSpriteEvent, SpriteEvent>("PathMoveSpriteEvent")
.def(luabind::constructor<std::string, uint32, float, float, bool>())
.def(luabind::constructor<std::string, VirtualSprite*, float, float, bool>())
.def("SetRelativeDestination", &PathMoveSpriteEvent::SetRelativeDestination)
.def("SetDestination", &PathMoveSpriteEvent::SetDestination)
.def(luabind::constructor<std::string, VirtualSprite*, VirtualSprite*, bool>())
.def("SetDestination", (void(PathMoveSpriteEvent::*)(float, float, bool))&PathMoveSpriteEvent::SetDestination)
.def("SetDestination", (void(PathMoveSpriteEvent::*)(VirtualSprite*, bool))&PathMoveSpriteEvent::SetDestination)
];

luabind::module(hoa_script::ScriptManager->GetGlobalState(), "hoa_map")
Expand Down

0 comments on commit b689201

Please sign in to comment.