Android Open Source - killingspree World Manager






From Project

Back to project page killingspree.

License

The source code is released under:

MIT License

If you think the Android project killingspree listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sillygames.killingSpree.managers;
//from   w  w  w  . j  av  a  2s .  co  m
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.TimeUtils;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.sillygames.killingSpree.helpers.Event;
import com.sillygames.killingSpree.helpers.MyConnection;
import com.sillygames.killingSpree.helpers.Event.State;
import com.sillygames.killingSpree.managers.physics.Body;
import com.sillygames.killingSpree.managers.physics.World;
import com.sillygames.killingSpree.networking.messages.AudioMessage;
import com.sillygames.killingSpree.networking.messages.ControlsMessage;
import com.sillygames.killingSpree.networking.messages.EntityState;
import com.sillygames.killingSpree.networking.messages.GameStateMessage;
import com.sillygames.killingSpree.networking.messages.ClientDetailsMessage;
import com.sillygames.killingSpree.networking.messages.PlayerNamesMessage;
import com.sillygames.killingSpree.networking.messages.ServerStatusMessage;
import com.sillygames.killingSpree.networking.messages.ServerStatusMessage.Status;
import com.sillygames.killingSpree.pool.MessageObjectPool;
import com.sillygames.killingSpree.screens.settings.Constants;
import com.sillygames.killingSpree.serverEntities.ServerBlob;
import com.sillygames.killingSpree.serverEntities.ServerEntity;
import com.sillygames.killingSpree.serverEntities.ServerFly;
import com.sillygames.killingSpree.serverEntities.ServerFrog;
import com.sillygames.killingSpree.serverEntities.ServerPlayer;

public class WorldManager{
    
    private World world;
    private Server server;
    public ConcurrentHashMap<Integer, ServerPlayer> playerList;
    public ArrayList<ServerEntity> entities;
    private WorldManager worldManager = this;
    private ArrayList<Event> incomingEventQueue;
    private ArrayList<Event> outgoingEventQueue;
    private Listener serverListener;
    private Listener outgoingEventListener;
    private WorldBodyUtils worldBodyUtils;
    public MyConnection dummyConnection;
    public short id;
    public AudioMessage audio;
//    public ServerFrog frog;
//    public ServerFly fly;
//    public ServerBlob blob;
    public LevelLoader loader;
    ArrayList<Vector2> playerPositions;
    
    public int i;
    
    public WorldManager(final Server server){
        i = 0;
        
        playerPositions = new ArrayList<Vector2>();
        playerPositions.add(new Vector2(50,85));
        playerPositions.add(new Vector2(395,85));
        playerPositions.add(new Vector2(50,230));
        playerPositions.add(new Vector2(395,230));
        
        playerList = new ConcurrentHashMap<Integer, ServerPlayer>();
        
        world = new World(new Vector2(0, -500f));
        
        serverListener = new WorldManagerServerListener();

        if  (server != null) {
            server.addListener(serverListener);
        }
        
        this.server = server;
        
        entities = new ArrayList<ServerEntity>();
        
        incomingEventQueue = new ArrayList<Event>();
        outgoingEventQueue = new ArrayList<Event>();
        dummyConnection = new MyConnection();
        incomingEventQueue.add(MessageObjectPool.instance.
                eventPool.obtain().set(State.CONNECTED, null));
        outgoingEventQueue.add(MessageObjectPool.instance.
                eventPool.obtain().set(State.CONNECTED, null));
        audio = new AudioMessage();

        worldBodyUtils = new WorldBodyUtils(worldManager);

        id = 0;
//        frog = new ServerFrog(id++, 20, 100, worldBodyUtils);
//        entities.add(frog);
//        fly = new ServerFly(id++, 20, 100, worldBodyUtils);
//        entities.add(fly);
//        blob = new ServerBlob(id++, 20, 100, worldBodyUtils);
//        entities.add(blob);
        if (server == null)
            loader = new LevelLoader("maps/retro-small.txt", this);
    }
    
    public void setOutgoingEventListener(Listener listener) {
        outgoingEventListener = listener;
    }
    
    public ServerFrog addFrog(float x, float y) {
        ServerFrog frog = new ServerFrog(id++, x, y, worldBodyUtils);
        entities.add(frog);
        return frog;
    }
    
    public ServerFly addFly(float x, float y) {
        ServerFly fly = new ServerFly(id++, x, y, worldBodyUtils);
        entities.add(fly);
        return fly;
    }
    
    public ServerBlob addBlob(float x, float y) {
        ServerBlob blob = new ServerBlob(id++, x, y, worldBodyUtils);
        entities.add(blob);
        return blob;
    }

    public void update(float delta) {
        audio.reset();
        if (server == null)
            loader.loadNextLine(delta);
        
        for(ServerEntity entity: entities) {
            entity.update(delta);
        }
        world.step(delta, 1, this);
        
        GameStateMessage gameStateMessage = MessageObjectPool.instance.
                gameStateMessagePool.obtain();
        for (ServerEntity entity: entities) {
            EntityState state = MessageObjectPool.instance.
                                    entityStatePool.obtain();
            entity.updateState(state);
            gameStateMessage.addNewState(state);
        }
        
        entities.addAll(worldBodyUtils.entities);
        worldBodyUtils.entities.clear();

        gameStateMessage.time = TimeUtils.nanoTime();
        if (server != null) {
            server.sendToAllTCP(gameStateMessage);
            if (audio.audio != 0) {
                server.sendToAllUDP(audio);
            }
        }
        addOutgoingEvent(MessageObjectPool.instance.
                eventPool.obtain().set(State.RECEIVED, gameStateMessage));
        addOutgoingEvent(MessageObjectPool.instance.
                eventPool.obtain().set(State.RECEIVED, audio));
        
        processEvents(serverListener, incomingEventQueue);
        processEvents(outgoingEventListener, outgoingEventQueue);
//        if (frog.body.toDestroy ) {
//            frog = new ServerFrog(id++, 20, 100, worldBodyUtils);
//            entities.add(frog);
//        }
//        if (fly.body.toDestroy ) {
//            fly = new ServerFly(id++, 20, 100, worldBodyUtils);
//            entities.add(fly);
//        }
//        if (blob.body.toDestroy ) {
//            blob = new ServerBlob(id++, 20, 100, worldBodyUtils);
//            entities.add(blob);
//        }
    }


    public World getWorld() {
        return world;
    }
    
    
    public ArrayList<ServerEntity> getEntities() {
        return entities;
    }

    private class WorldManagerServerListener extends Listener {
        
        @Override
        public void connected(Connection connection) {
//            if (server != null)
//                server.sendToTCP(connection.getID(), "start");
        }
        
        @Override
        public void received(Connection connection, Object object) {
            try {
                if (object instanceof ControlsMessage) {
                    playerList.get(connection.getID()).
                    setCurrentControls((ControlsMessage) object);
                }
                if (object instanceof ClientDetailsMessage) {
                    updateClientDetails(connection, object);
                }
            }
            catch(Exception e) {
//                if (server != null) {
//                    server.sendToTCP(connection.getID(), "start");
//                }
                ServerPlayer player = new ServerPlayer(id++, playerPositions.get(i).x,
                        playerPositions.get(i).y, worldBodyUtils);
                i++;
                i %= playerPositions.size();
                playerList.put(connection.getID(), player);
                entities.add(player);
                if (object instanceof ClientDetailsMessage) {
                    updateClientDetails(connection, object);
                }
            }
        }
        
        @Override
        public void disconnected(Connection connection) {
            ServerPlayer player = playerList.get(connection.getID());
            if (player != null) {
                player.dispose();
                playerList.remove(connection.getID());
                entities.remove(player);
                if (server!= null) {
                    server.sendToAllTCP(player.id);
                }
                addOutgoingEvent(MessageObjectPool.instance.
                        eventPool.obtain().set(State.RECEIVED, player.id));
            }
        }
    }

    private void updateClientDetails(Connection connection, Object object) {
        int version = ((ClientDetailsMessage)object).protocolVersion;
        if (version != Constants.PROTOCOL_VERSION) {
            ServerStatusMessage message = new ServerStatusMessage();
            message.status = Status.DISCONNECT;
            if (version > Constants.PROTOCOL_VERSION) {
                message.toastText = "Please update server";
            } else if (version < Constants.PROTOCOL_VERSION) {
                message.toastText = "Please update client";
            }
            server.sendToTCP(connection.getID(), message);
            return;
        }
        
        playerList.get(connection.getID()).
        setName(((ClientDetailsMessage) object).name);
        PlayerNamesMessage players = new PlayerNamesMessage();
        for (ServerPlayer tempPlayer: playerList.values()) {
            players.players.put(tempPlayer.id, tempPlayer.getName());
        }
        if (server != null)
            server.sendToAllTCP(players);
        addOutgoingEvent(MessageObjectPool.instance.eventPool.obtain().
                set(State.RECEIVED, players));
    }
    
    private void processEvents(Listener listener, ArrayList<Event> queue) {
        for (Event event: queue) {
            if (event.state == State.CONNECTED) {
                listener.connected(dummyConnection);
            } else if (event.state == State.RECEIVED) {
                listener.received(dummyConnection, event.object);
            } else if (event.state == State.DISCONNECTED) {
                listener.disconnected(dummyConnection);
            }
        }
        queue.clear();
    }
    
    public void addIncomingEvent(Event event) {
        incomingEventQueue.add(event);
    }
    
    public void addOutgoingEvent(Event event) {
        outgoingEventQueue.add(event);
    }

    public void createWorldObject(MapObject object) {
        worldBodyUtils.createWorldObject(object);
    }
    
    public void destroyBody(Body body) {
        entities.remove(body.getUserData());
        if (server != null)
            server.sendToAllTCP(body.getUserData().id);
        addOutgoingEvent(MessageObjectPool.instance.
                eventPool.obtain().set(State.RECEIVED, body.getUserData().id));
    }

    public void dispose() {
        if (server != null) {
            server.stop();
        }
    }
    
}




Java Source Code List

com.sillygames.killingSpree.AndroidLauncher.java
com.sillygames.killingSpree.KillingSpree.java
com.sillygames.killingSpree.PlatformServices.java
com.sillygames.killingSpree.categories.EnemyCategory.java
com.sillygames.killingSpree.categories.ExplodingWeaponCategory.java
com.sillygames.killingSpree.categories.LivingCategory.java
com.sillygames.killingSpree.categories.NonExplodingWeaponCategory.java
com.sillygames.killingSpree.clientEntities.ClientArrow.java
com.sillygames.killingSpree.clientEntities.ClientBlob.java
com.sillygames.killingSpree.clientEntities.ClientBomb.java
com.sillygames.killingSpree.clientEntities.ClientBullet.java
com.sillygames.killingSpree.clientEntities.ClientEntity.java
com.sillygames.killingSpree.clientEntities.ClientFly.java
com.sillygames.killingSpree.clientEntities.ClientFrog.java
com.sillygames.killingSpree.clientEntities.ClientPlayer.java
com.sillygames.killingSpree.clientEntities.ClientTestPlayer.java
com.sillygames.killingSpree.controls.InputController.java
com.sillygames.killingSpree.controls.onScreenControls.java
com.sillygames.killingSpree.desktop.DesktopLauncher.java
com.sillygames.killingSpree.desktop.DesktopServices.java
com.sillygames.killingSpree.helpers.EntityUtils.java
com.sillygames.killingSpree.helpers.Event.java
com.sillygames.killingSpree.helpers.MyButton.java
com.sillygames.killingSpree.helpers.MyConnection.java
com.sillygames.killingSpree.helpers.Utils.java
com.sillygames.killingSpree.managers.LevelLoader.java
com.sillygames.killingSpree.managers.WorldBodyUtils.java
com.sillygames.killingSpree.managers.WorldManager.java
com.sillygames.killingSpree.managers.WorldRenderer.java
com.sillygames.killingSpree.managers.physics.Body.java
com.sillygames.killingSpree.managers.physics.CollisionProcessor.java
com.sillygames.killingSpree.managers.physics.Ray.java
com.sillygames.killingSpree.managers.physics.WorldDebugRenderer.java
com.sillygames.killingSpree.managers.physics.World.java
com.sillygames.killingSpree.networking.ControlsSender.java
com.sillygames.killingSpree.networking.NetworkRegisterer.java
com.sillygames.killingSpree.networking.StateProcessor.java
com.sillygames.killingSpree.networking.messages.AudioMessage.java
com.sillygames.killingSpree.networking.messages.ClientDetailsMessage.java
com.sillygames.killingSpree.networking.messages.ConnectMessage.java
com.sillygames.killingSpree.networking.messages.ControlsMessage.java
com.sillygames.killingSpree.networking.messages.EntityState.java
com.sillygames.killingSpree.networking.messages.GameStateMessage.java
com.sillygames.killingSpree.networking.messages.PlayerNamesMessage.java
com.sillygames.killingSpree.networking.messages.ServerStatusMessage.java
com.sillygames.killingSpree.pool.AssetLoader.java
com.sillygames.killingSpree.pool.EntityPool.java
com.sillygames.killingSpree.pool.MessageObjectPool.java
com.sillygames.killingSpree.pool.Pool.java
com.sillygames.killingSpree.pool.Poolable.java
com.sillygames.killingSpree.renderers.HUDRenderer.java
com.sillygames.killingSpree.screens.AbstractScreen.java
com.sillygames.killingSpree.screens.ClientDiscoveryScreen.java
com.sillygames.killingSpree.screens.GameScreen.java
com.sillygames.killingSpree.screens.LobbyScreen.java
com.sillygames.killingSpree.screens.MainMenuScreen.java
com.sillygames.killingSpree.screens.OptionsScreen.java
com.sillygames.killingSpree.screens.SplashScreen.java
com.sillygames.killingSpree.screens.settings.Constants.java
com.sillygames.killingSpree.serverEntities.ServerArrow.java
com.sillygames.killingSpree.serverEntities.ServerBlob.java
com.sillygames.killingSpree.serverEntities.ServerBomb.java
com.sillygames.killingSpree.serverEntities.ServerBullet.java
com.sillygames.killingSpree.serverEntities.ServerEntity.java
com.sillygames.killingSpree.serverEntities.ServerFly.java
com.sillygames.killingSpree.serverEntities.ServerFrog.java
com.sillygames.killingSpree.serverEntities.ServerPlayer.java
com.sillygames.killingSpree.sound.SFXPlayer.java