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