public void UnregisterCharacter(GridCharacter c) { if (chars.Contains(c)) //if c is in the array { chars.Remove(c); } }
// constructor for pathfinder object public Pathfinder(GridCharacter c, Node start, Node target, PathfindingComplete callback, GridManager gm) { this.gridManager = gm; character = c; startNode = start; endNode = target; completeCallback = callback; }
public void RegisterCharacter(GridCharacter c) // use 'c' to clearly indicate it is a holder var (see below) { // (here using "if (!chars.Contains(character))" becomes posibly strange to read/unintuitive if (!chars.Contains(c)) //if c is not in the array { chars.Add(c); } }
public void PathfinderCall(GridCharacter character, Node targetNode) { if (!isPathFinding) { isPathFinding = true; Node start = character.currentNode; Node target = targetNode; if (start != null && target != null) { PathfinderMaster.masterPathfinder.RequestPathFind(character, start, target, PathfinderCallback, gridManager); } else { isPathFinding = false; } } }
void PathfinderCallback(List <Node> p, GridCharacter c) { //Debug.LogWarning("Path CallBack, p.count = " + p.Count); isPathFinding = false; if (p == null) { //Debug.LogWarning("Path not valid"); return; } pathVis.positionCount = p.Count + 1; // tell the line how many positions/coordinates to contain List <Vector3> allPositions = new List <Vector3>(); allPositions.Add(c.currentNode.worldPos + Vector3.up * 0.2f); // add the initial node to start the path for (int i = 0; i < p.Count; i++) // load all the path positions { allPositions.Add(p[i].worldPos + Vector3.up * 0.2f); } c.LoadPath(p); // tell the character what path to take pathVis.SetPositions(allPositions.ToArray()); // draw a line using the loaded positions to show the path }
bool firstInit; // for animation public override void Execute(StateManager states, SessionManager sm, Turn turn) { GridCharacter c = states.currChar; if (!isInitialized) { if (c == null || index > c.currPath.Count - 1) { states.SetStartingState(); return; } isInitialized = true; startNode = c.currentNode; targetNode = c.currPath[index]; // if we go a little eover 1 while moving from node to node then we dont want to cut off a digit? // possibly removes stutter/jitter while moving float t_ = t - 1; t_ = Mathf.Clamp01(t_); t = t_; //move consistant speed float distance = Vector3.Distance(startNode.worldPos, targetNode.worldPos); speed = c.GetSpeed() / distance; Vector3 direction = targetNode.worldPos - startNode.worldPos; targetRotation = Quaternion.LookRotation(direction); startingRotation = c.transform.rotation; if (!firstInit) { c.PlayMovementAnimation(); firstInit = true; } } t += states.delta * speed; rotationT += states.delta * c.GetSpeed() * 2; // add seperate rotation speed later if (rotationT > 1) { rotationT = 1; // dont want this above 1 } c.transform.rotation = Quaternion.Slerp(startingRotation, targetRotation, rotationT); if (t > 1) { isInitialized = false; c.currentNode.character = null; c.currentNode = targetNode; c.currentNode.character = c; // change the target node to have a character on it index++; if (index > states.currChar.currPath.Count - 1) // we moved onto our path { t = 1; index = 0; states.SetStartingState(); c.PlayIdleAnimation(); firstInit = false; } } Vector3 targetPos = Vector3.Lerp(startNode.worldPos, targetNode.worldPos, t); c.transform.position = targetPos; }
public void RequestPathFind(GridCharacter character, Node start, Node target, Pathfinder.PathfindingComplete callback, GridManager gm) { Pathfinder newJob = new Pathfinder(character, start, target, callback, gm); toDoJobs.Add(newJob); }