Skip to content

Commit

Permalink
[#151]: Fix issues with updating collision map
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Nov 27, 2023
1 parent 6c179f9 commit 692b23e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
2 changes: 2 additions & 0 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ Editor::CreateLevel(const std::string& name, const glm::ivec2& size)
m_levelFileName = (LEVELS_DIR / (name + ".dgl")).string();
gui_.LevelLoaded(m_currentLevel);

m_currentLevel->GenerateTextureForCollision();

SetupRendererData();
}

Expand Down
12 changes: 8 additions & 4 deletions editor/gui/editor_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,13 @@ void
EditorGUI::RenderCreateNewLevelWindow()
{
const auto halfSize = windowSize_ / 2.0f;
std::unordered_map< std::string, glm::ivec2 > sizes = {{"Small", glm::ivec2{4096, 4096}},
std::unordered_map< std::string, glm::ivec2 > sizes = {{"Small", glm::ivec2{8192, 8192}},
{"Medium", glm::ivec2{16384, 16384}},
{"Large", glm::ivec2{65536, 65536}}};
static glm::ivec2 size = {1024, 1024};

static std::string name = "DummyLevelName";
static std::string currentSize = "Small";
static glm::ivec2 size = sizes[currentSize];

ImGui::SetNextWindowPos({halfSize.x - 160, halfSize.y - 60});
ImGui::SetNextWindowSize({300, 180});
Expand Down Expand Up @@ -904,6 +905,7 @@ EditorGUI::RenderGameObjectMenu() // NOLINT
if (ImGui::Checkbox("##Has Collision", &collision))
{
currentlySelectedGameObject_->SetHasCollision(collision);
parent_.GetLevel().UpdateCollisionTexture();
}
});

Expand All @@ -926,6 +928,7 @@ EditorGUI::RenderGameObjectMenu() // NOLINT
if (ImGui::SliderFloat2("##Size", &sprite_size.x, 10, 1000))
{
currentlySelectedGameObject_->SetSize(sprite_size);
parent_.GetLevel().UpdateCollisionTexture();
}
});

Expand Down Expand Up @@ -1021,8 +1024,9 @@ EditorGUI::RenderGameObjectMenu() // NOLINT
time::Timer::ConvertToMs(animatablePtr->GetAnimationDuration()).count();
if (parent_.IsObjectAnimated())
{
timer += static_cast< float >(parent_.GetDeltaTime().count());
timer = glm::min(animationDuration, timer);
// timer += static_cast< float >(parent_.GetDeltaTime().count());
// timer = glm::min(animationDuration, timer);
timer = animatablePtr->GetTotalTimeElapsed().count();
}

ImGui::SameLine();
Expand Down
3 changes: 2 additions & 1 deletion editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
int
main(int /* argc */, char** /* argv */)
{
looper::Editor editor(looper::USE_DEFAULT_SIZE);
// looper::Editor editor(looper::USE_DEFAULT_SIZE);
looper::Editor editor({1920, 1080});
editor.MainLoop();

return EXIT_SUCCESS;
Expand Down
21 changes: 19 additions & 2 deletions engine/game/animatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ Animatable::GetAnimationType() const
void
Animatable::UpdateAnimationPoint()
{
if (currentState_.m_isReverse
and currentState_.m_currentAnimationPoint == m_animationPoints.begin())
{
return;
}
if (not currentState_.m_isReverse
and currentState_.m_currentAnimationPoint == m_animationPoints.end())
{
return;
}

currentState_.m_isReverse ? --currentState_.m_currentAnimationPoint
: ++currentState_.m_currentAnimationPoint;

Expand Down Expand Up @@ -85,8 +96,7 @@ Animatable::SetCorrectAnimationPoint(time::milliseconds& updateTime)
auto animationDurationMs =
time::Timer::ConvertToMs(currentState_.m_currentAnimationPoint->m_timeDuration);

while (updateTime >= animationDurationMs
&& currentState_.m_currentAnimationPoint != m_animationPoints.begin())
while (updateTime >= animationDurationMs)
{
updateTime -= animationDurationMs;
animationValue += currentState_.m_currentAnimationEnd - currentState_.m_currentAnimationBegin;
Expand Down Expand Up @@ -149,6 +159,7 @@ Animatable::Animate(time::milliseconds updateTime)
auto animateBy = glm::vec2();

currentState_.m_animationFinished = false;
currentState_.m_totalTimeElapsed += updateTime;

auto currentAnimationStepSize = AnimateInCurrentSection(updateTime);
if (currentState_.m_currentTimeElapsed < currentState_.m_currentAnimationPoint->m_timeDuration)
Expand Down Expand Up @@ -315,6 +326,12 @@ Animatable::ResetAnimation()
currentState_.m_totalTimeElapsed = time::milliseconds(0);
}

time::milliseconds
Animatable::GetTotalTimeElapsed() const
{
return currentState_.m_totalTimeElapsed;
}

void
Animatable::UpdateAnimationData()
{
Expand Down
3 changes: 3 additions & 0 deletions engine/game/animatable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class Animatable
[[nodiscard]] glm::vec2
GetAnimationStartLocation() const;

[[nodiscard]] time::milliseconds
GetTotalTimeElapsed() const;

void
Update(bool isReverse);

Expand Down
2 changes: 2 additions & 0 deletions engine/game/game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ GameObject::SetHasCollision(bool hasCollision)
{
m_appHandle.GetLevel().GetPathfinder().SetNodeFreed(node, m_id);
}

m_currentGameObjectState.m_occupiedNodes.clear();
}
else
{
Expand Down
10 changes: 7 additions & 3 deletions engine/game/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Level::GameObjectMoved(const std::array< glm::vec2, 4 >& box,

if (m_pathFinder.IsInitialized())
{
// TODO: Discard common tiles and only free/occupy unique ones
for (auto tileID : currentTiles)
{
m_pathFinder.SetNodeFreed(tileID, objectID);
Expand All @@ -356,18 +357,21 @@ Level::GameObjectMoved(const std::array< glm::vec2, 4 >& box,
std::vector< Tile >
Level::GetTilesFromBoundingBox(const std::array< glm::vec2, 4 >& box) const
{
std::vector< Tile > ret;
std::set< Tile > ret;

auto insertToVec = [&ret](std::vector< Tile > tiles) {
ret.insert(ret.end(), tiles.begin(), tiles.end());
for (const auto& tile : tiles)
{
ret.insert(tile);
}
};

insertToVec(GetTilesAlongTheLine(box[0], box[3]));
insertToVec(GetTilesAlongTheLine(box[3], box[2]));
insertToVec(GetTilesAlongTheLine(box[2], box[1]));
insertToVec(GetTilesAlongTheLine(box[1], box[0]));

return ret;
return std::vector< Tile >{ret.begin(), ret.end()};
}

Tile
Expand Down
8 changes: 4 additions & 4 deletions engine/game/path_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ PathFinder::SetNodeOccupied(const Tile& nodeCoords, Object::ID objectID)
{
if (nodeCoords != INVALID_TILE)
{
auto node_found = GetNodeItFromTile(nodes_, nodeCoords);
node_found->occupied_ = true;
node_found->objectsOccupyingThisNode_.push_back(objectID);
auto nodeFound = GetNodeItFromTile(nodes_, nodeCoords);
nodeFound->occupied_ = true;
nodeFound->objectsOccupyingThisNode_.push_back(objectID);

nodesModifiedLastFrame_.insert(node_found->id_);
nodesModifiedLastFrame_.insert(nodeFound->id_);
}
}

Expand Down
2 changes: 1 addition & 1 deletion engine/renderer/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void
Sprite::SetTranslateValue(const glm::vec2& translateBy)
{
currentState_.currentPosition_ = initialPosition_ + glm::vec3{translateBy, 0.0f};
currentState_.translateVal_ += translateBy;
currentState_.translateVal_ = translateBy;

changed_ = true;
}
Expand Down

0 comments on commit 692b23e

Please sign in to comment.