/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package marioRhythm;
import composition.java.Composition;
import composition.java.Compositor;
import composition.java.CompositorQueue;
import composition.java.MovingTranslator;
import factories.Points;
import factories.Sizes;
import factories.Vectors;
import graphics.common.Box;
import graphics.common.GraphicsObject;
import graphics.common.Point;
import graphics.common.Size;
import graphics.common.Vector;
import graphics.java.JavaSprite;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author Freezerburn
*/
public class RhythmBallNew implements GraphicsObject {
protected List< Point > mLocs;
protected long mMoveTime;
protected JavaSprite mSprite;
protected boolean mStarted;
protected Composition mComposed;
protected static final int SPRITE_SIZE = 30;
protected static final int OFFSCREEN = -999;
protected static final int ROUND_ACC = 1000;
public RhythmBallNew( String spriteName, long moveTime, Point... locs ) {
mSprite = new JavaSprite( spriteName,
Points.get( OFFSCREEN, OFFSCREEN ),
Sizes.get( SPRITE_SIZE, SPRITE_SIZE ) );
mLocs = new ArrayList< Point >();
mLocs.addAll( Arrays.asList( locs ) );
mMoveTime = moveTime;
mStarted = false;
setupComposition();
}
private void setupComposition() {
Compositor[] set = new Compositor[ mLocs.size() - 1 ];
Vector[] distVecs = new Vector[ mLocs.size() - 1 ];
double[] dists = new double[ mLocs.size() - 1 ];
double totalDist = 0;
double percentOfTotal;
long compositorTime;
for( int i = 0; i < distVecs.length; i++ ) {
distVecs[ i ] = Vectors.get( mLocs.get( i ), mLocs.get( i + 1 ) );
System.out.println( i );
System.out.println( distVecs[ i ].toString() );
dists[ i ] = distVecs[ i ].getLen();
totalDist += dists[ i ];
}
for( int i = 0; i < distVecs.length; i++ ) {
percentOfTotal = dists[ i ] / totalDist;
percentOfTotal = Math.floor( percentOfTotal * ROUND_ACC ) / ROUND_ACC;
compositorTime = (long)( mMoveTime * percentOfTotal );
set[ i ] = new MovingTranslator( mLocs.get( i ), mLocs.get( i + 1 ), compositorTime );
}
mComposed = new Composition( mSprite, new CompositorQueue( set ) );
}
public void start() {
mSprite.setLoc( mLocs.get( 0 ) );
mStarted = true;
}
@Override
public void draw( Graphics2D g, double delta ) {
if( mStarted ) {
mComposed.run( g, delta );
}
}
@Override
public String getName() {
return mSprite.getName();
}
@Override
public Point[] getPoints() {
return mSprite.getPoints();
}
@Override
public Size[] getSize() {
return mSprite.getSize();
}
@Override
public Box[] getBox() {
return mSprite.getBox();
}
@Override
public void dispose() {
throw new UnsupportedOperationException( "Not supported." );
}
}