Mercurial > projects > openmelee
annotate melee.d @ 14:af1e8620f027
fixed steering
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Mon, 23 Mar 2009 14:06:47 -0400 |
parents | e1004697cae1 |
children | 8e6a9e390cba |
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 { | |
14 | 87 float hz = 60; |
7
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 | |
14 | 162 worldAABB.lowerBound.set(-400.0f, -250.0f); |
163 worldAABB.upperBound.set(400.0f, 250.0f); | |
0 | 164 world = new bzWorld(worldAABB, gravity, allowSleep); |
165 world.boundaryListener = m_boundaryListener; | |
166 world.contactListener = m_contactListener; | |
167 viewCenter = vec2(10, 10); | |
13 | 168 ship1 = new UrQuan(world); |
169 ship2 = new Orz(world); | |
14 | 170 ship2.rBody.angle = 3.14159265/4; |
6 | 171 auto planet = new Planet(world); |
0 | 172 } |
173 | |
174 void drag() | |
175 { | |
176 | |
177 } | |
178 | |
179 EventHandling onClick(MouseButtonEvent e) | |
180 { | |
181 return EventHandling.Stop; | |
182 } | |
183 | |
184 EventHandling onKey(KeyboardEvent e) | |
185 { | |
5 | 186 // Key pressed |
0 | 187 if (e.down) { |
188 switch (e.keySym) { | |
5 | 189 case KeySym.space: |
190 settings.drawAABBs = !settings.drawAABBs; | |
191 break; | |
0 | 192 case KeySym.Escape: |
193 quit = true; | |
194 break; | |
5 | 195 case KeySym.Up: |
196 thrust = true; | |
197 break; | |
198 case KeySym.Left: | |
199 ship1.turnLeft(); | |
200 break; | |
201 case KeySym.Right: | |
202 ship1.turnRight(); | |
203 break; | |
204 case KeySym.Down: | |
205 break; | |
0 | 206 default: |
207 break; | |
208 } | |
209 // Key released | |
210 } else { | |
5 | 211 if(e.keySym == KeySym.Up) { |
0 | 212 thrust = false; |
5 | 213 } else if (e.keySym == KeySym.Left || e.keySym == KeySym.Right) { |
0 | 214 ship1.rBody.angularVelocity = 0.0f; |
215 } | |
216 } | |
217 return EventHandling.Stop; | |
218 } | |
219 | |
220 // Mouse move | |
221 EventHandling onMove(MouseMoveEvent e) | |
222 { | |
223 return EventHandling.Stop; | |
224 } | |
225 | |
226 EventHandling onDT(TimeUpdateEvent e) | |
227 { | |
228 return EventHandling.Continue; | |
229 } | |
230 | |
231 EventHandling onMouseEnter(MouseEnterEvent e) | |
232 { | |
233 return EventHandling.Continue; | |
234 } | |
235 | |
236 EventHandling onMouseLeave(MouseLeaveEvent e) | |
237 { | |
238 return EventHandling.Continue; | |
239 } | |
240 | |
241 protected | |
242 { | |
243 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
244 bool allowSleep = false; | |
245 | |
246 vec2 spawnRect; | |
247 | |
248 vec2[] drawing; | |
249 | |
250 vec2i screenSize = vec2i.zero; | |
251 vec2 mousePos = vec2.zero; | |
252 | |
253 bool scaling = false; | |
254 bool full = false; | |
255 | |
256 vec2 spawnStart; | |
257 | |
258 bool preserveBullet = false; | |
259 | |
260 float waterDelta = 0; | |
261 } | |
262 | |
263 void boundaryViolated(bzBody rBody) | |
264 { | |
5 | 265 float x,y; |
266 | |
267 if(rBody.position.x > worldAABB.upperBound.x) { | |
268 x = worldAABB.lowerBound.x + 5; | |
269 rBody.position = bzVec2(x, rBody.position.y); | |
270 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
271 x = worldAABB.upperBound.x - 5; | |
272 rBody.position = bzVec2(x, rBody.position.y); | |
273 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
274 y = worldAABB.lowerBound.y + 5; | |
275 rBody.position = bzVec2(rBody.position.x, y); | |
276 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
277 y = worldAABB.upperBound.y - 5; | |
278 rBody.position = bzVec2(rBody.position.x, y); | |
279 } | |
0 | 280 } |
281 | |
282 bool quit; | |
283 | |
284 // Ortho view zoom | |
285 float zoom = 40; | |
286 int pointCount; | |
287 vec2 viewCenter; | |
288 | |
289 bzWorld world; | |
290 Settings *settings; | |
291 | |
292 // bzWorld boundary listener. Destroy bodies that leave world bzAABB | |
293 bzBoundaryListener m_boundaryListener; | |
294 bzContactListener m_contactListener; | |
295 ContactPoint[k_maxContactPoints] points; | |
296 | |
297 ConfigChange!(vec2) editChange; | |
298 bool thrust; | |
2 | 299 bzAABB worldAABB; |
300 | |
0 | 301 Ship ship1; |
302 Ship ship2; | |
303 } | |
304 | |
305 // Utility functions | |
306 bzVec2 toBlaze(vec2 vec) | |
307 { | |
308 auto ret = bzVec2(vec.x, vec.y); | |
309 return ret; | |
310 } | |
311 | |
312 vec2 rotate(vec2 point, float rad) | |
313 { | |
314 return vec2(cos(rad) * point.x - sin(rad) * point.y, sin(rad) * point.x + cos(rad) * point.y); | |
315 } | |
316 | |
317 class Select | |
318 { | |
319 bool select; | |
320 } |