Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rogue-savage-rats/src/mightypork/rogue/world/WorldProvider.java

115 lines
1.9 KiB

package mightypork.rogue.world;
import java.io.File;
import java.io.IOException;
import mightypork.gamecore.eventbus.BusAccess;
import mightypork.gamecore.eventbus.clients.RootBusNode;
import mightypork.gamecore.util.ion.Ion;
import mightypork.rogue.world.entity.Entity;
import mightypork.rogue.world.level.LevelAccess;
10 years ago
public class WorldProvider extends RootBusNode {
public static synchronized void init(BusAccess busAccess)
{
if (inst == null) {
inst = new WorldProvider(busAccess);
}
}
10 years ago
public WorldProvider(BusAccess busAccess)
{
super(busAccess);
setListening(false);
}
private static WorldProvider inst;
public static WorldProvider get()
{
if (inst == null) {
throw new IllegalStateException("World provider not initialized.");
}
return inst;
}
private World world;
private final PlayerControl playerControl = new PlayerControl() {
@Override
10 years ago
protected World provideWorld()
{
return world;
}
};
public void createWorld(long seed)
{
setWorld(WorldCreator.createWorld(seed));
}
public World getWorld()
{
return world;
}
10 years ago
private void setWorld(World newWorld)
{
if (world != null) removeChildClient(world);
world = newWorld;
world.assignBus(this); // connect to bus (for event dispatching)
addChildClient(world);
}
public void loadWorld(File file) throws IOException
{
setWorld(Ion.fromFile(file, World.class));
}
public void saveWorld(File file) throws IOException
{
if (world == null) throw new IllegalStateException("Trying to save a NULL world.");
Ion.toFile(file, world);
}
public LevelAccess getCurrentLevel()
{
return getWorld().getCurrentLevel();
}
public Entity getPlayerEntity()
{
return getWorld().getPlayerEntity();
}
/**
* @return constant player control (world independent)
*/
public PlayerControl getPlayerControl()
{
return playerControl;
}
@Override
protected void deinit()
{
}
}