User:Pedersen/Code Samples/RobocodeSG

From Robowiki
< User:Pedersen‎ | Code Samples
Revision as of 19:07, 11 January 2008 by Pedersen (talk | contribs) (migration)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Following are some examples of my code to support debugging with RobocodeSG.
First is an example of the code in use, followed by my wrapper for the swing graphics, and finally a structure for holding drawable line segments, mostly used for ad-hoc tests such as ensuring my movement projection looks right, or finding the difference in escape angles between an opponent going at a tangent or turning into the wave. -- Martin


This is from my WaveOutboundBank class, which maintains a list of waves that I've fired. The StaticPositionImpl constructor being used starts at the wave's origin and finds the point radius units away in the direction of the firing angle.

public void onPaint( Graphics2D console )
{
	long time = Host.singleton.getTurn();
	
	Iterator iterator = this.iterator();
	while( iterator.hasNext() )
	{
		CombatWave wave = ( CombatWave ) iterator.next();
		double radius1 = wave.getRadius( time - 1 );
		double radius2 = wave.getRadius( time     );
		double radius3 = wave.getRadius( time + 1 );
		console.setColor( Color.darkGray );
		GraphicalDebugger.singleton.drawRing( console, wave, radius1 );
		GraphicalDebugger.singleton.drawRing( console, wave, radius2 );
		GraphicalDebugger.singleton.drawRing( console, wave, radius3 );
		Iterator firingAngleIterator = wave.getFiringAngleIterator();
		while( firingAngleIterator.hasNext() )
		{
			FiringAngle firingAngle = ( FiringAngle ) firingAngleIterator.next();
			StaticPosition point1 = new StaticPositionImpl( wave, firingAngle.getFiringAngle(), radius1 );
			StaticPosition point2 = new StaticPositionImpl( wave, firingAngle.getFiringAngle(), radius2 );
			StaticPosition point3 = new StaticPositionImpl( wave, firingAngle.getFiringAngle(), radius3 );
			console.setColor( Color.red );
			GraphicalDebugger.singleton.drawLine( console, point1, point2 );
			console.setColor( Color.yellow );
			GraphicalDebugger.singleton.drawLine( console, point2, point3 );
		}
	}
}

package pedersen.debug;

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.List;

import pedersen.physics.StaticPosition;

public class GraphicalDebugger
{

	//:://////////////////////////////////////
	//::  Constructors
	//:://////////////////////////////////////

	private GraphicalDebugger()
	{
	}

	//:://////////////////////////////////////
	//::  Methods
	//:://////////////////////////////////////

	public void init( double newMaxY )
	{
		this.maxY = newMaxY;
	}
	
	public void drawVehicleHitBox( Graphics2D console, StaticPosition center )
	{
		console.draw( new Rectangle2D.Double( center.getX() - 18.0, this.maxY - center.getY() - 18.0, 36.0, 36.0 ) );
	}

	public void drawRing( Graphics2D console, StaticPosition center, double radius )
	{
		console.draw( new Ellipse2D.Double( center.getX() - radius, this.maxY - center.getY() - radius, radius + radius, radius + radius ) );
	}

	public void drawRings( Graphics2D console, List centers, double radius )
	{
		Iterator iterator = centers.iterator();
		while( iterator.hasNext() )
		{
			this.drawRing( console, (StaticPosition)iterator.next(), radius );
		}
	}
	
	public void drawLine( Graphics2D console, StaticPosition a, StaticPosition b )
	{
		console.draw( new Line2D.Double( a.getX(), this.maxY - a.getY(), b.getX(), this.maxY - b.getY() ) );
	}
	
	public void drawRenderableLineSegment( Graphics2D console, RenderableLineSegment line )
	{
		console.setColor( line.color );
		console.draw( new Line2D.Double( line.x1, this.maxY - line.y1, line.x2, this.maxY - line.y2 ) );
	}
	
	public void drawRenderableLineSegments( Graphics2D console, List lines )
	{
		Iterator iterator = lines.iterator();
		while( iterator.hasNext() )
		{
			this.drawRenderableLineSegment( console, (RenderableLineSegment)iterator.next() );
		}
	}
	

	//:://////////////////////////////////////
	//::  Instance Variables
	//:://////////////////////////////////////

	private double maxY = 0.0;
	
	//:://////////////////////////////////////
	//::  Static Constants
	//:://////////////////////////////////////

	public static final GraphicalDebugger singleton = new GraphicalDebugger( );

}

package pedersen.debug;

import java.awt.Color;

import pedersen.physics.StaticPosition;

public class RenderableLineSegment
{
	
	public RenderableLineSegment( StaticPosition start, StaticPosition end, Color newColor )
	{
		this.x1 = start.getX();
		this.y1 = start.getY();
		this.x2 = end.getX();
		this.y2 = end.getY();
		this.color = newColor;
	}
	
	public final double x1;
	public final double y1;
	public final double x2;
	public final double y2;
	public final Color color;

}

Thanks. It think i will have a busy evening --Loki