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