Client LuaCsForBarotrauma
SteeringPath.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
8  {
9  private List<WayPoint> nodes;
10 
11  int currentIndex;
12 
13  private float? totalLength;
14 
15  public bool Unreachable
16  {
17  get;
18  set;
19  }
20 
21  public float TotalLength
22  {
23  get
24  {
25  if (Unreachable) { return float.PositiveInfinity; }
26  if (!totalLength.HasValue)
27  {
28  CalculateTotalLength();
29  }
30  return totalLength.Value;
31  }
32  }
33 
34  public float GetLength(int? startIndex = null, int? endIndex = null)
35  {
36  if (Unreachable) { return float.PositiveInfinity; }
37  startIndex ??= 0;
38  endIndex ??= Nodes.Count - 1;
39  if (startIndex == 0 && endIndex == Nodes.Count - 1)
40  {
41  return TotalLength;
42  }
43  if (!totalLength.HasValue)
44  {
45  CalculateTotalLength();
46  }
47  float length = 0.0f;
48  for (int i = startIndex.Value; i < endIndex.Value; i++)
49  {
50  length += nodeDistances[i];
51  }
52  return length;
53  }
54 
55  private void CalculateTotalLength()
56  {
57  totalLength = 0.0f;
58  nodeDistances.Clear();
59  for (int i = 0; i < nodes.Count - 1; i++)
60  {
61  float distance = Vector2.Distance(nodes[i].WorldPosition, nodes[i + 1].WorldPosition);
62  totalLength += distance;
63  nodeDistances.Add(distance);
64  }
65  }
66 
67  private readonly List<float> nodeDistances = new List<float>();
68 
69  public SteeringPath(bool unreachable = false)
70  {
71  nodes = new List<WayPoint>();
72  Unreachable = unreachable;
73  }
74 
75  public void AddNode(WayPoint node)
76  {
77  if (node == null) { return; }
78  nodes.Add(node);
79 
80  if (node.CurrentHull == null) { HasOutdoorsNodes = true; }
81  }
82 
83  public bool HasOutdoorsNodes
84  {
85  get;
86  private set;
87  }
88 
89  public int CurrentIndex
90  {
91  get { return currentIndex; }
92  }
93 
94  public float Cost
95  {
96  get;
97  set;
98  }
99 
101  {
102  get
103  {
104  if (currentIndex - 1 < 0 || currentIndex - 1 > nodes.Count - 1) { return null; }
105  return nodes[currentIndex - 1];
106  }
107  }
108 
110  {
111  get
112  {
113  if (currentIndex < 0 || currentIndex > nodes.Count - 1) { return null; }
114  return nodes[currentIndex];
115  }
116  }
117 
118  public bool IsAtEndNode => currentIndex >= nodes.Count - 1;
119 
120  public List<WayPoint> Nodes
121  {
122  get { return nodes; }
123  }
124 
126  {
127  get
128  {
129  if (currentIndex + 1 < 0 || currentIndex + 1 > nodes.Count - 1) { return null; }
130  return nodes[currentIndex+1];
131  }
132  }
133 
134  public bool Finished
135  {
136  get { return currentIndex >= nodes.Count; }
137  }
138 
139  public void SkipToNextNode()
140  {
141  currentIndex++;
142  }
143 
144  public void SkipToNode(int nodeIndex)
145  {
146  currentIndex = nodeIndex;
147  }
148 
149  public WayPoint CheckProgress(Vector2 simPosition, float minSimDistance = 0.1f)
150  {
151  if (nodes.Count == 0 || currentIndex > nodes.Count - 1) { return null; }
152  if (Vector2.Distance(simPosition, nodes[currentIndex].SimPosition) < minSimDistance) { currentIndex++; }
153 
154  return CurrentNode;
155  }
156 
157  public void ClearPath()
158  {
159  nodes.Clear();
160  }
161  }
162 }
void AddNode(WayPoint node)
Definition: SteeringPath.cs:75
SteeringPath(bool unreachable=false)
Definition: SteeringPath.cs:69
void SkipToNode(int nodeIndex)
WayPoint CheckProgress(Vector2 simPosition, float minSimDistance=0.1f)
List< WayPoint > Nodes
float GetLength(int? startIndex=null, int? endIndex=null)
Definition: SteeringPath.cs:34