Mercurial > projects > openmelee
annotate melee/melee.d @ 25:2bf818f8b005
fixed asteroids
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Fri, 27 Mar 2009 16:25:17 -0400 |
parents | 441eb7672404 |
children | 88cca12cc8b9 |
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 */ | |
18 | 31 module openmelee.melee.melee; |
0 | 32 |
18 | 33 import tango.io.Stdout; |
24 | 34 import tango.util.container.LinkedList : LinkedList; |
12 | 35 |
0 | 36 version(distrib) import tango.io.vfs.ZipFolder; |
37 import tango.time.StopWatch; | |
38 import fc = tango.text.convert.Float : toString; | |
39 import tango.util.log.Trace; | |
40 | |
41 import xf.core.JobHub; | |
42 import xf.hybrid.Hybrid; | |
43 import xf.hybrid.backend.GL; | |
44 | |
18 | 45 import blaze.common.bzMath : bzVec2; |
46 import blaze.bzWorld : bzWorld; | |
47 import blaze.collision.bzCollision : bzAABB; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
5
diff
changeset
|
48 |
18 | 49 import openmelee.melee.boundaryListener; |
50 import openmelee.melee.contactListener; | |
51 import openmelee.render.render; | |
22 | 52 import openmelee.ai.ai : AI; |
53 import openmelee.ai.human : Human; | |
54 import openmelee.ships.urQuan : UrQuan; | |
55 import openmelee.ships.orz : Orz; | |
56 import openmelee.ships.planet : Planet; | |
57 import openmelee.ships.asteroids : Asteroid; | |
0 | 58 |
17 | 59 const ITERS_PER_SECOND = 100; |
18 | 60 const k_maxContactPoints = 100; |
25 | 61 const NUM_ASTROIDS = 15; |
0 | 62 |
24 | 63 alias LinkedList!(Ship) ObjectList; |
64 | |
18 | 65 // Melee settings |
66 struct Settings { | |
67 float hz = 60; | |
68 int velocityIterations = 3; | |
69 int positionIterations = 1; | |
70 bool drawShapes = true; | |
71 bool drawJoints = true; | |
72 bool drawControllers; | |
73 bool drawCoreShapes; | |
74 bool drawAABBs; | |
75 bool drawOBBs; | |
76 bool drawPairs; | |
77 bool drawContactPoints; | |
78 bool drawContactNormals; | |
79 bool drawContactForces; | |
80 bool drawFrictionForces; | |
81 bool drawCOMs; | |
82 bool drawStats; | |
83 bool enableWarmStarting; | |
84 bool enableTOI; | |
85 } | |
86 | |
21 | 87 struct ContactPoint { |
88 bzShape shape1; | |
89 bzShape shape2; | |
90 bzVec2 normal; | |
91 bzVec2 position; | |
92 bzVec2 velocity; | |
93 bzContactID id; | |
94 //ContactState state; | |
95 } | |
96 | |
18 | 97 class Melee { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
98 |
24 | 99 ObjectList objectList; |
18 | 100 Settings settings; |
101 float timeStep; | |
102 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
103 bool allowSleep; | |
104 Render draw; | |
105 | |
106 AI ai; | |
107 Human human; | |
24 | 108 |
18 | 109 Ship ship1; |
110 Ship ship2; | |
24 | 111 Ship planet; |
18 | 112 |
113 bool running; | |
114 | |
0 | 115 StopWatch timer; |
18 | 116 bzAABB worldAABB; |
117 bzWorld world; | |
118 bzBoundaryListener m_boundaryListener; | |
119 bzContactListener m_contactListener; | |
120 ContactPoint[k_maxContactPoints] points; | |
121 int pointCount; | |
122 | |
123 this() { | |
24 | 124 |
125 objectList = new ObjectList; | |
18 | 126 } |
127 | |
128 void init() { | |
17 | 129 |
18 | 130 timeStep = settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f; |
131 version(distrib) gui.vfs.mount(new ZipFolder("./gui.zip")); | |
132 scope cfg = loadHybridConfig("./gui.cfg"); | |
133 scope renderer = new Renderer; | |
134 | |
135 m_boundaryListener = new BoundaryListener(this); | |
136 initWorld(); | |
137 running = true; | |
15 | 138 |
18 | 139 draw = new Render(world, ship1, ship2, settings); |
140 human = new Human(ship1); | |
24 | 141 |
142 objectList.add(planet); | |
143 objectList.add(ship1); | |
144 objectList.add(ship2); | |
145 | |
146 ai = new AI(ship2, objectList); | |
147 | |
18 | 148 gui.begin(cfg).retained; |
0 | 149 gui.push(`main`); |
18 | 150 GLViewport(`glview`).renderingHandler(&draw.draw) |
151 .addHandler(&human.onClick) | |
152 .addHandler(&human.onMove) | |
153 .addHandler(&human.onKey) | |
154 .addHandler(&human.onDT) | |
155 .addHandler(&human.onMouseEnter) | |
156 .addHandler(&human.onMouseLeave) | |
157 .grabKeyboardFocus; | |
158 gui.pop(); | |
159 gui.immediate.end; | |
160 | |
161 jobHub.addRepeatableJob( { | |
162 // Update physics | |
163 world.step(timeStep, settings.velocityIterations, settings.positionIterations); | |
164 }, ITERS_PER_SECOND); | |
165 | |
166 jobHub.addPreFrameJob( { | |
167 // Update AI | |
23 | 168 ai.move(ship1); |
18 | 169 }); |
2 | 170 |
18 | 171 jobHub.addPostFrameJob( { |
172 | |
173 // Limit velocity | |
174 ship1.limitVelocity(); | |
175 ship2.limitVelocity(); | |
176 ship1.updateState(); | |
177 ship2.updateState(); | |
178 | |
179 gui.begin(cfg); | |
180 gui.push(`main`); | |
181 if (gui().getProperty!(bool)("frame.closeClicked")) { | |
182 running = false; | |
183 } | |
0 | 184 |
18 | 185 if(human.thrust) { |
186 ship1.thrust(); | |
187 } | |
188 | |
189 gui().setProperty!(bool)("showCursor", true); | |
190 gui.pop(); | |
191 gui.end; | |
192 gui.render(renderer); | |
193 | |
194 }); | |
195 | |
196 while (running && !human.quit) { | |
197 float delta = timer.stop; | |
198 timer.start; | |
199 jobHub.update(delta); | |
200 } | |
201 } | |
0 | 202 |
18 | 203 void initWorld() { |
204 // Define world boundaries | |
24 | 205 worldAABB.lowerBound.set(-400.0f, -250.0f); |
206 worldAABB.upperBound.set(400.0f, 250.0f); | |
18 | 207 world = new bzWorld(worldAABB, gravity, allowSleep); |
208 world.boundaryListener = m_boundaryListener; | |
24 | 209 ship2 = new UrQuan(world); |
210 ship1 = new Orz(world); | |
18 | 211 ship2.rBody.angle = 3.14159265/4; |
22 | 212 planet = new Planet(world); |
25 | 213 |
214 for(int i; i < NUM_ASTROIDS; i++) { | |
215 auto asteroid = new Asteroid(world); | |
216 objectList.add(asteroid); | |
217 } | |
18 | 218 } |
219 | |
220 void boundaryViolated(bzBody rBody) | |
221 { | |
222 float x,y; | |
0 | 223 |
18 | 224 if(rBody.position.x > worldAABB.upperBound.x) { |
225 x = worldAABB.lowerBound.x + 5; | |
226 rBody.position = bzVec2(x, rBody.position.y); | |
227 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
228 x = worldAABB.upperBound.x - 5; | |
229 rBody.position = bzVec2(x, rBody.position.y); | |
230 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
231 y = worldAABB.lowerBound.y + 5; | |
232 rBody.position = bzVec2(rBody.position.x, y); | |
233 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
234 y = worldAABB.upperBound.y - 5; | |
235 rBody.position = bzVec2(rBody.position.x, y); | |
236 } | |
237 } | |
0 | 238 } |