// Turret shell // // Shells are just another physical object. // // Author Matthew Caryl // Created 8.12.96 // // Although under copywrite to the author (Matthew Caryl) this code can be copied and modified for non-commercial // purposes as long as any derivatives contain this condition. package ThinkTank; import java.awt.Color; import java.awt.Rectangle; import java.awt.Graphics; import java.lang.Math; import ADT.Physical; final class Shell implements Physical { // // Private interface of shell // // individual shell data private float x, y; private int oldX, oldY; private float cos_heading_speed; private float sin_heading_speed; private Color color; private Tank owner; // individual shell display data // outline and bounds are not used but must be supported for physical objects private int[][][] outline = new int[0][0][0]; private Rectangle bounds = new Rectangle(); // global shell data // having a larger collision radius prevents any graphical collisions static final int radius = 2; private static final int collision_radius = radius + 2; // // Public interface of shell to World object // // Initialise shell public Shell(Tank O, int X, int Y, float H, float S, Color C) { owner = O; x = X; y = Y; cos_heading_speed = (float) Math.cos(H) * S; // precalculate offset distances sin_heading_speed = (float) Math.sin(H) * S; color = C; } // Retrieve outlines of shell components public int[][][] outline() { return outline; } // Retrieve bounding rectangle of shell components public Rectangle bounds() { return bounds; } // Retrieve the maximum radius of the shall public int radius() { return collision_radius; } public int x() { return (int) x; } public int y() { return (int) y; } public void set(int X, int Y) { x = X; y = Y; } public boolean moved() { return true; } public void sensor() { } public void action() { oldX = (int) x; oldY = (int) y; x += sin_heading_speed; y -= cos_heading_speed; } // Single move public void tick() { sensor(); action(); } public void collision(boolean first) { // fogetting your owner prevents reporting of multiple collisions if (owner != null) { owner.hit(this); owner = null; } } public void collision(Physical p, boolean first) { // fogetting your owner prevents reporting of multiple collisions if (owner != null) { owner.hit(this, p); owner = null; } } public void paint(Graphics g) { g.setColor(color); g.drawOval((int) x - radius, (int) y - radius, 2 * radius, 2 * radius); } public void erase(Graphics g) { g.drawOval(oldX - radius, oldY - radius, 2 * radius, 2 * radius); } }