Mercurial > projects > openmelee
annotate melee.d @ 11:d998bf1b0654
Added utilities and AI; fixed steer
author | Mason Green <mason.green@gmail.com> |
---|---|
date | Sun, 22 Mar 2009 09:35:24 -0400 |
parents | 4ee9e4a0c03b |
children | e1004697cae1 |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2009, Mason Green (zzzzrrr) | |
8 | 3 * http://www.dsource.org/projects/openmelee |
0 | 4 * |
5 * All rights reserved. | |
6 * | |
7 * Redistribution and use in source and binary forms, with or without modification, | |
8 * are permitted provided that the following conditions are met: | |
9 * | |
10 * * Redistributions of source code must retain the above copyright notice, | |
11 * this list of conditions and the following disclaimer. | |
12 * * Redistributions in binary form must reproduce the above copyright notice, | |
13 * this list of conditions and the following disclaimer in the documentation | |
14 * and/or other materials provided with the distribution. | |
15 * * Neither the name of the polygonal nor the names of its contributors may be | |
16 * used to endorse or promote products derived from this software without specific | |
17 * prior written permission. | |
18 * | |
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 */ | |
2 | 31 module openmelee.melee; |
0 | 32 |
5 | 33 import tango.io.Stdout: Stdout; |
34 | |
0 | 35 import Integer = tango.text.convert.Integer; |
36 import tango.math.Math; | |
37 | |
38 import xf.hybrid.Event; | |
39 import xf.input.KeySym; | |
40 import xf.omg.core.LinearAlgebra; | |
41 | |
42 public import blaze.all; | |
43 | |
2 | 44 import openmelee.boundaryListener; |
45 import openmelee.contactListener; | |
46 import openmelee.ship; | |
47 import openmelee.urQuan; | |
48 import openmelee.orz; | |
6 | 49 import openmelee.planet; |
0 | 50 |
51 // Cursor scale factor | |
52 const CURSORSIZE = 0.05f; | |
53 | |
54 const INIT_SPAWN_SIZE = 0.5f; | |
55 | |
56 // Dragging stuffs | |
57 const BUNGEE_K = 1.5f; | |
58 // Damping factor for dragging | |
59 const DRAGDAMP = 20.0f; | |
60 | |
61 // Size of hinges | |
62 const HINGE_RADIUS = 0.05f; | |
63 | |
64 // Smallest allowed dimension | |
65 const MIN_DIMENSION = 0.1; | |
66 | |
67 const k_maxContactPoints = 2048; | |
68 | |
69 enum ContactState { | |
70 e_contactAdded, | |
71 e_contactPersisted, | |
72 e_contactRemoved | |
73 } | |
74 | |
75 struct ContactPoint { | |
76 bzShape shape1; | |
77 bzShape shape2; | |
78 bzVec2 normal; | |
79 bzVec2 position; | |
80 bzVec2 velocity; | |
81 bzContactID id; | |
82 ContactState state; | |
83 } | |
84 | |
85 // Melee settings. Some can be controlled in the GUI. | |
86 struct Settings { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
87 float hz = 30; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
88 int velocityIterations = 5; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
89 int positionIterations = 1; |
0 | 90 bool drawShapes = true; |
91 bool drawJoints = true; | |
92 bool drawControllers; | |
93 bool drawCoreShapes; | |
5 | 94 bool drawAABBs; |
0 | 95 bool drawOBBs; |
96 bool drawPairs; | |
97 bool drawContactPoints; | |
98 bool drawContactNormals; | |
99 bool drawContactForces; | |
100 bool drawFrictionForces; | |
101 bool drawCOMs; | |
102 bool drawStats; | |
103 bool enableWarmStarting; | |
104 bool enableTOI; | |
105 } | |
106 | |
107 // Dirty, dirty hack for communicating config changes to Main | |
108 // TODO: Harass h3 to add .changed to hybrid so this isn't necessary | |
109 struct ConfigChange (T) | |
110 { | |
111 protected | |
112 { | |
113 bool _pending = false; | |
114 T _value; | |
115 } | |
116 | |
117 T value() | |
118 { | |
119 _pending = false; | |
120 return _value; | |
121 } | |
122 | |
123 void value(T change) | |
124 { | |
125 _value = change; | |
126 _pending = true; | |
127 } | |
128 | |
129 bool pending() | |
130 { | |
131 return _pending; | |
132 } | |
133 | |
134 T opAssign(T change) | |
135 { | |
136 value(change); | |
137 return _value; | |
138 } | |
139 | |
140 bool opEquals(T other) | |
141 { | |
142 return _value == other; | |
143 } | |
144 } | |
145 | |
146 class Melee | |
147 { | |
148 | |
149 this(Settings *settings) | |
150 { | |
151 this.settings = settings; | |
152 spawnRect = vec2(INIT_SPAWN_SIZE, INIT_SPAWN_SIZE); | |
153 // bzWorld boundary callback | |
154 m_boundaryListener = new BoundaryListener(this); | |
155 // bzContact callback | |
156 m_contactListener = new ContactListener(this); | |
5 | 157 init(); |
0 | 158 } |
159 | |
160 void init() { | |
161 // Define world boundaries | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
162 worldAABB.lowerBound.set(-200.0f, -200.0f); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
163 worldAABB.upperBound.set(200.0f, 200.0f); |
0 | 164 world = new bzWorld(worldAABB, gravity, allowSleep); |
165 world.boundaryListener = m_boundaryListener; | |
166 world.contactListener = m_contactListener; | |
167 viewCenter = vec2(10, 10); | |
168 ship1 = new Orz(world); | |
169 ship2 = new UrQuan(world); | |
6 | 170 auto planet = new Planet(world); |
0 | 171 } |
172 | |
173 void drag() | |
174 { | |
175 | |
176 } | |
177 | |
178 EventHandling onClick(MouseButtonEvent e) | |
179 { | |
180 return EventHandling.Stop; | |
181 } | |
182 | |
183 EventHandling onKey(KeyboardEvent e) | |
184 { | |
5 | 185 // Key pressed |
0 | 186 if (e.down) { |
187 switch (e.keySym) { | |
5 | 188 case KeySym.space: |
189 settings.drawAABBs = !settings.drawAABBs; | |
190 break; | |
0 | 191 case KeySym.Escape: |
192 quit = true; | |
193 break; | |
5 | 194 case KeySym.Up: |
195 thrust = true; | |
196 break; | |
197 case KeySym.Left: | |
198 ship1.turnLeft(); | |
199 break; | |
200 case KeySym.Right: | |
201 ship1.turnRight(); | |
202 break; | |
203 case KeySym.Down: | |
204 break; | |
0 | 205 default: |
206 break; | |
207 } | |
208 // Key released | |
209 } else { | |
5 | 210 if(e.keySym == KeySym.Up) { |
0 | 211 thrust = false; |
5 | 212 } else if (e.keySym == KeySym.Left || e.keySym == KeySym.Right) { |
0 | 213 ship1.rBody.angularVelocity = 0.0f; |
214 } | |
215 } | |
216 return EventHandling.Stop; | |
217 } | |
218 | |
219 // Mouse move | |
220 EventHandling onMove(MouseMoveEvent e) | |
221 { | |
222 return EventHandling.Stop; | |
223 } | |
224 | |
225 EventHandling onDT(TimeUpdateEvent e) | |
226 { | |
227 return EventHandling.Continue; | |
228 } | |
229 | |
230 EventHandling onMouseEnter(MouseEnterEvent e) | |
231 { | |
232 return EventHandling.Continue; | |
233 } | |
234 | |
235 EventHandling onMouseLeave(MouseLeaveEvent e) | |
236 { | |
237 return EventHandling.Continue; | |
238 } | |
239 | |
240 protected | |
241 { | |
242 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
243 bool allowSleep = false; | |
244 | |
245 vec2 spawnRect; | |
246 | |
247 vec2[] drawing; | |
248 | |
249 vec2i screenSize = vec2i.zero; | |
250 vec2 mousePos = vec2.zero; | |
251 | |
252 bool scaling = false; | |
253 bool full = false; | |
254 | |
255 vec2 spawnStart; | |
256 | |
257 bool preserveBullet = false; | |
258 | |
259 float waterDelta = 0; | |
260 } | |
261 | |
262 void boundaryViolated(bzBody rBody) | |
263 { | |
5 | 264 float x,y; |
265 | |
266 if(rBody.position.x > worldAABB.upperBound.x) { | |
267 x = worldAABB.lowerBound.x + 5; | |
268 rBody.position = bzVec2(x, rBody.position.y); | |
269 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
270 x = worldAABB.upperBound.x - 5; | |
271 rBody.position = bzVec2(x, rBody.position.y); | |
272 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
273 y = worldAABB.lowerBound.y + 5; | |
274 rBody.position = bzVec2(rBody.position.x, y); | |
275 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
276 y = worldAABB.upperBound.y - 5; | |
277 rBody.position = bzVec2(rBody.position.x, y); | |
278 } | |
0 | 279 } |
280 | |
281 bool quit; | |
282 | |
283 // Ortho view zoom | |
284 float zoom = 40; | |
285 int pointCount; | |
286 vec2 viewCenter; | |
287 | |
288 bzWorld world; | |
289 Settings *settings; | |
290 | |
291 // bzWorld boundary listener. Destroy bodies that leave world bzAABB | |
292 bzBoundaryListener m_boundaryListener; | |
293 bzContactListener m_contactListener; | |
294 ContactPoint[k_maxContactPoints] points; | |
295 | |
296 ConfigChange!(vec2) editChange; | |
297 bool thrust; | |
2 | 298 bzAABB worldAABB; |
299 | |
0 | 300 Ship ship1; |
301 Ship ship2; | |
302 } | |
303 | |
304 // Utility functions | |
305 bzVec2 toBlaze(vec2 vec) | |
306 { | |
307 auto ret = bzVec2(vec.x, vec.y); | |
308 return ret; | |
309 } | |
310 | |
311 vec2 rotate(vec2 point, float rad) | |
312 { | |
313 return vec2(cos(rad) * point.x - sin(rad) * point.y, sin(rad) * point.x + cos(rad) * point.y); | |
314 } | |
315 | |
316 class Select | |
317 { | |
318 bool select; | |
319 } |