00001
00002
00003
00004
00005 package org.classroomgaming.cgp;
00006
00007 import java.beans.PropertyChangeEvent;
00008
00013 public class FrameTimer extends GameObject implements Runnable {
00014 public static final String CGP_CLASS_ID = "FrameTimer";
00015 public static final String PROP_FRAME = "frameCounter";
00016
00017 private Thread clock;
00018 private int frameCounter;
00019 public static long sleepTime = 30;
00020
00021 public FrameTimer() {
00022 clock = new Thread(this);
00023 frameCounter = 0;
00024 start();
00025 }
00026
00027 public FrameTimer(GameModule m) {
00028 parent = m;
00029 clock = new Thread(this);
00030 frameCounter = 0;
00031 m.addPropertyChangeListener(this);
00032 start();
00033 }
00034
00035 public int getFrameCounter() {
00036 return frameCounter;
00037 }
00038
00039 private void setFrameCounter(int value) {
00040 int oldValue = frameCounter;
00041 frameCounter = value;
00042 propertySupport.firePropertyChange(PROP_FRAME, oldValue, frameCounter);
00043 }
00044
00045 public void run() {
00046
00047
00048
00049
00050 while (enabled) {
00051 float dt = sleepTime / 1000;
00052
00053 ponder(dt);
00054 setFrameCounter(frameCounter + 1);
00055
00056 try {
00057 clock.sleep(sleepTime);
00058 } catch (InterruptedException e) {
00059 }
00060 }
00061 }
00062
00063 public void start() {
00064 if (!enabled) {
00065 clock.start();
00066 }
00067 setEnabled(true);
00068
00069 }
00070
00071 public void stop() {
00072 setEnabled(false);
00073
00074 }
00075
00076 @Override
00077 public void propertyChange(PropertyChangeEvent e) {
00078 if (e.getPropertyName().equals(GameModule.PROP_ENABLED)) {
00079 if (e.getNewValue().equals(true)) {
00080 start();
00081 } else {
00082 stop();
00083 }
00084
00085 }
00086 }
00087 }