Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package battle;
import asteroids.Action;
import asteroids.GameObject;
import asteroids.GameState;
import asteroids.Missile;
import math.Vector2d;
import java.awt.*;
import java.awt.geom.AffineTransform;
import static asteroids.Constants.*;
public class NeuroShip extends GameObject {
// define the shape of the ship
static int[] xp = {-2, 0, 2, 0};
static int[] yp = {2, -2, 2, 0};
// this is the thrust poly that will be drawn when the ship
// is thrusting
static int[] xpThrust = {-2, 0, 2, 0};
static int[] ypThrust = {2, 3, 2, 0};
public static double scale = 5;
// define how quickly the ship will rotate
static double steerStep = 10 * Math.PI / 180;
// this is the friction that makes the ship slow down over time
static double loss = 0.995;
double releaseVelocity = 0;
double minVelocity = 2;
public static double maxRelease = 10;
Color color = Color.white;
boolean thrusting = false;
static double gravity = 0.0;
public Action action;
// position and velocity
public Vector2d d;
public NeuroShip(Vector2d s, Vector2d v, Vector2d d) {
super(new Vector2d(s), new Vector2d(v));
this.d = new Vector2d(d);
}
public NeuroShip copy() {
NeuroShip ship = new NeuroShip(s, v, d);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
ship.action = new Action(action);
ship.releaseVelocity = releaseVelocity;
return ship;
}
public double r() {
return scale * 2.4;
}
// public Ship() {
// super(new Vector2d(), new Vector2d());
// d = new Vector2d(0, -1);
// }
//
public void reset() {
s.set(width / 2, height / 2);
v.zero();
d.set(0, -1);
dead = false;
// System.out.println("Reset the ship ");
}
public void update() {
update(action);
}
public NeuroShip update(Action action) {
// what if this is always on?
// action has fields to specify thrust, turn and shooting
// action.thrust = 1;
if (action.thrust > 0) {
thrusting = true;
} else {
thrusting = false;
}
d.rotate(action.turn * steerStep);
v.add(d, action.thrust * t * 0.3 / 2);
v.y += gravity;
// v.x = 0.5;
v.mul(loss);
s.add(v);
// now create a missile if necessary
// if the release velocity is zero
releaseVelocity += 1.0;
if (action.shoot) {
tryMissileLaunch();
} else {
}
return this;
}
private void tryMissileLaunch() {
// System.out.println("Trying a missile launch");
if (releaseVelocity > maxRelease) {
releaseVelocity = Math.max(releaseVelocity, missileMinVelocity * 2);
Missile m = new Missile(s, new Vector2d(0, 0));
releaseVelocity = Math.min(releaseVelocity, maxRelease);
m.v.add(d, releaseVelocity);
// make it clear the ship
m.s.add(m.v, (r() + missileRadius) * 1.5 / m.v.mag());
releaseVelocity = 0;
// System.out.println("Fired: " + m);
// sounds.fire();
} else {
// System.out.println("Failed!");
}
}
public String toString() {
return s + "\t " + v;
}
public void draw(Graphics2D g) {
AffineTransform at = g.getTransform();
g.translate(s.x, s.y);
double rot = Math.atan2(d.y, d.x) + Math.PI / 2;
g.rotate(rot);
g.scale(scale, scale);
g.setColor(color);
g.fillPolygon(xp, yp, xp.length);
if (thrusting) {
g.setColor(Color.red);
g.fillPolygon(xpThrust, ypThrust, xpThrust.length);
}
g.setTransform(at);
}
public void hit() {
// super.hit();
// System.out.println("Ship destroyed");
dead = true;
// sounds.play(sounds.bangLarge);
}
public boolean dead() {
return dead;
}
}