Ignore:
File:
1 edited

Legend:

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

    r0986844 r95509e1  
    44
    55import android.graphics.Canvas;
     6import android.graphics.Color;
    67import android.graphics.Paint;
    78import android.graphics.Path;
     
    1920        private int faction;
    2021        private boolean isNextToAPlanet;
    21         private boolean dirChanged, isClockwise;
    22 
     22        private boolean dirChanged;
     23       
    2324        /* Optimising: pre-calculate paths */
    2425        public Fleet(Planet source, Planet destination, int numShips, int faction) {
     26                source.setNumShips(source.getNumShips()-numShips);
     27               
    2528                //Calculate initial coordinates and direction
    2629                if((destination.getX() - source.getX()) != 0){
    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 
     30                //line formula
     31                slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
     32               
     33                xIntercept = destination.getY() - (slope*destination.getX());
     34               
     35                //direction
     36                direction = Math.atan(slope);
     37               
     38                //coordinates for all 4 coordinates
     39                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               
    4245                } else {
    4346                        if((destination.getY() - source.getY()) > 0 ){
     
    5053                                dblY = source.getY() - source.radius - 10;
    5154                        }
    52                         xIntercept = destination.getX();
    53                 }
    54 
     55                }
     56               
    5557                x = (int)dblX;
    5658                y = (int)dblY;
    57 
     59               
    5860                this.numShips = numShips;
    5961                this.faction = faction;
     
    6264                dirChanged = false;
    6365        }
    64 
    65 
     66       
     67               
    6668        public int getX() {
    6769                return x;
     
    134136        }
    135137
    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                
     138        public void draw(Canvas canvas, Paint linePaint) {     
    140139                Path p = new Path();
    141140               
     
    145144                p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
    146145               
     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);
    147169               
    148170                canvas.drawPath(p, linePaint);
     171               
     172                linePaint.setColor(prevC);
    149173        }
    150174
    151175        public void update(ArrayList<Planet> planets) {
    152176                int speed = 1; //pixels per move
    153                 double distance, tangentDirection, angle;
     177                double distance, tangentDirection;
    154178                Planet temp = null;
    155179                //is the ship going around a planet already
     
    169193                                dblY += (Math.sin(direction)*speed);
    170194                                dblX += (Math.cos(direction)*speed);
    171 
     195                               
    172196                                x = (int)dblX;
    173197                                y = (int)dblY;
    174198                        }else {                         
    175199                                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;
    181200                               
    182201                                if(dblX < temp.getX())
    183202                                        radAngle += Math.PI;
    184203                               
    185                                 angle = radAngle + Math.PI/2;
     204                                double angle = radAngle + Math.PI/2;
    186205                               
    187206                                double diff = direction-angle;
    188207                               
    189                                 if(diff > 0)
    190                                         isClockwise = false;
    191                                 else
    192                                         isClockwise = true;
    193208                                if(Math.abs(diff)>Math.PI/2)
    194209                                        direction = angle-Math.PI;
    195210                                else
    196211                                        direction = angle;
    197                                        
     212                               
    198213                                dirChanged = true;
    199214                               
     
    209224                        //if so set isNextToAPlanet to false and move
    210225                        //otherwise continue moving along the circumferenceds4
    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                 }
    228         }
    229 
     226               
     227                }
     228               
     229               
     230        }
     231       
    230232        // attack the destination planet
    231233        //after the method is called the fleet needs to removed
     
    238240                }
    239241        }
    240 
     242       
    241243        //helper functions
    242244        private double getDistanceBetween(double x1, double y1, double x2, double y2) {
    243245                return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
    244246        }
    245 
     247       
    246248        private double getSlope(double x1, double y1, double x2, double y2) {
    247249                return ((y2 - y1)/(double)(x2 - x1));
Note: See TracChangeset for help on using the changeset viewer.