Mercurial > projects > openmelee
annotate ship.d @ 17:82efafc87d54
fixed camera zoom
author | zzzzrrr <mason.green@gmail.com> |
---|---|
date | Tue, 24 Mar 2009 16:47:42 -0400 |
parents | 8e6a9e390cba |
children |
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 */ | |
2 | 31 module openmelee.ship; |
0 | 32 |
33 import tango.io.Stdout: Stdout; | |
34 | |
35 import tango.util.container.LinkedList : LinkedList; | |
36 import blaze.dynamics.bzBody : bzBody; | |
37 import blaze.collision.shapes.bzShape : bzShape; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
38 import blaze.common.bzMath: bzVec2, bzCross, bzClamp; |
6 | 39 import blaze.bzWorld : bzWorld; |
40 import blaze.dynamics.forces.bzAttractor: bzAttractor; | |
0 | 41 |
42 alias LinkedList!(bzShape) ShapeList; | |
43 | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
44 struct State |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
45 { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
46 bzVec2 position; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
47 bzVec2 velocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
48 bzVec2 up; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
49 bzVec2 side; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
50 bzVec2 forward; |
12 | 51 bzVec2 target; |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
52 float speed = 0; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
53 float maxForce = 0; |
13 | 54 bool turn; |
55 | |
56 float enemyAngle; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
57 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
58 bzVec2 predictFuturePosition(float dt) { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
59 return (position + velocity * dt); |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
60 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
61 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
62 |
0 | 63 abstract class Ship |
64 { | |
6 | 65 bzWorld world; |
0 | 66 bzBody rBody; |
67 ShapeList shapes; | |
68 bzVec2 engineForce; | |
69 bzVec2 turnForce; | |
70 bzVec2 leftTurnPoint; | |
71 bzVec2 rightTurnPoint; | |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
72 State state; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
73 |
15 | 74 float battery; |
75 float crew; | |
76 | |
77 float maxLinVel = 50; | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
78 float maxAngVel = 2; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
79 |
6 | 80 this(bzWorld world) { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
81 |
6 | 82 this.world = world; |
0 | 83 shapes = new ShapeList; |
84 } | |
85 | |
86 void thrust() { | |
87 rBody.force += engineForce.rotate(rBody.angle); | |
88 } | |
89 | |
5 | 90 void turnLeft() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
91 |
5 | 92 rBody.torque += bzCross(leftTurnPoint.rotate(rBody.angle), |
0 | 93 turnForce.rotate(rBody.angle)); |
5 | 94 } |
95 | |
96 void turnRight() { | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
97 |
5 | 98 rBody.torque += bzCross(rightTurnPoint.rotate(rBody.angle), |
0 | 99 turnForce.rotate(rBody.angle)); |
100 } | |
6 | 101 |
14 | 102 void setPlanetGravity() { |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
103 float minRadius = 0.1; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
104 float maxRadius = 10; |
15 | 105 float strength = 4.0; |
6 | 106 bzVec2 center = bzVec2(0,0); |
107 auto attractor = new bzAttractor(rBody, center, strength, minRadius, maxRadius); | |
108 world.addForce(attractor); | |
109 } | |
7
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
110 |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
111 void limitVelocity() { |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
112 float vx = rBody.linearVelocity.x; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
113 float vy = rBody.linearVelocity.y; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
114 float av = rBody.angularVelocity; |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
115 rBody.linearVelocity.x = bzClamp(vx, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
116 rBody.linearVelocity.y = bzClamp(vy, -maxLinVel, maxLinVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
117 rBody.angularVelocity = bzClamp(av, -maxAngVel, maxAngVel); |
2217fd1fe384
added angular and linear velocity limit
zzzzrrr <mason.green@gmail.com>
parents:
6
diff
changeset
|
118 } |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
119 |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
120 void updateState() { |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
121 state.velocity = rBody.linearVelocity; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
122 state.speed = state.velocity.length; |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
123 state.position = rBody.position; |
17 | 124 state.forward = engineForce.rotate(rBody.angle); |
11
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
125 } |
d998bf1b0654
Added utilities and AI; fixed steer
Mason Green <mason.green@gmail.com>
parents:
8
diff
changeset
|
126 |
0 | 127 } |