Example #1
0
        public void DestroyResource(GatherResource Collector)
        {
            //if it's a single player game:
            if (GameManager.MultiplayerGame == false)
            {
                DestroyResourceLocal(Collector); //directly destroy the resource
            }
            //multiplayer game:
            else
            {
                //if this is the local player that is gathering the resource:
                if (GameManager.Instance.IsLocalPlayer(Collector.UnitMvt.gameObject))
                {
                    //destroy the resource
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Destroy;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Resource;

                    NewInputAction.Source = gameObject;
                    NewInputAction.Target = Collector.gameObject;

                    InputManager.SendInput(NewInputAction);
                }
            }
        }
Example #2
0
        //add a health to the building.
        public void AddResourceAmount(float Value, GatherResource Source)
        {
            //AddHealthLocal (Value, Source);

            if (GameManager.MultiplayerGame == false)
            {
                AddResourceAmountLocal(Value, Source);
            }
            else
            {
                if (GameManager.Instance.IsLocalPlayer(Source.gameObject)) //if it's the local player.
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Resource;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Self;

                    NewInputAction.Source = gameObject;
                    NewInputAction.Target = Source.gameObject;

                    NewInputAction.Value = (int)Value;

                    InputManager.SendInput(NewInputAction);
                }
            }
        }
Example #3
0
        public void AddResourceAmountLocal(float Value, GatherResource Source)
        {
            if (Infinite == false)
            {
                Amount += Mathf.FloorToInt(Value);
            }

            //if it's a multiplayer game and the unit belongs to the local player or a single player game.
            if (GameManager.MultiplayerGame == false || (GameManager.MultiplayerGame == true && Source.UnitMvt.FactionID == GameManager.PlayerFactionID))
            {
                if (ResourceMgr.AutoCollect == true)                                                   //if resources are automatically collected
                {
                    ResourceMgr.AddResource(Source.UnitMvt.FactionID, Name, -Mathf.FloorToInt(Value)); //then add the resource to the faction
                }
                else
                {
                    //if we need to drop off resources
                    if (Source.DropOff[ResourceID].CurrentQuantity >= Source.MaxHoldQuantity) //make sure that we have the max quantity.
                    {
                        Source.GoingToDropOffBuilding = false;                                //start as stating the unit is not going to the drop off building yet.
                        Source.DroppingOff            = true;                                 //but is actually at the phase at picking the drop off building.
                        Source.UnitMvt.SetAnimState(UnitAnimState.Idle);
                        Source.SendUnitToDropOffResources();                                  //send the player to drop off resources
                        AudioManager.StopAudio(gameObject);                                   //stop the collection audio.

                        //hide the collection obj:
                        if (Source.CurrentCollectionObj != null)
                        {
                            Source.CurrentCollectionObj.SetActive(false);
                        }
                        //activate the drop off object:
                        if (Source.DropOffObj != null)
                        {
                            Source.DropOffObj.SetActive(true);
                        }
                    }
                    else
                    {
                        Source.DropOff[ResourceID].CurrentQuantity += Mathf.FloorToInt(-Value); //if we haven't reached the max amount to drop off, keep collecting
                    }
                }

                if (Amount <= 0 && Infinite == false)   //if the resource is empty
                {
                    DestroyResource(Source);
                }
            }

            //If the target resource is the one that's selected:
            if (SelectionMgr.SelectedResource == this)
            {
                //Update the resource UI:

                SelectionMgr.UIMgr.UpdateResourceUI(this);
            }
        }
Example #4
0
        public void DestroyResource(GatherResource Source)
        {
            //custom event:
            Source.UnitMvt.GameMgr.Events.OnResourceEmpty(this);

            Source.UnitMvt.CancelCollecting();
            //stop collecting
            Source.UnitMvt.CancelCollecting();

            if (DestroyOnComplete == true)               //if the resource is meant to be destroyed when it has 0 amount
            {
                if (gameObject.GetComponent <Treasure> ())
                {
                    Treasure Treasure = gameObject.GetComponent <Treasure> ();
                    if (Treasure.Resources.Length > 0)
                    {
                        for (int i = 0; i < Treasure.Resources.Length; i++)                           //go through all the resources to reward
                        {
                            ResourceMgr.AddResource(Source.UnitMvt.FactionID, Treasure.Resources[i].Name, Treasure.Resources[i].Amount);
                        }

                        //play the claimed reward sound:
                        if (GameManager.PlayerFactionID == Source.UnitMvt.FactionID && Treasure.ClaimedAudio != null)                           //only if the unit is the local player's faction:
                        {
                            AudioManager.PlayAudio(Source.UnitMvt.GameMgr.GeneralAudioSource.gameObject, Treasure.ClaimedAudio, false);
                        }
                        //create the effect
                        if (Treasure.ClaimedEffect != null)
                        {
                            Instantiate(Treasure.ClaimedEffect, transform.position, Treasure.ClaimedEffect.transform.rotation);
                        }
                    }
                }

                //remove resource from list:
                GameManager.Instance.ResourceMgr.AllResources.Remove(this);

                //if it's a single player game:
                if (GameManager.MultiplayerGame == false)
                {
                    //automatically destroy the resource object.
                    Destroy(gameObject);
                }
                //multiplayer game:
                else
                {
                    //if this is the local player that is gathering the resource:
                    if (GameManager.PlayerFactionID == Source.UnitMvt.FactionID)
                    {
                        //ask the server to destroy it then:
                        Source.UnitMvt.MFactionMgr.TryToDestroyResource(gameObject.GetComponent <NetworkIdentity>().netId);
                    }
                }
            }
        }
Example #5
0
        public void DestroyResourceLocal(GatherResource Collector)
        {
            //custom event:
            if (GameManager.Instance.Events)
            {
                GameManager.Instance.Events.OnResourceEmpty(this);
            }

            //stop collecting
            Collector.UnitMvt.CancelCollecting();

            if (DestroyOnComplete == true)
            { //if the resource is meant to be destroyed when it has 0 amount
                if (gameObject.GetComponent <Treasure>())
                {
                    Treasure Treasure = gameObject.GetComponent <Treasure>();
                    if (Treasure.Resources.Length > 0)
                    {
                        for (int i = 0; i < Treasure.Resources.Length; i++)
                        { //go through all the resources to reward
                            ResourceMgr.AddResource(Collector.UnitMvt.FactionID, Treasure.Resources[i].Name, Treasure.Resources[i].Amount);
                        }

                        //play the claimed reward sound:
                        if (GameManager.PlayerFactionID == Collector.UnitMvt.FactionID && Treasure.ClaimedAudio != null)
                        { //only if the unit is the local player's faction:
                            AudioManager.PlayAudio(GameManager.Instance.GeneralAudioSource.gameObject, Treasure.ClaimedAudio, false);
                        }
                        //create the effect
                        if (Treasure.ClaimedEffect != null)
                        {
                            Instantiate(Treasure.ClaimedEffect, transform.position, Treasure.ClaimedEffect.transform.rotation);
                        }
                    }
                }

                //remove resource from list:
                GameManager.Instance.ResourceMgr.AllResources.Remove(this);

                //remove the resource minimap icon:
                MinimapIconManager.Instance.RemoveMinimapIcon(PlayerSelection);

                Destroy(gameObject);
            }
        }
Example #6
0
        //add a health to the building.
        public void AddResourceAmount(float Value, GatherResource Source)
        {
            //AddHealthLocal (Value, Source);

            if (GameManager.MultiplayerGame == false)
            {
                AddResourceAmountLocal(Value, Source);
            }
            else
            {
                if (GameManager.PlayerFactionID == Source.UnitMvt.FactionID)                //if it's the local player.
                {
                    //send input action to the MP faction manager if it's a MP game:
                    MFactionManager.InputActionsVars NewInputAction = new MFactionManager.InputActionsVars();
                    NewInputAction.Source           = gameObject.GetComponent <NetworkIdentity>().netId;
                    NewInputAction.Target           = Source.UnitMvt.netId;
                    NewInputAction.StoppingDistance = Value;

                    GameManager.Instance.Factions[Source.UnitMvt.FactionID].MFactionMgr.InputActions.Add(NewInputAction);
                }
            }
        }
Example #7
0
        public void LaunchCommand(InputVars Command)
        {
            float Value = (float)Command.Value;

            if (Command.SourceMode == (byte)InputSourceMode.FactionSpawn)
            { //spawning the faction capital building
                GameObject Capital = null;

                //search for the capital building to spawn for this faction
                if (GameMgr.Factions[Command.FactionID].TypeInfo != null) //valid faction type info is required
                {
                    if (GameMgr.Factions[Command.FactionID].TypeInfo.CapitalBuilding != null)
                    { //if the faction belongs to a certain type
                        //set the new capital building:
                        Capital = Instantiate(GameMgr.Factions[Command.FactionID].TypeInfo.CapitalBuilding.gameObject, Command.InitialPos, GameMgr.Factions[Command.FactionID].TypeInfo.CapitalBuilding.transform.rotation);
                    }
                }

                Capital.GetComponent <Building>().FactionID       = Command.FactionID;
                Capital.GetComponent <Building>().FactionCapital  = true;
                Capital.GetComponent <Building>().PlacedByDefault = true;

                InputManager.Instance.SpawnedObjects.Add(Capital); //add the new object to the list

                if (Command.FactionID == GameManager.MasterFactionID)
                { //if this is the master client
                  //spawn resources
                    if (GameMgr.ResourceMgr.AllResources.Count > 0)
                    {                                              //make sure there are resources to spawn
                        //add the scene resources to list.
                        GameMgr.ResourceMgr.RegisterResourcesMP(); //here resource objects will also be added to the spawn objects list
                    }
                    //register free units and buildings:
                    if (GameMgr.UnitMgr.FreeUnits.Length > 0)
                    {
                        //go through all free units
                        foreach (Unit FreeUnit in GameMgr.UnitMgr.FreeUnits)
                        {
                            //register them
                            InputManager.Instance.SpawnedObjects.Add(FreeUnit.gameObject);
                        }
                    }
                    if (GameMgr.UnitMgr.FreeUnits.Length > 0)
                    {
                        //go through all free buildings
                        foreach (Building FreeBuilding in GameMgr.BuildingMgr.FreeBuildings)
                        {
                            //register them
                            InputManager.Instance.SpawnedObjects.Add(FreeBuilding.gameObject);
                        }
                    }
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.Create)
            { //object creation
                GameObject NewObj = Instantiate(InputManager.Instance.SpawnablePrefabs[Command.SourceID], Command.InitialPos, InputManager.Instance.SpawnablePrefabs[Command.SourceID].transform.rotation);
                InputManager.Instance.SpawnedObjects.Add(NewObj);

                if (NewObj.gameObject.GetComponent <Unit>())
                { //if the new object is a unit
                  //set the faction ID:
                    NewObj.gameObject.GetComponent <Unit>().FactionID = Command.FactionID;
                    //set the creator:
                    if (Command.TargetID >= 0)
                    {
                        NewObj.gameObject.GetComponent <Unit>().CreatedBy = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject.GetComponent <Building>();
                    }
                    NewObj.gameObject.GetComponent <NavMeshAgent>().enabled = true; //enable the nav mesh agent component for the newly created unit
                }
                else if (NewObj.gameObject.GetComponent <Building>())
                {
                    //if the new obj is a building

                    /*
                     * 0 -> PlacedByDefault = false & Capital = false
                     * 1 -> PlacedByDefault = true & Capital = false
                     * 2 -> PlacedByDefault = false & Capital = true
                     * 3 -> PlacedByDefault = true & Capital = true
                     * */

                    bool PlacedByDefault = false;
                    bool Capital         = false;

                    if (Value == 1)
                    {
                        PlacedByDefault = true;
                    }
                    else if (Value == 2)
                    {
                        Capital = true;
                    }
                    else if (Value == 3)
                    {
                        PlacedByDefault = true;
                        Capital         = true;
                    }

                    //building settings
                    NewObj.GetComponent <Building>().FactionMgr      = GameMgr.Factions[Command.FactionID].FactionMgr;
                    NewObj.GetComponent <Building>().FactionID       = Command.FactionID;
                    NewObj.GetComponent <Building>().PlacedByDefault = PlacedByDefault;
                    NewObj.GetComponent <Building>().FactionCapital  = Capital;

                    //building is placed
                    NewObj.GetComponent <Building>().PlaceBuilding();
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.Destroy)
            {
                if (Command.TargetMode == (byte)InputTargetMode.None)
                { //this means we're destroying an object (unit, building or resource)
                    GameObject SourceObj = InputManager.Instance.SpawnedObjects[Command.SourceID];

                    //destroy the object:
                    if (SourceObj.gameObject.GetComponent <Unit>())
                    {
                        SourceObj.gameObject.GetComponent <Unit>().DestroyUnitLocal();
                    }
                    else if (SourceObj.gameObject.GetComponent <Building>())
                    {
                        SourceObj.gameObject.GetComponent <Building>().DestroyBuildingLocal(((int)Value == 1) ? true : false); // 1 means upgrade, 0 means completely destroy
                    }
                    else if (SourceObj.gameObject.GetComponent <Resource>())
                    {
                        SourceObj.gameObject.GetComponent <Resource>().DestroyResourceLocal(InputManager.Instance.SpawnedObjects[Command.TargetID].GetComponent <GatherResource>());
                    }

                    //Find an alternative
                    //InputManager.Instance.SpawnedObjects.RemoveAt(Command.SourceID); //remove the ojbect to destroy from the spawn objects list
                }
                else if (Command.TargetMode == (byte)InputTargetMode.Faction)
                { //and this means that a faction gets defeated
                    GameMgr.OnFactionDefeated(Command.Value);
                    GameMgr.UIMgr.ShowPlayerMessage(GameMgr.Factions[Command.Value].Name + " (Faction ID:" + Command.Value.ToString() + ") has been defeated.", UIManager.MessageTypes.Error);

                    //If this is the server:
                    if (GameManager.PlayerFactionID == GameManager.MasterFactionID)
                    {
                        int  i           = 0;
                        bool ClientFound = false;

                        //mark this faction as disconnected:
                        //go through all the client infos as long as the faction that disconnected or lost
                        while (i < UNET_Mgr.ClientsInfo.Count && ClientFound == false)
                        {
                            //if this faction is the one that disconncted
                            if (UNET_Mgr.ClientsInfo[i].FactionID == Command.Value)
                            {
                                UNET_Mgr.ClientsInfo[i].Disconneted = true; //mark the player as disconnected
                                //stop the while loop
                                ClientFound = true;

                                //if the game is frozen:
                                if (GameManager.GameState == GameStates.Frozen)
                                {
                                    //perform a sync test:
                                    UNET_Mgr.SyncTest();
                                }
                            }
                            i++;
                        }
                    }
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.CustomCommand)
            {
                if (GameMgr.Events)
                {
                    GameObject Source = null;
                    GameObject Target = null;

                    //get the source and target objects if they exist.
                    if (Command.SourceID >= 0)
                    {
                        Source = InputManager.Instance.SpawnedObjects[Command.SourceID];
                    }
                    if (Command.TargetID >= 0)
                    {
                        Target = InputManager.Instance.SpawnedObjects[Command.TargetID];
                    }

                    //Pre defined custom actions for some events that need to be synced between all clients
                    switch (Command.TargetMode)
                    {
                    //switching attack types:
                    case (byte)InputCustomMode.MultipleAttacks:
                        Source.GetComponent <Unit>().MultipleAttacksMgr.EnableAttackTypeLocal(Command.Value);
                        break;

                    //Converting a unit:
                    case (byte)InputCustomMode.Convert:
                        Source.GetComponent <Unit>().ConvertUnitLocal(Target.GetComponent <Unit>());
                        break;

                    //Toggling invisiblity:
                    case (byte)InputCustomMode.Invisibility:
                        Source.GetComponent <Unit>().InvisibilityMgr.ToggleInvisibility();
                        break;

                    //APC dropping units
                    case (byte)InputCustomMode.APCDrop:
                        Source.GetComponent <APC>().DropOffUnitsLocal(Command.Value);
                        break;

                    //Triggering a research task in a building:
                    case (byte)InputCustomMode.Research:
                        Source.GetComponent <TaskLauncher>().LaunchResearchTaskLocal(Command.Value);
                        break;

                    //Unit escaping
                    case (byte)InputCustomMode.UnitEscape:
                        Source.GetComponent <Unit>().EscapeLocal(Command.TargetPos);
                        break;

                    case (byte)InputCustomMode.Event:
                        GameMgr.Events.OnCustomCommand(Source, Target, Command.InitialPos, Command.TargetPos, Command.Value);
                        break;

                    default:
                        Debug.LogError("Invalid custom command target mode!");
                        break;
                    }
                }
            }
            //if a group of units is the source
            else if (Command.SourceMode == (byte)InputSourceMode.Group)
            {
                //get the units list
                List <Unit> UnitList = StringToUnitList(Command.GroupSourceID);
                //if there's units in the list:
                if (UnitList.Count > 0)
                {
                    //if the target mode is none:
                    if (Command.TargetMode == (byte)InputTargetMode.None)
                    {
                        //move units:
                        MovementManager.Instance.MoveLocal(UnitList, Command.TargetPos, Value, null, InputTargetMode.None);
                    }
                    //if the target mode is attack:
                    else if (Command.TargetMode == (byte)InputTargetMode.Attack)
                    {
                        //group attack:
                        MovementManager.Instance.LaunchAttackLocal(UnitList, InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject, (MovementManager.AttackModes)Value);
                    }
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.Unit)
            {
                //invalid source id?
                if (Command.SourceID < 0 || Command.SourceID >= InputManager.Instance.SpawnedObjects.Count)
                {
                    return; //do not proceed.
                }

                Unit SourceUnit = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Unit>();

                //see if we need to snap its position or not.
                if (Vector3.Distance(SourceUnit.transform.position, Command.InitialPos) > SnapDistance)
                {
                    SourceUnit.transform.position = Command.InitialPos;
                }

                if (Command.TargetMode == (byte)InputTargetMode.None)
                { //if there's no target
                  //move unit.
                    MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, null, InputTargetMode.None);
                }
                else
                { //if there's a target object:
                    GameObject TargetObj = null;

                    if (Command.TargetID >= 0 && Command.TargetID < InputManager.Instance.SpawnedObjects.Count)
                    {
                        TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj
                    }

                    if (Command.TargetMode == (byte)InputTargetMode.Self)
                    {                                                //if the target mode is self
                        SourceUnit.AddHealthLocal(Value, TargetObj); //update unit health
                    }
                    else if (TargetObj != null)
                    {
                        if (Command.TargetMode == (byte)InputTargetMode.Unit)
                        {     //if the target mode is a unit
                            if (TargetObj.GetComponent <Unit>().FactionID != SourceUnit.FactionID)
                            { //if the target unit is from another faction
                                if (SourceUnit.ConvertMgr)
                                {
                                    //convert the target unit.
                                    SourceUnit.ConvertMgr.SetTargetUnitLocal(TargetObj.GetComponent <Unit>());
                                }
                            }
                            else
                            { //if hte target unit belongs to the source unit's faction
                              //APC
                                if (TargetObj.GetComponent <APC>())
                                {
                                    SourceUnit.TargetAPC = TargetObj.GetComponent <APC>();
                                    MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Unit);
                                }
                                else if (SourceUnit.HealMgr != null)
                                { //healer:
                                    SourceUnit.HealMgr.SetTargetUnitLocal(TargetObj.GetComponent <Unit>());
                                }
                            }
                        }
                        else if (Command.TargetMode == (byte)InputTargetMode.Building)
                        {         //if the target mode is a building
                            if (TargetObj.GetComponent <Building>().FactionID == SourceUnit.FactionID)
                            {     //and it belongs to the source's faction
                                if (TargetObj.GetComponent <Building>().Health < TargetObj.GetComponent <Building>().MaxHealth)
                                { //if it doesn't have max health
                                  //construct building
                                    Builder BuilderComp = SourceUnit.gameObject.GetComponent <Builder>();

                                    BuilderComp.SetTargetBuildingLocal(TargetObj.GetComponent <Building>());
                                }
                                else if (TargetObj.GetComponent <APC>())
                                { //if target building is APC
                                    SourceUnit.TargetAPC = TargetObj.GetComponent <APC>();
                                    MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Building);
                                }
                            }
                        }
                        else if (Command.TargetMode == (byte)InputTargetMode.Resource)
                        { //if the target mode is a resource
                            GatherResource ResourceComp = SourceUnit.gameObject.GetComponent <GatherResource>();

                            if (TargetObj.GetComponent <Resource>())
                            { //if the target obj is a resource
                              //collect resources:
                                ResourceComp.SetTargetResourceLocal(TargetObj.GetComponent <Resource>());
                            }
                            //but if the target obj is a building
                            else if (TargetObj.GetComponent <Building>())
                            {
                                //send unit to the drop off building.
                                MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Building);
                            }
                        }
                        else if (Command.TargetMode == (byte)InputTargetMode.Portal)
                        { //if the target mode is a portal
                          //move unit to the portal
                            MovementManager.Instance.MoveLocal(SourceUnit, Command.TargetPos, Value, TargetObj, InputTargetMode.Portal);
                        }
                        else if (Command.TargetMode == (byte)InputTargetMode.Attack)
                        {
                            MovementManager.Instance.LaunchAttackLocal(SourceUnit, InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject, (MovementManager.AttackModes)Value);
                        }
                    }
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.Building)
            { //if the source mode is a building
                Building SourceBuilding = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Building>();
                if (SourceBuilding)
                {
                    if (Command.TargetMode != (byte)InputTargetMode.None)
                    {                                //if there's a target object:
                        GameObject TargetObj = null; //get the target obj
                        if (Command.TargetID >= 0)
                        {
                            TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj
                        }
                        if (Command.TargetMode == (byte)InputTargetMode.Self)
                        { //if the target mode is self = update health
                            SourceBuilding.AddHealthLocal(Value, TargetObj);
                        }
                        else if (Command.TargetMode == (byte)InputTargetMode.Attack && TargetObj != null)
                        { //if the target mode is attack = attack unit
                          //Attack.
                            SourceBuilding.AttackMgr.SetAttackTargetLocal(TargetObj);
                        }
                    }
                }
            }
            else if (Command.SourceMode == (byte)InputSourceMode.Resource)
            { //if the source mode is a resource
                Resource SourceResource = InputManager.Instance.SpawnedObjects[Command.SourceID].gameObject.GetComponent <Resource>();
                if (Command.TargetMode != (byte)InputTargetMode.None)
                {                                                                                                 //if there's a target object:
                    if (Command.TargetMode == (byte)InputTargetMode.Self)
                    {                                                                                             //if the target mode is self = update health
                        GameObject TargetObj = InputManager.Instance.SpawnedObjects[Command.TargetID].gameObject; //get the target obj
                        SourceResource.AddResourceAmountLocal(Value, TargetObj.GetComponent <GatherResource>());
                    }
                }
            }
        }
Example #8
0
        public void DestroyResourceLocal(GatherResource Collector)
        {
            //custom event:
            if (GameManager.Instance.Events)
            {
                GameManager.Instance.Events.OnResourceEmpty(this);
            }

            //stop collecting
            Collector.UnitMvt.CancelCollecting();

            if (DestroyOnComplete == true)
            { //if the resource is meant to be destroyed when it has 0 amount
                if (gameObject.GetComponent <Treasure>())
                {
                    Treasure Treasure = gameObject.GetComponent <Treasure>();
                    if (Treasure.Resources.Length > 0)
                    {
                        for (int i = 0; i < Treasure.Resources.Length; i++)
                        { //go through all the resources to reward
                            ResourceMgr.AddResource(Collector.UnitMvt.FactionID, Treasure.Resources[i].Name, Treasure.Resources[i].Amount);
                        }

                        //play the claimed reward sound:
                        if (GameManager.PlayerFactionID == Collector.UnitMvt.FactionID && Treasure.ClaimedAudio != null)
                        { //only if the unit is the local player's faction:
                            AudioManager.PlayAudio(GameManager.Instance.GeneralAudioSource.gameObject, Treasure.ClaimedAudio, false);
                        }
                        //create the effect
                        if (Treasure.ClaimedEffect != null)
                        {
                            Instantiate(Treasure.ClaimedEffect, transform.position, Treasure.ClaimedEffect.transform.rotation);
                        }
                    }
                }

                //remove resource from list:
                GameManager.Instance.ResourceMgr.AllResources.Remove(this);

                //remove the resource minimap icon:
                MinimapIconManager.Instance.RemoveMinimapIcon(PlayerSelection);

                //if this is a single player game
                if (GameManager.MultiplayerGame == false)
                {
                    //if this unit belongs to a NPC faction
                    if (Collector.UnitMvt.FactionID != GameManager.PlayerFactionID)
                    {
                        //if the targer resource is already inside the exploited resource lists of the NPC Resource mgr.
                        NPCResource NPCResourceMgr = GameManager.Instance.Factions[Collector.UnitMvt.FactionID].FactionMgr.ResourceMgr;
                        if (NPCResourceMgr.ExploitedResources[ResourceMgr.GetResourceID(Name)].Contains(this) == false)
                        {
                            //remove it
                            NPCResourceMgr.ExploitedResources[ResourceMgr.GetResourceID(Name)].Remove(this);
                            //trigger searching for resources:
                            NPCResourceMgr.CheckResources = true;
                        }
                    }
                }

                Destroy(gameObject);
            }
        }