Skip to content

Commit

Permalink
fix is_walkable
Browse files Browse the repository at this point in the history
do not compare vec2
remove internal goToPosition function
use using for Lua not defines
  • Loading branch information
pinguin999 committed Nov 21, 2024
1 parent fef8050 commit bf0ee53
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ std::deque<jngl::Vec2> Background::getPathToTarget(jngl::Vec2 start, jngl::Vec2
}
}

if (current->coordinates == target)
if (boost::qvm::mag_sqr(current->coordinates - target) < 1)
{
break;
}
Expand Down Expand Up @@ -289,7 +289,7 @@ Node *Background::findNodeOnList(const std::vector<Node *> &nodes_, jngl::Vec2 c
{
for (const auto node : nodes_)
{
if (node->coordinates == coordinates_)
if (boost::qvm::mag_sqr(node->coordinates - coordinates_) < 0.5)
{
return node;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ bool Background::is_walkable(jngl::Vec2 position) const
{
for (const auto &obj : _game->gameObjects)
{
auto *non_walkable = spine::spSkeletonBounds_containsPointMatchingName(obj->bounds, "non_walkable_area", static_cast<float>(position.x) - static_cast<float>(obj->getPosition().x), static_cast<float>(position.y) - static_cast<float>(obj->getPosition().y));
auto *non_walkable = spine::spSkeletonBounds_containsPointMatchingName(obj->bounds, "non_walkable_area", static_cast<float>(position.x), static_cast<float>(position.y));
if (non_walkable)
{
return false;
Expand Down
6 changes: 3 additions & 3 deletions src/dialog/dialog_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void DialogManager::play(const std::string &dialogName, jngl::Vec2, const sol::f
dialog_callback = callback;
}else
{
jngl::debugLn("Dialog " + dialogName + " not found.");
jngl::error("Dialog " + dialogName + " not found.");
}
}

Expand Down Expand Up @@ -68,12 +68,12 @@ void DialogManager::step()
if(selected_index >= 0)
{
auto direction = _game->pointer->getMovementStep();
if (direction == jngl::Vec2(0, 1))
if (boost::qvm::mag_sqr(direction - jngl::Vec2(0, 1)) < 0.5)
{
selected_index = std::max(selected_index - 1, 0);
}

if (direction == jngl::Vec2(0, -1))
if (boost::qvm::mag_sqr(direction - jngl::Vec2(0, -1)) < 0.5)
{
selected_index = std::min(selected_index + 1, (int)choiceTexts.size() - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void Game::loadScene(const std::string& level)
setCameraPositionImmediately(player->calcCamPos());
}

runAction(level, std::static_pointer_cast<SpineObject>(newScene->background));
runAction(level, newScene->background);
}

Game::~Game()
Expand Down
8 changes: 0 additions & 8 deletions src/interactable_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,3 @@ void InteractableObject::draw() const
}
#endif
}

void InteractableObject::goToPosition(jngl::Vec2 position, const sol::function &callback)
{
if (auto _game = game.lock())
{
_game->player->addTargetPositionImmediately(this->position + position, callback);
}
}
2 changes: 0 additions & 2 deletions src/interactable_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class InteractableObject : public SpineObject

void draw() const override;

void goToPosition(jngl::Vec2 position, const sol::function &callback);

void registerToDelete();
void setLuaIndex(const std::string &index) { luaIndex = index; };

Expand Down
24 changes: 12 additions & 12 deletions src/lua.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "game.hpp"
#include "interactable_object.hpp"

#define LuaSpineObject std::string
#define LuaSpineAnimation std::string
#define LuaSpineSkin std::string
#define LuaSpinePoint std::string
#define LuaDialog std::string
#define LuaScene std::string
#define LuaAudio std::string
#define LuaLanguage std::string
using LuaSpineObject = std::string;
using LuaSpineAnimation = std::string;
using LuaSpineSkin = std::string;
using LuaSpinePoint = std::string;
using LuaDialog = std::string;
using LuaScene = std::string;
using LuaAudio = std::string;
using LuaLanguage = std::string;

static std::optional<jngl::Vec2> getPointPosition(std::shared_ptr<Game> game, const std::string &pointName)
{
Expand Down Expand Up @@ -413,10 +413,10 @@ void Game::setupLuaFunctions()
auto position = obj->getPoint(point_name);
if (!position)
{
jngl::debugLn("Point " + point_name + " not found.");
jngl::error("Point " + point_name + " not found.");
return;
}
std::static_pointer_cast<InteractableObject>(obj)->goToPosition(*position, callback.value());
player->addTargetPositionImmediately(obj->getPosition() + *position, callback.value());
pointer->setPrimaryHandled();
// TODO Write Players position to Lua
});
Expand All @@ -437,10 +437,10 @@ void Game::setupLuaFunctions()
auto position = obj->getPoint(point_name);
if (!position)
{
jngl::debugLn("Point " + point_name + " not found.");
jngl::error("Point " + point_name + " not found.");
return;
}
std::static_pointer_cast<InteractableObject>(obj)->goToPosition(*position, callback.value());
player->addTargetPositionImmediately(obj->getPosition() + *position, callback.value());
pointer->setPrimaryHandled();
// TODO Write Players position to Lua
}
Expand Down
6 changes: 3 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void Player::addTargetPositionImmediately(jngl::Vec2 target, const sol::function
position = target;
}
path.clear();
if (target != position)
if (boost::qvm::mag_sqr(target - position) > 0.5)
{
newPath = _game->currentScene->background->getPathToTarget(position, target);

Expand Down Expand Up @@ -143,15 +143,15 @@ bool Player::step(bool /*force*/)
return false;
}

if (path.size() > 1 && position == path.front())
if (path.size() > 1 && boost::qvm::mag_sqr(position - path.front()) < 0.5)
{
path.pop_front();
setTargentPosition(path.front());
setDirection();
}

jngl::Vec2 tmp_target_position = target_position - position;
if (tmp_target_position == jngl::Vec2(0, 0) && currentAnimation == (*_game->lua_state)["config"]["player_walk_animation"])
if (boost::qvm::mag_sqr(tmp_target_position - jngl::Vec2(0, 0)) < 0.5 && currentAnimation == (*_game->lua_state)["config"]["player_walk_animation"])
{
currentAnimation = (*_game->lua_state)["config"]["player_idle_animation"];
// Callback to Lua
Expand Down
6 changes: 3 additions & 3 deletions src/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool Pointer::step(bool)
float gamepad_speed_multiplier = (*_game->lua_state)["config"]["gamepad_speed_multiplier"];
auto move = control->getMovement() * gamepad_speed_multiplier;
auto movesec = control->getSecondaryMovement();
if (move != jngl::Vec2(0, 0))
if (boost::qvm::mag_sqr(move - jngl::Vec2(0, 0)) > 0.5)
{
position += move;
position.x = std::max(position.x, -screensize.x / _game->getCameraZoom() / 2);
Expand All @@ -56,13 +56,13 @@ bool Pointer::step(bool)
position.y = std::min(position.y, screensize.y / _game->getCameraZoom() / 2);
target_position = position;
}
else if (movesec != jngl::Vec2(0, 0))
else if (boost::qvm::mag_sqr(movesec - jngl::Vec2(0, 0)) > 0.5)
{
position.x = screensize.x * jngl::getScaleFactor() / 2.0 + movesec.x * screensize.x * jngl::getScaleFactor() / 2.0;
position.y = screensize.y * jngl::getScaleFactor() / 2.0 + movesec.y * screensize.y * jngl::getScaleFactor() / 2.0;
target_position = position;
}
else if (target_position != mouse_pose && last_mouse_pose != mouse_pose)
else if (boost::qvm::mag_sqr(target_position - mouse_pose) > 0.5 && boost::qvm::mag_sqr(last_mouse_pose - mouse_pose) > 0.5)
{
target_position = mouse_pose;
position = mouse_pose;
Expand Down

0 comments on commit bf0ee53

Please sign in to comment.