Jypeli 10
The simple game programming library
RoadMap.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Collections.Generic;
3
4namespace 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;
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
183 public bool IsInside(Vector point)
184 {
185 return ListHelpers.ArrayFind(Segments, seg => seg.IsInside(point)) != null;
186 }
187 }
188}
void Add(Light light)
Lisää valon peliin. Nykyisellään valoja voi olla ainoastaan yksi kappale. Toistaiseksi ei tuettu Wind...
Definition: Effects.cs:27
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:96
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
Definition: Dimensions.cs:155
virtual Color Color
Väri, jonka värisenä olio piirretään, jos tekstuuria ei ole määritelty.
Definition: Appearance.cs:65
Apufunktioita listojen ja muiden tietorakenteiden käyttöön.
Definition: ListHelpers.cs:11
Kappale joka noudattaa fysiikan lakeja, johon voi törmätä. Vaatii että käytössä on fysiikkapeli.
Definition: Collisions.cs:7
double KineticFriction
Liikekitka (hidastaa kun olio on jo liikkeessä). Ks. StaticFriction (lepokitka)
Definition: Collisions.cs:90
static PhysicsObject CreateStaticObject(double width, double height, Shape shape)
Alustaa fysiikkaolion käyttöön ja tekee siitä staattisen (liikkumattoman).
override Vector?? Position
Definition: Dimensions.cs:30
bool IgnoresCollisionResponse
Jättääkö törmäykset huomiotta.
Definition: Collisions.cs:52
Monikulmio.
Definition: Shapes.cs:535
Luo tien. Tie koostuu useasta pienemmästä "pätkästä".
Definition: RoadMap.cs:10
Angle[] angles
Definition: RoadMap.cs:12
RoadMap(IList< Vector > wayPoints)
Luo uuden RoadMapin.
Definition: RoadMap.cs:45
double DefaultWidth
Tien oletusleveys.
Definition: RoadMap.cs:17
void Insert()
Luo tien kentälle.
Definition: RoadMap.cs:70
bool IsInside(Vector point)
Onko annettu piste radan sisällä
Definition: RoadMap.cs:183
GameObject[] Segments
Tienpätkät.
Definition: RoadMap.cs:27
double DefaultFriction
Tien oletuskitka.
Definition: RoadMap.cs:22
Angle GetAngle(int wayPointIndex)
Etenemissuunta pisteen kohdalla.
Definition: RoadMap.cs:59
void CalculatePoints(Vector previous, Vector current, Vector next, out Vector left, out Vector right)
Definition: RoadMap.cs:164
PhysicsObject CreateSegment(double width, double height, Shape shape)
Definition: RoadMap.cs:143
Func< double, double, Shape, PhysicsObject > CreateSegmentFunction
Funktio, joka luo yksittäisen tienpätkän.
Definition: RoadMap.cs:38
Vector[] wayPoints
Definition: RoadMap.cs:11
Sisältää valmiiksi lasketut kolmiot, joiden avulla piirtäminen on suoraviivaista.
Definition: Shapes.cs:628
Kuvio.
Definition: Shapes.cs:47
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
Väri.
Definition: Color.cs:13
static readonly Color Gray
Harmaa.
Definition: Color.cs:696
Muotojen määrityksessä käytettävä kolmio.
Definition: Shapes.cs:598
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334