AStarNode2D.cs :  » Game » Killer-Instinct » KillerInstinct » AI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » Killer Instinct 
Killer Instinct » KillerInstinct » AI » AStarNode2D.cs
using System;
using System.Collections;

namespace KillerInstinct.AI{
  /// A node class for doing pathfinding on a 2-dimensional map
  public class AStarNode2D : AStarNode
  {
    private int FX;
    private int FY;
    // the X-coordinate of the node
    public int X 
    {
      get 
      { return FX; }
    }
    // the Y-coordinate of the node
    public int Y
    {
      get
      { return FY; }
    }
    // Constructor for a node in a 2-dimensional map
    public AStarNode2D(AStarNode parent, AStarNode goalNode, double ACost, int AX, int AY)
      : base(parent, goalNode, ACost)
    {
      FX = AX;
      FY = AY;
    }
    // Adds a successor to a list if it is not impassible or the parent node
    private void AddSuccessor(ArrayList ASuccessors,int AX,int AY) 
    {
      int CurrentCost = MapClass.GetMap(AX,AY);
      if(CurrentCost == -1) 
      {
        return;
      }
      AStarNode2D NewNode = new AStarNode2D(this,GoalNode,Cost + CurrentCost,AX,AY);
      if(NewNode.IsSameState(Parent)) 
      {
        return;
      }
      ASuccessors.Add(NewNode);
    }

    #region Overidden Methods
    // Determines whether the current node is the same state as the on passed.
    // Returns true if they are the same state
    public override bool IsSameState(AStarNode ANode)
    {
      if(ANode == null) 
        return false;
      return ((((AStarNode2D)ANode).X == FX) &&
        (((AStarNode2D)ANode).Y == FY));
    }
    // Calculates the estimated cost for the remaining trip to the goal.
    public override void Calculate()
    {
      if(GoalNode != null) 
      {
        double xd = FX - ((AStarNode2D)GoalNode).X;
        double yd = FY - ((AStarNode2D)GoalNode).Y;
        // "Euclidean distance" - Used when search can move at any angle.
        //GoalEstimate = Math.Sqrt((xd*xd) + (yd*yd));
        // "Manhattan Distance" - Used when search can only move vertically and 
        // horizontally.
        //GoalEstimate = Math.Abs(xd) + Math.Abs(yd); 
        // "Diagonal Distance" - Used when the search can move in 8 directions.
        GoalEstimate = Math.Max(Math.Abs(xd),Math.Abs(yd));
      }
      else
      {
        GoalEstimate = 0;
      }
    }
    // Gets all successors nodes from the current node and adds them to the passed successor list
    public override void GetSuccessors(ArrayList ASuccessors)
    {
      ASuccessors.Clear();
      AddSuccessor(ASuccessors,FX-1,FY  );
      AddSuccessor(ASuccessors,FX-1,FY-1);
      AddSuccessor(ASuccessors,FX  ,FY-1);
      AddSuccessor(ASuccessors,FX+1,FY-1);
      AddSuccessor(ASuccessors,FX+1,FY  );
      AddSuccessor(ASuccessors,FX+1,FY+1);
      AddSuccessor(ASuccessors,FX  ,FY+1);
      AddSuccessor(ASuccessors,FX-1,FY+1);
    }  
    // Prints information about the current node
    public override void PrintNodeInfo()
    {
      Console.WriteLine("X:\t{0}\tY:\t{1}\tCost:\t{2}\tEst:\t{3}\tTotal:\t{4}",FX,FY,Cost,GoalEstimate,TotalCost);
    }
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.