From 9b908a5310045336bdcd810d98a3324aca4f496f Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 3 Jan 2018 19:19:19 -0500 Subject: [PATCH] Biomass count on NPC death is loaded from config --- .../net/simon987/npcplugin/HarvesterNPC.java | 2 -- .../simon987/biomassplugin/BiomassPlugin.java | 2 ++ .../event/ObjectDeathListener.java | 28 +++++++++++-------- Server/src/main/resources/config.properties | 2 ++ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java index 2723ad7..fc01e42 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/HarvesterNPC.java @@ -5,10 +5,8 @@ import com.mongodb.DBObject; import net.simon987.server.GameServer; import net.simon987.server.event.ObjectDeathEvent; import net.simon987.server.game.Direction; -import net.simon987.server.logging.LogManager; import org.json.simple.JSONObject; -import java.lang.util.Random; public class HarvesterNPC extends NonPlayerCharacter { diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java index 6465720..fe5ab0a 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java @@ -1,6 +1,7 @@ package net.simon987.biomassplugin; import com.mongodb.DBObject; +import net.simon987.biomassplugin.event.ObjectDeathListener; import net.simon987.biomassplugin.event.WorldCreationListener; import net.simon987.biomassplugin.event.WorldUpdateListener; import net.simon987.server.ServerConfiguration; @@ -16,6 +17,7 @@ public class BiomassPlugin extends ServerPlugin implements GameObjectDeserialize public void init(ServerConfiguration config) { listeners.add(new WorldCreationListener()); listeners.add(new WorldUpdateListener(config)); + listeners.add(new ObjectDeathListener(config)); LogManager.LOGGER.info("Initialised Biomass plugin"); } diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java index a1dfd12..036352e 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java @@ -1,39 +1,46 @@ package net.simon987.biomassplugin.event; +import net.simon987.biomassplugin.BiomassBlob; import net.simon987.server.GameServer; +import net.simon987.server.ServerConfiguration; 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 { + + private int biomassDropCount; + + public ObjectDeathListener(ServerConfiguration config) { + biomassDropCount = config.getInt("harvester_biomass_drop_count"); + + } + @Override public Class getListenedEventType() { - return ObjectDeathEvent.getClass(); + return ObjectDeathEvent.class; } @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)) { + if (((ObjectDeathEvent) event).getSourceObjectId() == 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())); + dyingHarvesterNPC.getWorld().getGameObjects().add(newBiomassBlob); + LogManager.LOGGER.fine("Spawned biomass at (" + newBiomassBlob.getX() + + ", " + newBiomassBlob.getY() + ")"); } } @@ -45,14 +52,11 @@ public class ObjectDeathListener implements GameEventListener { * @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.setBiomassCount(biomassDropCount); biomassBlob.setX(x); biomassBlob.setY(y); biomassBlob.setWorld(world); diff --git a/Server/src/main/resources/config.properties b/Server/src/main/resources/config.properties index c26b4d3..1c925dc 100644 --- a/Server/src/main/resources/config.properties +++ b/Server/src/main/resources/config.properties @@ -62,6 +62,8 @@ factory_max_npc_count=16 harvester_hp_max=100 # Harvester hp regeneration per tick harvester_regen=5 +# Number of biomass units dropped on death +harvester_biomass_drop_count=8 # ---------------------------------------------- # Minimum center point count for the WorldGenerator wg_centerPointCountMin=5