/** * Copyright 2019 The Knights Of Unity, created by Piotr Stoch * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Collections.Generic; using DemoGame.Scripts.Gameplay.Cards; using DemoGame.Scripts.Gameplay.NetworkCommunication; using DemoGame.Scripts.Gameplay.NetworkCommunication.MatchStates; using DemoGame.Scripts.Gameplay.Units; using UnityEngine; namespace DemoGame.Scripts.Gameplay.Spells { /// /// Manager resolving spells activation. Contains also object pooling mechanic for spells. /// public class SpellsManager : MonoBehaviour { /// /// List of disabled spells objects /// private List _nonactiveSpells = new List(); /// /// List of active spells objects /// private List _activeSpells = new List(); private void Start() { MatchCommunicationManager.Instance.OnSpellActivated += ResolveSpellActivation; MatchCommunicationManager.Instance.OnCardPlayed += ActivateSpell; } /// /// Activates spell object of type given in message on node given in message /// /// private void ActivateSpell(MatchMessageCardPlayed message) { if (message.Card.cardType == CardType.Fireball) { GetNonactiveSpellOfType(SpellType.Fireball).Activate(GameManager.Instance.Nodes[message.NodeX, message.NodeY], message.PlayerId); } } /// /// Resolves spell activation on given node /// /// private void ResolveSpellActivation(MatchMessageSpellActivated message) { ISpell spell = GetNonactiveSpellOfType(message.SpellType); _nonactiveSpells.Remove(spell); _activeSpells.Add(spell); spell.OnHide += HideSpell; spell.Show(GameManager.Instance.Nodes[message.NodeX, message.NodeY]); List impactedUnits = new List(); foreach (int unitId in message.ImpactedUnitsIds) { impactedUnits.Add(UnitsManager.Instance.GetUnit(unitId)); } spell.MakeImpactOnUnits(impactedUnits); } /// /// Gets disabled spell object or spawn one if could not find any /// /// /// private ISpell GetNonactiveSpellOfType(SpellType spellType) { ISpell spell = _nonactiveSpells.Find(s => s.SpellType == spellType); if (spell == null) { GameObject prefab = Resources.Load("Spells/" + spellType.ToString()); spell = Instantiate(prefab, transform).GetComponent(); _nonactiveSpells.Add(spell); } return spell; } /// /// Hides spell object and adds it to nonactive spells list /// /// private void HideSpell(ISpell spell) { _activeSpells.Remove(spell); _nonactiveSpells.Add(spell); } } }