HarvesterNPC should spawn a biomass in-place after they die (#33)

This commit is contained in:
James T 2017-12-29 22:35:02 -08:00
parent 9e402fe8a1
commit eea9420192
6 changed files with 81 additions and 19 deletions

View File

@ -1,9 +1,14 @@
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;
import java.lang.util.Random;
public class HarvesterNPC extends NonPlayerCharacter {
public static final int ID = 10;
@ -36,6 +41,36 @@ 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

@ -0,0 +1,22 @@
package net.simon987.biomassplugin.event;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.npcplugin.HarvesterNPC;
public class ObjectDeathListener implements GameEventListener {
@Override
public Class getListenedEventType() {
return ObjectDeathEvent.getClass();
}
@Override
public void handle(GameEvent event) {
// a HarvesterNPC ObjectDeathEvent is received
if (((ObjectDeathEvent)event).getSourceObjectId().equals(HarvesterNPC.ID)) {
HarvesterNPC dyingHarvesterNPC = (HarvesterNPC)event.getSource();
dyingHarvesterNPC.getWorld().getGameObjects.add(dyingHarvesterNPC.createBiomassBlobInPlace());
}
}
}

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
</component>
</module>

View File

@ -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; }
}

View File

@ -251,4 +251,9 @@ public abstract class GameObject implements JSONSerialisable {
public void setDead(boolean dead) {
this.dead = dead;
}
/**
* Called before this GameObject is removed from the world - defaults to doing nothing
*/
public void onDeadCallback() { }
}

View File

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