File: AlienComposition.java

package info (click to toggle)
robocode 1.6.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,896 kB
  • ctags: 6,795
  • sloc: java: 35,724; xml: 408; sh: 90; makefile: 65
file content (99 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2001, 2008 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://robocode.sourceforge.net/license/cpl-v10.html
 *
 * Contributors:
 *     Pavel Savara
 *     - Initial implementation
 *******************************************************************************/
package sampleex;


import robocode.*;
import robocode.robotinterfaces.IBasicEvents;
import robocode.robotinterfaces.IBasicRobot;
import robocode.robotinterfaces.peer.IBasicRobotPeer;
import robocode.robotinterfaces.peer.IStandardRobotPeer;

import java.io.PrintStream;


/**
 * A sample robot.
 * Is not inherited from classic base robots, uses new experimental access to RobotPeer.
 * Use -DEXPERIMENTAL=true to start robocode for this robot.
 * This composition version is showing possible decomposition of robot, main runnable and event handler to different classes.
 *
 * @author Pavel Savara (original)
 */
public class AlienComposition implements IBasicRobot {
	PrintStream out;
	IStandardRobotPeer peer;
	final AlienMain main;
	final AlienEventHandler handler;

	public AlienComposition() {
		main = new AlienMain();
		handler = new AlienEventHandler();
	}

	public void setPeer(IBasicRobotPeer iRobotPeer) {
		peer = (IStandardRobotPeer) iRobotPeer;
	}

	public void setOut(PrintStream printStream) {
		out = printStream;
	}

	public Runnable getRobotRunnable() {
		return main;
	}

	public IBasicEvents getBasicEventListener() {
		return handler;
	}

	class AlienMain implements Runnable {
		public void run() {
			while (true) {
				peer.move(100); // Move ahead 100
				peer.turnGun(Math.PI * 2); // Spin gun around
				peer.move(-100); // Move back 100
				peer.turnGun(Math.PI * 2); // Spin gun around
			}
		}
	}


	class AlienEventHandler implements IBasicEvents {
		public void onScannedRobot(ScannedRobotEvent e) {
			peer.setFire(1);
		}

		public void onHitByBullet(HitByBulletEvent e) {
			peer.turnBody(Math.PI / 2 + e.getBearingRadians());
		}

		public void onStatus(StatusEvent e) {}

		public void onBulletHit(BulletHitEvent e) {}

		public void onBulletHitBullet(BulletHitBulletEvent e) {}

		public void onBulletMissed(BulletMissedEvent e) {}

		public void onDeath(DeathEvent e) {}

		public void onHitRobot(HitRobotEvent e) {}

		public void onHitWall(HitWallEvent e) {}

		public void onRobotDeath(RobotDeathEvent e) {}

		public void onWin(WinEvent e) {}
	}
}