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