Updates for #66: reorganize and relocate some methods and calls

This commit is contained in:
James T 2017-12-30 09:01:58 -08:00
parent eea9420192
commit 81767ed5cf
3 changed files with 41 additions and 27 deletions

View File

@ -3,7 +3,6 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.server.game.Direction;
import net.simon987.biomassplugin.BiomassBlob;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;
@ -41,36 +40,12 @@ public class HarvesterNPC extends NonPlayerCharacter {
}
}
@Override
public void setDead(boolean dead) {
super.setDead(dead);
// GameServer.INSTANCE.getEventDispatcher().dispatch(
// new ObjectDeathEvent((Object)this, ID));
}
@Override
public void onDeadCallback() {
GameServer.INSTANCE.getEventDispatcher().dispatch(
new ObjectDeathEvent((Object)this, ID));
}
public BiomassBlob createBiomassBlobInPlace() {
Random random = new Random();
// random integer in range [2, 4]
int yield = random.nextInt(2) + 2;
BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
// biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
biomassBlob.setBiomassCount(yield);
biomassBlob.setX(getX());
biomassBlob.setY(getY());
biomassBlob.setWorld(getWorld());
LogManager.LOGGER.info("HarvesterNPC spawned biomass in its place");
return biomassBlob;
}
@Override
public JSONObject serialise() {
JSONObject json = super.serialise();

View File

@ -1,10 +1,19 @@
package net.simon987.biomassplugin.event;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.npcplugin.HarvesterNPC;
import net.simon987.biomassplugin.BiomassBlob;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
import java.lang.Random;
/**
* Handles ObjectDeathEvent events
*/
public class ObjectDeathListener implements GameEventListener {
@Override
public Class getListenedEventType() {
@ -16,7 +25,37 @@ public class ObjectDeathListener implements GameEventListener {
// a HarvesterNPC ObjectDeathEvent is received
if (((ObjectDeathEvent)event).getSourceObjectId().equals(HarvesterNPC.ID)) {
HarvesterNPC dyingHarvesterNPC = (HarvesterNPC)event.getSource();
dyingHarvesterNPC.getWorld().getGameObjects.add(dyingHarvesterNPC.createBiomassBlobInPlace());
// create a new biomass
BiomassBlob newBiomassBlob = createBiomassBlobAt(
dyingHarvesterNPC.getX(), dyingHarvesterNPC.getY(), dyingHarvesterNPC.getWorld());
// add it to the world game objects
dyingHarvesterNPC.getWorld().getGameObjects.add(newBiomassBlob);
LogManager.LOGGER.fine("Spawned biomass at (%d, %d)".format(
newBiomassBlob.getX(),newBiomassBlob.getY()));
}
}
/**
* Create and return a biomass at the given x, y coordinates and in the world
* @param x x coord of biomass location
* @param y y coord of biomass location
* @param world world in which the biomass will be created in
* @return the new BiomassBlob created
*/
private BiomassBlob createBiomassBlobAt(int x, int y, World world) {
Random random = new Random();
// random integer in range [2, 4]
int yield = random.nextInt(2) + 2;
BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
// biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
biomassBlob.setBiomassCount(yield);
biomassBlob.setX(x);
biomassBlob.setY(y);
biomassBlob.setWorld(world);
return biomassBlob;
}
}

View File

@ -102,8 +102,8 @@ public class World implements JSONSerialisable {
for (GameObject object : gameObjects_) {
//Clean up dead objects
if (object.isDead()) {
gameObjects.remove(object);
object.onDeadCallback();
gameObjects.remove(object);
//LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
} else if (object instanceof Updatable) {
((Updatable) object).update();