1 | import java.awt.image.ImageObserver;
|
---|
2 | import java.awt.Image;
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.awt.image.BufferedImage;
|
---|
6 |
|
---|
7 | public class Ship {
|
---|
8 | private int x;
|
---|
9 | private int y;
|
---|
10 | private int startX;
|
---|
11 | private int startY;
|
---|
12 | private int speed;
|
---|
13 | private int hitpoints;
|
---|
14 | private int maxHitpoints;
|
---|
15 | private int patrolRadius;
|
---|
16 | private long timeCreated;
|
---|
17 | private BufferedImage pic;
|
---|
18 | private ArrayList shotTypes;
|
---|
19 | private ArrayList shots;
|
---|
20 |
|
---|
21 | public Ship() {
|
---|
22 | this.x = 0;
|
---|
23 | this.y = 0;
|
---|
24 | this.startX = 0;
|
---|
25 | this.startY = 0;
|
---|
26 | this.speed = 0;
|
---|
27 | this.hitpoints = 0;
|
---|
28 | this.maxHitpoints = 0;
|
---|
29 | this.patrolRadius = 0;
|
---|
30 | this.pic = null;
|
---|
31 | this.shotTypes = new ArrayList();
|
---|
32 | this.shots = new ArrayList();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public Ship(final int newX, final int newY, final int newSpeed, final int newMaxHitpoints, final int newRadius, final int newDamage, final BufferedImage newPic, final BufferedImage newShotPic) {
|
---|
36 | this.x = newX;
|
---|
37 | this.y = newY;
|
---|
38 | this.startX = newX;
|
---|
39 | this.startY = newY;
|
---|
40 | this.speed = newSpeed;
|
---|
41 | this.hitpoints = newMaxHitpoints;
|
---|
42 | this.maxHitpoints = newMaxHitpoints;
|
---|
43 | this.patrolRadius = newRadius;
|
---|
44 | this.pic = newPic;
|
---|
45 | this.shotTypes = new ArrayList();
|
---|
46 | this.shots = new ArrayList();
|
---|
47 | this.shotTypes.add(new ShotType(0, 25, newDamage, 400, 500, newShotPic));
|
---|
48 | }
|
---|
49 |
|
---|
50 | public Ship(final int newX, final int newY, final int newSpeed, final int newMaxHitpoints, final int newRadius, final int newDamage, final int newShotSpeed, final int newShotInterval, final BufferedImage newPic, final BufferedImage newShotPic) {
|
---|
51 | this.x = newX;
|
---|
52 | this.y = newY;
|
---|
53 | this.startX = newX;
|
---|
54 | this.startY = newY;
|
---|
55 | this.speed = newSpeed;
|
---|
56 | this.hitpoints = newMaxHitpoints;
|
---|
57 | this.maxHitpoints = newMaxHitpoints;
|
---|
58 | this.patrolRadius = newRadius;
|
---|
59 | this.pic = newPic;
|
---|
60 | this.shotTypes = new ArrayList();
|
---|
61 | this.shots = new ArrayList();
|
---|
62 | this.shotTypes.add(new ShotType(0, 25, newDamage, newShotSpeed, newShotInterval, newShotPic));
|
---|
63 | }
|
---|
64 |
|
---|
65 | public Ship(final int newX, final int newY, final int newSpeed, final int newMaxHitpoints, final int newRadius, final int newDamage, final int newShotSpeed, final int newShotInterval, final int shotX, final int shotY, final BufferedImage newPic, final BufferedImage newShotPic) {
|
---|
66 | this.x = newX;
|
---|
67 | this.y = newY;
|
---|
68 | this.startX = newX;
|
---|
69 | this.startY = newY;
|
---|
70 | this.speed = newSpeed;
|
---|
71 | this.hitpoints = newMaxHitpoints;
|
---|
72 | this.maxHitpoints = newMaxHitpoints;
|
---|
73 | this.patrolRadius = newRadius;
|
---|
74 | this.pic = newPic;
|
---|
75 | this.shotTypes = new ArrayList();
|
---|
76 | this.shots = new ArrayList();
|
---|
77 | this.shotTypes.add(new ShotType(shotX, shotY, newDamage, newShotSpeed, newShotInterval, newShotPic));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void produceVolley() {
|
---|
81 | if (this.hitpoints > 0) {
|
---|
82 | for (int i = 0; i < this.shotTypes.size(); ++i) {
|
---|
83 | if (((ShotType)this.shotTypes.get(i)).ready()) {
|
---|
84 | this.shots.add(((ShotType)this.shotTypes.get(i)).produceBullet(this.x, this.y));
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void patrol() {
|
---|
91 | if (this.timeCreated == 0L) {
|
---|
92 | this.timeCreated = System.currentTimeMillis();
|
---|
93 | }
|
---|
94 | if (this.hitpoints > 0) {
|
---|
95 | int dist = (int)(System.currentTimeMillis() - this.timeCreated) * this.speed / 1000;
|
---|
96 | dist %= 2 * this.patrolRadius;
|
---|
97 | if (this.patrolRadius / 2 < dist && dist <= 3 * this.patrolRadius / 2) {
|
---|
98 | dist = this.patrolRadius - dist;
|
---|
99 | }
|
---|
100 | if (3 * this.patrolRadius / 2 < dist && dist <= 2 * this.patrolRadius) {
|
---|
101 | dist -= 2 * this.patrolRadius;
|
---|
102 | }
|
---|
103 | this.x = this.startX + dist;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void draw(final Graphics g) {
|
---|
108 | for (int x = 0; x < this.shots.size(); ++x) {
|
---|
109 | if (((Shot)this.shots.get(x)).outOfBounds()) {
|
---|
110 | this.shots.remove(this.shots.get(x));
|
---|
111 | } else {
|
---|
112 | ((Shot)this.shots.get(x)).draw(g);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if (this.hitpoints > 0) {
|
---|
116 | g.drawImage(this.pic, this.x - this.pic.getWidth() / 2, this.y - this.pic.getHeight() / 2, null);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public boolean collides(final Shot incomingShot) {
|
---|
121 | return (this.x <= incomingShot.getX() || this.x <= incomingShot.getX() + (this.pic.getWidth() + incomingShot.getPic().getWidth()) / 2) && (this.x >= incomingShot.getX() || this.x + (this.pic.getWidth() + incomingShot.getPic().getWidth()) / 2 >= incomingShot.getX()) && (this.y <= incomingShot.getY() || this.y <= incomingShot.getY() + (this.pic.getHeight() + incomingShot.getPic().getHeight()) / 2) && (this.y >= incomingShot.getY() || this.y + (this.pic.getHeight() + incomingShot.getPic().getHeight()) / 2 >= incomingShot.getY());
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void reset() {
|
---|
125 | this.x = this.startX;
|
---|
126 | this.y = this.startY;
|
---|
127 | this.hitpoints = this.maxHitpoints;
|
---|
128 | this.timeCreated = 0L;
|
---|
129 | this.shots.clear();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public int getX() {
|
---|
133 | return this.x;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public int getY() {
|
---|
137 | return this.y;
|
---|
138 | }
|
---|
139 |
|
---|
140 | public int getSpeed() {
|
---|
141 | return this.speed;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public int getHitpoints() {
|
---|
145 | return this.hitpoints;
|
---|
146 | }
|
---|
147 |
|
---|
148 | public int getMaxHitpoints() {
|
---|
149 | return this.maxHitpoints;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public BufferedImage getPic() {
|
---|
153 | return this.pic;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public ArrayList getShots() {
|
---|
157 | return this.shots;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void setX(final int newX) {
|
---|
161 | this.x = newX;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void setY(final int newY) {
|
---|
165 | this.y = newY;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void setHitpoints(final int newHitpoints) {
|
---|
169 | this.hitpoints = newHitpoints;
|
---|
170 | if (this.hitpoints > this.maxHitpoints) {
|
---|
171 | this.hitpoints = this.maxHitpoints;
|
---|
172 | }
|
---|
173 | else if (this.hitpoints < 0) {
|
---|
174 | this.hitpoints = 0;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | public void addShotType(final ShotType newType) {
|
---|
179 | this.shotTypes.add(newType);
|
---|
180 | }
|
---|
181 | }
|
---|