Changeset 61c4fbc in galcon-client for src/com/example/helloandroid/Fleet.java


Ignore:
Timestamp:
Jun 5, 2010, 5:08:40 PM (15 years ago)
Author:
dportnoy <devnull@…>
Branches:
master
Children:
0986844, 95509e1
Parents:
9ef6f68
Message:

The player can now send fleets by clicking on the source and destination planets.

File:
1 edited

Legend:

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

    r9ef6f68 r61c4fbc  
    22
    33import java.util.ArrayList;
     4
     5import android.graphics.Canvas;
     6import android.graphics.Paint;
     7import android.graphics.Path;
     8import android.util.Log;
    49
    510public class Fleet {
     
    1419        private int faction;
    1520        private boolean isNextToAPlanet;
     21        private boolean dirChanged;
    1622       
    1723        /* Optimising: pre-calculate paths */
     
    2127                //line formula
    2228                slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
     29               
    2330                xIntercept = destination.getY() - (slope*destination.getX());
    2431               
     
    5259                this.destination = destination;
    5360                this.isNextToAPlanet = false;
     61                dirChanged = false;
    5462        }
    5563       
     
    125133        }
    126134
    127 
     135        public void draw(Canvas canvas, Paint linePaint) {
     136                //Log.i("Gencon", "direction: "+direction);
     137                canvas.drawLine((float)x, (float)y, (float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)), linePaint);
     138               
     139                Path p = new Path();
     140               
     141                p.moveTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
     142                p.lineTo((float)(x+5*Math.cos(direction-Math.PI/2)), (float)(y+5*Math.sin(direction-Math.PI/2)));
     143                p.lineTo((float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)));
     144                p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
     145               
     146                canvas.drawPath(p, linePaint);
     147        }
    128148
    129149        public void update(ArrayList<Planet> planets) {
    130150                int speed = 1; //pixels per move
    131                 double distance, tangentDirection, angle;
     151                double distance, tangentDirection;
    132152                Planet temp = null;
    133153                //is the ship going around a planet already
     
    144164                        }
    145165                        //if temp planet is not picked move along the direction by #speed
    146                         if(temp == null){
     166                        if(temp == null || dirChanged) {
    147167                                dblY += (Math.sin(direction)*speed);
    148168                                dblX += (Math.cos(direction)*speed);
     
    150170                                x = (int)dblX;
    151171                                y = (int)dblY;
    152                         } else
    153                         //otherwise
    154                         {
     172                        }else {                         
     173                                double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY));
     174                               
     175                                if(dblX < temp.getX())
     176                                        radAngle += Math.PI;
     177                               
     178                                double angle = radAngle + Math.PI/2;
     179                               
     180                                double diff = direction-angle;
     181                               
     182                                if(Math.abs(diff)>Math.PI/2)
     183                                        direction = angle-Math.PI;
     184                                else
     185                                        direction = angle;
     186                               
     187                                dirChanged = true;
     188                               
    155189                                //figure out which way to go clockwise or counter clockwise
    156                                 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
     190                                /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);
    157191                                angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));
    158192                                if (angle <= Math.PI/2)
    159                                         angle = Math.PI - angle;
     193                                        angle = Math.PI - angle;*/
    160194                                //get next point and the direction and set it
    161195                        }
     
    182216       
    183217        //helper functions
    184         private double getDistanceBetween(double x1, double y1, double x2, double y2)
    185         {
     218        private double getDistanceBetween(double x1, double y1, double x2, double y2) {
    186219                return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
    187220        }
    188221       
    189         private double getSlope(double x1, double y1, double x2, double y2)
    190         {
    191                 return ((y1 - y1)/(x2 - x1));
     222        private double getSlope(double x1, double y1, double x2, double y2) {
     223                return ((y2 - y1)/(double)(x2 - x1));
    192224        }
    193225}
Note: See TracChangeset for help on using the changeset viewer.