package spacefront;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DemoController extends TimerTask {

    private static final Random RNG = new Random();
    private static final long DELAY = 100l;

    private final Spacefront space;
    private final double accuracy;
    private final Timer timer = new Timer();

    public DemoController(Spacefront spacefront, double accuracy) {
        this.space = spacefront;
        this.accuracy = accuracy;
        timer.schedule(this, DELAY, DELAY);
    }

    @Override
    public void run() {
        /* Target the nearest meteoroid. */
        Meteoroid target = null;
        double d = Double.POSITIVE_INFINITY;
        for (Meteoroid m : space.getMeteoroids()) {
            if (m.getDistance() < d) {
                d = m.getDistance();
                target = m;
            }
        }
        if (target != null) {
            space.fire(true);
            /* Nudge the angle a little bit, for inaccuracy. */
            double x = target.getX();
            double y = target.getY();
            double a = Math.atan2(y, x);
            a += a * (1 - accuracy) * RNG.nextGaussian();
            space.fireXY(Math.cos(a) * d, Math.sin(a) * d);
        } else {
            space.fire(false);
        }
    }

    @Override
    public void finalize() {
        timer.cancel();
    }
}

@@ -14,16 +14,39 @@
 front;%0A%0A
+import java.util.List;%0A
 import j
@@ -62,16 +62,16 @@
 Random;%0A
-
 import j
@@ -259,16 +259,67 @@
  = 100l;
+%0A    private static final long WEAPON_TIME = 5000l;
 %0A%0A    pr
@@ -379,24 +379,24 @@
 e accuracy;%0A
-
     private 
@@ -429,16 +429,81 @@
 mer();%0A%0A
+    private long lastWeaponChange = System.currentTimeMillis();%0A%0A
     publ
@@ -1432,24 +1432,299 @@
 );%0A        %7D
+%0A%0A        long now = System.currentTimeMillis();%0A        if (now - lastWeaponChange %3E WEAPON_TIME) %7B%0A            List%3CWeapon%3E weapons = space.getWeapons();%0A            space.selectWeapon(weapons.get(RNG.nextInt(weapons.size())));%0A            lastWeaponChange = now;%0A        %7D
 %0A    %7D%0A%0A    

Add weapon changing to the demo.