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

Last change on this file since 0986844 was 0986844, checked in by Zero Cool <devnull@…>, 14 years ago

Some more updates, almost done going round the planet

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