examples/moveongrid/GridTracker

From FANG

Jump to: navigation, search
001 package examples.moveongrid;
002 
003 import java.awt.geom.*;
004 import fang.TrackerAdapter;
005 
006 /**Moves a sprite smoothly over
007  * a set of cells in a given
008  * period of time. 
009  @author Jam Jenkins
010  */
011 public class GridTracker
012          extends TrackerAdapter
013 {
014    /**the number of vertical cells*/
015    private int rows;
016 
017    /**the number of horizontal cells*/
018    private int columns;
019 
020    /**the time remaining to move*/
021    private double timeLeft=0;
022 
023    /**the total time given to move*/
024    private double totalTime=1;
025 
026    /**the total distance required to move*/
027    private Point2D.Double totalDistance;
028 
029    /**how far to move at this frame*/
030    private Point2D.Double translate;
031 
032    /**makes a grid tracker to work on
033     * a square screen split up into the
034     * given number of rows and columns.
035     @param rows the number of horizontal
036     * divisions in the screen
037     @param columns the number of vertical
038     * divisions in the screen
039     */
040    public GridTracker(int rows, int columns)
041    {
042       this.rows=rows;
043       this.columns=columns;
044       totalDistance=new Point2D.Double();
045       translate=new Point2D.Double();
046    }
047 
048    /**sets the time given to move from
049     * one cell to another
050     @param totalTime the time in seconds
051     */
052    public void setMoveTime(double totalTime)
053    {
054       this.totalTime=totalTime;
055    }
056 
057    /**moves a given number of cells up/down
058     * and left/right.  The parameters represent
059     * the distance to move, not the target cell.
060     * If a move is currently underway, this
061     * method call returns immediately with no
062     * action taken and the call is ignored.
063     @param r the number of cells to move
064     * vertically.  Positive means move to the
065     * right and negative means move to the left.
066     @param c the number of cells to move
067     * horizontally.  Positive means move down
068     * and negative means move up.
069     */
070    public void move(int r, int c)
071    {
072       if(timeLeft>0)
073          return;
074       totalDistance.x=c/(double)columns;
075       totalDistance.y=r/(double)rows;
076       timeLeft=totalTime;
077    }
078 
079    /**gets the translation amount
080     @return the amount to move at this frame
081     */
082    public Point2D.Double getTranslation()
083    {
084       return translate;
085    }
086 
087    /**updates the amount to translate
088     * given the amount of time that has
089     * passed.
090     @param time the time since the last
091     * advanced time in seconds
092     */
093    public void advanceTime(double time)
094    {
095       if(timeLeft>0)
096       {
097          double timePassed=Math.min(timeLeft, time);
098          translate.x=totalDistance.x*timePassed/totalTime;
099          translate.y=totalDistance.y*timePassed/totalTime;
100          timeLeft-=timePassed;
101       }
102       else
103       {
104          translate.x=0;
105          translate.y=0;
106       }
107    }
108 }

Download/View examples/moveongrid/GridTracker.java




Views
Personal tools