Package game.impl

Source Code of game.impl.AbstractGameController

package game.impl;

import game.ClientRemovedEvent;
import game.FireEvent;
import game.GameAdminEvent;
import game.GameController;
import game.GameListener;
import game.GameObject;
import game.GameObjectLocation;
import game.NewClientEvent;
import game.ObjectMoveEvent;
import game.ObjectsUpdateEvent;
import game.RemoveClientEvent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.event.EventListenerList;

import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;

public abstract class AbstractGameController implements GameController, Runnable {

   private EventListenerList listeners = new EventListenerList();
   protected Map<String, GameObject> gameObjects = Collections.synchronizedMap(new HashMap<String, GameObject>());
   private Thread thread;
   protected int boardX = 640;
   protected int boardY = 480;

   public void addGameListener(GameListener listener) {
      listeners.add(GameListener.class, listener);
   }

   public void removeGameListener(GameListener listener) {
      listeners.remove(GameListener.class, listener);
   }

   @Override
   public void onNewClient(NewClientEvent e) {
      final GameObject gameObject = new GameObject(e.getSource());
      gameObject.setX(100+(int) (System.currentTimeMillis() % 100));
      gameObject.setY(100+(int) (System.currentTimeMillis() % 100));
      gameObjects.put(gameObject.getId(), gameObject);
      updateAllClients();
   }

   @Override
   public void onRemoveClient(RemoveClientEvent e) {
     gameObjects.remove(e.getSource());
     notifyClientRemoved(new ClientRemovedEvent(e.getSource(), e.getSource()));
   }

   @Override
   public void onGameAdmin(GameAdminEvent e) {
      boardX = e.getX();
      boardY = e.getY();
   }

   @Override
   public void onUserControl(ObjectMoveEvent e) {
      final GameObject gameObject = gameObjects.get(e.getSource());
      if (gameObject != null) {
         gameObject.processDirections(e.getDirection(), e.getTurns());
      }
   }

   public synchronized  void start() {
      if (thread == null) {
         thread = new Thread(this);
         thread.start();
      }
   }

   public synchronized void stop() {
      if (thread != null) {
         thread.interrupt();
         thread = null;
      }
   }

   protected void updateAllClients() {
      notifyObjectMove(getAllObjects());
   }

   protected void notifyObjectMove(Collection<GameObjectLocation> objects) {
      for(GameListener listener : listeners.getListeners(GameListener.class)) {
         listener.onObjectsUpdate(new ObjectsUpdateEvent(objects));
      }
   }

   protected void notifyClientRemoved(ClientRemovedEvent e) {
      for(GameListener listener : listeners.getListeners(GameListener.class)) {
         listener.onClientRemoved(e);
      }
   }

   @Override
   public void onFire(FireEvent e) {
      final GameObject gameObject = gameObjects.get(e.getSource());
      GameObject bullet = gameObject.createBullet();
      gameObjects.put(bullet.getId(), bullet);
   }

   @Override
   public Collection<GameObjectLocation> getAllObjects() {
      final List<GameObjectLocation> locations = new ArrayList<GameObjectLocation>(gameObjects.size());
      synchronized (gameObjects) {
         CollectionUtils.forAllDo(gameObjects.values(), new Closure() {
            @Override
            public void execute(Object input) {
               locations.add(((GameObject) input).getLocation());
            }
         });
      }
      return locations;
   }
}
TOP

Related Classes of game.impl.AbstractGameController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.