Changes in src/com/example/helloandroid/Fleet.java [6ebf60f:0986844] in galcon-client
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r6ebf60f 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; … … 16 15 private double slope, xIntercept; 17 16 private double direction; 18 private Planet destination , nearPlanet;17 private Planet destination; 19 18 private int numShips; 20 19 private int faction; 21 20 private boolean isNextToAPlanet; 22 private boolean isClockwise; 23 21 private boolean dirChanged, isClockwise; 24 22 25 23 /* Optimising: pre-calculate paths */ 26 24 public Fleet(Planet source, Planet destination, int numShips, int faction) { 27 source.setNumShips(source.getNumShips()-numShips);28 29 25 //Calculate initial coordinates and direction 30 26 if((destination.getX() - source.getX()) != 0){ … … 64 60 this.destination = destination; 65 61 this.isNextToAPlanet = false; 62 dirChanged = false; 66 63 } 67 64 … … 137 134 } 138 135 139 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 140 140 Path p = new Path(); 141 141 … … 145 145 p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2))); 146 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 147 171 148 canvas.drawPath(p, linePaint); 172 173 linePaint.setColor(prevC);174 149 } 175 150 176 151 public void update(ArrayList<Planet> planets) { 177 int speed = 3; //pixels per move152 int speed = 1; //pixels per move 178 153 double distance, tangentDirection, angle; 179 154 Planet temp = null; 180 155 //is the ship going around a planet already 181 156 if(!isNextToAPlanet){ … … 186 161 distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY()); 187 162 if(distance <= p.radius){ 188 nearPlanet= p;163 temp = p; 189 164 break; 190 165 } 191 166 } 192 167 //if temp planet is not picked move along the direction by #speed 193 if( nearPlanet == null) {168 if(temp == null || dirChanged) { 194 169 dblY += (Math.sin(direction)*speed); 195 170 dblX += (Math.cos(direction)*speed); … … 198 173 y = (int)dblY; 199 174 }else { 200 if(nearPlanet == destination){ 201 attack(); 202 return; 203 } 204 205 double radAngle = Math.atan(getSlope(nearPlanet.getX(), nearPlanet.getY(), dblX, dblY)); 175 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 206 176 //figure out which way to go clockwise or counter clockwise 207 tangentDirection = (Math.atan(getSlope( nearPlanet.getX(),nearPlanet.getY(),dblX,dblY))) + (Math.PI/2);177 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 208 178 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 209 210 179 if (angle <= Math.PI/2) 211 180 angle = Math.PI - angle; 212 181 213 if(dblX < nearPlanet.getX())182 if(dblX < temp.getX()) 214 183 radAngle += Math.PI; 215 184 … … 222 191 else 223 192 isClockwise = true; 224 225 193 if(Math.abs(diff)>Math.PI/2) 226 194 direction = angle-Math.PI; 227 195 else 228 196 direction = angle; 229 230 xIntercept = dblY - (dblX*Math.tan(direction)); 231 232 isNextToAPlanet = true; 197 198 dirChanged = true; 199 233 200 //figure out which way to go clockwise or counter clockwise 234 201 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); … … 243 210 //otherwise continue moving along the circumferenceds4 244 211 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; 253 254 if (getDistanceBetween(dblX,dblY,nearPlanet.getX(),nearPlanet.getY()) > nearPlanet.radius){ 255 isNextToAPlanet = false; 256 nearPlanet = null; 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); 257 225 } 258 } else {259 angle = speed/(double)nearPlanet.radius;260 Log.i("Gencon", angle + "%");261 if(isClockwise){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);265 } else {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);269 }270 x = (int)dblX;271 y = (int)dblY;272 xIntercept = dblY - (dblX*Math.tan(direction));273 226 } 274 227 } … … 278 231 //after the method is called the fleet needs to removed 279 232 public void attack() { 280 if(this.faction == destination.getFaction()) 281 destination.setNumShips(destination.getNumShips() + numShips); 282 else if(numShips <= destination.getNumShips()){ 233 if(numShips <= destination.getNumShips()){ 283 234 destination.setNumShips(destination.getNumShips() - numShips); 284 235 } else { … … 286 237 destination.setFaction(this.faction); 287 238 } 288 this.numShips = 0;289 239 } 290 240
Note:
See TracChangeset
for help on using the changeset viewer.