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