package graphics;

import scheme.lib.htdch.graphics.rename;
import scheme.lib.mred.mred;

public abstract class GameWorld extends World {

  dynamic timer;

  public GameWorld() {
    super(new View());
  }

  public World endOfWorld() {
     timer.stop();
     return this;
  }

  World nextWorld = this;

  void oneStepPrivate(World oldWorld, World newWorld) {
     ((GameWorld) newWorld).timer = ((GameWorld) oldWorld).timer;
     newWorld.display = oldWorld.display;
     display.allowImage(false);
//     oldWorld.erase();
     newWorld.draw();
     display.allowImage(true);
  }

  //Produces a World that will animate with a clock tick of rate
  public final boolean animate( int width, int height, int rate ) {

   class TimerCallBack {
      public void callBack() {
         World old = GameWorld.this.nextWorld;
         GameWorld.this.nextWorld = GameWorld.this.nextWorld.onTick();
         GameWorld.this.oneStepPrivate(old, GameWorld.this.nextWorld);
      }
   }

   class KeyCallBack { 
     public void callBack(String key) {
        World old = GameWorld.this.nextWorld;
        GameWorld.this.nextWorld = GameWorld.this.nextWorld.onKey(key);
        GameWorld.this.oneStepPrivate(old,GameWorld.this.nextWorld);
     }
   }

    display.display(width, height);
    dynamic tCB = new TimerCallBack();
    
    display.keyCallBack(rename.innerToFunction(1, new KeyCallBack()));
    timer = rename.newObject(mred.timerObj, rename.innerToFunction(0,tCB));
    timer.start(rate, false);
    return true;
  }

}
