-
Notifications
You must be signed in to change notification settings - Fork 86
Pick object to follow when activity window is shown #1604
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
Draft
falbrechtskirchinger
wants to merge
6
commits into
Return-To-The-Roots:master
Choose a base branch
from
falbrechtskirchinger:follow-object
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4178da
Debug stuff (REMOVE BEFORE MERGE)
falbrechtskirchinger 199cc6b
Move detail namespace in Point.h
falbrechtskirchinger e42c4d5
Add euclidean distance function to Point class
falbrechtskirchinger b3a7e6a
Add function returning GameWorldView zoom factor
falbrechtskirchinger 021c270
Add class for picking and tracking objects
falbrechtskirchinger 4d318e4
Pick object to follow early
falbrechtskirchinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright (C) 2005 - 2023 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "PickedMovableObject.h" | ||
| #include "desktops/dskGameInterface.h" | ||
| #include "drivers/VideoDriverWrapper.h" | ||
| #include "world/GameWorldBase.h" | ||
| #include "world/GameWorldView.h" | ||
| #include "nodeObjs/noMovable.h" | ||
| #include <cmath> | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| namespace { | ||
| constexpr unsigned PickRadius = 2; | ||
| constexpr unsigned TrackRadius = 5; | ||
| constexpr auto PickedObjectExpiration = 5s; | ||
|
|
||
| // Make (ab-)use of CheckPointsInRadius() easier | ||
| constexpr bool CheckPointsBreak = true; | ||
| constexpr bool CheckPointsContinue = false; | ||
| } // namespace | ||
|
|
||
| PickedMovableObject PickedMovableObject::pick(const GameWorldView& gwv, MapPoint mapPt, DrawPoint drawPt, bool expire) | ||
| { | ||
| // DEBUG REMOVE BEFORE MERGE | ||
| unsigned i = 1; | ||
|
|
||
| const auto offset = gwv.GetOffset(); | ||
| const auto center = gwv.GetSize() / 2.f; | ||
| const auto zoomFactor = gwv.GetZoomFactor(); | ||
| const auto& world = gwv.GetWorld(); | ||
| const auto worldSize = world.GetSize() * DrawPoint(TR_W, TR_H); | ||
| auto minDistance = std::numeric_limits<float>::max(); | ||
|
|
||
| PickedMovableObject pmo; | ||
|
|
||
| world.CheckPointsInRadius( | ||
| mapPt, PickRadius, | ||
| [&](MapPoint curPt, unsigned) { | ||
| if(gwv.GetViewer().GetVisibility(curPt) != Visibility::Visible) | ||
| return CheckPointsContinue; | ||
|
|
||
| DrawPoint curDrawPt = world.GetNodePos(curPt); | ||
| if(curDrawPt.x < offset.x) | ||
| curDrawPt.x += worldSize.x; | ||
| if(curDrawPt.y < offset.y) | ||
| curDrawPt.y += worldSize.y; | ||
| curDrawPt -= offset; | ||
| for(const noBase& obj : world.GetFigures(curPt)) | ||
| { | ||
| const auto* movable = dynamic_cast<const noMovable*>(&obj); | ||
| if(!movable) | ||
| continue; | ||
|
|
||
| DrawPoint objDrawPt = curDrawPt; | ||
| if(movable->IsMoving()) | ||
| objDrawPt += movable->CalcWalkingRelative(); | ||
| objDrawPt = DrawPoint((objDrawPt - center) * zoomFactor + center); | ||
|
|
||
| // DEBUG REMOVE BEFORE MERGE | ||
| dskGameInterface::SetDebugPoint(i++, objDrawPt, 8, 4, MakeColor(255, 255, 0, 255)); | ||
|
|
||
| auto diff = Point<float>(objDrawPt - drawPt); | ||
| float distance = std::sqrt(diff.x * diff.x + diff.y * diff.y); | ||
| if(distance < minDistance) | ||
| { | ||
| pmo.id_ = movable->GetObjId(); | ||
| pmo.mapPt_ = curPt; | ||
| pmo.drawPt_ = objDrawPt; // TODO Do we need the unwrapped point here or was this always wrong? | ||
| minDistance = distance; | ||
| } | ||
| } | ||
|
|
||
| return CheckPointsContinue; | ||
| }, | ||
| true); | ||
|
|
||
| if(pmo.isValid() && expire) | ||
| pmo.expiration_.start(); | ||
|
|
||
| return pmo; | ||
| } | ||
|
|
||
| PickedMovableObject PickedMovableObject::pickAtCursor(const GameWorldView& gwv, bool expire) | ||
| { | ||
| return pick(gwv, gwv.GetSelectedPt(), DrawPoint(VIDEODRIVER.GetMousePos()), expire); | ||
| } | ||
|
|
||
| PickedMovableObject PickedMovableObject::pickAtViewCenter(const GameWorldView& gwv, bool expire) | ||
| { | ||
| const auto centerMapPt = gwv.GetWorld().MakeMapPoint((gwv.GetFirstPt() + gwv.GetLastPt()) / 2); | ||
| const auto centerDrawPt = DrawPoint(gwv.GetSize() / 2u); | ||
| return pick(gwv, centerMapPt, centerDrawPt, expire); | ||
| } | ||
|
|
||
| bool PickedMovableObject::isValid() const | ||
| { | ||
| return id_ != 0 && (!expiration_.isRunning() || expiration_.getElapsed() < PickedObjectExpiration); | ||
| } | ||
|
|
||
| void PickedMovableObject::cancelExpiration() | ||
| { | ||
| expiration_.stop(); | ||
| } | ||
|
|
||
| void PickedMovableObject::invalidate() | ||
| { | ||
| id_ = 0; | ||
| } | ||
|
|
||
| bool PickedMovableObject::track(const GameWorldView& gwv) | ||
| { | ||
| if(!isValid()) | ||
| return false; | ||
|
|
||
| const auto& world = gwv.GetWorld(); | ||
| const auto success = world.CheckPointsInRadius( | ||
| mapPt_, TrackRadius, | ||
| [&](MapPoint curPt, unsigned) { | ||
| if(gwv.GetViewer().GetVisibility(curPt) != Visibility::Visible) | ||
| return CheckPointsContinue; | ||
|
|
||
| for(const noBase& obj : world.GetFigures(curPt)) | ||
| { | ||
| if(obj.GetObjId() != id_) | ||
| continue; | ||
|
|
||
| const auto& movable = dynamic_cast<const noMovable&>(obj); | ||
| mapPt_ = curPt; | ||
| drawPt_ = world.GetNodePos(curPt); | ||
| if(movable.IsMoving()) | ||
| drawPt_ += movable.CalcWalkingRelative(); | ||
|
|
||
| return CheckPointsBreak; | ||
| } | ||
| return CheckPointsContinue; | ||
| }, | ||
| true); | ||
|
|
||
| if(!success) | ||
| invalidate(); | ||
|
|
||
| return success; | ||
| } | ||
|
|
||
| bool PickedMovableObject::track(GameWorldView& gwv, bool moveTo) | ||
| { | ||
| const auto success = track(gwv); | ||
| if(success && moveTo) | ||
| gwv.MoveTo(drawPt_ - gwv.GetSize() / 2u); | ||
| return success; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (C) 2005 - 2023 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "DrawPoint.h" | ||
| #include "Timer.h" | ||
| #include "gameTypes/MapCoordinates.h" | ||
|
|
||
| class GameWorldView; | ||
|
|
||
| class PickedMovableObject | ||
| { | ||
| public: | ||
| static PickedMovableObject pick(const GameWorldView& gwv, MapPoint mapPt, DrawPoint drawPt, bool expire); | ||
| static PickedMovableObject pickAtCursor(const GameWorldView& gwv, bool expire); | ||
| static PickedMovableObject pickAtViewCenter(const GameWorldView& gwv, bool expire); | ||
|
|
||
| PickedMovableObject() = default; | ||
|
|
||
| unsigned id() const { return id_; } | ||
| bool isValid() const; | ||
|
|
||
| void cancelExpiration(); | ||
| void invalidate(); | ||
|
|
||
| bool track(const GameWorldView& gwv); | ||
| bool track(GameWorldView& gwv, bool moveTo); | ||
|
|
||
| private: | ||
| unsigned id_ = 0; | ||
| MapPoint mapPt_{}; | ||
| DrawPoint drawPt_{}; | ||
| Timer expiration_{}; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.