public void Update() { //Console.WriteLine("Updating Adjacency List of Entity " + m_Owner.ID + ".\nSearching through " + m_Entities.Count + " entities.\n Using a radius of " + m_Radius); //work in squared distances float radiusSquared = m_Radius * m_Radius; //clear the list m_Edges.Clear(); //loop through all the entities for (int i = 0; i < m_Entities.Count; i++) { //Console.Write("Entity " + m_Entities[i].ID + ": "); if (m_Entities[i].ID != m_Owner.ID) { //get vector to target Vector3 toTarget = m_Entities[i].Position - m_Owner.Position; //get length(squared) of toTarget Vector float lengthSquared = toTarget.LengthSquared(); //Console.WriteLine("Squared Distance = " + lengthSquared + "."); //see if it's within the radius if (lengthSquared <= radiusSquared) { //get the actual distance float distance = (float)Math.Sqrt(lengthSquared); //get the direction of the toTarget vector float angle = (float)Math.Atan2(-toTarget.Z, toTarget.X); //[-pi, pi) angle = MathHelper.WrapAngle(angle); //target angle float targetAngle = -MathHelper.WrapAngle(m_Owner.Heading - angle + MathHelper.ToRadians(90)); targetAngle = MathHelper.WrapAngle(targetAngle); if (targetAngle < 0) targetAngle += MathHelper.ToRadians(360.0f); //create the new object to insert into the list AgentAdjacencyListEdge edge = new AgentAdjacencyListEdge(m_Entities[i], distance, targetAngle); //insert it into the list m_Edges.Add(edge); //Console.WriteLine("Entity " + edge.Entity.ID + " added to list"); } } } //Console.WriteLine("Update Complete"); }
/// <summary> /// /// </summary> /// <param name="agent"></param> public override void Execute(Agent agent) { //agent.MaxSpeed = Agent.PANIC_SPEED; agent.TurnSpeed = Agent.PANIC_SPEED; if (!agent.NearBomb()) { agent.NavPath = null; agent.ChangeState(new State_Navigate()); } else { //Console.WriteLine("Executing navigate for agent " + agent.ID); //return; // Give the agent time to move to prevent constant course correction if (agent.NavDelay > DateTime.Now && agent.NavPath != null && agent.NextNode != null) { //Console.WriteLine("Navigation Delay"); agent.NextNode = agent.MoveAlongPath(agent.NavPath, agent.NextNode); return; } else if (agent.NavDelay < DateTime.Now) { //Console.WriteLine("Adding NavDelay"); agent.NavDelay = DateTime.Now.AddSeconds(5); } //find the smallest distance to the escape position AgentAdjacencyListEdge escapeEdge = new AgentAdjacencyListEdge(); GameEntity escapeNode = escapeEdge.Entity; escapeEdge.Distance = float.MaxValue; //find node closest to agent AgentAdjacencyListEdge closestEdge = new AgentAdjacencyListEdge(); GameEntity closestNode = closestEdge.Entity; closestEdge.Distance = float.MaxValue; //loop through all the edges in the adjacency list of nodes //check all the distances and retain the smallest distance //and record that node as the node to traverse to for (int i = 0; i < agent.AdjacencyListNodes.Edges.Count; i++) { //get distance from node to escape position float distance = Vector3.Distance(agent.StartingLocation, agent.AdjacencyListNodes.Edges[i].Entity.Position); //compare the distances if (distance < escapeEdge.Distance) { //set new escape edge escapeEdge = agent.AdjacencyListNodes.Edges[i]; //set the new escape node escapeNode = ((Node)escapeEdge.Entity); } //comparison for closest edge distance = Vector3.Distance(agent.Position, agent.AdjacencyListNodes.Edges[i].Entity.Position); //compare the distances if (distance < closestEdge.Distance) { //set the closest edge closestEdge = agent.AdjacencyListNodes.Edges[i]; //set the closest node closestNode = ((Node)closestEdge.Entity); } } agent.NavPath = agent.FindPath(((Node)closestNode), ((Node)escapeNode)); agent.NextNode = (Node)closestNode; } }