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;
|
---|
19 | private int numShips;
|
---|
20 | private int faction;
|
---|
21 | private boolean isNextToAPlanet;
|
---|
22 | private boolean dirChanged, isClockwise;
|
---|
23 |
|
---|
24 | /* Optimising: pre-calculate paths */
|
---|
25 | public Fleet(Planet source, Planet destination, int numShips, int faction) {
|
---|
26 | source.setNumShips(source.getNumShips()-numShips);
|
---|
27 |
|
---|
28 | //Calculate initial coordinates and direction
|
---|
29 | if((destination.getX() - source.getX()) != 0){
|
---|
30 | //line formula
|
---|
31 | slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
|
---|
32 |
|
---|
33 | xIntercept = destination.getY() - (slope*destination.getX());
|
---|
34 |
|
---|
35 | //direction
|
---|
36 | direction = Math.atan(slope);
|
---|
37 |
|
---|
38 | //coordinates for all 4 coordinates
|
---|
39 | if((destination.getX() - source.getX()) < 0 )
|
---|
40 | direction += Math.PI;
|
---|
41 |
|
---|
42 | dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));
|
---|
43 | dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));
|
---|
44 |
|
---|
45 | } else {
|
---|
46 | if((destination.getY() - source.getY()) > 0 ){
|
---|
47 | direction = Math.PI/2;
|
---|
48 | dblX = destination.getX();
|
---|
49 | dblY = source.getY() + source.radius + 10;
|
---|
50 | } else {
|
---|
51 | direction = 3*Math.PI/2;
|
---|
52 | dblX = destination.getX();
|
---|
53 | dblY = source.getY() - source.radius - 10;
|
---|
54 | }
|
---|
55 | xIntercept = destination.getX();
|
---|
56 | }
|
---|
57 |
|
---|
58 | x = (int)dblX;
|
---|
59 | y = (int)dblY;
|
---|
60 |
|
---|
61 | this.numShips = numShips;
|
---|
62 | this.faction = faction;
|
---|
63 | this.destination = destination;
|
---|
64 | this.isNextToAPlanet = false;
|
---|
65 | dirChanged = 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 = 1; //pixels per move
|
---|
178 | double distance, tangentDirection, angle;
|
---|
179 | Planet temp = null;
|
---|
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 | temp = p;
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | //if temp planet is not picked move along the direction by #speed
|
---|
193 | if(temp == null || dirChanged) {
|
---|
194 | dblY += (Math.sin(direction)*speed);
|
---|
195 | dblX += (Math.cos(direction)*speed);
|
---|
196 |
|
---|
197 | x = (int)dblX;
|
---|
198 | y = (int)dblY;
|
---|
199 | }else {
|
---|
200 | double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY));
|
---|
201 | //figure out which way to go clockwise or counter clockwise
|
---|
202 | tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
203 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
204 | if (angle <= Math.PI/2)
|
---|
205 | angle = Math.PI - angle;
|
---|
206 |
|
---|
207 | if(dblX < temp.getX())
|
---|
208 | radAngle += Math.PI;
|
---|
209 |
|
---|
210 | angle = radAngle + Math.PI/2;
|
---|
211 |
|
---|
212 | double diff = direction-angle;
|
---|
213 |
|
---|
214 | if(diff > 0)
|
---|
215 | isClockwise = false;
|
---|
216 | else
|
---|
217 | isClockwise = true;
|
---|
218 | if(Math.abs(diff)>Math.PI/2)
|
---|
219 | direction = angle-Math.PI;
|
---|
220 | else
|
---|
221 | direction = angle;
|
---|
222 |
|
---|
223 | dirChanged = true;
|
---|
224 |
|
---|
225 | //figure out which way to go clockwise or counter clockwise
|
---|
226 | /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
|
---|
227 | angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
|
---|
228 | if (angle <= Math.PI/2)
|
---|
229 | angle = Math.PI - angle;*/
|
---|
230 | //get next point and the direction and set it
|
---|
231 | }
|
---|
232 | } else {
|
---|
233 | //can you reach the center of the planet by following this direction
|
---|
234 | //if so set isNextToAPlanet to false and move
|
---|
235 | //otherwise continue moving along the circumferenceds4
|
---|
236 |
|
---|
237 |
|
---|
238 | if(true){
|
---|
239 |
|
---|
240 | } else {
|
---|
241 | angle = speed/temp.radius;
|
---|
242 | if(isClockwise){
|
---|
243 | dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX()));
|
---|
244 | dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY()));
|
---|
245 | direction = direction - (Math.PI/2);
|
---|
246 | } else {
|
---|
247 | dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX()));
|
---|
248 | dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY()));
|
---|
249 | direction = direction + (Math.PI/2);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | // attack the destination planet
|
---|
256 | //after the method is called the fleet needs to removed
|
---|
257 | public void attack() {
|
---|
258 | if(numShips <= destination.getNumShips()){
|
---|
259 | destination.setNumShips(destination.getNumShips() - numShips);
|
---|
260 | } else {
|
---|
261 | destination.setNumShips(numShips - destination.getNumShips());
|
---|
262 | destination.setFaction(this.faction);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | //helper functions
|
---|
267 | private double getDistanceBetween(double x1, double y1, double x2, double y2) {
|
---|
268 | return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
|
---|
269 | }
|
---|
270 |
|
---|
271 | private double getSlope(double x1, double y1, double x2, double y2) {
|
---|
272 | return ((y2 - y1)/(double)(x2 - x1));
|
---|
273 | }
|
---|
274 | }
|
---|