Changes in / [b15ff93:1bc5794] in galcon-client


Ignore:
Location:
src/com/example/helloandroid
Files:
2 edited

Legend:

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

    rb15ff93 r1bc5794  
    1616        private double slope, xIntercept;
    1717        private double direction;
    18         private Planet destination, nearPlanet;
     18        private Planet destination;
    1919        private int numShips;
    2020        private int faction;
    2121        private boolean isNextToAPlanet;
    22         private boolean isClockwise;
    23        
     22        private boolean dirChanged, isClockwise;
    2423
    2524        /* Optimising: pre-calculate paths */
     
    6463                this.destination = destination;
    6564                this.isNextToAPlanet = false;
     65                dirChanged = false;
    6666        }
    6767
     
    175175
    176176        public void update(ArrayList<Planet> planets) {
    177                 int speed = 3; //pixels per move
     177                int speed = 1; //pixels per move
    178178                double distance, tangentDirection, angle;
    179                
     179                Planet temp = null;
    180180                //is the ship going around a planet already
    181181                if(!isNextToAPlanet){
     
    186186                                distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY());
    187187                                if(distance <= p.radius){
    188                                         nearPlanet = p;
     188                                        temp = p;
    189189                                        break;
    190190                                }
    191191                        }
    192192                        //if temp planet is not picked move along the direction by #speed
    193                         if(nearPlanet == null) {
     193                        if(temp == null || dirChanged) {
    194194                                dblY += (Math.sin(direction)*speed);
    195195                                dblX += (Math.cos(direction)*speed);
     
    198198                                y = (int)dblY;
    199199                        }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));
    206201                                //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);
    208203                                angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
    209                                
    210204                                if (angle <= Math.PI/2)
    211205                                        angle = Math.PI - angle;
    212206                               
    213                                 if(dblX < nearPlanet.getX())
     207                                if(dblX < temp.getX())
    214208                                        radAngle += Math.PI;
    215209                               
     
    222216                                else
    223217                                        isClockwise = true;
    224                                
    225218                                if(Math.abs(diff)>Math.PI/2)
    226219                                        direction = angle-Math.PI;
    227220                                else
    228221                                        direction = angle;
    229                                
    230                                 xIntercept = dblY - (dblX*Math.tan(direction));
    231                                
    232                                 isNextToAPlanet = true;
     222                                       
     223                                dirChanged = true;
     224                               
    233225                                //figure out which way to go clockwise or counter clockwise
    234226                                /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
     
    243235                        //otherwise continue moving along the circumferenceds4
    244236                       
    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);
    257250                                }
    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));
    273251                        }
    274252                }
     
    278256        //after the method is called the fleet needs to removed
    279257        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()){
    283259                        destination.setNumShips(destination.getNumShips() - numShips);
    284260                } else {
     
    286262                        destination.setFaction(this.faction);
    287263                }
    288                 this.numShips = 0;
    289264        }
    290265
  • src/com/example/helloandroid/GameView.java

    rb15ff93 r1bc5794  
    22
    33import java.util.ArrayList;
    4 import java.util.Iterator;
    54import java.util.Random;
    65
     
    367366           
    368367            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);
    377370                }
    378371            }
Note: See TracChangeset for help on using the changeset viewer.