22
|
1 /*
|
|
2 * Copyright (c) 2009 Blaze Contributors http://www.dsource.org/projects/blaze
|
|
3 * Maintained by Mason Green (zzzzrrr)
|
|
4 *
|
|
5 * This software is provided 'as-is', without any express or implied
|
|
6 * warranty. In no event will the authors be held liable for any damages
|
|
7 * arising from the use of this software.
|
|
8 *
|
|
9 * Permission is granted to anyone to use this software for any purpose,
|
|
10 * including commercial applications, and to alter it and redistribute it
|
|
11 * freely, subject to the following restrictions:
|
|
12 *
|
|
13 * 1. The origin of this software must not be misrepresented; you must not
|
|
14 * claim that you wrote the original software. If you use this software
|
|
15 * in a product, an acknowledgment in the product documentation would be
|
|
16 * appreciated but is not required.
|
|
17 *
|
|
18 * 2. Altered source versions must be plainly marked as such, and must not be
|
|
19 * misrepresented as being the original software.
|
|
20 *
|
|
21 * 3. This notice may not be removed or altered from any source
|
|
22 * distribution.
|
|
23 */
|
|
24 module openmelee.ships.asteroids;
|
|
25
|
|
26 import blaze.dynamics.bzBody : bzBody;
|
|
27 import blaze.bzWorld: bzWorld;
|
|
28 import blaze.dynamics.bzBodyDef;
|
|
29 import blaze.collision.shapes.bzCircle : bzCircleDef;
|
|
30 import blaze.collision.shapes.bzPolygon : bzPolyDef;
|
|
31 import blaze.collision.shapes.bzShape : bzShapeDef;
|
|
32 import blaze.common.bzMath: bzVec2, bzMul, bzXForm;
|
|
33 import blaze.dynamics.forces.bzAttractor: bzAttractor;
|
|
34
|
|
35
|
|
36 import openmelee.ai.utilities;
|
|
37
|
|
38 const PI = 3.141593;
|
|
39
|
|
40 class Asteroid
|
|
41 {
|
|
42
|
|
43 bzBodyDef bd;
|
|
44 bzBody rBody;
|
|
45 bzShapeDef sd;
|
|
46
|
|
47 bzWorld world;
|
|
48
|
|
49 this(bzWorld world) {
|
|
50 this.world = world;
|
|
51 init();
|
|
52 }
|
|
53
|
|
54 void init() {
|
|
55
|
|
56 float minRadius = 0.1;
|
|
57 float maxRadius = 10;
|
|
58 float strength = 0.5f;
|
|
59 bzVec2 center = bzVec2(0,0);
|
|
60
|
|
61 {
|
|
62 float radius = 0.5f;
|
|
63 float density = 5.0f;
|
|
64 auto sd1 = new bzCircleDef(density, radius);
|
|
65 sd1.localPosition.set(-0.5f, 0.5f);
|
|
66
|
|
67 density = 0.0f;
|
|
68 auto sd2 = new bzCircleDef(density, radius);
|
|
69 sd2.localPosition.set(0.5f, 0.5f);
|
|
70
|
|
71 for (int i = 0; i < 5; ++i)
|
|
72 {
|
|
73 float x = randomRange(-100f, 100f);
|
|
74 float y = randomRange(-100f, 100f);
|
|
75 bzVec2 position = bzVec2(x , y);
|
|
76 float angle = randomRange(-PI, PI);
|
|
77 bd = new bzBodyDef(position, angle);
|
|
78 rBody = world.createBody(bd);
|
|
79 rBody.createShape(sd1);
|
|
80 rBody.createShape(sd2);
|
|
81 rBody.setMassFromShapes();
|
|
82 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
|
|
83 world.addForce(attractor);
|
|
84 rBody.linearVelocity = bzVec2(x*20, y*20);
|
|
85 }
|
|
86 }
|
|
87
|
|
88 {
|
|
89 auto sd1 = new bzPolyDef();
|
|
90 sd1.setAsBox(0.25f, 0.5f);
|
|
91 sd1.density = 5.0f;
|
|
92
|
|
93 auto sd2 = new bzPolyDef();
|
|
94 sd2.setAsBox(0.25f, 0.5f, bzVec2(0.0f, -0.5f), 0.5f * PI);
|
|
95 sd2.density = 5.0f;
|
|
96
|
|
97 for (int i = 0; i < 5; ++i)
|
|
98 {
|
|
99 float x = randomRange(-100f, 100f);
|
|
100 float y = randomRange(-100f, 100f);
|
|
101 bzVec2 position = bzVec2(x , y);
|
|
102 float angle = randomRange(-PI, PI);
|
|
103 bd = new bzBodyDef(position, angle);
|
|
104 rBody = world.createBody(bd);
|
|
105 rBody.createShape(sd1);
|
|
106 rBody.createShape(sd2);
|
|
107 rBody.setMassFromShapes();
|
|
108 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
|
|
109 world.addForce(attractor);
|
|
110 rBody.linearVelocity = bzVec2(x*20, y*20);
|
|
111 }
|
|
112 }
|
|
113
|
|
114 {
|
|
115 bzXForm xf1;
|
|
116 xf1.R.set(0.3524f * PI);
|
|
117 xf1.position = bzMul(xf1.R, bzVec2(1.0f, 0.0f));
|
|
118
|
|
119 auto sd1 = new bzPolyDef();
|
|
120 sd1.vertices.length = 3;
|
|
121 sd1.vertices[0] = bzMul(xf1, bzVec2(-1.0f, 0.0f));
|
|
122 sd1.vertices[1] = bzMul(xf1, bzVec2(1.0f, 0.0f));
|
|
123 sd1.vertices[2] = bzMul(xf1, bzVec2(0.0f, 0.5f));
|
|
124 sd1.density = 5.0f;
|
|
125
|
|
126 bzXForm xf2;
|
|
127 xf2.R.set(-0.3524f * PI);
|
|
128 xf2.position = bzMul(xf2.R, bzVec2(-1.0f, 0.0f));
|
|
129
|
|
130 auto sd2 = new bzPolyDef();
|
|
131 sd2.vertices.length = 3;
|
|
132 sd2.vertices[0] = bzMul(xf2, bzVec2(-1.0f, 0.0f));
|
|
133 sd2.vertices[1] = bzMul(xf2, bzVec2(1.0f, 0.0f));
|
|
134 sd2.vertices[2] = bzMul(xf2, bzVec2(0.0f, 0.5f));
|
|
135 sd2.density = 5.0f;
|
|
136
|
|
137 for (int i = 0; i < 5; ++i)
|
|
138 {
|
|
139 float x = randomRange(-100f, 100f);
|
|
140 float y = randomRange(-100f, 100f);
|
|
141 bzVec2 position = bzVec2(x , y);
|
|
142 float angle = 0.0f;
|
|
143 bd = new bzBodyDef(position, angle);
|
|
144 rBody = world.createBody(bd);
|
|
145 rBody.createShape(sd1);
|
|
146 rBody.createShape(sd2);
|
|
147 rBody.setMassFromShapes();
|
|
148 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius);
|
|
149 world.addForce(attractor);
|
|
150 rBody.linearVelocity = bzVec2(x, y);
|
|
151 }
|
|
152 }
|
|
153 }
|
|
154 }
|