Android Open Source - hackfmi-ragdoll-physics Base Strategy






From Project

Back to project page hackfmi-ragdoll-physics.

License

The source code is released under:

GNU General Public License

If you think the Android project hackfmi-ragdoll-physics 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.midtownmadness.bubblecombat.multiplay;
//  w w w . ja v a  2  s  . com
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

public abstract class BaseStrategy implements MultiplayStrategy {

  private static final String TAG = BaseStrategy.class.getSimpleName();
  private Map<Integer, BluetoothSocket> connectPlayers = new HashMap<Integer, BluetoothSocket>();
  private LooperThread looper;
  protected Context context;
  private Handler uiHandler = new Handler(Looper.getMainLooper());
  protected MultiplayManager manager;

  private Queue<MultiplayEvent> queue = new ConcurrentLinkedQueue<MultiplayEvent>();

  protected BluetoothSocket otherPlayer;
  protected volatile boolean stop;

  public BaseStrategy(Context context, MultiplayManager manager) {
    this.context = context;
    this.manager = manager;
    this.looper = new LooperThread();
    this.looper.setHandlerReadyListener(new Callback<Void>() {

      @Override
      public void call(Void argument) {
        BaseStrategy.this.onLooperReady();
      }
    });
  }

  public void onPlayerConnected(final int id, final BluetoothSocket socket) {
    connectPlayers.put(id, socket);
  }

  protected void sendMessage(Object payload, MessageType type,
      BluetoothSocket... players) {
    BluetoothMessage message = new BluetoothMessage(type, payload);
    // toast("Sending message" + message.toString());
    for (BluetoothSocket playerSocket : players) {
      if (players == null) {
        Log.d(TAG, "ERROR!: ATTEMPTED TO FIND PLAYER WITH id "
            + playerSocket
            + " => NOT PRESENT IN MULTIPLAY MANAGER!");
        continue;
      }

      try {
        playerSocket.getOutputStream().write(message.toBytes());
        playerSocket.getOutputStream().flush();
        // toast("Written to " + playerSocket.toString() +
        // " and flushed");
      } catch (IOException e) {
        e.printStackTrace();
      }

    }
  }

  protected void sendEmptyMessage(MessageType type,
      BluetoothSocket... sockets) {
    sendMessage(new String(), type, sockets);
  }

  @Override
  public void start() {
    looper.start();
  }

  public Handler getHandler() {
    return looper.getHandler();
  }

  public LooperThread getThread() {
    return looper;
  }

  public Map<Integer, BluetoothSocket> getConnectedPlayers() {
    return connectPlayers;
  }

  public abstract void onLooperReady();

  protected BluetoothMessage obtainMessage(BluetoothSocket otherPlayer2) {
    BluetoothMessage message = new BluetoothMessage(MessageType.ERROR, null);
    try {
      ObjectInputStream inputStream = new ObjectInputStream(
          otherPlayer2.getInputStream());
      int messageTypeOrdinal = inputStream.readInt();
      MessageType type = MessageType.getByOrdinal(messageTypeOrdinal);
      Object payload = inputStream.readObject();
      message = new BluetoothMessage(type, payload);
    } catch (StreamCorruptedException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } finally {
      return message;
    }

  }

  public void toast(final String text) {
    uiHandler.post(new Runnable() {

      @Override
      public void run() {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();

      }
    });
  }

  @Override
  public void action() {
    getHandler().post(new Runnable() {

      @Override
      public void run() {
        while (!stop) {
          // write one
          if (!queue.isEmpty()) {
            sendMessage(queue.poll(), MessageType.EVENT,
                otherPlayer);
          }

          // read one
          try {
            if (otherPlayer.getInputStream().available() > 0) {
              BluetoothMessage message = obtainMessage(otherPlayer);
              manager.onMultiplayEvent((MultiplayEvent) message.payload);
            }
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    });
  }

  public void endGame() {
    stop = true;
  }

  @Override
  public void add(MultiplayEvent event) {
    queue.offer(event);
  }
}




Java Source Code List

com.midtownmadness.bubblecombar.listeners.GameRoomListener.java
com.midtownmadness.bubblecombar.model.GameModel.java
com.midtownmadness.bubblecombar.model.GamesAdapter.java
com.midtownmadness.bubblecombar.model.MockAdapter.java
com.midtownmadness.bubblecombat.BaseActivity.java
com.midtownmadness.bubblecombat.BluetoothGamesAdapter.java
com.midtownmadness.bubblecombat.GameActivity.java
com.midtownmadness.bubblecombat.GameView.java
com.midtownmadness.bubblecombat.GlobalContext.java
com.midtownmadness.bubblecombat.MenuActivity.java
com.midtownmadness.bubblecombat.MockMultiplayerGame.java
com.midtownmadness.bubblecombat.Settings.java
com.midtownmadness.bubblecombat.game.DrawThread.java
com.midtownmadness.bubblecombat.game.GameObject.java
com.midtownmadness.bubblecombat.game.GameWallObject.java
com.midtownmadness.bubblecombat.game.LevelObject.java
com.midtownmadness.bubblecombat.game.PlayerObject.java
com.midtownmadness.bubblecombat.multiplay.BaseStrategy.java
com.midtownmadness.bubblecombat.multiplay.BluetoothConnectException.java
com.midtownmadness.bubblecombat.multiplay.BluetoothMessage.java
com.midtownmadness.bubblecombat.multiplay.Callback.java
com.midtownmadness.bubblecombat.multiplay.ClientStrategy.java
com.midtownmadness.bubblecombat.multiplay.HostStrategy.java
com.midtownmadness.bubblecombat.multiplay.LoggingListener.java
com.midtownmadness.bubblecombat.multiplay.LooperThread.java
com.midtownmadness.bubblecombat.multiplay.MessageType.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEventListener.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEvent.java
com.midtownmadness.bubblecombat.multiplay.MultiplayManager.java
com.midtownmadness.bubblecombat.multiplay.MultiplayStrategy.java
com.midtownmadness.bubblecombat.multiplay.MultiplayUtil.java
com.midtownmadness.bubblecombat.multiplay.MultiplayerGame.java
com.midtownmadness.bubblecombat.multiplay.commobjects.GoMessageObject.java
com.midtownmadness.bubblecombat.physics.BodyCreationRequest.java
com.midtownmadness.bubblecombat.physics.BodyUserData.java
com.midtownmadness.bubblecombat.physics.CentralHurdle.java
com.midtownmadness.bubblecombat.physics.CollisionListener.java
com.midtownmadness.bubblecombat.physics.DefaultLevelBuilder.java
com.midtownmadness.bubblecombat.physics.LevelBuilder.java
com.midtownmadness.bubblecombat.physics.MovementRequest.java
com.midtownmadness.bubblecombat.physics.MovementStateRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsService.java
com.midtownmadness.bubblecombat.views.MenuGameView.java
com.midtownmadness.bubblecombat.views.MenuView.java