private static void HandleSpawnSnowball(SpawnSnowball spawn) { if (spawn == null) { throw new WrongPacketTypeException(); } Player player = World.Instance.GetPlayer(spawn.UserID); if (player.Carrying) { return; } GameObject snowball = GameObject.Instantiate(Prefabs.Instance.SnowballPrefab); snowball.transform.position = new Vector2(spawn.PosX, spawn.PosY); Vector2 angle = Quaternion.AngleAxis(spawn.Direction, Vector3.forward) * Vector3.down; Rigidbody2D rigidbody = snowball.GetComponent <Rigidbody2D>(); // Ignore collisions with player who threw it Rigidbody2D playerRigidbody = player.GetComponent <Rigidbody2D>(); // TODO: Make this a helper function in utilities Collider2D[] playerCols = new Collider2D[playerRigidbody.attachedColliderCount]; Collider2D[] snowCols = new Collider2D[rigidbody.attachedColliderCount]; rigidbody.GetAttachedColliders(snowCols); foreach (Collider2D snowCol in snowCols) { foreach (Collider2D playerCol in playerCols) { Physics2D.IgnoreCollision(snowCol, playerCol); } } rigidbody.velocity = angle * Snowball.Speed; int id = World.Instance.AddObject(snowball); snowball.GetComponent <Snowball>().Initialize(id, spawn.UserID); SnowballSync sync = new SnowballSync { ObjectID = id, PosX = spawn.PosX, PosY = spawn.PosY, Direction = spawn.Direction, Velocity = Snowball.Speed, }; Socket.Instance.SendPacket(sync, Packets.SnowballSync); }
private static void HandleSnowballSync(SnowballSync sync) { if (sync == null) { throw new WrongPacketTypeException(); } GameObject snowball = World.Instance.GetObject(sync.ObjectID); if (snowball == null) { // Instantiate a snowball snowball = GameObject.Instantiate(Prefabs.Instance.SnowballPrefab); World.Instance.AddObject(snowball, sync.ObjectID); } snowball.transform.position = new Vector2(sync.PosX, sync.PosY); Vector2 angle = Quaternion.AngleAxis(sync.Direction, Vector3.forward) * Vector3.down; snowball.GetComponent <Rigidbody2D>().velocity = angle * sync.Velocity; }
public static void ReadMessage(byte[] message) { Envelope envelope = SerializationHandler.Deserialize <Envelope>(message); if (envelope == null) { throw new NotAnEnvelopeException(); } switch (envelope.PacketType) { case Packets.None: Debug.Log("None"); break; case Packets.String: string stringVal = SerializationHandler.Deserialize <string>(envelope.Packet); Debug.Log(stringVal); break; case Packets.AckLogin: AckLogin ackLogin = SerializationHandler.Deserialize <AckLogin>(envelope.Packet); HandleAckLogin(ackLogin); break; case Packets.AckJoinTeam: AckJoinTeam ackJoinTeam = SerializationHandler.Deserialize <AckJoinTeam>(envelope.Packet); HandleAckJoinTeam(ackJoinTeam); break; case Packets.DestroyObject: DestroyObject destroyObject = SerializationHandler.Deserialize <DestroyObject>(envelope.Packet); HandleDestroyObject(destroyObject); break; case Packets.PlayerSync: PlayerSync playerSync = SerializationHandler.Deserialize <PlayerSync>(envelope.Packet); HandlePlayerSync(playerSync); break; case Packets.SnowballSync: SnowballSync snowballSync = SerializationHandler.Deserialize <SnowballSync>(envelope.Packet); HandleSnowballSync(snowballSync); break; case Packets.SnowPileSync: SnowPileSync snowPileSync = SerializationHandler.Deserialize <SnowPileSync>(envelope.Packet); HandleSnowPileSync(snowPileSync); break; case Packets.LeaderboardData: LeaderboardDataEntry leaderboardDataResponse = SerializationHandler.Deserialize <LeaderboardDataEntry>(envelope.Packet); HandleLeaderboardDataEntry(leaderboardDataResponse); break; case Packets.EndLeaderboardResponse: HandleEndLeaderboardResponse(); break; case Packets.AckConnection: HandleAckConnection(); break; default: break; } }