/// <summary> /// Sends attack request to other players and self. /// Use only on host! /// </summary> protected virtual void SendAttackRequest(Unit enemy, int damage, AttackType attackType) { MatchMessageUnitAttacked message = new MatchMessageUnitAttacked(_unit.Id, _unit.OwnerId, enemy.Id, _unit.Damage, _unit.AttackType); MatchCommunicationManager.Instance.SendMatchStateMessage(MatchMessageType.UnitAttacked, message); MatchCommunicationManager.Instance.SendMatchStateMessageSelf(MatchMessageType.UnitAttacked, message); }
/// <summary> /// Order unit to show attack given in the match state message /// </summary> /// <param name="message"></param> private void MakeUnitAttack(MatchMessageUnitAttacked message) { PlayerColor color = message.OwnerId == NakamaSessionManager.Instance.Session.UserId ? PlayerColor.Black : PlayerColor.Red; PlayerColor enemyColor = color == PlayerColor.Black ? PlayerColor.Red : PlayerColor.Black; Unit unit = _units[color][message.UnitId]; Unit enemy = _units[enemyColor][message.EnemyId]; unit.Attack(enemy, message.Damage, message.AttackType); }
/// <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; } }