1 | package com.example.helloandroid;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import android.graphics.Canvas;
|
---|
6 | import android.graphics.Color;
|
---|
7 | import android.graphics.Paint;
|
---|
8 | import android.graphics.Path;
|
---|
9 | import android.util.Log;
|
---|
10 |
|
---|
11 | public class Fleet {
|
---|
12 | private int x;
|
---|
13 | private int y;
|
---|
14 | private double dblX;
|
---|
15 | private double dblY;
|
---|
16 | private double slope, xIntercept;
|
---|
17 | private double direction;
|
---|
18 | private Planet destination, nearPlanet;
|
---|
19 | private int numShips;
|
---|
20 | private int faction;
|
---|
21 | private boolean isNextToAPlanet;
|
---|
22 | private boolean isClockwise;
|
---|
23 |
|
---|
24 |
|
---|
25 | /* Optimising: pre-calculate paths */
|
---|
26 | public Fleet(Planet source, Planet destination, int numShips, int faction) {
|
---|
27 | source.setNumShips(source.getNumShips()-numShips);
|
---|
28 |
|
---|
29 | //Calculate initial coordinates and direction
|
---|
30 | if((destination.getX() - source.getX()) != 0){
|
---|
31 | //line formula
|
---|
32 | slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
|
---|
33 |
|
---|
34 | xIntercept = destination.getY() - (slope*destination.getX());
|
---|
35 |
|
---|
36 | //direction
|
---|
37 | direction = Math.atan(slope);
|
---|
38 |
|
---|
39 | //coordinates for all 4 coordinates
|
---|
40 | if((destination.getX() - source.getX()) < 0 )
|
---|
41 | direction += Math.PI;
|
---|
42 |
|
---|
43 | dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
|
---|
44 | dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
|
---|
45 |
|
---|
46 | } else {
|
---|
47 | if((destination.getY() - source.getY()) > 0 ){
|
---|
48 | direction = Math.PI/2;
|
---|
49 | dblX = destination.getX();
|
---|
50 | dblY = source.getY() + source.radius + 10;
|
---|
51 | } else {
|
---|
52 | direction = 3*Math.PI/2;
|
---|
53 | dblX = destination.getX();
|
---|
54 | dblY = source.getY() - source.radius - 10;
|
---|
55 | }
|
---|
56 | xIntercept = destination.getX();
|
---|
57 | }
|
---|
58 |
|
---|
59 | x = (int)dblX;
|
---|
60 | y = (int)dblY;
|
---|
61 |
|
---|
62 | this.numShips = numShips;
|
---|
63 | this.faction = faction;
|
---|
64 | this.destination = destination;
|
---|
65 | this.isNextToAPlanet = false;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | public int getX() {
|
---|
70 | return x;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | public void setX(int x) {
|
---|
76 | this.x = x;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 | public int getY() {
|
---|
82 | return y;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | public void setY(int y) {
|
---|
88 | this.y = y;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | public double getDirection() {
|
---|
94 | return direction;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | public void setDirection(double direction) {
|
---|
100 | this.direction = direction;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | public Planet getDestination() {
|
---|
106 | return destination;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | public void setDestination(Planet destination) {
|
---|
112 | this.destination = destination;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | public int getNumShips() {
|
---|
118 | return numShips;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 | public void setNumShips(int numShips) {
|
---|
124 | this.numShips = numShips;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | public int getFaction() {
|
---|
130 | return faction;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | public void setFaction(int faction) {
|
---|
136 | this.faction = faction;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void draw(Canvas canvas, Paint linePaint) {
|
---|
140 | Path p = new Path();
|
---|
141 |
|
---|
142 | p.moveTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
|
---|
143 | p.lineTo((float)(x+5*Math.cos(direction-Math.PI/2)), (float)(y+5*Math.sin(direction-Math.PI/2)));
|
---|
144 | p.lineTo((float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)));
|
---|
145 | p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
|
---|
146 |
|
---|
147 | int c, prevC = linePaint.getColor();
|
---|
148 |
|
---|
149 | switch(faction) {
|
---|
150 | case 0:
|
---|
151 | c = Color.argb(255, 100, 100, 100);
|
---|
152 | break;
|
---|
153 | case 1:
|
---|
154 | c = Color.argb(255, 255, 0, 0);
|
---|
155 | break;
|
---|
156 | case 2:
|
---|
157 | c = Color.argb(255, 0, 180, 0);
|
---|
158 | break;
|
---|
159 | case 3:
|
---|
160 | c = Color.argb(255, 0, 0, 255);
|
---|
161 | break;
|
---|
162 | case 4:
|
---|
163 | c = Color.argb(255, 150, 150, 0);
|
---|
164 | break;
|
---|
165 | default:
|
---|
166 | c = prevC;
|
---|
167 | }
|
---|
168 |
|
---|
169 | linePaint.setColor(c);
|
---|
170 |
|
---|
171 | canvas.drawPath(p, linePaint);
|
---|
172 |
|
---|
173 | linePaint.setColor(prevC);
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void update(ArrayList<Planet> planets) {
|
---|
177 | int speed = 3; //pixels per move
|
---|
178 | double distance, tangentDirection, angle;
|
---|
179 |
|
---|
180 | //is the ship going around a planet already
|
---|
181 | if(!isNextToAPlanet){
|
---|
182 | /*looks through all the planets to figure out if
|
---|
183 | the ship's path is about to intersect a planet*/
|
---|
184 | for(Planet p: planets){
|
---|
185 | //if two point of intersection are found save planet
|
---|
186 | distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
|
---|
187 | if(distance <= p.radius){
|
---|
188 | nearPlanet = p;
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | //if temp planet is not picked move along the direction by #speed
|
---|
193 | if(nearPlanet == null) {
|
---|
194 | dblY += (Math.sin(direction)*speed);
|
---|
195 | dblX += (Math.cos(direction)*speed);
|
---|
196 |
|
---|
197 | x = (int)dblX;
|
---|
198 | y = (int)dblY;
|
---|
199 | }else {
|
---|
200 | if(nearPlanet == destination){
|
---|
201 | attack();
|
---|
202 | return;
|
---|
203 | }
|
---|
204 |
|
---|
205 | double radAngle = Math.atan(getSlope(nearPlanet.getX(), nearPlanet.getY(), dblX, dblY));
|
---|
206 | //figure out which way to go clockwise or counter clockwise
|
---|
207 | tangentDirection = (Math.atan(getSlope(nearPlanet.getX(),nearPlanet.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
208 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
209 |
|
---|
210 | if (angle <= Math.PI/2)
|
---|
211 | angle = Math.PI - angle;
|
---|
212 |
|
---|
213 | if(dblX < nearPlanet.getX())
|
---|
214 | radAngle += Math.PI;
|
---|
215 |
|
---|
216 | angle = radAngle + Math.PI/2;
|
---|
217 |
|
---|
218 | double diff = direction-angle;
|
---|
219 |
|
---|
220 | if(diff > 0)
|
---|
221 | isClockwise = false;
|
---|
222 | else
|
---|
223 | isClockwise = true;
|
---|
224 |
|
---|
225 | if(Math.abs(diff)>Math.PI/2)
|
---|
226 | direction = angle-Math.PI;
|
---|
227 | else
|
---|
228 | direction = angle;
|
---|
229 |
|
---|
230 | xIntercept = dblY - (dblX*Math.tan(direction));
|
---|
231 |
|
---|
232 | isNextToAPlanet = true;
|
---|
233 | //figure out which way to go clockwise or counter clockwise
|
---|
234 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
235 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
236 | if (angle <= Math.PI/2)
|
---|
237 | angle = Math.PI - angle;*/
|
---|
238 | //get next point and the direction and set it
|
---|
239 | }
|
---|
240 | } else {
|
---|
241 | //can you reach the center of the planet by following this direction
|
---|
242 | //if so set isNextToAPlanet to false and move
|
---|
243 | //otherwise continue moving along the circumferenceds4
|
---|
244 |
|
---|
245 | double dist = Math.abs(destination.getY() - (direction*destination.getY()) - xIntercept)/(double)Math.sqrt(Math.pow(direction,2)+1);
|
---|
246 | Log.i("Gencon", dist + " units");
|
---|
247 | if(dist < 5){
|
---|
248 | dblY += (Math.sin(direction)*speed);
|
---|
249 | dblX += (Math.cos(direction)*speed);
|
---|
250 |
|
---|
251 | x = (int)dblX;
|
---|
252 | y = (int)dblY;
|
---|
253 |
|
---|
254 | if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){
|
---|
255 | isNextToAPlanet = false;
|
---|
256 | nearPlanet = null;
|
---|
257 | }
|
---|
258 | } else {
|
---|
259 | angle = speed/(double)nearPlanet.radius;
|
---|
260 | Log.i("Gencon", angle + "%");
|
---|
261 | if(isClockwise){
|
---|
262 | dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(nearPlanet.radius) + nearPlanet.getX()));
|
---|
263 | dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(nearPlanet.radius) + nearPlanet.getY()));
|
---|
264 | direction = direction - (angle*.4);
|
---|
265 | } else {
|
---|
266 | dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(nearPlanet.radius) + nearPlanet.getX()));
|
---|
267 | dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(nearPlanet.radius) + nearPlanet.getY()));
|
---|
268 | direction = direction + (angle*.4);
|
---|
269 | }
|
---|
270 | x = (int)dblX;
|
---|
271 | y = (int)dblY;
|
---|
272 | xIntercept = dblY - (dblX*Math.tan(direction));
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | // attack the destination planet
|
---|
278 | //after the method is called the fleet needs to removed
|
---|
279 | public void attack() {
|
---|
280 | if(this.faction == destination.getFaction())
|
---|
281 | destination.setNumShips(destination.getNumShips() + numShips);
|
---|
282 | else if(numShips <= destination.getNumShips()){
|
---|
283 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
284 | } else {
|
---|
285 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
286 | destination.setFaction(this.faction);
|
---|
287 | }
|
---|
288 | this.numShips = 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | //helper functions
|
---|
292 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
293 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
294 | }
|
---|
295 |
|
---|
296 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
297 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
298 | }
|
---|
299 | } |
---|