Skip to content

Commit

Permalink
Improved navigation control. Fixes #116
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorma committed Nov 26, 2020
1 parent bea19d4 commit 6a47de4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Assets/uAdventureGeo/Scripts/Runner/GeoElementMB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ protected override bool CustomChecks()

protected override void Execute()
{
if (NavigationController.Instance)
{
NavigationController.Instance.SomethingReached(Holder);
}

Game.Instance.GameState.BeginChangeAmbit();
latLonOnExecute = LatLon;
base.Execute();
Expand Down
53 changes: 44 additions & 9 deletions Assets/uAdventureGeo/Scripts/Runner/NavigationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ public void Update()
}
}

public void SomethingReached(GameObject gb)
{
if (navigating && currentStep != null && currentStep.Reference == gb.name)
{
var mb = GetReference(gb.name);
if(mb != null)
{
// Check if reached
bool reached = IsReached(mb);
UpdateArrow(reached);

// Go next
if (reached && !currentStep.LockNavigation && CompleteStep(currentStep))
{
// If the step is completed successfully we set the current step to null
currentStep = null;

if (stepCompleted.All(kv => kv.Value))
{
// Navigation finished
navigating = false;
SaveNavigation(Game.Instance.GameState.GetMemory("geo_extension"));
DestroyImmediate(this.gameObject);
}
}
}
}
}



// --------------------
Expand All @@ -204,7 +233,6 @@ private NavigationStep FindNextStep(List<NavigationStep> steps)
return steps.Find(s => !stepCompleted[s]); // The first not completed
}


private bool IsReached(NavigationStep currentStep)
{
if (!character)
Expand All @@ -213,23 +241,28 @@ private bool IsReached(NavigationStep currentStep)
}

var mb = GetReference(currentStep.Reference);
if (mb == null)
return false; // If the element is not there, just try to skip it

return IsReached(mb);
}

private bool IsReached(MonoBehaviour mb)
{
if (mb == null)
return false; // If the element is not there, just try to skip it
else if (mb is GeoPositioner)
{
var wrap = mb as GeoPositioner;
var position = (Vector2d)wrap.Context.TransformManagerParameters["Position"];
var interactionRange = (float) wrap.Context.TransformManagerParameters["InteractionRange"];

var distance = GM.SeparationInMeters(position, character.LatLon);
var interactionRange = (float)wrap.Context.TransformManagerParameters["InteractionRange"];

var realDistance = GM.SeparationInMeters(position, GeoExtension.Instance.Location);

// Is inside if the character is in range but also the real coords are saying so
// Otherwise, if there is no character or gps, only one of the checks is valid

return (!character || distance < interactionRange)
&& (!GeoExtension.Instance.IsStarted() || realDistance < interactionRange)
&& (GeoExtension.Instance.IsStarted() || character);
return realDistance < interactionRange;
}
else if (mb is GeoElementMB)
{
Expand All @@ -238,12 +271,12 @@ private bool IsReached(NavigationStep currentStep)
// Is inside if the character is inside the influence but also the real coords are saying so
// Otherwise, if there is no character or gps, only one of the checks is valid

var location = character ? character.LatLon : GeoExtension.Instance.Location;
var location = GeoExtension.Instance.Location;
if (geomb.Geometry.InsideInfluence(location))
{
if (geomb.Geometry.Type == GMLGeometry.GeometryType.LineString)
{
var step = 0;
var step = 0;
completedElementsForStep.TryGetValue(currentStep, out step);
var position = geomb.Geometry.Points[step];
var distance = GM.SeparationInMeters(position, location);
Expand All @@ -256,6 +289,8 @@ private bool IsReached(NavigationStep currentStep)
else return false;
}



/// <summary>
/// To complete the step, all the elements inside the step must be completed.
/// For all the elements except for paths (LineString geometries) the elements are one,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void Update()
Type = GMLGeometry.GeometryType.Point
};
newManager.Player = character;
newManager.Holder = positioner.gameObject;
geoActionManagers.Add(newManager);
}
}
Expand Down

0 comments on commit 6a47de4

Please sign in to comment.