Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PartsBot extends AdvancedRobot
- {
- Random rand = new Random();
- private AdvancedEnemyBot enemy = new AdvancedEnemyBot();
- private RobotPart[] parts = new RobotPart[3]; // make three parts
- private final static int RADAR = 0;
- private final static int GUN = 1;
- private final static int TANK = 2;
- /**
- * This runs all the methods in the code.
- */
- public void run()
- {
- parts[TANK] = new Tank();
- parts[GUN] = new Gun();
- parts[RADAR] = new Radar();
- // initialize each part
- for ( int i = 0; i < parts.length; i++ )
- {
- // behold, the magic of polymorphism
- parts[i].init();
- }
- // iterate through each part, moving them as we go
- for ( int i = 0; true; i = ( i + 1 ) % parts.length )
- {
- // polymorphism galore!
- parts[i].move();
- if ( i == 0 )
- {
- execute();
- }
- }
- }
- /**
- * This is the event depending on if it gets hit by a bullet.
- *
- * @param e
- * This is the situation that it gets hit by a bullet.
- */
- public void onHitByBullet( HitByBulletEvent e )
- {
- }
- /**
- * This is the event depending on if it scans another robot.
- *
- * @param e
- * This is the situation that it scanned a robot.
- */
- public void onScannedRobot( ScannedRobotEvent e )
- {
- Radar radar = (Radar)parts[RADAR];
- if ( radar.shouldTrack( e ) )
- {
- // This looks for a new target
- enemy.update( e, this );
- }
- }
- /**
- * This is the event depending on if it scans a dead robot.
- *
- * @param e
- * This is the situation that it scanned a dead robot.
- */
- public void onRobotDeath( RobotDeathEvent e )
- {
- Radar radar = (Radar)parts[RADAR];
- if ( radar.wasTracking( e ) )
- {
- // This prints out Victory upon killing an enemy :)
- this.out.println( "VICTORY!!" );
- enemy.reset();
- }
- }
- /**
- * This is the event depending on if it hits the wall.
- *
- * @param e
- * This is the situation that it hits the wall.
- */
- public void onHitWall( HitWallEvent e )
- {
- // This is if the robot hits a wall
- Tank tank = (Tank)parts[TANK];
- tank.hitWalls();
- }
- // ... put normalizeBearing and absoluteBearing methods here
- /**
- * computes the absolute bearing between two points
- *
- * @param x1
- * This is the original x.
- * @param y1
- * This is the original y.
- * @param x2
- * This is the next x.
- * @param y2
- * This is the next y
- * @return bearing This is the double that describes the bearing to enemy.
- */
- public double absoluteBearing( double x1, double y1, double x2, double y2 )
- {
- double xo = x2 - x1;
- double yo = y2 - y1;
- double hyp = Point2D.distance( x1, y1, x2, y2 );
- double arcSin = Math.toDegrees( Math.asin( xo / hyp ) );
- double bearing = 0;
- if ( xo > 0 && yo > 0 )
- { // both pos: lower-Left
- bearing = arcSin;
- }
- else if ( xo < 0 && yo > 0 )
- { // x neg, y pos: lower-right
- bearing = 360 + arcSin; // arcsin is negative here, actually 360
- // -
- // ang
- }
- else if ( xo > 0 && yo < 0 )
- { // x pos, y neg: upper-left
- bearing = 180 - arcSin;
- }
- else if ( xo < 0 && yo < 0 )
- { // both neg: upper-right
- bearing = 180 - arcSin; // arcsin is negative here, actually 180
- // +
- // ang
- }
- return bearing;
- }
- /**
- * normalizes a bearing to between +180 and -180
- *
- * @param angle
- * This is the angle to turn the radar.
- * @return angle This is the smallest angle change.
- */
- public double normalizeBearing( double angle )
- {
- while ( angle > 180 )
- {
- angle -= 360;
- }
- while ( angle < -180 )
- {
- angle += 360;
- }
- return angle;
- }
- /**
- * ... declare the RobotPart interface and classes that implement it here
- * They will be _inner_ classes.
- */
- public interface RobotPart
- {
- /**
- * This initializes the bot.
- */
- public void init();
- /**
- * This moves the bot.
- */
- public void move();
- }
- /**
- * This class controls the radar.
- */
- public class Radar implements RobotPart
- {
- /**
- * This controls the radar.
- */
- public void init()
- {
- // initialize radar operation
- setAdjustRadarForGunTurn( true );
- }
- /**
- * This moves the radar and turns it.
- */
- public void move()
- {
- // Absolute angle towards target
- double angleToEnemy = getHeading() + enemy.getBearing();
- // Subtract current radar heading to get the turn required to
- // face
- // the enemy, be sure it is normalized
- double radarTurn = normalizeBearing(angleToEnemy - getRadarHeading() );
- // Distance we want to scan from middle of enemy to either side
- // The 36.0 is how many units from the center of the enemy
- // robot it scans.
- if (radarTurn > 0) {
- radarTurn += 10.0;
- }
- else {
- radarTurn -= 10.0;
- }
- // Adjust the radar turn so it goes that much further in the
- // direction it is going to turn
- // Basically if we were going to turn it left, turn it even
- // more left, if right, turn more right.
- // This allows us to overshoot our enemy so that we get a good
- // sweep
- // that will not slip.
- // Turn the radar
- setTurnRadarRightRadians( radarTurn );
- }
- /**
- * This is the boolean statement
- *
- * @param e
- * This is the event of scanning a robot.
- *
- * @return ( enemy.none() || e.getDistance() < enemy.getDistance() - 70
- * || e.getName().equals( enemy.getName() ) ) This determines
- * whether the enemy should be tracked.
- */
- public boolean shouldTrack( ScannedRobotEvent e )
- {
- // track if we have no enemy, the one we found is significantly
- // closer, or we scanned the one we've been tracking.
- return ( enemy.none() || e.getDistance() < enemy.getDistance()
- - 100
- || e.getName().equals( enemy.getName() ) );
- }
- /**
- * This controls the radar.
- *
- * @param e
- * This is the event of scanning a dead robot.
- *
- * @return e.getName().equals( enemy.getName() ) If enemy is dead, the
- * robot finds another target.
- */
- public boolean wasTracking( RobotDeathEvent e )
- {
- // if enemy is dead, the robot finds another target.
- return e.getName().equals( enemy.getName() );
- }
- }
- /**
- * This is the class that controls the gun.
- */
- public class Gun implements RobotPart
- {
- /**
- * This is the initialization method.
- */
- public void init()
- {
- // initialize gun operation
- setAdjustGunForRobotTurn( true );
- }
- public void onScannedRobot( ScannedRobotEvent e )
- {
- // track if we have no enemy, the one we found is significantly
- // closer, or we scanned the one we've been tracking.
- if ( enemy.none() || e.getDistance() < enemy.getDistance() - 70
- || e.getName().equals( enemy.getName() ) )
- {
- // track him using the NEW update method
- enemy.update( e );
- }
- }
- public void move()
- {
- // TODO: gun implementation
- if ( enemy.none() )
- return;
- // calculate firepower based on distance
- double firePower = Math.min( 500 / enemy.getDistance(), 3 );
- // calculate speed of bullet
- double bulletSpeed = 20 - firePower * 3;
- // distance = rate * time, solved for time
- long time = (long)( enemy.getDistance() / bulletSpeed );
- // calculate gun turn to predicted x,y location
- double futureX = enemy.getFutureX( time );
- double futureY = enemy.getFutureY( time );
- double absDeg = absoluteBearing( getX(), getY(), futureX, futureY );
- // non-predictive firing can be done like this:
- // double absDeg = absoluteBearing(getX(), getY(), enemy.getX(),
- // enemy.getY());
- // turn the gun to the predicted x,y location
- setTurnGunRight( normalizeBearing( absDeg - getGunHeading() ) );
- // if the gun is cool and we're pointed in the right direction, shoot!
- if ( getGunHeat() == 0 && Math.abs( getGunTurnRemaining() ) < 10 )
- {
- setFire( firePower );
- }
- }
- }
- /**
- * This moves the tanks and turns it.
- */
- public class Tank implements RobotPart
- {
- /**
- * This is the initialization for the tank.
- */
- public void init()
- {
- // this is the initial color.
- setColors( Color.white, Color.white, Color.red, Color.white,
- Color.white );
- }
- // this is used when strafing
- private byte moveDirection = 1;
- public void move()
- {
- setTurnRight( normalizeBearing( enemy.getBearing() + 90 ) );
- setAhead(100 * moveDirection);
- }
- public void hitWalls( ) {
- setMaxVelocity( 10 );
- moveDirection *= -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement