/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testinggrounds;
import factories.Boxes;
import factories.Dimensions;
import factories.Points;
import factories.Sizes;
import graphics.common.Box;
import graphics.common.GraphicsDB;
import graphics.common.GraphicsObject;
import graphics.common.Point;
import graphics.common.Size;
import graphics.java.JavaSprite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import utils.GlobalData;
import utils.InputManager;
import utils.JavaWindow;
import utils.MockMouse;
import utils.Mouse;
import utils.SpacialQuadTree;
import utils.ThreadManager;
/**
*
* @author Freezerburn
*/
public class QuadTreeTest {
private static Points pf;
private static Sizes sf;
private static GraphicsDB gdb;
private static JavaWindow theWindow;
private static InputManager input;
private static Mouse mouseInput;
private static List< GraphicsObject > allGraphics;
private static final int WINDOW_WIDTH = 400;
private static final int WINDOW_HEIGHT = 400;
private static SpacialQuadTree< GraphicsObject > tree;
private static double[] moveValX, moveValY;
public static void main( String []args ) {
init();
final int NUM_ITEMS = 50;
final double RAND_RANGE = 2.0;
moveValX = new double[ NUM_ITEMS ];
moveValY = new double[ NUM_ITEMS ];
Random rand = new Random( System.currentTimeMillis() );
//tree.buildTreeToDepth( 8 );
for( int i = 0; i < NUM_ITEMS; i++ ) {
Point tempPoint = Points.get( rand.nextInt( theWindow.getWidth() - 30 ) + 30, rand.nextInt( theWindow.getHeight() - 30 ) + 30 );
Size tempSize = Sizes.get( rand.nextInt( 30 ) + 21, rand.nextInt( 30 ) + 21 );
JavaSprite temp = new JavaSprite( "fenceright", tempPoint, tempSize );
allGraphics.add( temp );
tree.insert( temp );
moveValX[ i ] = rand.nextDouble() * RAND_RANGE * ( rand.nextInt( 50 ) > 24 ? -1 : 1 );
moveValY[ i ] = rand.nextDouble() * RAND_RANGE * ( rand.nextInt( 50 ) > 24 ? -1 : 1 );
}
long prevTime = System.nanoTime() / 1000000,
curTime = System.nanoTime() / 1000000;
long delta;
long gameLoop = 0;
while( true ) {
//System.out.println( "Main loop has run " + gameLoop + " times." );
++gameLoop;
input.update();
mouseInput.poll();
if( input.isKeyDown( KeyEvent.VK_Q ) ) {
break;
}
if( mouseInput.buttonDown( 0 ) ) {
//System.out.println( "Button event 1 detected." );
Point p = Points.get( mouseInput.cx, mouseInput.cy );
for( int i = 0; i < allGraphics.size(); i++ ) {
if( allGraphics.get( i ).getBox()[ 0 ].contains( p ) ) {
System.out.println( "Removing graphics object: " + i );
tree.remove( allGraphics.get( i ) );
allGraphics.remove( allGraphics.get( i ) );
break;
}
}
}
delta = curTime - prevTime;
prevTime = curTime;
curTime = System.nanoTime() / 1000000;
theWindow.clear();
for( int i = 0; i < allGraphics.size(); i++ ) {
GraphicsObject go = allGraphics.get( i );
Point goP = go.getPoints()[ 0 ];
goP.modify( moveValX[ i ], moveValY[ i ], 0.0 );
if( goP.getRealX() < 0.0 ) {
goP.setX( 5.0 );
moveValX[ i ] = -moveValX[ i ];
}
if( goP.getRealX() > theWindow.getWidth() ) {
goP.setX( theWindow.getWidth() - 5.0 );
moveValX[ i ] = -moveValX[ i ];
}
if( goP.getRealY() < 0.0 ) {
goP.setY( 5.0 );
moveValY[ i ] = -moveValY[ i ];
}
if( goP.getRealY() > theWindow.getHeight() ) {
goP.setY( theWindow.getHeight() - 5.0 );
moveValY[ i ] = -moveValY[ i ];
}
tree.update( go );
/*List< GraphicsObject > list = tree.findColliding( go );
for( GraphicsObject graphics : list ) {
JavaSprite js = (JavaSprite)graphics;
Graphics2D g = js.getGraphics();
g.setColor( new Color( 0, 100, 0, 200 ) );
g.drawRect( 0, 0, js.getSize()[ 0 ].getWidth(), js.getSize()[ 0 ].getHeight() );
g.dispose();
}*/
go.draw( JavaWindow.getGraphics2D(), delta );
}
tree.drawTree( JavaWindow.getGraphics2D() );
theWindow.update();
try {
Thread.sleep( 10 );
}
catch ( InterruptedException e ) {
e.printStackTrace();
}
}
theWindow.shutdown();
ThreadManager.shutdown();
}
private static void init() {
GlobalData.GRAPHICS_DIMENSIONS = Dimensions.TWO_DIMENSION;
theWindow = new JavaWindow( "Tree test", WINDOW_WIDTH, WINDOW_HEIGHT );
theWindow.addKeyListener( ( input = InputManager.getInstance() ) );
//mouseInput = new Mouse();
theWindow.addMouseListener( ( mouseInput = new Mouse() ) );
theWindow.addMouseMotionListener( mouseInput );
//theWindow.addMouseListener( new MockMouse() );
allGraphics = new LinkedList< GraphicsObject >();
pf = new Points();
sf = new Sizes();
gdb = new GraphicsDB();
new Boxes();
GraphicsDB.cacheJavaImage( "images/Fence_endright.png", "fenceright" );
tree = new SpacialQuadTree< GraphicsObject >( 4, 6, 3, WINDOW_WIDTH, WINDOW_HEIGHT );
}
}