Mercurial > projects > openmelee
annotate melee/melee.d @ 27:d63faa81a5e4
removed Dog, added derelict and glfw
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Mon, 30 Mar 2009 12:41:29 -0400 |
parents | 88cca12cc8b9 |
children | 1cc6b8c0acd2 |
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 | |
18 | 41 import blaze.common.bzMath : bzVec2; |
42 import blaze.bzWorld : bzWorld; | |
43 import blaze.collision.bzCollision : bzAABB; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
5
diff
changeset
|
44 |
18 | 45 import openmelee.melee.boundaryListener; |
46 import openmelee.melee.contactListener; | |
47 import openmelee.render.render; | |
22 | 48 import openmelee.ai.ai : AI; |
49 import openmelee.ai.human : Human; | |
50 import openmelee.ships.urQuan : UrQuan; | |
51 import openmelee.ships.orz : Orz; | |
52 import openmelee.ships.planet : Planet; | |
53 import openmelee.ships.asteroids : Asteroid; | |
0 | 54 |
17 | 55 const ITERS_PER_SECOND = 100; |
18 | 56 const k_maxContactPoints = 100; |
26 | 57 const NUM_ASTROIDS = 12; |
0 | 58 |
24 | 59 alias LinkedList!(Ship) ObjectList; |
60 | |
18 | 61 // Melee settings |
62 struct Settings { | |
63 float hz = 60; | |
64 int velocityIterations = 3; | |
65 int positionIterations = 1; | |
66 bool drawShapes = true; | |
67 bool drawJoints = true; | |
68 bool drawControllers; | |
69 bool drawCoreShapes; | |
70 bool drawAABBs; | |
71 bool drawOBBs; | |
72 bool drawPairs; | |
73 bool drawContactPoints; | |
74 bool drawContactNormals; | |
75 bool drawContactForces; | |
76 bool drawFrictionForces; | |
77 bool drawCOMs; | |
78 bool drawStats; | |
79 bool enableWarmStarting; | |
80 bool enableTOI; | |
81 } | |
82 | |
21 | 83 struct ContactPoint { |
84 bzShape shape1; | |
85 bzShape shape2; | |
86 bzVec2 normal; | |
87 bzVec2 position; | |
88 bzVec2 velocity; | |
89 bzContactID id; | |
90 //ContactState state; | |
91 } | |
92 | |
18 | 93 class Melee { |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
9
diff
changeset
|
94 |
24 | 95 ObjectList objectList; |
18 | 96 Settings settings; |
97 float timeStep; | |
98 const bzVec2 gravity = bzVec2(0.0f, 0.0f); | |
99 bool allowSleep; | |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
100 Render render; |
18 | 101 |
102 AI ai; | |
103 Human human; | |
24 | 104 |
18 | 105 Ship ship1; |
106 Ship ship2; | |
24 | 107 Ship planet; |
18 | 108 |
109 bool running; | |
110 | |
0 | 111 StopWatch timer; |
18 | 112 bzAABB worldAABB; |
113 bzWorld world; | |
114 bzBoundaryListener m_boundaryListener; | |
115 bzContactListener m_contactListener; | |
116 ContactPoint[k_maxContactPoints] points; | |
117 int pointCount; | |
118 | |
119 this() { | |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
120 timeStep = settings.hz > 0.0f ? 1.0f / settings.hz : 0.0f; |
24 | 121 objectList = new ObjectList; |
18 | 122 m_boundaryListener = new BoundaryListener(this); |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
123 |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
124 |
18 | 125 initWorld(); |
126 running = true; | |
15 | 127 |
26 | 128 human = new Human(ship1, this); |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
129 ai = new AI(ship2, objectList); |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
130 |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
131 render = new Render(world, ship1, ship2, human, settings); |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
132 render.keys(); |
24 | 133 |
134 objectList.add(planet); | |
135 objectList.add(ship1); | |
136 objectList.add(ship2); | |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
137 } |
18 | 138 |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
139 void run() { |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
140 // Main game loop |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
141 while (running && !human.quit) { |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
142 |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
143 float delta = timer.stop; |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
144 timer.start; |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
145 |
18 | 146 // Update AI |
23 | 147 ai.move(ship1); |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
148 // Update Physics |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
149 world.step(timeStep, settings.velocityIterations, settings.positionIterations); |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
150 // Update screen |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
151 render.update(); |
18 | 152 |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
153 // Limit velocities |
26 | 154 foreach(o; objectList) { |
155 o.limitVelocity(); | |
156 o.updateState(); | |
157 } | |
18 | 158 |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
159 // Apply thrust |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
160 if(human.thrust && ship1) { |
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
161 ship1.thrust(); |
18 | 162 } |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
163 } |
18 | 164 |
27
d63faa81a5e4
removed Dog, added derelict and glfw
zzzzrrr <mason.green@gmail.com>
parents:
26
diff
changeset
|
165 delete render; |
18 | 166 } |
0 | 167 |
18 | 168 void initWorld() { |
169 // Define world boundaries | |
24 | 170 worldAABB.lowerBound.set(-400.0f, -250.0f); |
171 worldAABB.upperBound.set(400.0f, 250.0f); | |
18 | 172 world = new bzWorld(worldAABB, gravity, allowSleep); |
173 world.boundaryListener = m_boundaryListener; | |
24 | 174 ship2 = new UrQuan(world); |
175 ship1 = new Orz(world); | |
18 | 176 ship2.rBody.angle = 3.14159265/4; |
22 | 177 planet = new Planet(world); |
25 | 178 for(int i; i < NUM_ASTROIDS; i++) { |
179 auto asteroid = new Asteroid(world); | |
180 objectList.add(asteroid); | |
181 } | |
18 | 182 } |
183 | |
184 void boundaryViolated(bzBody rBody) | |
185 { | |
186 float x,y; | |
0 | 187 |
18 | 188 if(rBody.position.x > worldAABB.upperBound.x) { |
189 x = worldAABB.lowerBound.x + 5; | |
190 rBody.position = bzVec2(x, rBody.position.y); | |
191 } else if (rBody.position.x < worldAABB.lowerBound.x) { | |
192 x = worldAABB.upperBound.x - 5; | |
193 rBody.position = bzVec2(x, rBody.position.y); | |
194 } else if (rBody.position.y > worldAABB.upperBound.y) { | |
195 y = worldAABB.lowerBound.y + 5; | |
196 rBody.position = bzVec2(rBody.position.x, y); | |
197 } else if(rBody.position.y < worldAABB.lowerBound.y) { | |
198 y = worldAABB.upperBound.y - 5; | |
199 rBody.position = bzVec2(rBody.position.x, y); | |
200 } | |
201 } | |
0 | 202 } |