Changeset 61c4fbc in galcon-client for src


Ignore:
Timestamp:
Jun 5, 2010, 5:08:40 PM (14 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.

Location:
src/com/example/helloandroid
Files:
3 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}
  • src/com/example/helloandroid/GameView.java

    r9ef6f68 r61c4fbc  
    171171        private double mY;
    172172       
    173         public Object planetsLock;
     173        public Object planetsLock, fleetsLock;
    174174       
    175175        public ArrayList<Planet> planets;
     176        public ArrayList<Fleet> fleets;
    176177        public Planet planetSelected;
    177178
     
    221222       
    222223            planetsLock = new Object();
     224            fleetsLock = new Object();
    223225           
    224226            planets = new ArrayList<Planet>();
    225227            planetSelected = null;
     228           
     229            fleets = new ArrayList<Fleet>();
    226230        }
    227231
     
    589593                }
    590594               
     595                synchronized(fleetsLock) {
     596                        for(Fleet f : fleets) {
     597                        f.draw(canvas, mLinePaint);
     598                }
     599                }
     600               
    591601            int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2);
    592602            int xLeft = (int) mX - mLanderWidth / 2;
     
    665675            }
    666676           
     677            synchronized(fleetsLock) {
     678                for(Fleet f : fleets) {
     679                        f.update(planets);
     680                }
     681            }
     682           
    667683            double dxOld = mDX;
    668684            double dyOld = mDY;
     
    734750        Log.i("Gencon", "Detected touch event");
    735751       
     752        if(event.getAction() != MotionEvent.ACTION_DOWN)
     753                return true;
     754       
    736755        synchronized(thread.planetsLock) {
    737756                if(thread.planetSelected != null) {
     757                        Planet target = null;
     758                       
     759                        for(Planet p : thread.planets) {
     760                                if(p.contains((int)event.getX(), (int)event.getY())) {
     761                                        target = p;
     762                                        break;
     763                                }
     764                        }
     765                       
     766                        if(target != null) {
     767                                synchronized(thread.fleetsLock) {
     768                                Fleet f = new Fleet(thread.planetSelected, target, 1, 1);
     769                                thread.fleets.add(f);
     770                    }
     771                        }
     772                               
    738773                        thread.planetSelected.unselect();
    739774                        thread.planetSelected = null;
     775                }else {
     776                        for(Planet p : thread.planets) {
     777                                if(p.contains((int)event.getX(), (int)event.getY())) {
     778                                        p.select();
     779                                        thread.planetSelected = p;
     780                                        break;
     781                                }
     782                        }
    740783                }
    741                
    742                 for(Planet p : thread.planets) {
    743                         if(p.contains((int)event.getX(), (int)event.getY())) {
    744                                 p.select();
    745                                 thread.planetSelected = p;
    746                                 break;
    747                         }
    748                 }
    749784        }
    750785       
  • src/com/example/helloandroid/Planet.java

    r9ef6f68 r61c4fbc  
    101101                        break;
    102102                case 2:
    103                         c = Color.argb(255, 0, 255, 0);
     103                        c = Color.argb(255, 0, 180, 0);
    104104                        break;
    105105                case 3:
     
    107107                        break;
    108108                case 4:
    109                         c = Color.argb(255, 255, 255, 0);
     109                        c = Color.argb(255, 150, 150, 0);
    110110                        break;
    111111                default:
Note: See TracChangeset for help on using the changeset viewer.