Biomass count on NPC death is loaded from config

This commit is contained in:
simon 2018-01-03 19:19:19 -05:00
parent 8ceeca564a
commit 9b908a5310
4 changed files with 20 additions and 14 deletions

View File

@ -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 {

View File

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

View File

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

View File

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