Real time object updating
Well this is what I have so far, it uses my javascript engine.
Code:
import javax.script.ScriptException;
public class ObjectHandler extends Thread {
/**
* Starts the init of the object handler, it will run the
* initObjectSpawn method which will start this.
*/
public ObjectHandler() {
misc.println("Object handler init started");
initObjectSpawn();
misc.println("Object handler init sucsessful - object spawned and waiting for updates!");
}
/**
* Adds a new object then adds it to a javascript engine to be run
* by the doUpdate method.
* @param x
* @param y
* @param typeID
* @param orientation
* @param tileObjectType
*/
public void addNewObject(int x, int y, int typeID, int orientation, int tileObjectType) {
try {
scriptLoad****Engine.eval("function newObjects { js.getInstance().makeGlobalObject("+
x+", "+y+", "+typeID+", "+orientation+", "+tileObjectType);
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setObjectNeedsUpdate(true);
}
/**
* Initalizes the object spawning process on a new thread
*/
public void initObjectSpawn() {
c.NewObjects();
this.start();
}
/**
* Executes the newObjects method in the file objectSpawn****
*/
public void doUpdate() {
for(int i = 0; i < server.MaxConnections; i++) {
this.scriptLoad = new ScriptLoader("objectSpawn", "newObjects", i);
}
this.setObjectNeedsUpdate(false);
}
/**
* Returns an instance of this class
* @return ObjectHandler
*/
public ObjectHandler getInstance() {
return this;
}
/**
* Returns if an object update is needed
* @return objectNeedsUpdate
*/
public boolean isObjectNeedsUpdate() {
return objectNeedsUpdate;
}
/**
* Sets weather the object needs an update or not
* @param objectNeedsUpdate
*/
public void setObjectNeedsUpdate(boolean objectNeedsUpdate) {
this.objectNeedsUpdate = objectNeedsUpdate;
notify();
}
/**
* Check if the object needs an update while true
*/
@Override
public void run() {
do {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isObjectNeedsUpdate()) {
this.doUpdate();
}
}while(true);
}
private client c;
private boolean objectNeedsUpdate;
private ScriptLoader scriptLoad;
}
Some thanks to Kelvin for a small part in the javascript engine.
Also thanks to Graham for some performance issues fix.