Jypeli  9
The simple game programming library
RoadMap.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 
4 namespace Jypeli
5 {
9  public class RoadMap
10  {
11  private Vector[] wayPoints;
12  private Angle[] angles = null;
13 
17  public double DefaultWidth { get; set; }
18 
22  public double DefaultFriction { get; set; }
23 
27  public GameObject[] Segments { get; private set; }
28 
38  public Func<double, double, Shape, PhysicsObject> CreateSegmentFunction { get; set; }
39 
40 
45  public RoadMap(IList<Vector> wayPoints)
46  {
47  this.DefaultWidth = 10.0;
48  this.DefaultFriction = 1.0;
50  this.wayPoints = new Vector[wayPoints.Count];
51  wayPoints.CopyTo(this.wayPoints, 0);
52  this.Segments = new GameObject[wayPoints.Count - 1];
53  }
54 
59  public Angle GetAngle(int wayPointIndex)
60  {
61  // TODO: calculate this on the fly so that this check becomes unnecessary.
62  if (angles == null)
63  throw new Exception("Call Insert() first");
64  return angles[wayPointIndex];
65  }
66 
70  public void Insert()
71  {
72  if (wayPoints.Length < 2)
73  {
74  throw new ArgumentException("Must have at least 2 points");
75  }
76 
77  angles = new Angle[wayPoints.Length];
78 
79  Vector first = wayPoints[0];
80  Vector second = wayPoints[1];
81  Vector toBeginning = (first - second).Normalize();
82  Vector beginning = first + toBeginning;
83 
84  Vector previousLeft, previousRight;
85  CalculatePoints(beginning, first, second, out previousLeft, out previousRight);
86  angles[0] = (second - first).Angle;
87 
88  Vector secondToLast = wayPoints[wayPoints.Length - 2];
89  Vector last = wayPoints[wayPoints.Length - 1];
90  Vector toVeryLast = (last - secondToLast).Normalize();
91  Vector veryLast = last + toVeryLast;
92 
93  for (int i = 1; i < wayPoints.Length; i++)
94  {
95  Vector previous = wayPoints[i - 1];
96  Vector current = wayPoints[i];
97  Vector next;
98 
99  Vector toPrevious = (previous - current).Normalize();
100  Vector toNext;
101 
102  if (i == wayPoints.Length - 1)
103  {
104  next = veryLast;
105  toNext = toVeryLast;
106  }
107  else
108  {
109  next = wayPoints[i + 1];
110  toNext = (next - current).Normalize();
111  }
112 
113  Vector left, right;
114  CalculatePoints(previous, current, next, out left, out right);
115  angles[i] = (next - previous).Angle;
116 
117  Vector center = previous + toNext / 2;
118 
119  Vector[] vertices = new Vector[4];
120  vertices[0] = previousLeft - center;
121  vertices[1] = previousRight - center;
122  vertices[2] = right - center;
123  vertices[3] = left - center;
124 
125  IndexTriangle[] triangles = new IndexTriangle[]
126  {
127  new IndexTriangle(0, 3, 1),
128  new IndexTriangle(1, 3, 2)
129  };
130 
131  ShapeCache cache = new ShapeCache(vertices, triangles);
132  Polygon shape = new Polygon(cache, false);
133 
134  PhysicsObject segment = CreateSegmentFunction(100, 100, shape);
135  segment.Position = center;
136  Segments[i - 1] = segment;
137 
138  previousLeft = left;
139  previousRight = right;
140  }
141  }
142 
143  private PhysicsObject CreateSegment(double width, double height, Shape shape)
144  {
145  PhysicsObject o = PhysicsObject.CreateStaticObject(width, height, shape);
146  o.Color = Color.Gray;
147  o.IgnoresCollisionResponse = true;
149 
150  // TopDownPhysicsGame does not exist in MonoJypeli
151  //if (Game.Instance is TopDownPhysicsGame)
152  //{
153  // TopDownPhysicsGame g = (TopDownPhysicsGame)Game.Instance;
154  // g.AddSurface(o);
155  //}
156  //else
157  //{
158  Game.Instance.Add(o);
159  //}
160 
161  return o;
162  }
163 
164  private void CalculatePoints(Vector previous, Vector current, Vector next, out Vector left, out Vector right)
165  {
166  Vector toNext = (next - current).Normalize();
167  Vector toPrevious = (previous - current).Normalize();
168  Vector direction = (next - previous).Normalize();
169 
170  Vector perpendicular = new Vector(direction.Y, -direction.X);
171  Vector toLeft = -perpendicular;
172  Vector toRight = perpendicular;
173 
174  left = current + toLeft * DefaultWidth / 2;
175  right = current + toRight * DefaultWidth / 2;
176  }
177 
178  public bool IsInside(Vector point)
179  {
180  return ListHelpers.ArrayFind(Segments, seg => seg.IsInside(point)) != null;
181  }
182  }
183 }
Jypeli.PhysicsObject.IgnoresCollisionResponse
bool IgnoresCollisionResponse
Jättääkö törmäykset huomiotta.
Definition: Collisions.cs:50
Jypeli.PhysicsObject.CreateStaticObject
static PhysicsObject CreateStaticObject(double width, double height, Shape shape)
Alustaa fysiikkaolion käyttöön ja tekee siitä staattisen (liikkumattoman).
Definition: PhysicsObject.cs:104
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.RoadMap.Segments
GameObject[] Segments
Tienpätkät.
Definition: RoadMap.cs:27
Jypeli
Definition: Automobile.cs:5
Jypeli.ShapeCache
Sisältää valmiiksi lasketut kolmiot, joiden avulla piirtäminen on suoraviivaista.
Definition: Shapes.cs:583
Jypeli.Shape
Kuvio.
Definition: Shapes.cs:47
Jypeli.GameObject.IsInside
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
Definition: Dimensions.cs:103
Jypeli.RoadMap.DefaultFriction
double DefaultFriction
Tien oletuskitka.
Definition: RoadMap.cs:22
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.PhysicsObject.KineticFriction
double KineticFriction
Liikekitka (hidastaa kun olio on jo liikkeessä). Ks. StaticFriction (lepokitka)
Definition: Collisions.cs:88
Jypeli.RoadMap.angles
Angle[] angles
Definition: RoadMap.cs:12
Jypeli.RoadMap.CreateSegmentFunction
Func< double, double, Shape, PhysicsObject > CreateSegmentFunction
Funktio, joka luo yksittäisen tienpätkän.
Definition: RoadMap.cs:38
Jypeli.Game.Add
void Add(Light light)
Lisää valon peliin. Nykyisellään valoja voi olla ainoastaan yksi kappale. Toistaiseksi ei tuettu Wind...
Definition: Effects.cs:27
Jypeli.ListHelpers
Apufunktioita listojen ja muiden tietorakenteiden käyttöön.
Definition: ListHelpers.cs:11
Jypeli.RoadMap.RoadMap
RoadMap(IList< Vector > wayPoints)
Luo uuden RoadMapin.
Definition: RoadMap.cs:45
Jypeli.RoadMap.Insert
void Insert()
Luo tien kentälle.
Definition: RoadMap.cs:70
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.RoadMap.wayPoints
Vector[] wayPoints
Definition: RoadMap.cs:11
Jypeli.PhysicsObject.Position
override Vector Position
Definition: Dimensions.cs:27
Jypeli.RoadMap.IsInside
bool IsInside(Vector point)
Definition: RoadMap.cs:178
Jypeli.Polygon
Monikulmio.
Definition: Shapes.cs:502
Jypeli.IndexTriangle
Muotojen määrityksessä käytettävä kolmio.
Definition: Shapes.cs:553
Jypeli.RoadMap.GetAngle
Angle GetAngle(int wayPointIndex)
Etenemissuunta pisteen kohdalla.
Definition: RoadMap.cs:59
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.PhysicsObject
Definition: Collisions.cs:6
Jypeli.RoadMap
Luo tien. Tie koostuu useasta pienemmästä "pätkästä".
Definition: RoadMap.cs:10
Jypeli.GameObject
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
Jypeli.Color.Gray
static readonly Color Gray
Harmaa.
Definition: Color.cs:643
Jypeli.RoadMap.CreateSegment
PhysicsObject CreateSegment(double width, double height, Shape shape)
Definition: RoadMap.cs:143
Jypeli.RoadMap.CalculatePoints
void CalculatePoints(Vector previous, Vector current, Vector next, out Vector left, out Vector right)
Definition: RoadMap.cs:164
Jypeli.RoadMap.DefaultWidth
double DefaultWidth
Tien oletusleveys.
Definition: RoadMap.cs:17
Jypeli.Game
Definition: Content.cs:46
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313
Jypeli.Angle
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40