Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/com/example/helloandroid/Fleet.java

    r6ebf60f r0986844  
    44
    55import android.graphics.Canvas;
    6 import android.graphics.Color;
    76import android.graphics.Paint;
    87import android.graphics.Path;
     
    1615        private double slope, xIntercept;
    1716        private double direction;
    18         private Planet destination, nearPlanet;
     17        private Planet destination;
    1918        private int numShips;
    2019        private int faction;
    2120        private boolean isNextToAPlanet;
    22         private boolean isClockwise;
    23        
     21        private boolean dirChanged, isClockwise;
    2422
    2523        /* Optimising: pre-calculate paths */
    2624        public Fleet(Planet source, Planet destination, int numShips, int faction) {
    27                 source.setNumShips(source.getNumShips()-numShips);
    28                
    2925                //Calculate initial coordinates and direction
    3026                if((destination.getX() - source.getX()) != 0){
     
    6460                this.destination = destination;
    6561                this.isNextToAPlanet = false;
     62                dirChanged = false;
    6663        }
    6764
     
    137134        }
    138135
    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               
    140140                Path p = new Path();
    141141               
     
    145145                p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
    146146               
    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);
    170147               
    171148                canvas.drawPath(p, linePaint);
    172                
    173                 linePaint.setColor(prevC);
    174149        }
    175150
    176151        public void update(ArrayList<Planet> planets) {
    177                 int speed = 3; //pixels per move
     152                int speed = 1; //pixels per move
    178153                double distance, tangentDirection, angle;
    179                
     154                Planet temp = null;
    180155                //is the ship going around a planet already
    181156                if(!isNextToAPlanet){
     
    186161                                distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
    187162                                if(distance <= p.radius){
    188                                         nearPlanet = p;
     163                                        temp = p;
    189164                                        break;
    190165                                }
    191166                        }
    192167                        //if temp planet is not picked move along the direction by #speed
    193                         if(nearPlanet == null) {
     168                        if(temp == null || dirChanged) {
    194169                                dblY += (Math.sin(direction)*speed);
    195170                                dblX += (Math.cos(direction)*speed);
     
    198173                                y = (int)dblY;
    199174                        }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));
    206176                                //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);
    208178                                angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
    209                                
    210179                                if (angle <= Math.PI/2)
    211180                                        angle = Math.PI - angle;
    212181                               
    213                                 if(dblX < nearPlanet.getX())
     182                                if(dblX < temp.getX())
    214183                                        radAngle += Math.PI;
    215184                               
     
    222191                                else
    223192                                        isClockwise = true;
    224                                
    225193                                if(Math.abs(diff)>Math.PI/2)
    226194                                        direction = angle-Math.PI;
    227195                                else
    228196                                        direction = angle;
    229                                
    230                                 xIntercept = dblY - (dblX*Math.tan(direction));
    231                                
    232                                 isNextToAPlanet = true;
     197                                       
     198                                dirChanged = true;
     199                               
    233200                                //figure out which way to go clockwise or counter clockwise
    234201                                /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
     
    243210                        //otherwise continue moving along the circumferenceds4
    244211                       
    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);
    257225                                }
    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));
    273226                        }
    274227                }
     
    278231        //after the method is called the fleet needs to removed
    279232        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()){
    283234                        destination.setNumShips(destination.getNumShips() - numShips);
    284235                } else {
     
    286237                        destination.setFaction(this.faction);
    287238                }
    288                 this.numShips = 0;
    289239        }
    290240
Note: See TracChangeset for help on using the changeset viewer.