Changes in src/com/example/helloandroid/Fleet.java [0986844:95509e1] in galcon-client
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r0986844 r95509e1 4 4 5 5 import android.graphics.Canvas; 6 import android.graphics.Color; 6 7 import android.graphics.Paint; 7 8 import android.graphics.Path; … … 19 20 private int faction; 20 21 private boolean isNextToAPlanet; 21 private boolean dirChanged , isClockwise;22 22 private boolean dirChanged; 23 23 24 /* Optimising: pre-calculate paths */ 24 25 public Fleet(Planet source, Planet destination, int numShips, int faction) { 26 source.setNumShips(source.getNumShips()-numShips); 27 25 28 //Calculate initial coordinates and direction 26 29 if((destination.getX() - source.getX()) != 0){ 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 42 45 } else { 43 46 if((destination.getY() - source.getY()) > 0 ){ … … 50 53 dblY = source.getY() - source.radius - 10; 51 54 } 52 xIntercept = destination.getX(); 53 } 54 55 } 56 55 57 x = (int)dblX; 56 58 y = (int)dblY; 57 59 58 60 this.numShips = numShips; 59 61 this.faction = faction; … … 62 64 dirChanged = false; 63 65 } 64 65 66 67 66 68 public int getX() { 67 69 return x; … … 134 136 } 135 137 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 138 public void draw(Canvas canvas, Paint linePaint) { 140 139 Path p = new Path(); 141 140 … … 145 144 p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2))); 146 145 146 int c, prevC = linePaint.getColor(); 147 148 switch(faction) { 149 case 0: 150 c = Color.argb(255, 100, 100, 100); 151 break; 152 case 1: 153 c = Color.argb(255, 255, 0, 0); 154 break; 155 case 2: 156 c = Color.argb(255, 0, 180, 0); 157 break; 158 case 3: 159 c = Color.argb(255, 0, 0, 255); 160 break; 161 case 4: 162 c = Color.argb(255, 150, 150, 0); 163 break; 164 default: 165 c = prevC; 166 } 167 168 linePaint.setColor(c); 147 169 148 170 canvas.drawPath(p, linePaint); 171 172 linePaint.setColor(prevC); 149 173 } 150 174 151 175 public void update(ArrayList<Planet> planets) { 152 176 int speed = 1; //pixels per move 153 double distance, tangentDirection , angle;177 double distance, tangentDirection; 154 178 Planet temp = null; 155 179 //is the ship going around a planet already … … 169 193 dblY += (Math.sin(direction)*speed); 170 194 dblX += (Math.cos(direction)*speed); 171 195 172 196 x = (int)dblX; 173 197 y = (int)dblY; 174 198 }else { 175 199 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 176 //figure out which way to go clockwise or counter clockwise177 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 200 182 201 if(dblX < temp.getX()) 183 202 radAngle += Math.PI; 184 203 185 angle = radAngle + Math.PI/2;204 double angle = radAngle + Math.PI/2; 186 205 187 206 double diff = direction-angle; 188 207 189 if(diff > 0)190 isClockwise = false;191 else192 isClockwise = true;193 208 if(Math.abs(diff)>Math.PI/2) 194 209 direction = angle-Math.PI; 195 210 else 196 211 direction = angle; 197 212 198 213 dirChanged = true; 199 214 … … 209 224 //if so set isNextToAPlanet to false and move 210 225 //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 226 227 } 228 229 230 } 231 230 232 // attack the destination planet 231 233 //after the method is called the fleet needs to removed … … 238 240 } 239 241 } 240 242 241 243 //helper functions 242 244 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 243 245 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 244 246 } 245 247 246 248 private double getSlope(double x1, double y1, double x2, double y2) { 247 249 return ((y2 - y1)/(double)(x2 - x1));
Note:
See TracChangeset
for help on using the changeset viewer.