blob: 02bd4e067b5188ea4436927da30c54b484b55c4f [file] [log] [blame] [raw]
/*
* Copyright (c) 2010-2011 Graham Edgecombe.
*
* This file is part of Lightstone.
*
* Lightstone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Lightstone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Lightstone. If not, see <http://www.gnu.org/licenses/>.
*/
package net.lightstone.world;
import java.util.Collection;
import net.lightstone.io.ChunkIoService;
import net.lightstone.model.ChunkManager;
import net.lightstone.model.Entity;
import net.lightstone.model.EntityManager;
import net.lightstone.model.Player;
import net.lightstone.model.Position;
import net.lightstone.msg.ChatMessage;
/**
* A class which represents the in-game world.
* @author Graham Edgecombe
*/
public class World {
/**
* The spawn position.
*/
private final Position spawnPosition = new Position(0, 63, 0);
/**
* The chunk manager.
*/
private final ChunkManager chunks;
/**
* The entity manager.
*/
private final EntityManager entities = new EntityManager();
/**
* Creates a new world with the specified chunk I/O service and world
* generator.
* @param service The chunk I/O service.
* @param generator The world generator.
*/
public World(ChunkIoService service, WorldGenerator generator) {
chunks = new ChunkManager(service, generator);
}
/**
* Updates all the entities within this world.
*/
public void pulse() {
for (Entity entity : entities)
entity.pulse();
for (Entity entity : entities)
entity.reset();
}
/**
* Gets the chunk manager.
* @return The chunk manager.
*/
public ChunkManager getChunks() {
return chunks;
}
/**
* Gets the entity manager.
* @return The entity manager.
*/
public EntityManager getEntities() {
return entities;
}
/**
* Gets a collection of all the players within this world.
* @return A {@link Collection} of {@link Player} objects.
*/
public Collection<Player> getPlayers() {
return entities.getAll(Player.class);
}
/**
* Gets the spawn position.
* @return The spawn position.
*/
public Position getSpawnPosition() {
return spawnPosition;
}
/**
* Broadcats a message to every player.
* @param text The message text.
*/
public void broadcastMessage(String text) {
ChatMessage message = new ChatMessage(text);
for (Player player : getPlayers())
player.getSession().send(message);
}
}