Changes in / [b15ff93:1bc5794] in galcon-client
- Location:
- src/com/example/helloandroid
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
rb15ff93 r1bc5794 16 16 private double slope, xIntercept; 17 17 private double direction; 18 private Planet destination , nearPlanet;18 private Planet destination; 19 19 private int numShips; 20 20 private int faction; 21 21 private boolean isNextToAPlanet; 22 private boolean isClockwise; 23 22 private boolean dirChanged, isClockwise; 24 23 25 24 /* Optimising: pre-calculate paths */ … … 64 63 this.destination = destination; 65 64 this.isNextToAPlanet = false; 65 dirChanged = false; 66 66 } 67 67 … … 175 175 176 176 public void update(ArrayList<Planet> planets) { 177 int speed = 3; //pixels per move177 int speed = 1; //pixels per move 178 178 double distance, tangentDirection, angle; 179 179 Planet temp = null; 180 180 //is the ship going around a planet already 181 181 if(!isNextToAPlanet){ … … 186 186 distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY()); 187 187 if(distance <= p.radius){ 188 nearPlanet= p;188 temp = p; 189 189 break; 190 190 } 191 191 } 192 192 //if temp planet is not picked move along the direction by #speed 193 if( nearPlanet == null) {193 if(temp == null || dirChanged) { 194 194 dblY += (Math.sin(direction)*speed); 195 195 dblX += (Math.cos(direction)*speed); … … 198 198 y = (int)dblY; 199 199 }else { 200 if(nearPlanet == destination){ 201 attack(); 202 return; 203 } 204 205 double radAngle = Math.atan(getSlope(nearPlanet.getX(), nearPlanet.getY(), dblX, dblY)); 200 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 206 201 //figure out which way to go clockwise or counter clockwise 207 tangentDirection = (Math.atan(getSlope( nearPlanet.getX(),nearPlanet.getY(),dblX,dblY))) + (Math.PI/2);202 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 208 203 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 209 210 204 if (angle <= Math.PI/2) 211 205 angle = Math.PI - angle; 212 206 213 if(dblX < nearPlanet.getX())207 if(dblX < temp.getX()) 214 208 radAngle += Math.PI; 215 209 … … 222 216 else 223 217 isClockwise = true; 224 225 218 if(Math.abs(diff)>Math.PI/2) 226 219 direction = angle-Math.PI; 227 220 else 228 221 direction = angle; 229 230 xIntercept = dblY - (dblX*Math.tan(direction)); 231 232 isNextToAPlanet = true; 222 223 dirChanged = true; 224 233 225 //figure out which way to go clockwise or counter clockwise 234 226 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); … … 243 235 //otherwise continue moving along the circumferenceds4 244 236 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; 237 238 if(true){ 239 240 } else { 241 angle = speed/temp.radius; 242 if(isClockwise){ 243 dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX())); 244 dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY())); 245 direction = direction - (Math.PI/2); 246 } else { 247 dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX())); 248 dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY())); 249 direction = direction + (Math.PI/2); 257 250 } 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 251 } 274 252 } … … 278 256 //after the method is called the fleet needs to removed 279 257 public void attack() { 280 if(this.faction == destination.getFaction()) 281 destination.setNumShips(destination.getNumShips() + numShips); 282 else if(numShips <= destination.getNumShips()){ 258 if(numShips <= destination.getNumShips()){ 283 259 destination.setNumShips(destination.getNumShips() - numShips); 284 260 } else { … … 286 262 destination.setFaction(this.faction); 287 263 } 288 this.numShips = 0;289 264 } 290 265 -
src/com/example/helloandroid/GameView.java
rb15ff93 r1bc5794 2 2 3 3 import java.util.ArrayList; 4 import java.util.Iterator;5 4 import java.util.Random; 6 5 … … 367 366 368 367 synchronized(fleetsLock) { 369 Iterator<Fleet> i = fleets.iterator(); 370 Fleet f = null; 371 while(i.hasNext()){ 372 f = i.next(); 373 if(f.getNumShips() == 0) 374 i.remove(); 375 else 376 f.update(planets); 368 for(Fleet f : fleets) { 369 f.update(planets); 377 370 } 378 371 }
Note:
See TracChangeset
for help on using the changeset viewer.