From 9b742cf9cc82bd5484c84106d7ee1cb99de55de5 Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Thu, 9 May 2024 11:09:06 +0800 Subject: [PATCH 1/2] sovled avatar remove failure, would create nullptr on map --- include/Map/Tile.hpp | 11 ++++++----- src/Avatar/Avatar.cpp | 3 ++- src/Map/Map.cpp | 5 +++-- src/Mechanics/AvatarNavigator.cpp | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/Map/Tile.hpp b/include/Map/Tile.hpp index 23d1cc60..5c833e4a 100644 --- a/include/Map/Tile.hpp +++ b/include/Map/Tile.hpp @@ -68,19 +68,20 @@ class TileClass { } void removeAvatar(std::shared_ptr avatar) { - for (auto i : m_Avatars) { - if (i == avatar) { - std::remove(m_Avatars.begin(), m_Avatars.end(), i); + for (int i = 0; i < m_Avatars.size(); i++) { + if (m_Avatars[i] == avatar) { + auto it = m_Avatars.begin() + i; + m_Avatars.erase(it); + i--; + if (m_Avatars.size() < 4) { setWalkable(true); } if (m_Avatars.size() == 0) { setBuildable(true); } - return; } } - LOG_DEBUG("Remove Avatar Failed"); } void removeStructure() { diff --git a/src/Avatar/Avatar.cpp b/src/Avatar/Avatar.cpp index a58620f7..fbce6255 100644 --- a/src/Avatar/Avatar.cpp +++ b/src/Avatar/Avatar.cpp @@ -49,7 +49,8 @@ void Avatar::spawnedUpdate() { void Avatar::moveUpdate() { if (ifArrivedAtNextCell()) { - if (m_MovePath.size() >= 1) { + m_PrevCell = getCurrentCell(); + if (!m_MovePath.empty()) { m_CurrentDir = m_MovePath.front(); m_MovePath.pop_front(); } else { diff --git a/src/Map/Map.cpp b/src/Map/Map.cpp index 00e6cd1f..50802643 100644 --- a/src/Map/Map.cpp +++ b/src/Map/Map.cpp @@ -38,8 +38,9 @@ std::shared_ptr MapClass::getTileByCellPosition(glm::vec2 position) { if (position.x > m_MapWdith - 1 || position.y > m_MapHeight - 1 || position.x < 0 || position.y < 0) { - LOG_DEBUG("False Position Getting"); - return std::make_shared(UnitType::null, 0, 0, 0, ""); + std::cerr << "False Position Getting" << position.x << position.y + << std::endl; + return nullptr; } return m_Map[position.x][position.y]; } diff --git a/src/Mechanics/AvatarNavigator.cpp b/src/Mechanics/AvatarNavigator.cpp index 65c2f502..81dd25e3 100644 --- a/src/Mechanics/AvatarNavigator.cpp +++ b/src/Mechanics/AvatarNavigator.cpp @@ -282,7 +282,7 @@ bool AvatarNavigator::canResumeWalkingStraight(glm::vec2 currentcell, glm::vec2 destinationcell) { std::vector path; auto arrived = findStraightPath(currentcell, destinationcell, &path); - if (arrived || !path.empty()) { + if (arrived) { return true; } return false; From 621ebe089e7d61f5ab10f46273fdc0f2403108b9 Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Thu, 9 May 2024 11:42:21 +0800 Subject: [PATCH 2/2] map draw shift and zindex --- include/Map/Map.hpp | 1 + src/Map/Map.cpp | 10 +++++----- src/Scene/MapScene.cpp | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/Map/Map.hpp b/include/Map/Map.hpp index 2022d493..0bb97b6a 100644 --- a/include/Map/Map.hpp +++ b/include/Map/Map.hpp @@ -54,6 +54,7 @@ class MapClass : public Core::Drawable { void InitGrid(); private: + const glm::vec2 m_MapTransShift = {10, -10}; std::vector> m_Images; std::unordered_map> m_Tiles; unsigned int m_MapWdith = 0; diff --git a/src/Map/Map.cpp b/src/Map/Map.cpp index 50802643..9d8097cf 100644 --- a/src/Map/Map.cpp +++ b/src/Map/Map.cpp @@ -46,14 +46,14 @@ std::shared_ptr MapClass::getTileByCellPosition(glm::vec2 position) { } void MapClass::Draw(const Util::Transform &trans, const float zindex) { + auto maptrans = trans; + maptrans.translation.x += m_MapTransShift.x; + maptrans.translation.y += m_MapTransShift.y; - Util::Transform mapTrans; - - mapTrans.translation = m_MapPosition; for (auto i : m_Images) { - i->DrawUsingCamera(mapTrans, 1); + i->DrawUsingCamera(maptrans, zindex); } - m_Grid.DrawUsingCamera(mapTrans, zindex); + m_Grid.DrawUsingCamera(trans, zindex - 0.2); } void MapClass::Init(std::vector>> map, diff --git a/src/Scene/MapScene.cpp b/src/Scene/MapScene.cpp index 0fc95d73..802a71c2 100644 --- a/src/Scene/MapScene.cpp +++ b/src/Scene/MapScene.cpp @@ -17,5 +17,5 @@ void MapScene::Update() { Util::Transform trans; trans.scale = {1, 1}; trans.translation = {0, 0}; - m_Map->Draw(trans, 0); + m_Map->Draw(trans, -0.1); }