private void HandleMessages() { while (true) { NetIncomingMessage msg; while ((msg = client.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryResponse: NetOutgoingMessage outTrans = client.CreateMessage(); outTrans.Write((Byte)PacketTypes.ConnectionRequest); client.Connect(msg.SenderEndPoint, outTrans); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Game1.multiplayerLobbyTest.text += msg.ReadString(); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { NetOutgoingMessage playerStatusMsg = client.CreateMessage(); playerStatusMsg.Write((Byte)PacketTypes.PlayerLogin); playerStatusMsg.Write(loggedInPlayer.playerName); client.SendMessage(playerStatusMsg, NetDeliveryMethod.ReliableOrdered); //Game1.multiplayerLobbyTest.text += "\nclient connected"; //Console.WriteLine(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " connected!"); } break; case NetIncomingMessageType.Data: //Game1.multiplayerLobbyTest.text += "\nReceiving incoming transmission."; switch (msg.ReadByte()) { case (Byte)PacketTypes.PlayerLogin: loggedInPlayer.playerID = msg.ReadInt32(); if (loggedInPlayer.playerID == 1) { loggedInPlayer.playerColor = Constant.player1Color; } else { loggedInPlayer.playerColor = Constant.player2Color; } Game1.multiplayerLobbyTest.text += "\nMe = " + loggedInPlayer.playerName + " " + loggedInPlayer.playerColor.ToString(); if (msg.ReadBoolean()) { int oppId = msg.ReadInt32(); String oppName = msg.ReadString(); opponentPlayer = new Player(oppName, oppId, (oppId == 1 ? Constant.player1Color : Constant.player2Color)); Game1.multiplayerLobbyTest.text += "\nOther = " + opponentPlayer.playerName + " " + opponentPlayer.playerID.ToString(); } break; case (Byte)PacketTypes.PlayerJoinedGameUpdate: int oppId2 = msg.ReadInt32(); String oppName2 = msg.ReadString(); opponentPlayer = new Player(oppName2, oppId2, (oppId2 == 1 ? Constant.player1Color : Constant.player2Color)); Game1.multiplayerLobbyTest.text += "\nOther = " + opponentPlayer.playerName + " " + opponentPlayer.playerID.ToString(); break; case (Byte)PacketTypes.DiceConfiguration: int playerID = msg.ReadInt32(); int diceCount = msg.ReadInt32(); Player ownedByPlayer = null; if (LoggedInPlayer.playerID == playerID) { ownedByPlayer = LoggedInPlayer; } else if (OpponentPlayer.playerID == playerID) { ownedByPlayer = OpponentPlayer; } for (int i = 0; i < diceCount; i++) { Diceman.Class diceClass = (Diceman.Class)msg.ReadByte(); int diceID = msg.ReadInt32(); float diceHp = msg.ReadFloat(); float diceStr = msg.ReadFloat(); float diceSpd = msg.ReadFloat(); Vector2 dicePos = msg.ReadVector2(); String diceAbility = msg.ReadString(); Diceman newDice = new Diceman(diceClass, ownedByPlayer, diceAbility, diceID); ownedByPlayer.ownedDicemen.Add(newDice); while (BattleBoard.board == null) { ; } newDice.Summon(dicePos, diceHp, diceStr, diceSpd); newDice.sprite.tintColor = ownedByPlayer.playerColor; } break; case (Byte)PacketTypes.GameStartSync: isGameStarted = true; break; case (Byte)PacketTypes.Disconnecting: StopClient(); break; case (Byte)PacketTypes.MovesList: Game1.multiplayerLobbyTest.text += "\nGot opponent moves list.!!!!!!!!!!!!!!!"; int count = msg.ReadInt32(); List <Diceman> diceInGame = new List <Diceman>(); diceInGame.AddRange(LoggedInPlayer.ownedDicemen); diceInGame.AddRange(OpponentPlayer.ownedDicemen); for (int i = 0; i < count; i++) { int selectedDiceID = msg.ReadInt32(); String selectedSpellName = msg.ReadString(); int targetedDiceID = -1; if (msg.ReadBoolean()) { targetedDiceID = msg.ReadInt32(); } Vector2 fireLoc = msg.ReadVector2(); Diceman selectedDice = null; Diceman targetedDice = null; foreach (Diceman dice in diceInGame) { if (dice.DiceID == selectedDiceID) { selectedDice = dice; } if (dice.DiceID == targetedDiceID) { targetedDice = dice; } } Ability selectedSpell = null; foreach (Ability spell in selectedDice.activeAbilities) { if (spell.name == selectedSpellName) { selectedSpell = spell; } } opponentInputs.Add(new AbilityInput(selectedDice, selectedSpell, targetedDice, fireLoc)); } break; default: break; } break; default: break; } } Thread.Sleep(1); } }
private void UpdateDiceConfiguration(NetIncomingMessage msg) { int playerID = msg.ReadInt32(); int diceCount = msg.ReadInt32(); NetOutgoingMessage outTrans = server.CreateMessage(); outTrans.Write((Byte)PacketTypes.DiceConfiguration); outTrans.Write(playerID); outTrans.Write(diceCount); for (int i = 0; i < diceCount; i++) { float randomDivider1 = RandomGenerator.RandomValue(0f, 10f); float randomDivider2 = RandomGenerator.RandomValue(0f, 10f); Diceman.Class diceClass = (Diceman.Class)msg.ReadByte(); float health = Constant.d_baseHealths[diceClass] + Math.Min(randomDivider1, randomDivider2); float strength = Constant.d_baseStrengths[diceClass] + Math.Max(randomDivider1, randomDivider2) - Math.Min(randomDivider1, randomDivider2); float speed = Constant.d_baseSpeeds[diceClass] + 10f - Math.Max(randomDivider1, randomDivider2); Vector2 dicePosition = Vector2.Zero; int startYIndex = 0; int endYIndex = (int)(0.6f * Constant.o_boardSize); if (playerID == 2) { startYIndex = (int)(0.4f * Constant.o_boardSize); endYIndex = Constant.o_boardSize; } bool positionAssigned = false; while (!positionAssigned) { positionAssigned = true; dicePosition.X = RandomGenerator.Random.Next(0, Constant.o_boardSize); dicePosition.Y = RandomGenerator.Random.Next(startYIndex, endYIndex); foreach (Vector2 vec in occupiedDicePositions) { if (dicePosition == vec) { positionAssigned = false; } } } String ability = ""; double randomNo = RandomGenerator.Random.NextDouble(); if (diceClass == Diceman.Class.Bruiser) { if (randomNo <= 0.33) { ability = Constant.a_bashName; } else if (randomNo <= 0.66) { ability = Constant.a_biteName; } else { ability = Constant.a_chargeName; } } else if (diceClass == Diceman.Class.Ranger) { if (randomNo <= 0.33) { ability = Constant.a_multishotName; } else if (randomNo <= 0.66) { ability = Constant.a_poisonAttackName; } else { ability = Constant.a_vaultName; } } else if (diceClass == Diceman.Class.Mage) { } occupiedDicePositions.Add(dicePosition); outTrans.Write((Byte)diceClass); outTrans.Write(idCount++); outTrans.Write(health); outTrans.Write(strength); outTrans.Write(speed); outTrans.Write(dicePosition); outTrans.Write(ability); } server.SendToAll(outTrans, NetDeliveryMethod.ReliableOrdered); }