Skip to content

Commit

Permalink
piece movement changes
Browse files Browse the repository at this point in the history
stopped moving coroutine from firing every frame, added softdamp to move to curve movement, added OnCompleteMove delegate event
  • Loading branch information
wysiwyggins committed Apr 29, 2020
1 parent f3ba06e commit 2a1027b
Show file tree
Hide file tree
Showing 153 changed files with 1,758 additions and 19,744 deletions.
21 changes: 7 additions & 14 deletions Prototype/.vs/Prototype/xs/UserPrefs.xml
@@ -1,26 +1,19 @@
<Properties StartupConfiguration="{B1235EE4-71EE-C815-28F8-C3378E5ADB84}|">
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/SimpleTilePathfinding/Script/Path.cs">
<MonoDevelop.Ide.Workbench ActiveDocument="Assets/Scripts/Piece.cs">
<Files>
<File FileName="Assets/Scripts/Piece.cs" Line="75" Column="38" />
<File FileName="Assets/Scripts/GameController.cs" />
<File FileName="Assets/Scripts/Cursor.cs" Line="1" Column="1" />
<File FileName="Assets/SimpleTilePathfinding/Demo/Ant.cs" Line="1" Column="1" />
<File FileName="Assets/SimpleTilePathfinding/Demo/Mouse.cs" Line="1" Column="1" />
<File FileName="Assets/SimpleTilePathfinding/Demo/Peter.cs" Line="1" Column="1" />
<File FileName="Assets/SimpleTilePathfinding/Script/NavNode.cs" Line="1" Column="1" />
<File FileName="Assets/SimpleTilePathfinding/Script/Path.cs" Line="1" Column="1" />
<File FileName="Assets/Scripts/Piece.cs" Line="1" Column="1" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State name="__root__">
<Node name="Prototype" expanded="True">
<Node name="Assets" expanded="True">
<Node name="Scripts" expanded="True" />
<Node name="Scripts" expanded="True">
<Node name="Piece.cs" selected="True" />
</Node>
<Node name="SimpleTilePathfinding" expanded="True">
<Node name="Demo" expanded="True" />
<Node name="Script" expanded="True">
<Node name="Path.cs" selected="True" />
</Node>
<Node name="Script" expanded="True" />
</Node>
</Node>
</Node>
Expand All @@ -30,7 +23,7 @@
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.ItemProperties.Assembly-CSharp PreferredExecutionTarget="9e6d3057-faa8-431f-8e50-182dc00381f4" />
<MonoDevelop.Ide.ItemProperties.Assembly-CSharp PreferredExecutionTarget="0ed823ec-d964-4e40-9bb1-9c6e3e768c7f" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Prototype/Assembly-CSharp.csproj
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Assets\Scripts\GameController.cs" />
<Compile Include="Assets\Scripts\MapTile.cs" />
<Compile Include="Assets\Scripts\Piece.cs" />
<Compile Include="Assets\Scripts\PieceManager.cs" />
<Compile Include="Assets\SimpleTilePathfinding\Demo\Ant.cs" />
<Compile Include="Assets\SimpleTilePathfinding\Demo\Mouse.cs" />
<Compile Include="Assets\SimpleTilePathfinding\Demo\Peter.cs" />
Expand Down
67 changes: 38 additions & 29 deletions Prototype/Assets/Scripts/Piece.cs
Expand Up @@ -6,11 +6,12 @@
public class Piece : MonoBehaviour
{
//piece attributes
public delegate void PieceAction();
public delegate void PieceAction();
public static event PieceAction OnCompleteMove;

Vector3Int cellPosition;
public Vector3Int[] cellPositions;
public string pieceName = "unnamed";
public string pieceName = "unnamed";
public int strength = 0; // can take pieces with strength under this number
public int range = 1; //this is the move range
public bool isPlayer = true;
Expand All @@ -23,20 +24,28 @@ public class Piece : MonoBehaviour
//pathing
private SimplePF2D.Path path;
private Rigidbody2D rb;
private float moveSpeed = 2; //speed for Moving()
private float moveSpeed = 6f; //speed for Moving()
private bool isStationary = true; //not using this yet
Coroutine MoveIE;

SimplePathFinding2D pf;

// Start is called before the first frame update
void Start()
{
grid = GameController.instance.grid;
rb = GetComponent<Rigidbody2D>();
SimplePathFinding2D pf = GameObject.Find("Grid").GetComponent<SimplePathFinding2D>();
pf = GameObject.Find("Grid").GetComponent<SimplePathFinding2D>();
path = new SimplePF2D.Path(pf);
PieceManager.AllPieces.Add(this);
}


private void OnDestroy()
{
PieceManager.AllPieces.Remove(this);
}


// Update is called once per frame
void Update()
{
Expand All @@ -47,11 +56,7 @@ void Update()
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseWorldPos.z = 0.0f;





if (Input.GetMouseButtonDown(0)) //click the mouse
if (Input.GetMouseButtonDown(0) && path.IsGenerated() == false) //click the mouse
{

Vector3Int coordinate = grid.WorldToCell(mouseWorldPos); //get a hex cell coordinate from a mouse click
Expand All @@ -60,40 +65,44 @@ void Update()
path.CreatePath(position, mouseWorldPos); // generate a path

}
if (path.IsGenerated()) //once there's a path
if (path.IsGenerated() && !following) //once there's a path
{

StartCoroutine(followPath());
isStationary = false;
}
}

}

bool following = false;

IEnumerator followPath()
{
following = true;
List<Vector3Int> cellPositions = path.GetPathPointList(); // a list of grid positions in the path
for (int i = 0; i < cellPositions.Count; i++) //Loop through them (lists have a "count", not a "length")
{
MoveIE = StartCoroutine(Moving(i));
yield return MoveIE;

}
}
Debug.Log("Get path point");
Vector3 targetPos = path.GetPathPointWorld(i);

IEnumerator Moving(int positionNumber)
{
Vector3 vel = Vector3.zero;
while (Vector3.Distance(transform.position, targetPos) > 0.01f)
{
transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref vel, 0.5f, moveSpeed);

// transform.position = Vector3.MoveTowards(transform.position, targetPos, Time.deltaTime * moveSpeed);
yield return new WaitForEndOfFrame();
}

Debug.Log("Reached path point");
yield return new WaitForSeconds(0.05f);

while (transform.position != path.GetPathPointWorld(positionNumber)) //as long as you're not at the destination (world point converted from grid point)
{
Debug.Log("Moving to: " + path.GetPathPointWorld(positionNumber));
transform.position = Vector3.MoveTowards(transform.position, path.GetPathPointWorld(positionNumber), moveSpeed * Time.deltaTime); //this is not happy
//transform.position = path.GetPathPointWorld(currentPosition);
yield return null;
}
isStationary = true;

if (OnCompleteMove != null)
OnCompleteMove();
path = new SimplePF2D.Path(pf);
following = false;
}

}

}
23 changes: 23 additions & 0 deletions Prototype/Assets/Scripts/PieceManager.cs
@@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class PieceManager
{
public static List<Piece> AllPieces = new List<Piece>();

public static Piece GetPieceAtPos(Vector3 aPos)
{
foreach (Piece aPiece in AllPieces)
{
//if the piece is at aPos, return that piece
// return aPiece;
}
return null;
}

//public static Piece[] GetAllPiecesOfKind()
//{
// //do some stuff
//}
}
11 changes: 11 additions & 0 deletions Prototype/Assets/Scripts/PieceManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Prototype/Library/ArtifactDB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2a1027b

Please sign in to comment.