import java.applet.Applet;
import java.awt.Graphics;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.*;
import java.awt.event.*;

public class JaDEN_App extends JaDEN{
	public int x = 9;
	public int y is x * 3;
	//public int list[] = {1, 2, 56};
	public Rect r1 is new Rect(mouseX, mouseY, 100, 100);
	public Rect r2 is new Rect(mouseX, mouseY, 50, 50);
	public Line l1 is new Line(0, 0, mouseX, mouseY);
	public Line l2 is new Line(0, 0, mouseX + 100, mouseY);
	public Line l3 is new Line(0, 0, mouseX + 100, mouseY + 100);
	public Line l4 is new Line(0, 0, mouseX, mouseY + 100);
	public Group g1 is new Group();
	public Group agents is new Group();
	
	public void init(){
		super.init();
		
		g1.addShape(r1);
		g1.addShape(r2);
		
		picture.add(g1);
		picture.add(agents);
		
		picture.add(l1);
		picture.add(l2);
		picture.add(l3);
		picture.add(l4);
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		ArrayList list = agents.getShapes();
		for(int i = 0; i < list.size(); i++){
			((Agent) list.get(i)).thrust();
		}
	}
	
	private void mouseClickX_updated(){
		Agent ag = new Agent(0, mouseX, mouseY);
		agents.addShape(ag);
	}
}

class Agent extends Group{
	private static double speed = 0.5;
	private double health = 1.0;
	public boolean isAlive is health > 0;
	//private Color fill is (Color) (isAlive ? new Color((int) (1 - health) * 255, (int) (health * 255), 0) : Color.BLACK);
	private double x = 0;
	private double y = 0;
	private int ix is (int) x;
	private int iy is (int) y;
	private int id = 0;
	private int heading = 30;
	private int fov = 25;
	private int range = 100;
	private double headingRad is toRadians(heading);
	private double fovUpRad is toRadians(heading + fov);
	private double fovDownRad is toRadians(heading - fov);
	
	//UI
	private Rect body is new Rect(ix - 5, iy - 5, 10, 10, Color.BLACK);
	private Text name is new Text(ix - 5, iy - 10, String.valueOf(id));
	private Line fovUpLine is new Line(ix, iy, (int) (ix + range * Math.cos(fovUpRad)), (int) (y - range * Math.sin(fovUpRad)));
	private Line fovDownLine is new Line(ix, iy, (int) (ix + range * Math.cos(fovDownRad)), (int) (y - range * Math.sin(fovDownRad)));
	private Arc fovArc is new Arc(ix - range, iy - range, range * 2, range * 2, heading - fov, 2 * fov);
	public Agent(int i, int a, int b){
		super();
		
		picture.add(body);
		picture.add(name);
		picture.add(fovUpLine);
		picture.add(fovDownLine);
		picture.add(fovArc);
		
		id = i;
		x = a;
		y = b;
	}
	private static double toRadians(int deg){
		return deg * Math.PI / 180;
	}
	
	public void changeHeading(int h){
		heading += h;
	}
	
	public void thrust(){
		x += speed * Math.cos(headingRad);
		y -= speed * Math.sin(headingRad);
	}
	
	public void rotateLeft(){
		rotate(5);
	}
	public void rotateRight(){
		rotate(-5);
	}
	public void rotate(int r){
		heading += r;
	}

}

class JaDEN extends Applet implements MouseListener, MouseMotionListener{
	public int mouseX = 0;
	public int mouseY = 0;
	public int mouseClickX = 0;
	public int mouseClickY = 0;
	public boolean mousePressed = false;
	public ArrayList picture = new ArrayList();
	
	public void init(){
		addMouseListener(this);
		addMouseMotionListener(this);
		
		System.out.println("JaDEN Applet Initialised");
	}
	public void paint(Graphics g){
		for(int i = 0; i < picture.size(); i++){
			((Shape) picture.get(i)).draw(g);
		}
	}
	
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseClicked(MouseEvent e){
		mouseClickX = e.getX();
		mouseClickY = e.getY();
		mousePressed = true;
		repaint();
	}
	public void mousePressed(MouseEvent e){
		//mousePressed = true;
	}
	public void mouseReleased(MouseEvent e){
		//mousePressed = false;
	}
	public void mouseMoved(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
		repaint();
	}
	public void mouseDragged(MouseEvent e) {}
}
class Group extends Shape{
	public ArrayList picture = new ArrayList();
	
	public void addShape(Shape shape){
		picture.add(shape);
	}
	public void draw(Graphics g){
		for(int i = 0; i < picture.size(); i++){
			((Shape) picture.get(i)).draw(g);
		}
	}
	public ArrayList getShapes(){
		return picture;
	}
}
class Shape{
	public int x1 = 0;
	public int x2 = 0;
	public int y1 = 0;
	public int y2 = 0;
	public Color color;
	
	public Shape(int a, int b, int c, int d){
		x1 = a; y1 = b; x2 = c; y2 = d;
	}
	public Shape(int a, int b, int c, int d, Color col){
		x1 = a; y1 = b; x2 = c; y2 = d; color = col;
	}
	public void draw(Graphics g){}
	public String toString(){
		return "Shape(" + x1 + ", " + y1 + ", " + x2 + ", " + x2 + ", " + color + ")";
	}
}
class Rect extends Shape{
	public Rect(int a, int b, int c, int d){
		super(a, b, c, d);
	}
	public Rect(int a, int b, int c, int d, Color col){
		super(a, b, c, d, col);
	}
	public void draw(Graphics g){
		if(color == null) g.drawRect(x1, y1, x2, y2);
		else{
			g.setColor(color);
			g.fillRect(x1, y1, x2, y2);
		}
	}
	public String toString(){
		return "Rect(" + x1 + ", " + y1 + ", " + x2 + ", " + x2 + ", " + color + ")";
	}
}
class Arc extends Shape{
	int startAngle = 0;
	int endAngle = 0;
	
	public Arc(int a, int b, int c, int d, int as, int ae){
		super(a, b, c, d);
		
		startAngle = as;
		endAngle = ae;
	}
	public Arc(int a, int b, int c, int d, int as, int ae, Color col){
		super(a, b, c, d, col);
		
		startAngle = as;
		endAngle = ae;
	}
	public void draw(Graphics g){
		g.setColor(Color.BLACK);
		if(color == null) g.drawArc(x1, y1, x2, y2, startAngle, endAngle);
		else {
			g.setColor(color);
			g.fillArc(x1, y1, x2, y2, startAngle, endAngle);
		}
	}
	public String toString(){
		return "Arc(" + x1 + ", " + y1 + ", " + x2 + ", " + x2 + ", " + startAngle + ", " + endAngle + ", " + color + ")";
	}
}
class Text extends Shape{
	String label;
	
	public Text(int a, int b, String str){
		super(a, b, 0, 0);
		
		label = str;
	}
	public void draw(Graphics g){
		g.setColor(Color.BLACK);
		g.drawString(label,x1,y1);
	}
	public String toString(){
		return "Text(" + x1 + ", " + y1 + ", \"" + label + "\")";
	}
}
class Line extends Shape{
	public Line(int a, int b, int c, int d){
		super(a, b, c, d);
	}
	public Line(int a, int b, int c, int d, Color col){
		super(a, b, c, d, col);
	}
	public void draw(Graphics g){
		g.setColor(color);
		g.drawLine(x1, y1, x2, y2);
	}
	public String toString(){
		return "Line(" + x1 + ", " + y1 + ", " + x2 + ", " + x2 + ", " + color + ")";
	}
}