source: galcon-client/src/com/example/helloandroid/Fleet.java@ 4a1f590

Last change on this file since 4a1f590 was 6ebf60f, checked in by Zero Cool <devnull@…>, 15 years ago

Updates to fleet traveling + planet attack and fleet deletion

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[159eef8]1package com.example.helloandroid;
2
[9ef6f68]3import java.util.ArrayList;
4
[61c4fbc]5import android.graphics.Canvas;
[95509e1]6import android.graphics.Color;
[61c4fbc]7import android.graphics.Paint;
8import android.graphics.Path;
9import android.util.Log;
10
[159eef8]11public class Fleet {
[96857ee]12 private int x;
13 private int y;
[5a03a06]14 private double dblX;
15 private double dblY;
[96857ee]16 private double slope, xIntercept;
17 private double direction;
[6ebf60f]18 private Planet destination, nearPlanet;
[96857ee]19 private int numShips;
20 private int faction;
[9ef6f68]21 private boolean isNextToAPlanet;
[6ebf60f]22 private boolean isClockwise;
23
[0986844]24
[96857ee]25 /* Optimising: pre-calculate paths */
[159eef8]26 public Fleet(Planet source, Planet destination, int numShips, int faction) {
[95509e1]27 source.setNumShips(source.getNumShips()-numShips);
28
[96857ee]29 //Calculate initial coordinates and direction
[5a03a06]30 if((destination.getX() - source.getX()) != 0){
[0986844]31 //line formula
32 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
[5a03a06]33
[0986844]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
[96857ee]46 } else {
[1291908]47 if((destination.getY() - source.getY()) > 0 ){
[1a91f0d]48 direction = Math.PI/2;
[5a03a06]49 dblX = destination.getX();
50 dblY = source.getY() + source.radius + 10;
[1a91f0d]51 } else {
52 direction = 3*Math.PI/2;
[5a03a06]53 dblX = destination.getX();
54 dblY = source.getY() - source.radius - 10;
[1a91f0d]55 }
[0986844]56 xIntercept = destination.getX();
[96857ee]57 }
[0986844]58
[5a03a06]59 x = (int)dblX;
60 y = (int)dblY;
[0986844]61
[159eef8]62 this.numShips = numShips;
63 this.faction = faction;
64 this.destination = destination;
[9ef6f68]65 this.isNextToAPlanet = false;
[159eef8]66 }
[0986844]67
68
[96857ee]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
[95509e1]139 public void draw(Canvas canvas, Paint linePaint) {
[61c4fbc]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
[95509e1]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
[61c4fbc]171 canvas.drawPath(p, linePaint);
[95509e1]172
173 linePaint.setColor(prevC);
[61c4fbc]174 }
[96857ee]175
[9ef6f68]176 public void update(ArrayList<Planet> planets) {
[6ebf60f]177 int speed = 3; //pixels per move
[0986844]178 double distance, tangentDirection, angle;
[6ebf60f]179
[9ef6f68]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){
[6ebf60f]188 nearPlanet = p;
[9ef6f68]189 break;
190 }
191 }
192 //if temp planet is not picked move along the direction by #speed
[6ebf60f]193 if(nearPlanet == null) {
[9ef6f68]194 dblY += (Math.sin(direction)*speed);
195 dblX += (Math.cos(direction)*speed);
[0986844]196
[9ef6f68]197 x = (int)dblX;
198 y = (int)dblY;
[61c4fbc]199 }else {
[6ebf60f]200 if(nearPlanet == destination){
201 attack();
202 return;
203 }
204
205 double radAngle = Math.atan(getSlope(nearPlanet.getX(), nearPlanet.getY(), dblX, dblY));
[0986844]206 //figure out which way to go clockwise or counter clockwise
[6ebf60f]207 tangentDirection = (Math.atan(getSlope(nearPlanet.getX(),nearPlanet.getY(),dblX,dblY))) + (Math.PI/2);
[0986844]208 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
[6ebf60f]209
[0986844]210 if (angle <= Math.PI/2)
211 angle = Math.PI - angle;
[61c4fbc]212
[6ebf60f]213 if(dblX < nearPlanet.getX())
[61c4fbc]214 radAngle += Math.PI;
215
[0986844]216 angle = radAngle + Math.PI/2;
[61c4fbc]217
218 double diff = direction-angle;
219
[0986844]220 if(diff > 0)
221 isClockwise = false;
222 else
223 isClockwise = true;
[6ebf60f]224
[61c4fbc]225 if(Math.abs(diff)>Math.PI/2)
226 direction = angle-Math.PI;
227 else
228 direction = angle;
229
[6ebf60f]230 xIntercept = dblY - (dblX*Math.tan(direction));
231
232 isNextToAPlanet = true;
[9ef6f68]233 //figure out which way to go clockwise or counter clockwise
[61c4fbc]234 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
[9ef6f68]235 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
236 if (angle <= Math.PI/2)
[61c4fbc]237 angle = Math.PI - angle;*/
[9ef6f68]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
[0986844]244
[6ebf60f]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;
[0986844]253
[6ebf60f]254 if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){
255 isNextToAPlanet = false;
256 nearPlanet = null;
257 }
[0986844]258 } else {
[6ebf60f]259 angle = speed/(double)nearPlanet.radius;
260 Log.i("Gencon", angle + "%");
[0986844]261 if(isClockwise){
[6ebf60f]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);
[0986844]265 } else {
[6ebf60f]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);
[0986844]269 }
[6ebf60f]270 x = (int)dblX;
271 y = (int)dblY;
272 xIntercept = dblY - (dblX*Math.tan(direction));
[0986844]273 }
[9ef6f68]274 }
[159eef8]275 }
[0986844]276
[159eef8]277 // attack the destination planet
[5a03a06]278 //after the method is called the fleet needs to removed
[159eef8]279 public void attack() {
[6ebf60f]280 if(this.faction == destination.getFaction())
281 destination.setNumShips(destination.getNumShips() + numShips);
282 else if(numShips <= destination.getNumShips()){
[1291908]283 destination.setNumShips(destination.getNumShips() - numShips);
284 } else {
285 destination.setNumShips(numShips - destination.getNumShips());
286 destination.setFaction(this.faction);
[1a91f0d]287 }
[6ebf60f]288 this.numShips = 0;
[159eef8]289 }
[0986844]290
[9ef6f68]291 //helper functions
[61c4fbc]292 private double getDistanceBetween(double x1, double y1, double x2, double y2) {
[9ef6f68]293 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
294 }
[0986844]295
[61c4fbc]296 private double getSlope(double x1, double y1, double x2, double y2) {
297 return ((y2 - y1)/(double)(x2 - x1));
[9ef6f68]298 }
[6ebf60f]299}
Note: See TracBrowser for help on using the repository browser.