Ignore:
File:
1 edited

Legend:

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

    r0986844 r6ebf60f  
    44
    55import android.graphics.Canvas;
     6import android.graphics.Color;
    67import android.graphics.Paint;
    78import android.graphics.Path;
     
    1516        private double slope, xIntercept;
    1617        private double direction;
    17         private Planet destination;
     18        private Planet destination, nearPlanet;
    1819        private int numShips;
    1920        private int faction;
    2021        private boolean isNextToAPlanet;
    21         private boolean dirChanged, isClockwise;
     22        private boolean isClockwise;
     23       
    2224
    2325        /* Optimising: pre-calculate paths */
    2426        public Fleet(Planet source, Planet destination, int numShips, int faction) {
     27                source.setNumShips(source.getNumShips()-numShips);
     28               
    2529                //Calculate initial coordinates and direction
    2630                if((destination.getX() - source.getX()) != 0){
     
    6064                this.destination = destination;
    6165                this.isNextToAPlanet = false;
    62                 dirChanged = false;
    6366        }
    6467
     
    134137        }
    135138
    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) {     
    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);
    147170               
    148171                canvas.drawPath(p, linePaint);
     172               
     173                linePaint.setColor(prevC);
    149174        }
    150175
    151176        public void update(ArrayList<Planet> planets) {
    152                 int speed = 1; //pixels per move
     177                int speed = 3; //pixels per move
    153178                double distance, tangentDirection, angle;
    154                 Planet temp = null;
     179               
    155180                //is the ship going around a planet already
    156181                if(!isNextToAPlanet){
     
    161186                                distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
    162187                                if(distance <= p.radius){
    163                                         temp = p;
     188                                        nearPlanet = p;
    164189                                        break;
    165190                                }
    166191                        }
    167192                        //if temp planet is not picked move along the direction by #speed
    168                         if(temp == null || dirChanged) {
     193                        if(nearPlanet == null) {
    169194                                dblY += (Math.sin(direction)*speed);
    170195                                dblX += (Math.cos(direction)*speed);
     
    173198                                y = (int)dblY;
    174199                        }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));
    176206                                //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);
    178208                                angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
     209                               
    179210                                if (angle <= Math.PI/2)
    180211                                        angle = Math.PI - angle;
    181212                               
    182                                 if(dblX < temp.getX())
     213                                if(dblX < nearPlanet.getX())
    183214                                        radAngle += Math.PI;
    184215                               
     
    191222                                else
    192223                                        isClockwise = true;
     224                               
    193225                                if(Math.abs(diff)>Math.PI/2)
    194226                                        direction = angle-Math.PI;
    195227                                else
    196228                                        direction = angle;
    197                                        
    198                                 dirChanged = true;
    199                                
     229                               
     230                                xIntercept = dblY - (dblX*Math.tan(direction));
     231                               
     232                                isNextToAPlanet = true;
    200233                                //figure out which way to go clockwise or counter clockwise
    201234                                /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
     
    210243                        //otherwise continue moving along the circumferenceds4
    211244                       
    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                                }
    215258                        } else {
    216                                 angle = speed/temp.radius;
     259                                angle = speed/(double)nearPlanet.radius;
     260                                Log.i("Gencon", angle + "%");
    217261                                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);
    221265                                } 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);
    225269                                }
     270                                x = (int)dblX;
     271                                y = (int)dblY;
     272                                xIntercept = dblY - (dblX*Math.tan(direction));
    226273                        }
    227274                }
     
    231278        //after the method is called the fleet needs to removed
    232279        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()){
    234283                        destination.setNumShips(destination.getNumShips() - numShips);
    235284                } else {
     
    237286                        destination.setFaction(this.faction);
    238287                }
     288                this.numShips = 0;
    239289        }
    240290
Note: See TracChangeset for help on using the changeset viewer.