public void add(Node node) { for (int k = 0; k < size; k++) { if (node.f < list[k].f) { list.Insert(k, node); size++; return; } } size++; list.Add(node); }
public List<Node> AStar(Node start, Node dest) { for (int k = 0; k < graph.Length; k++) { graph[k].g = 0; graph[k].f = 0; } List <Node> path= new List<Node>(); Node[] Closed = new Node[graph.Length]; int closed_counter = 0; JasonQ Open = new JasonQ(); JasonQ neighborSet; Open.add(start); for (int k = 0; k < graph.Length; k++) { if (graph[k] != start) { Open.add(graph[k]); graph[k].g = 0; graph[k].f = 0; } } Node current; double g_score = 0; double f_score = g_score + start.H(dest); start.g = g_score; start.f = f_score; double tempg = 0; while (Open.Size() > 0) { current = Open.dequeue(); if (current == dest) { path.Add(current); return path; } Closed[closed_counter++] = current; for (int k = 0; k < current.neighbors.Count; k++) { tempg = current.g + current.getTo(k); if (isIn(Closed, current.neighbors[k])) { if (tempg >= current.neighbors[k].g) { } else { //put camefrom to neighbor somehow. this following line is my attempt path.Add(current.neighbors[k]); current.neighbors[k].g = tempg; current.neighbors[k].f = tempg + current.neighbors[k].H(dest); if (!isIn(Open, current.neighbors[k])) { Open.add(current.neighbors[k]); } } } else { //put camefrom to neighbor somehow. this following line is my attempt path.Add(current.neighbors[k]); current.neighbors[k].g = tempg; current.neighbors[k].f = tempg + current.neighbors[k].H(dest); if (!isIn(Open, current.neighbors[k])) { Open.add(current.neighbors[k]); } } } } return path; }
public NodeGraph(Node[] map) { graph = map; }
public bool isIn(Node[] c, Node node) { for (int k = 0; k < c.Length; k++) { if (c[k] == node) return true; } return false; }
public bool isIn(JasonQ o, Node node) { for (int k = 0; k < o.Size(); k++) { if (o.get(k) == node) return true; } return false; }
public NPC(Vector2 newPosition, String newName, Node spawnNode) { position = newPosition; name = newName; sensor = new Sensor(300, 3); //rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); rectangle = new Rectangle((int)position.X, (int)position.Y, 50, 50); location = spawnNode; health = 100; maxHealth = 100; strength = 10; defense = 5; isAlive = true; isHostile = false; }
public List<Node> nodeMove(NodeGraph nodegraph, Node desiredEnd) { List<Node> answer=nodegraph.AStar(location, desiredEnd); shortDest = answer[1]; return answer; }
/* public void Load(ContentManager Content, Texture2D newTexture) { npcIdle = new Animation(newTexture, 32, 0.1f, true); animationPlayer.PlayAnimation(npcIdle); }*/ public override void Update(GameTime gameTime) { if (shortDest.point.X == this.rectangle.X && shortDest.point.Y == this.rectangle.Y) { //A* shit goes here location = shortDest; //calculate using finalDest and the new location A* path } }
public NPC(Texture2D newTexture, Vector2 newPosition, String newName, Node spawnNode, Vector2 newAttackRange) { texture = newTexture; position = newPosition; name = newName; sensor = new Sensor(300, 3); rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height); location = spawnNode; health = 100; maxHealth = 100; strength = 10; defense = 5; isAlive = true; isHostile = false; attackRange = newAttackRange; withinRange = false; }
public void testAdd(Node node) { neighbors.Add(node); distanceTo.Add(50); }
public double H(Node dest) { return Math.Sqrt(Math.Pow((point.X - dest.point.X),2) - Math.Pow((point.Y - dest.point.Y),2)); }
public double G(Node dest) { return 0; }
public void addNeighbor(Node node, double dist) { neighbors.Add(node); distanceTo.Add(dist); }