Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright © 2011 J.M. Linden
- // This program 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 2
- // of the License, or (at your option) any later version.
- //
- // This program 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 this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- package org.mike.gielinor.game.service.event;
- import static org.mike.gielinor.game.GameConstants.CYCLE_TIME;
- import static org.mike.gielinor.game.model.World.getWorld;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.mike.core.service.event.Event;
- import org.mike.core.service.task.ConsecutiveTask;
- import org.mike.core.service.task.ParallelTask;
- import org.mike.core.service.task.Task;
- import org.mike.gielinor.game.model.Entity;
- import org.mike.gielinor.game.model.NPC;
- import org.mike.gielinor.game.model.Player;
- import org.mike.gielinor.game.model.update.UpdateAttributes;
- import org.mike.gielinor.game.service.GameEngine;
- /**
- * A {@link Task} which globally updates all registered {@link Entity entities}
- * each server cycle.
- *
- * @author J.M. Linden
- *
- */
- public final class EntityUpdateEvent extends Event {
- /**
- * The {@link List} of all tick {@link Task}s.
- */
- private final List<Task> tickTasks = new ArrayList<Task>();
- /**
- * The {@link List} of all update {@link Task}s.
- */
- private final List<Task> updateTasks = new ArrayList<Task>();
- /**
- * The {@link List} of all reset {@link Task}s.
- */
- private final List<Task> resetTasks = new ArrayList<Task>();
- /**
- * The {@link GameEngine} to {@link #execute() execute} the cycle
- * {@link Task}s in.
- */
- private final GameEngine engine = getWorld().getEngine();
- /**
- * Creates a new {@link EntityUpdateEvent} with the default delay of the
- * server's cycle time..
- */
- public EntityUpdateEvent() {
- super(CYCLE_TIME);
- }
- @Override
- public void execute() {
- tickTasks.clear();
- updateTasks.clear();
- resetTasks.clear();
- for (NPC npc : getWorld().getNPCs()) {
- // fetch the cached updating tasks
- final UpdateAttributes ua = npc.getUpdateAttributes();
- tickTasks.add(ua.getTickTask());
- resetTasks.add(ua.getResetTask());
- }
- final Iterator<Player> it$ = getWorld().getPlayers().iterator();
- while (it$.hasNext()) {
- final Player player = it$.next();
- if (!player.getChannel().isConnected()) {
- it$.remove();
- } else {
- // fetch the cached updating tasks
- final UpdateAttributes ua = player.getUpdateAttributes();
- tickTasks.add(ua.getTickTask());
- updateTasks.add(new ConsecutiveTask(ua.getUpdateTasks()));
- resetTasks.add(ua.getResetTask());
- }
- }
- // tick tasks can no longer be parallel due to region code
- final Task tickTask = new ConsecutiveTask(
- tickTasks.toArray(new Task[0]));
- final Task updateTask = new ParallelTask(engine,
- updateTasks.toArray(new Task[0]));
- final Task resetTask = new ParallelTask(engine,
- resetTasks.toArray(new Task[0]));
- getWorld().submit(new ConsecutiveTask(tickTask, updateTask, resetTask));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement