mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
Merge pull request #66 from jtara1/master
HarvesterNPC should spawn a biomass in-place after they die. Fixes #33
This commit is contained in:
commit
8ceeca564a
@ -3,9 +3,13 @@ package net.simon987.npcplugin;
|
|||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
|
import net.simon987.server.event.ObjectDeathEvent;
|
||||||
import net.simon987.server.game.Direction;
|
import net.simon987.server.game.Direction;
|
||||||
|
import net.simon987.server.logging.LogManager;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import java.lang.util.Random;
|
||||||
|
|
||||||
public class HarvesterNPC extends NonPlayerCharacter {
|
public class HarvesterNPC extends NonPlayerCharacter {
|
||||||
|
|
||||||
public static final int ID = 10;
|
public static final int ID = 10;
|
||||||
@ -44,6 +48,12 @@ public class HarvesterNPC extends NonPlayerCharacter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeadCallback() {
|
||||||
|
GameServer.INSTANCE.getEventDispatcher().dispatch(
|
||||||
|
new ObjectDeathEvent((Object)this, ID));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSONObject serialise() {
|
public JSONObject serialise() {
|
||||||
JSONObject json = super.serialise();
|
JSONObject json = super.serialise();
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
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.biomassplugin.BiomassBlob;
|
||||||
|
import net.simon987.server.game.GameObject;
|
||||||
|
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() {
|
||||||
|
return ObjectDeathEvent.getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(GameEvent event) {
|
||||||
|
// a HarvesterNPC ObjectDeathEvent is received
|
||||||
|
// TODO: setup enum with all GameObject type IDs
|
||||||
|
if (((ObjectDeathEvent)event).getSourceObjectId().equals(10)) {
|
||||||
|
GameObject dyingHarvesterNPC = (GameObject)event.getSource();
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package net.simon987.server.event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event dispatched by a GameObject who has needed callbacks on death
|
||||||
|
*/
|
||||||
|
public class ObjectDeathEvent extends GameEvent {
|
||||||
|
/**
|
||||||
|
* The GameObject type ID of object that init this event
|
||||||
|
*/
|
||||||
|
private int sourceObjectId;
|
||||||
|
|
||||||
|
public ObjectDeathEvent(Object source, int sourceObjectId) {
|
||||||
|
setSource(source);
|
||||||
|
this.sourceObjectId = sourceObjectId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSourceObjectId() { return sourceObjectId; }
|
||||||
|
}
|
@ -254,4 +254,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
|
|||||||
public void setDead(boolean dead) {
|
public void setDead(boolean dead) {
|
||||||
this.dead = dead;
|
this.dead = dead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called before this GameObject is removed from the world - defaults to doing nothing
|
||||||
|
*/
|
||||||
|
public void onDeadCallback() { }
|
||||||
}
|
}
|
@ -104,6 +104,7 @@ public class World implements MongoSerialisable {
|
|||||||
for (GameObject object : gameObjects_) {
|
for (GameObject object : gameObjects_) {
|
||||||
//Clean up dead objects
|
//Clean up dead objects
|
||||||
if (object.isDead()) {
|
if (object.isDead()) {
|
||||||
|
object.onDeadCallback();
|
||||||
gameObjects.remove(object);
|
gameObjects.remove(object);
|
||||||
//LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
|
//LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
|
||||||
} else if (object instanceof Updatable) {
|
} else if (object instanceof Updatable) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user