Beispiel #1
0
        /// <summary>
        /// Spawns new unit and adds it to dictionary
        /// </summary>
        /// <param name="message"></param>
        private void SpawnUnit(MatchMessageUnitSpawned message)
        {
            Debug.Log("Spawned Unit Beg: " + message.UnitId);
            CardType    cardType = message.Card.GetCardInfo().CardType;
            Node        node     = GameManager.Instance.Nodes[message.NodeX, message.NodeY];
            PlayerColor player   = message.OwnerId == NakamaSessionManager.Instance.Session.UserId ? PlayerColor.Black : PlayerColor.Red;
            int         id       = message.UnitId;
            string      ownerId  = message.OwnerId;

            Quaternion rotation;

            if (MatchCommunicationManager.Instance.IsHost)
            {
                rotation = player == PlayerColor.Black ? Quaternion.Euler(0, 90, 0) : Quaternion.Euler(0, -90, 0);
            }
            else
            {
                rotation = player == PlayerColor.Black ? Quaternion.Euler(0, -90, 0) : Quaternion.Euler(0, 90, 0);
            }

            GameObject prefab = Resources.Load <GameObject>("Units/" + cardType.ToString() + "_" + player.ToString());

            Unit unit = Instantiate(prefab, node.transform.position, rotation, transform).GetComponent <Unit>();

            if (unit)
            {
                unit.Init(player, ownerId, id, node, message.Card);
                Debug.Log("Spawned Unit: " + id);
                _units[player].Add(id, unit);
                unit.OnDestroy += RemoveUnit;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Resolves card playing, then spawns unit of given type
        /// </summary>
        /// <param name="message"></param>
        private void CardPlayed(MatchMessageCardPlayed message)
        {
            if (MatchCommunicationManager.Instance.IsHost == true)
            {
                MatchMessageUnitSpawned unitSpawned = null;
                switch (message.Card.cardType)
                {
                case CardType.BigShip:
                case CardType.AoEShip:
                case CardType.Boats:
                    int         id    = _nextId + 1;
                    PlayerColor color = message.PlayerId == MatchCommunicationManager.Instance.HostId ? PlayerColor.Black : PlayerColor.Red;
                    unitSpawned = new MatchMessageUnitSpawned(message.PlayerId, color, id, message.Card, message.NodeX, message.NodeY);
                    break;

                default:
                    break;
                }

                if (unitSpawned != null)
                {
                    _nextId += 1;
                    MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, unitSpawned);
                    MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, unitSpawned);
                }
            }
        }
Beispiel #3
0
        /*
         * public void InitMap(Node[,] nodes, Vector2Int mapSize)
         * {
         *  Nodes = nodes;
         *  MapSize = mapSize;
         * }
         */

        private async Task SendMatchMessageUnitSpawnedAsync()
        {
            MatchMessageUnitSpawned message = new MatchMessageUnitSpawned("abc", 2, 3, 4);

            //string json = JsonUtility.ToJson(message, true);
            //string json = MatchMessage<T>.ToJson(message);
            MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, message);
        }
        /// <summary>
        /// Reads match messages sent by other players, and fires locally events basing on opCode.
        /// </summary>
        /// <param name="opCode"></param>
        /// <param name="messageJson"></param>
        public void ReceiveMatchStateHandle(long opCode, string messageJson)
        {
            if (GameStarted == false)
            {
                _incommingMessages.Enqueue(new IncommingMessageState(opCode, messageJson));
                return;
            }

            //Choosing which event should be invoked basing on opCode, then parsing json to MatchMessage class and firing event
            switch ((MatchMessageType)opCode)
            {
            //GAME
            case MatchMessageType.MatchEnded:
                MatchMessageGameEnded matchMessageGameEnded = MatchMessageGameEnded.Parse(messageJson);
                OnGameEnded?.Invoke(matchMessageGameEnded);
                break;

            //UNITS
            case MatchMessageType.UnitSpawned:
                MatchMessageUnitSpawned matchMessageUnitSpawned = MatchMessageUnitSpawned.Parse(messageJson);
                OnUnitSpawned?.Invoke(matchMessageUnitSpawned);
                break;

            case MatchMessageType.UnitMoved:
                MatchMessageUnitMoved matchMessageUnitMoved = MatchMessageUnitMoved.Parse(messageJson);
                OnUnitMoved?.Invoke(matchMessageUnitMoved);
                break;

            case MatchMessageType.UnitAttacked:
                MatchMessageUnitAttacked matchMessageUnitAttacked = MatchMessageUnitAttacked.Parse(messageJson);
                OnUnitAttacked?.Invoke(matchMessageUnitAttacked);
                break;

            //SPELLS
            case MatchMessageType.SpellActivated:
                MatchMessageSpellActivated matchMessageSpellActivated = MatchMessageSpellActivated.Parse(messageJson);
                OnSpellActivated?.Invoke(matchMessageSpellActivated);
                break;

            //CARDS
            case MatchMessageType.CardPlayRequest:
                if (IsHost == true)
                {
                    MatchMessageCardPlayRequest matchMessageCardPlayRequest = MatchMessageCardPlayRequest.Parse(messageJson);
                    OnCardRequested?.Invoke(matchMessageCardPlayRequest);
                }
                break;

            case MatchMessageType.CardPlayed:
                MatchMessageCardPlayed matchMessageCardPlayed = MatchMessageCardPlayed.Parse(messageJson);
                OnCardPlayed?.Invoke(matchMessageCardPlayed);
                break;


            case MatchMessageType.CardCanceled:
                MatchMessageCardCanceled matchMessageCardCancelled = MatchMessageCardCanceled.Parse(messageJson);
                OnCardCancelled?.Invoke(matchMessageCardCancelled);
                break;

            case MatchMessageType.StartingHand:
                MatchMessageStartingHand matchMessageStartingHand = MatchMessageStartingHand.Parse(messageJson);
                OnStartingHandReceived?.Invoke(matchMessageStartingHand);
                break;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Builds forts at the start of the game
        /// </summary>
        /// <param name="userId"></param>
        public void BuildStartingStructures(string userId)
        {
            if (MatchCommunicationManager.Instance.IsHost == false)
            {
                Debug.Log("Only host can spawn starting structures.");
                return;
            }

            Card fortCard = new Card()
            {
                cardType = CardType.Fort,
                level    = 1
            };
            Card mainFortCard = new Card()
            {
                cardType = CardType.MainFort,
                level    = 1
            };

            if (userId == MatchCommunicationManager.Instance.HostId)
            {
                MatchMessageUnitSpawned fortTop = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Black, _nextId++, fortCard, 4, 10);
                MatchMessageUnitSpawned fortBot = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Black, _nextId++, fortCard, 4, 2);
                MatchMessageUnitSpawned fortMain = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Black, _nextId++, mainFortCard, 2, 6);

                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortTop);
                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortBot);
                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortMain);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortTop);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortBot);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortMain);

                Unit tower2 = _units[PlayerColor.Black][_nextId - 3];
                Unit tower1 = _units[PlayerColor.Black][_nextId - 2];
                Unit castle = _units[PlayerColor.Black][_nextId - 1];
                GameManager.Instance.SetTowers(PlayerColor.Black, castle, tower1, tower2);
            }
            else
            {
                MatchMessageUnitSpawned fortTop = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Red, _nextId++, fortCard, 10, 10);
                MatchMessageUnitSpawned fortBot = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Red, _nextId++, fortCard, 10, 2);
                MatchMessageUnitSpawned fortMain = new MatchMessageUnitSpawned(
                    userId, PlayerColor.Red, _nextId++, mainFortCard, 12, 6);

                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortTop);
                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortBot);
                MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitSpawned, fortMain);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortTop);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortBot);
                MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitSpawned, fortMain);

                Unit tower2 = _units[PlayerColor.Red][_nextId - 3];
                Unit tower1 = _units[PlayerColor.Red][_nextId - 2];
                Unit castle = _units[PlayerColor.Red][_nextId - 1];
                GameManager.Instance.SetTowers(PlayerColor.Red, castle, tower1, tower2);
            }
        }