Added Day/Night Cycle #14

This commit is contained in:
simon 2017-12-09 10:50:37 -05:00
parent 9597a80edf
commit 9dd9b45d2d
16 changed files with 134 additions and 18 deletions

View File

@ -0,0 +1,4 @@
package net.simon987.npcplugin;
public class HarvestTask {
}

View File

@ -0,0 +1,4 @@
package net.simon987.npcplugin;
public class HarvesterNPC {
}

View File

@ -0,0 +1,4 @@
package net.simon987.npcplugin;
public class NPCTask {
}

View File

@ -0,0 +1,4 @@
package net.simon987.npcplugin;
public class NonPlayerCharacter {
}

View File

@ -0,0 +1,4 @@
package net.simon987.npcplugin;
public class NpcPlugin {
}

View File

@ -0,0 +1,24 @@
package net.simon987.npcplugin.event;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.TickEvent;
import net.simon987.server.logging.LogManager;
public class TickListener implements GameEventListener {
@Override
public Class getListenedEventType() {
return TickEvent.class;
}
@Override
public void handle(GameEvent event) {
LogManager.LOGGER.info("Time is " + ((TickEvent)event).getTime());
// NonPlayerCharacter npc = new HarvesterNPC();
// GameServer.INSTANCE.getGameUniverse().getWorld(0,0).getGameObjects().add(npc);
}
}

View File

@ -4,8 +4,10 @@ package net.simon987.server;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher; import net.simon987.server.event.GameEventDispatcher;
import net.simon987.server.event.TickEvent; import net.simon987.server.event.TickEvent;
import net.simon987.server.game.DayNightCycle;
import net.simon987.server.game.GameUniverse; import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.World; import net.simon987.server.game.World;
import net.simon987.server.io.FileUtils;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.PluginManager; import net.simon987.server.plugin.PluginManager;
import net.simon987.server.plugin.ServerPlugin; import net.simon987.server.plugin.ServerPlugin;
@ -34,6 +36,8 @@ public class GameServer implements Runnable {
private int maxExecutionTime; private int maxExecutionTime;
private DayNightCycle dayNightCycle;
public GameServer() { public GameServer() {
this.config = new ServerConfiguration(new File("config.properties")); this.config = new ServerConfiguration(new File("config.properties"));
@ -43,6 +47,9 @@ public class GameServer implements Runnable {
maxExecutionTime = config.getInt("user_timeout"); maxExecutionTime = config.getInt("user_timeout");
dayNightCycle = new DayNightCycle();
//Load all plugins in plugins folder, if it doesn't exist, create it //Load all plugins in plugins folder, if it doesn't exist, create it
File pluginDir = new File("plugins/"); File pluginDir = new File("plugins/");
File[] pluginDirListing = pluginDir.listFiles(); File[] pluginDirListing = pluginDir.listFiles();
@ -62,6 +69,7 @@ public class GameServer implements Runnable {
} }
eventDispatcher = new GameEventDispatcher(pluginManager); eventDispatcher = new GameEventDispatcher(pluginManager);
eventDispatcher.getListeners().add(dayNightCycle);
} }
@ -112,7 +120,7 @@ public class GameServer implements Runnable {
//Dispatch tick event //Dispatch tick event
GameEvent event = new TickEvent(gameUniverse.getTime()); GameEvent event = new TickEvent(gameUniverse.getTime());
GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation eventDispatcher.dispatch(event); //Ignore cancellation
//Process user code //Process user code
@ -212,4 +220,8 @@ public class GameServer implements Runnable {
public void setSocketServer(SocketServer socketServer) { public void setSocketServer(SocketServer socketServer) {
this.socketServer = socketServer; this.socketServer = socketServer;
} }
public DayNightCycle getDayNightCycle() {
return dayNightCycle;
}
} }

View File

@ -3,16 +3,30 @@ package net.simon987.server.event;
import net.simon987.server.plugin.PluginManager; import net.simon987.server.plugin.PluginManager;
import net.simon987.server.plugin.ServerPlugin; import net.simon987.server.plugin.ServerPlugin;
import java.util.ArrayList;
public class GameEventDispatcher { public class GameEventDispatcher {
private PluginManager pluginManager; private PluginManager pluginManager;
private ArrayList<GameEventListener> listeners;
public GameEventDispatcher(PluginManager pluginManager) { public GameEventDispatcher(PluginManager pluginManager) {
this.pluginManager = pluginManager; this.pluginManager = pluginManager;
listeners = new ArrayList<>(5);
} }
public void dispatch(GameEvent event) { public void dispatch(GameEvent event) {
//Dispatch to 'local' listeners
for (GameEventListener listener : listeners) {
if (event.getClass().equals(listener.getListenedEventType())) {
listener.handle(event);
}
}
//Dispatch to plugins
for (ServerPlugin plugin : pluginManager.getPlugins()) { for (ServerPlugin plugin : pluginManager.getPlugins()) {
for (GameEventListener listener : plugin.getListeners()) { for (GameEventListener listener : plugin.getListeners()) {
if (event.getClass().equals(listener.getListenedEventType())) { if (event.getClass().equals(listener.getListenedEventType())) {
@ -23,4 +37,7 @@ public class GameEventDispatcher {
} }
public ArrayList<GameEventListener> getListeners() {
return listeners;
}
} }

View File

@ -0,0 +1,41 @@
package net.simon987.server.game;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.TickEvent;
public class DayNightCycle implements GameEventListener {
/**
* Length of an hour in ticks
*/
private static final int HOUR_LENGTH = 16;
//Current time of the day (0-23)
private int currentDayTime;
//Current light intensity (0-10)
private int sunIntensity;
@Override
public Class getListenedEventType() {
return TickEvent.class;
}
@Override
public void handle(GameEvent event) {
currentDayTime = (int) ((TickEvent) event).getTime() / HOUR_LENGTH % 24;
// -0.25x² + 6x - 27 with a minimum of 1
sunIntensity = Math.max((int) Math.round((-(0.25 * currentDayTime * currentDayTime) + (6 * currentDayTime) - 27)), 0) + 1;
}
public int getCurrentDayTime() {
return currentDayTime;
}
public int getSunIntensity() {
return sunIntensity;
}
}

View File

@ -1,16 +1,10 @@
package net.simon987.server; package net.simon987.server.io;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -49,8 +43,8 @@ public class FileUtils {
/** /**
* Created a directory if none exists with the specified name * Created a directory if none exists with the specified name
* *
* @param name folder to create * @param directory folder to create
* @return true is the file exists or create operation is successful * @return true is the file exists or create operation is successful
*/ */
public static boolean prepDirectory(Path directory) { public static boolean prepDirectory(Path directory) {
File file = directory.toFile(); File file = directory.toFile();
@ -68,8 +62,8 @@ public class FileUtils {
/** /**
* Converts a file into an array of bytes * Converts a file into an array of bytes
* *
* @param fileName the file to be converted into bytes * @param path the file to be converted into bytes
* @return the byte array of the given file * @return the byte array of the given file
*/ */
public static byte[] bytifyFile(Path path) { public static byte[] bytifyFile(Path path) {
byte[] bytes = null; byte[] bytes = null;
@ -89,8 +83,8 @@ public class FileUtils {
* Takes in a file that had been converted to a byte[] to be written to a new * Takes in a file that had been converted to a byte[] to be written to a new
* zip file * zip file
* *
* @param payload * @param data
* contains data in byte array form to be written, typically a file * contains data in byte array form to be written, typically a file
* that has been converted with bytifyFile() * that has been converted with bytifyFile()
* @throws IOException * @throws IOException
* if an error occurs during the write process * if an error occurs during the write process

View File

@ -0,0 +1,4 @@
package net.simon987.server.event;
public class TickEvent {
}

View File

@ -0,0 +1,4 @@
package net.simon987.server.event;
public class UpdateEvent {
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
plugins/Plugin NPC.jar Normal file

Binary file not shown.