void OnPlayerMove(string data) { //print("WebSocketManager OnPlayerMove: START"); //print("WebSocketManager OnPlayerMove: Received string: " + data); UserJson userJSON = UserJson.CreateFromJson(data); // if it is the current player, exit if (userJSON.name == playerNameStr) { return; } GameObject p = GameObject.Find(userJSON.name) as GameObject; if (p != null) { //print("Got userJSON: " + userJSON); Vector3 position = new Vector3(userJSON.position[0], userJSON.position[1], userJSON.position[2]); Vector2 velocity = new Vector2(userJSON.velocity[0], userJSON.velocity[1]); Vector2 acceleration = new Vector2(userJSON.acceleration[0], userJSON.acceleration[1]); Quaternion rotation = Quaternion.Euler(userJSON.rotation[0], userJSON.rotation[1], userJSON.rotation[2]); CarController carController = p.GetComponent <CarController>(); carController.updateDestination(position, velocity, acceleration, rotation); // Send back an ack to the player that sent this message to get a RTT estimate simStepAckJson simStepAck = new simStepAckJson(userJSON.simulationStep, userJSON.name); Dispatch("ackMessage", JsonUtility.ToJson(simStepAck), true); } }
void OnWeaponRotateAndFire(string data) { print("Player weapon rotated and possibly fired"); UserJson userJson = UserJson.CreateFromJson(data); // todo weapon rotates and fires (true/false), use or rework BulletJson? }
/** * This region contains all the action */ #region Listening void OnOtherPlayerConnected(string data) { print("Another player joined Angry Alex."); UserJson userJson = UserJson.CreateFromJson(data); // Check to make sure we're not adding ourselves if (userJson.name != playerNameStr) { print("Player name: " + userJson.name); // Check to see if we don't already have this player in the game, return if we do GameObject obj = GameObject.Find(userJson.name) as GameObject; if (obj != null) { print("Found reference to obj associated with " + userJson.name + ", ending OnOtherPlayerConnected"); return; } // Get position and rotation for new player Vector3 position = new Vector3(userJson.position[0], userJson.position[1], userJson.position[2]); Quaternion rotation = Quaternion.Euler(userJson.rotation[0], userJson.rotation[1], userJson.rotation[2]); // Assign vehicle and weapon int vehicle = userJson.vehicleSelection[0] > 0 ? userJson.vehicleSelection[0] : 1; int weapon = userJson.vehicleSelection[1] > 0 ? userJson.vehicleSelection[1] : 1; print("Other player selection: " + vehicle + " " + weapon); // Get a vehicle/weapon combo object to assign to our new player player = (GameObject)Resources.Load(_vehicleWeaponNames[new Tuple <int, int>(vehicle, weapon)]); player.name = userJson.name; //player.tag = "LocalPlayer"; //base.LocalPlayer = player; // Instantiate player object at position and rotation, assign name to vehicle and gun objects GameObject p = Instantiate(player, position, rotation) as GameObject; p.name = userJson.name; Weapon gun = p.GetComponent <Weapon>(); gun.playerName = userJson.name; print("Weapon player name: " + gun.playerName + " for player " + userJson.name); // Add health bar to vehicle HealthBar hb = p.GetComponent <HealthBar>(); hb.carObject = p; hb.m_Slider_self = healthSlider; hb.m_Fill_self = healthFill; hb.playerName = userJson.name; print("Health bar player name: " + hb.playerName + " for player " + userJson.name); // Set leaderboard for new player print("About to update leaderboard for player " + userJson.name); //leaderboardManager.ChangeScore(userJson.name, "kills", userJson.killCount); print("Leaderboard updated for player " + userJson.name); } }
void OnAckMessage(string data) { print("WebSocketManager OnAckMessage: START"); UserJson userJSON = UserJson.CreateFromJson(data); // We only care to continue if this is the current player if (userJSON.name == playerNameStr) { GameObject p = GameObject.Find(userJSON.name) as GameObject; if (p != null) { print("Got userJSON: " + userJSON); CarController carController = p.GetComponent <CarController>(); carController.processAcknowledgement(userJSON.simulationStep); } return; } }
private void OnFire(string data) { UserJson userJSON = UserJson.CreateFromJson(data); // if it is the current player exit if (userJSON.name == playerNameStr) { return; } Quaternion rotation = Quaternion.Euler(userJSON.weapon.rotation[0], userJSON.weapon.rotation[1], userJSON.weapon.rotation[2]); GameObject p = GameObject.Find(userJSON.name) as GameObject; if (p != null) { Weapon weapon = p.GetComponentInChildren <Weapon>(); weapon.transform.rotation = rotation; weapon.fireWeapon(); } }
void OnCollision(string data) { print("WebSocketManager OnCollision: START, data:\n" + data); UserJson userJSON = UserJson.CreateFromJson(data); // We only care to continue if this is the remote player if (userJSON.name != playerNameStr) { GameObject p = GameObject.Find(userJSON.name) as GameObject; if (p != null) { print("Got userJSON: " + userJSON); // Set remote car player's position Vector3 position = new Vector3(userJSON.position[0], userJSON.position[1], userJSON.position[2]); Vector2 velocity = new Vector2(userJSON.velocity[0], userJSON.velocity[1]); Vector2 acceleration = new Vector2(userJSON.acceleration[0], userJSON.acceleration[1]); Quaternion rotation = Quaternion.Euler(userJSON.rotation[0], userJSON.rotation[1], userJSON.rotation[2]); CarController carController = p.GetComponent <CarController>(); carController.setCollisionPosVelAccRot(position, velocity, acceleration, rotation); // Set ourselves back by RTT/2 and wait RTT/2 before resuming simulation GameObject selfObj = GameObject.Find(playerNameStr) as GameObject; CarController selfCarController = selfObj.GetComponent <CarController>(); if (selfCarController.inCollision) // If we are already in a collision, we can resolve now { print("We were already in collision, set lockStep to false and startBlendCollision"); selfCarController.lockStep = false; selfCarController.startBlendCollision(); } else // Otherwise, we need to go back half a RTT and see where we were when the other player said we collided { print("We were not aware of a collision, sending new collision with prior position"); selfCarController.inCollision = true; int updateIndex = selfCarController.currentSimulationStep - selfCarController.rtt; CarController.PositionVelocityAccelerationRotationJson updateAtCollision = selfCarController.updateBuffer[updateIndex]; Dispatch("collision", JsonUtility.ToJson(updateAtCollision), true); } } return; } }
void OnCollisionAck(string data) { print("WebSocketManager OnCollisionAck: START, data:\n" + data); UserJson userJSON = UserJson.CreateFromJson(data); // We only care to continue if this is the remote player if (userJSON.name != playerNameStr) { GameObject p = GameObject.Find(userJSON.name) as GameObject; if (p != null) { print("Got userJSON: " + userJSON); // Set remote car player's position Vector3 position = new Vector3(userJSON.position[0], userJSON.position[1], userJSON.position[2]); Vector2 velocity = new Vector2(userJSON.velocity[0], userJSON.velocity[1]); Vector2 acceleration = new Vector2(userJSON.acceleration[0], userJSON.acceleration[1]); Quaternion rotation = Quaternion.Euler(userJSON.rotation[0], userJSON.rotation[1], userJSON.rotation[2]); CarController carController = p.GetComponent <CarController>(); carController.setCollisionPosVelAccRot(position, velocity, acceleration, rotation); // Set ourselves back by RTT/2 and wait RTT/2 before resuming simulation } return; } }