Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
0
        //Set the resource's that the unit will collect from:
        public void SetTargetResource(Resource Target)
        {
            //if it's as single player game.
            if (GameManager.MultiplayerGame == false)
            {
                //directly send the unit to collect
                SetTargetResourceLocal(Target);
            }
            else
            {
                //in a case of a MP game
                //and the unit belongs to the local player:
                if (GameManager.Instance.IsLocalPlayer(gameObject))
                {
                    //ask the server to tell all clients at once that this unit is going to resource from this resource:

                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Resource;

                    NewInputAction.Source = gameObject;

                    NewInputAction.Target = Target.gameObject;

                    NewInputAction.InitialPos = transform.position;
                    NewInputAction.TargetPos  = Target.transform.position;

                    InputManager.SendInput(NewInputAction);
                }
            }
        }
Esempio n. 3
0
        public void LaunchAttack(Unit RefUnit, GameObject TargetObj, AttackModes AttackMode)
        {
            //if this is an online game
            if (GameManager.MultiplayerGame == true)
            {
                //and this is the owner of the unit
                if (GameMgr.IsLocalPlayer(RefUnit.gameObject) == true)
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Attack;

                    //source
                    NewInputAction.Source = RefUnit.gameObject;
                    NewInputAction.Target = TargetObj;

                    NewInputAction.InitialPos = RefUnit.transform.position;

                    NewInputAction.Value = (int)AttackMode;

                    //sent input
                    InputManager.SendInput(NewInputAction);
                }
            }
            else //single player game
            {
                //directly move the unit
                LaunchAttackLocal(RefUnit, TargetObj, AttackMode);
            }
        }
Esempio n. 4
0
        public void SetAttackTarget(GameObject Obj)
        {
            if (GameManager.MultiplayerGame == false)
            {
                SetAttackTargetLocal(Obj); //attack target settings
            }
            else
            {
                if (GameMgr.IsLocalPlayer(gameObject) == true && AttackerType == AttackerTypes.Building)
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Building;

                    NewInputAction.TargetMode = (byte)InputTargetMode.Attack; //initially, this is set to none

                    //source
                    NewInputAction.Source = gameObject;

                    //set the target:
                    NewInputAction.Target = Obj;

                    //sent input
                    InputManager.SendInput(NewInputAction);
                }
            }
        }
Esempio n. 5
0
        public void LaunchAttack(List <Unit> UnitsList, GameObject TargetObj, AttackModes AttackMode)
        {
            //if this is an online game
            if (GameManager.MultiplayerGame == true)
            {
                //and this is the owner of the unit
                if (GameMgr.IsLocalPlayer(UnitsList[0].gameObject) == true)
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Group;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Attack;

                    //source
                    NewInputAction.GroupSourceID = InputManager.UnitListToString(UnitsList);
                    NewInputAction.Target        = TargetObj;

                    NewInputAction.Value = (int)AttackMode;

                    //sent input
                    InputManager.SendInput(NewInputAction);
                }
            }
            else //single player game
            {
                //directly move the unit
                LaunchAttackLocal(UnitsList, TargetObj, AttackMode);
            }
        }
Esempio n. 6
0
        public void Move(Unit RefUnit, Vector3 Destination, float CircleRadius, GameObject TargetObj, InputTargetMode TargetMode)
        {
            //if this is an online game
            if (GameManager.MultiplayerGame == true)
            {
                //and this is the owner of the unit
                if (GameMgr.IsLocalPlayer(RefUnit.gameObject) == true)
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                    NewInputAction.TargetMode = (byte)TargetMode; //initially, this is set to none

                    //source
                    NewInputAction.Source = RefUnit.gameObject;
                    NewInputAction.Target = TargetObj;

                    NewInputAction.InitialPos = RefUnit.transform.position;
                    NewInputAction.TargetPos  = Destination;

                    NewInputAction.Value = (int)CircleRadius;

                    //sent input
                    InputManager.SendInput(NewInputAction);
                }
            }
            else //single player game
            {
                //directly move the unit
                MoveLocal(RefUnit, Destination, CircleRadius, TargetObj, TargetMode);
            }
        }
Esempio n. 7
0
        //a method to drop off units
        public void DropOffUnits(int ID)
        {
            if (GameManager.MultiplayerGame == true)
            { //if this is a MP game and it's the local player:
                if (GameMgr.IsLocalPlayer(gameObject))
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.CustomCommand;
                    NewInputAction.TargetMode = (byte)InputCustomMode.APCDrop;

                    NewInputAction.Source = gameObject;

                    NewInputAction.Value = ID;

                    InputManager.SendInput(NewInputAction);
                }
            }
            else
            {
                //offline game? update the attack type directly:
                DropOffUnitsLocal(ID);
            }
        }
Esempio n. 8
0
        //Set the building's that the unit will construct:
        public void SetTargetBuilding(Building Target)
        {
            //if it's as single player game.
            if (GameManager.MultiplayerGame == false)
            {
                //directly send the unit to build
                SetTargetBuildingLocal(Target);
            }
            else
            {
                //in a case of a MP game
                //and it's the unit belongs to the local player:
                if (GameManager.Instance.IsLocalPlayer(gameObject))
                {
                    Debug.Log("entered to mp");
                    //ask the server to tell all clients at once that this unit is going to construct a building.

                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Building;

                    NewInputAction.Source = gameObject;

                    NewInputAction.Target = Target.gameObject;

                    NewInputAction.InitialPos = transform.position;
                    NewInputAction.TargetPos  = Target.transform.position;

                    InputManager.SendInput(NewInputAction);
                }
            }
        }
Esempio n. 9
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);
                }
            }
        }
Esempio n. 10
0
        //this is the only way to communicate between objects and multiplayer faction managers.
        public static void SendInput(InputVars Input)
        {
            if (NetworkType == NetworkTypes.UNET) //if we're using UNET
            {
                if (UNET_Mgr != null)             //by checking if there's a valid UNET Faction Manager, we check if the multiplayer game is ready and that the faction has successfully initiliazed.
                {
                    //send the input to UNET after converting all attributes:

                    if (Input.Source) //if there's a source object
                    {
                        if (Input.SourceMode == (byte)InputSourceMode.Create)
                        { //if we're creating an object, then look in the spawnable prefabs list
                            Input.SourceID = InputManager.Instance.SpawnablePrefabs.IndexOf(Input.Source);
                        }
                        else
                        { //if not, check the spawned objects list
                            Input.SourceID = InputManager.Instance.SpawnedObjects.IndexOf(Input.Source);


                            //if the objects don't match:
                            if (InputManager.Instance.SpawnedObjects[Input.SourceID] != Input.Source)
                            {
                                //don't send the input message:
                                return;
                            }
                        }
                    }
                    else
                    {
                        Input.SourceID = -1;
                    }

                    //target
                    if (Input.Target)
                    {
                        Input.TargetID = InputManager.Instance.SpawnedObjects.IndexOf(Input.Target);

                        //if the objects don't match:
                        if (InputManager.Instance.SpawnedObjects[Input.TargetID] != Input.Target)
                        {
                            //don't send the input message:
                            return;
                        }
                    }
                    else
                    {
                        Input.TargetID = -1;
                    }

                    UNET_Mgr.CmdSendInput(Input.SourceMode, Input.TargetMode, Input.SourceID, Input.GroupSourceID, Input.TargetID, Input.InitialPos, Input.TargetPos, Input.Value, GameManager.PlayerFactionID);
                }
            }
            else
            {
                Debug.LogError("Invalid network type!");
            }
        }
Esempio n. 11
0
        //the method that allows us to place the building
        void PlaceBuilding()
        {
            if (GameManager.MultiplayerGame == false)               //if it's a single player game.

            {
                TakeBuildingResources(CurrentBuilding); //Remove the resources needed to create the building.

                CurrentBuilding.PlaceBuilding();        //place the building

                if (BuildingsInsideBorders == true)
                {
                    CurrentBuilding.CurrentCenter.RegisterBuildingInBorder(CurrentBuilding); //register building in the territory that it belongs to.
                }

                CurrentBuilding = null;
            }
            else                 //in case it's a multiplayer game:

            {
                TakeBuildingResources(CurrentBuilding); //Remove the resources needed to create the building.

                //ask the server to spawn the building for all clients:
                //send input action to the input manager
                InputVars NewInputAction = new InputVars();
                //mode:
                NewInputAction.SourceMode = (byte)InputSourceMode.Create;

                NewInputAction.Source     = AllBuildings[LastBuildingID].gameObject;
                NewInputAction.InitialPos = CurrentBuilding.transform.position;
                NewInputAction.Value      = 0;

                InputManager.SendInput(NewInputAction);

                Destroy(CurrentBuilding.gameObject);

                CurrentBuilding = null;
            }

            //Show the tasks panel after placing the building:
            AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, PlaceBuildingAudio, false);

            //if holding and spawning is enabled and the player is holding the right key to do that:
            if (HoldAndSpawn == true && Input.GetKey(HoldAndSpawnKey))
            {
                //start placing the same building again
                StartPlacingBuilding(LastBuildingID);
            }
            else
            {
                //Show the tasks again for the builders again:
                if (SelectionMgr.SelectedUnits.Count > 0)
                {
                    SelectionMgr.UIMgr.UpdateUnitTasks();
                }
            }
        }
Esempio n. 12
0
        public void LaunchAttack(List <Unit> UnitsList, GameObject TargetObj, AttackModes AttackMode)
        {
            //if this is an online game
            if (GameManager.MultiplayerGame == true)
            {
                Debug.Log("entered here1");

                //and this is the owner of the unit
                if (GameMgr.IsLocalPlayer(UnitsList[0].gameObject) == true)
                {
                    stats rpgPlayer = null;
                    if (TargetObj.GetComponent <SelectionObj>())
                    {
                        rpgPlayer = TargetObj.GetComponent <SelectionObj>().MainObj.GetComponent <stats>();
                    }
                    if (rpgPlayer)
                    {
                        if (UnitsList[0].team != rpgPlayer.Team && !rpgPlayer.DeadFlag)
                        {
                            Debug.Log("entered here3");
                            string unitIdString = InputManager.UnitListToString(UnitsList);
                            //  if (unitId >= 0)
                            //  {
                            InputManager.UNET_Mgr.CmdAttackRpgList(unitIdString, TargetObj.GetComponent <SelectionObj>().MainObj.GetComponent <stats>().Team);
                            // }
                        }
                    }
                    else
                    {
                        //send input action to the input manager
                        InputVars NewInputAction = new InputVars();
                        //mode:
                        NewInputAction.SourceMode = (byte)InputSourceMode.Group;
                        NewInputAction.TargetMode = (byte)InputTargetMode.Attack;

                        //source
                        NewInputAction.GroupSourceID = InputManager.UnitListToString(UnitsList);
                        NewInputAction.Target        = TargetObj;

                        NewInputAction.Value = (int)AttackMode;

                        //sent input
                        InputManager.SendInput(NewInputAction);
                    }
                }
            }
            else //single player game
            {
                //directly move the unit
                LaunchAttackLocal(UnitsList, TargetObj, AttackMode);
            }
        }
Esempio n. 13
0
        public void RpcOnCommandReceived(byte SourceMode, byte TargetMode, int SourceID, string GroupSourceID, int TargetID, Vector3 InitialPos, Vector3 TargetPos, int Value, int FactionID)
        {
            InputVars Command = new InputVars();

            Command.SourceMode    = SourceMode;
            Command.TargetMode    = TargetMode;
            Command.SourceID      = SourceID;
            Command.GroupSourceID = GroupSourceID;
            Command.TargetID      = TargetID;
            Command.InitialPos    = InitialPos;
            Command.TargetPos     = TargetPos;
            Command.Value         = Value;
            Command.FactionID     = FactionID;
            Command.Team          = GameMgr.Factions[FactionID].team;
            InputManager.Instance.LaunchCommand(Command);
        }
Esempio n. 14
0
        public void LaunchAttack(Unit RefUnit, GameObject TargetObj, AttackModes AttackMode)
        {
            //if this is an online game
            if (GameManager.MultiplayerGame == true)
            {
                //and this is the owner of the unit


                if (GameMgr.IsLocalPlayer(RefUnit.gameObject) == true)
                {
                    if (TargetObj.GetComponent <stats>())
                    {
                        if (!TargetObj.GetComponent <stats>().DeadFlag&& RefUnit.team != TargetObj.GetComponent <stats>().Team)
                        {
                            GameObject item   = RefUnit.gameObject;
                            int        UnitId = InputManager.Instance.SpawnedObjects.IndexOf(item);

                            InputManager.UNET_Mgr.CmdAttackRpg(UnitId, TargetObj.GetComponent <stats>().Team);
                        }
                    }
                    else
                    {
                        //send input action to the input manager
                        InputVars NewInputAction = new InputVars();
                        //mode:
                        NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                        NewInputAction.TargetMode = (byte)InputTargetMode.Attack;

                        //source
                        NewInputAction.Source = RefUnit.gameObject;
                        NewInputAction.Target = TargetObj;

                        NewInputAction.InitialPos = RefUnit.transform.position;

                        NewInputAction.Value = (int)AttackMode;

                        //sent input
                        InputManager.SendInput(NewInputAction);
                    }
                }
            }
            else //single player game
            {
                //directly move the unit
                LaunchAttackLocal(RefUnit, TargetObj, AttackMode);
            }
        }
Esempio n. 15
0
        //the method that allows us to place the building
        void PlaceBuilding()
        {
            TakeBuildingResources(CurrentBuilding); //Remove the resources needed to create the building.

            if (GameManager.MultiplayerGame == false)
            {                                                                                                                                                                       //if it's a single player game.
                BuildingManager.CreatePlacedInstance(AllBuildings[LastBuildingID], CurrentBuilding.transform.position, CurrentBuilding.CurrentCenter, GameManager.PlayerFactionID); //place the building
            }
            else
            { //in case it's a multiplayer game:
                //ask the server to spawn the building for all clients:
                //send input action to the input manager
                InputVars NewInputAction = new InputVars();
                //mode:
                NewInputAction.SourceMode = (byte)InputSourceMode.Create;

                NewInputAction.Source     = AllBuildings[LastBuildingID].gameObject;
                NewInputAction.InitialPos = CurrentBuilding.transform.position;
                NewInputAction.Target     = CurrentBuilding.CurrentCenter.gameObject;
                NewInputAction.Value      = 0;

                InputManager.SendInput(NewInputAction);
            }

            //remove instance that was being used to place building
            Destroy(CurrentBuilding.gameObject);
            CurrentBuilding = null;

            //Play building placement audio
            AudioManager.PlayAudio(GameMgr.GeneralAudioSource.gameObject, PlaceBuildingAudio, false);

            //if holding and spawning is enabled and the player is holding the right key to do that:
            if (HoldAndSpawn == true && Input.GetKey(HoldAndSpawnKey))
            {
                //start placing the same building again
                StartPlacingBuilding(LastBuildingID);
            }
            else
            {
                //Show the tasks again for the builders again:
                if (SelectionMgr.SelectedUnits.Count > 0)
                {
                    SelectionMgr.UIMgr.UpdateTaskPanel();
                }
            }
        }
Esempio n. 16
0
        public void CmdSendInput(byte SourceMode, byte TargetMode, int SourceID, string GroupSourceID, int TargetID, Vector3 InitialPos, Vector3 TargetPos, int Value, int FactionID)
        {
            InputVars Input = new InputVars();

            Input.SourceMode    = SourceMode;
            Input.TargetMode    = TargetMode;
            Input.SourceID      = SourceID;
            Input.GroupSourceID = GroupSourceID;
            Input.TargetID      = TargetID;
            Input.InitialPos    = InitialPos;
            Input.TargetPos     = TargetPos;
            Input.Value         = Value;
            Input.FactionID     = FactionID;

            //add this input command in the list:
            GameMgr.Factions[GameManager.MasterFactionID].MFactionMgr_UNET.InputActions.Add(Input);
        }
Esempio n. 17
0
        public void RpcOnCommandReceived(byte SourceMode, byte TargetMode, int SourceID, string GroupSourceID, int TargetID, Vector3 InitialPos, Vector3 TargetPos, int Value, int FactionID)
        {
            InputVars Command = new InputVars();

            Command.SourceMode    = SourceMode;
            Command.TargetMode    = TargetMode;
            Command.SourceID      = SourceID;
            Command.GroupSourceID = GroupSourceID;
            Command.TargetID      = TargetID;
            Command.InitialPos    = InitialPos;
            Command.TargetPos     = TargetPos;
            Command.Value         = Value;
            Command.FactionID     = FactionID;

            //ask the input manager to execute this command:
            InputManager.Instance.LaunchCommand(Command);
        }
Esempio n. 18
0
        //Sending unit to drop off resources:
        public void SendUnitToDropOffResources()
        {
            //if there's no drop off building
            if (DropOffBuilding == null)
            {
                FindClosetDropOffBuilding(); //try to find one:
            }

            if (DropOffBuilding != null)
            { //if there's a drop off building
              //stop the gathering audio:
                AudioManager.StopAudio(gameObject);

                //if we're using legacy mvt then set the drop off building as the drop pos, if not use the pre defined drop off pos
                Vector3 DropPos = (DropOffBuilding.DropOffPos == null) ? DropOffBuilding.transform.position : DropOffBuilding.DropOffPos.transform.position;
                float   Radius  = (DropOffBuilding.DropOffPos == null) ? DropOffBuilding.Radius : 0.0f;
                if (GameManager.MultiplayerGame == false)
                { //in case it's a singleplayer game:
                    MovementManager.Instance.MoveLocal(UnitMvt, DropPos, Radius, DropOffBuilding.gameObject, InputTargetMode.Building);
                }
                else
                {
                    if (GameManager.Instance.IsLocalPlayer(gameObject))
                    { //if local player
                      //send input action to the input manager
                        InputVars NewInputAction = new InputVars();
                        //mode:
                        NewInputAction.SourceMode = (byte)InputSourceMode.Unit;
                        NewInputAction.TargetMode = (byte)InputTargetMode.Resource;

                        NewInputAction.Source = gameObject;

                        NewInputAction.Target = DropOffBuilding.gameObject;

                        NewInputAction.InitialPos = transform.position;
                        NewInputAction.TargetPos  = DropPos;

                        InputManager.SendInput(NewInputAction);
                    }
                }
            }
        }
Esempio n. 19
0
        //called on the server when a player disconnects
        public override void OnLobbyServerDisconnect(NetworkConnection conn)
        {
            //checked defeated factions if we are inside a game:
            GameManager GameMgr = FindObjectOfType(typeof(GameManager)) as GameManager;

            if (GameMgr != null)
            {                       //if we're already in a game
                int FactionID = -1; //holds the faction ID of the client that disconnected:
                int i         = 0;

                while (i < GameMgr.Factions.Count && FactionID == -1) //go through all the factions if we still haven't found the disconnected faction ID yet
                {
                    //Check if this is client's faction by comparing the unique connection ID:
                    if (GameMgr.Factions[i].ConnID_UNET == conn.connectionId)
                    {
                        //this is the faction
                        FactionID = i;
                    }
                    i++;
                }

                if (FactionID != -1) //if a valid faction has been found
                {
                    //ask the server to announce that the faction has been defeated.
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();

                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Destroy;
                    NewInputAction.TargetMode = (byte)InputTargetMode.Faction;

                    //Faction ID that disconnected:
                    NewInputAction.Value = FactionID;

                    InputManager.SendInput(NewInputAction);
                }
            }

            base.OnLobbyServerDisconnect(conn);
        }
Esempio n. 20
0
        public void ToggleInvisibility()
        {
            if (GameManager.MultiplayerGame == true)
            { //if this is a MP game and it's the local player:
                if (GameManager.Instance.IsLocalPlayer(gameObject))
                {
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.CustomCommand;
                    NewInputAction.TargetMode = (byte)InputCustomMode.Invisibility;

                    NewInputAction.Source = gameObject;

                    InputManager.SendInput(NewInputAction);
                }
            }
            else
            {
                //offline game? update the attack type directly:
                ToggleInvisibilityLocal();
            }
        }
Esempio n. 21
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>());
                    }
                }
            }
        }
Esempio n. 22
0
        //a method called when a normal task (not an upgrade one) is complete:
        void OnTaskCompleted()
        {
            //If the first task in the queue is about creating units.
            if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.CreateUnit)
            {
                //Randomly pick a prefab to produce:
                Unit UnitPrefab = TasksList[TasksQueue[0].ID].UnitCreationSettings.Prefabs[Random.Range(0, TasksList[TasksQueue[0].ID].UnitCreationSettings.Prefabs.Length)];

                if (GameManager.MultiplayerGame == false) //if this is a single player game
                {
                    bool Cancel = false;
                    //if this is a NPC faction
                    if (GameManager.PlayerFactionID != FactionID)
                    {
                        //If the new unit is supposed to go contrusct a building or go collect resources:
                        //Check if there are places available to construct or collect the resource, if not, we'll cancel creating the unit
                        if (TasksQueue[0].TargetBuilding != null)
                        {
                            if (TasksQueue[0].TargetBuilding.WorkerMgr.CurrentWorkers == TasksQueue[0].TargetBuilding.WorkerMgr.WorkerPositions.Length)
                            {
                                CancelInProgressTask(0);
                                Cancel = true;
                            }
                        }
                        else if (TasksQueue[0].TargetResource != null)
                        {
                            if (TasksQueue[0].TargetResource.WorkerMgr.CurrentWorkers == TasksQueue[0].TargetResource.WorkerMgr.WorkerPositions.Length)
                            {
                                CancelInProgressTask(0);
                                Cancel = true;
                            }
                        }
                    }

                    if (Cancel == false) //if the task is not to be cancelled
                    {
                        Unit NewUnit = UnitManager.CreateUnit(UnitPrefab, GetSpawnPosition(), FactionID, RefBuilding);

                        //rallypoint for NPC players:
                        //if the new unit must construct a building, send the unit to build.
                        if (TasksQueue[0].TargetBuilding != null && NewUnit.GetComponent <Builder>())
                        {
                            NewUnit.GetComponent <Builder>().SetTargetBuilding(TasksQueue[0].TargetBuilding);
                        }
                        //if the new unit is entitled to collect a resource, send the unit to collect.
                        else if (TasksQueue[0].TargetResource != null && NewUnit.GetComponent <GatherResource>())
                        {
                            NewUnit.GetComponent <GatherResource>().SetTargetResource(TasksQueue[0].TargetResource);
                        }
                    }
                }
                else
                {
                    //if it's a MP game, then ask the server to spawn the unit.
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Create;

                    NewInputAction.Source = UnitPrefab.gameObject;
                    NewInputAction.Target = (TaskHolder == TaskHolders.Building) ? RefBuilding.gameObject : null;

                    NewInputAction.InitialPos = GetSpawnPosition();

                    InputManager.SendInput(NewInputAction);
                }
            }
            else if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.Research)
            {                                                  //if the tasks upgrades certain units' abilities:
                if (GameManager.MultiplayerGame == false)
                {                                              //if this an offline game:
                    LaunchResearchTaskLocal(TasksQueue[0].ID); //launch the task directly
                }
                else
                {
                    LaunchResearchTask(TasksQueue[0].ID);
                }
            }
            else if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.Destroy)
            { //if this task has a goal to self destroy the building.
                DestroyTaskHolder();
            }
        }
Esempio n. 23
0
        //a method called when a normal task (not an upgrade one) is complete:
        void OnTaskCompleted()
        {
            //If the first task in the queue is about creating units.
            if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.CreateUnit)
            {
                //Randomly pick a prefab to produce:
                Unit UnitPrefab = TasksList[TasksQueue[0].ID].UnitCreationSettings.Prefabs[Random.Range(0, TasksList[TasksQueue[0].ID].UnitCreationSettings.Prefabs.Length)];

                if (GameManager.MultiplayerGame == false) //if this is a single player game
                {
                    bool Cancel = false;
                    //if this is a NPC faction
                    if (GameManager.PlayerFactionID != FactionID)
                    {
                        //If the new unit is supposed to go contrusct a building or go collect resources:
                        //Check if there are places available to construct or collect the resource, if not, we'll cancel creating the unit
                        if (TasksQueue[0].TargetBuilding != null)
                        {
                            if (TasksQueue[0].TargetBuilding.WorkerMgr.CurrentWorkers == TasksQueue[0].TargetBuilding.WorkerMgr.WorkerPositions.Length)
                            {
                                CancelInProgressTask(0);
                                Cancel = true;
                            }
                        }
                        else if (TasksQueue[0].TargetResource != null)
                        {
                            if (TasksQueue[0].TargetResource.WorkerMgr.CurrentWorkers == TasksQueue[0].TargetResource.WorkerMgr.WorkerPositions.Length)
                            {
                                CancelInProgressTask(0);
                                Cancel = true;
                            }
                        }
                    }

                    if (Cancel == false) //if the task is not to be cancelled
                    {
                        // create the new unit object.
                        UnitPrefab.gameObject.GetComponent <NavMeshAgent>().enabled = false; //disable this component before spawning the unit as it might place the unit in an unwanted position when spawned
                        Unit NewUnit = Instantiate(UnitPrefab.gameObject, GetSpawnPosition(UnitPrefab.FlyingUnit), UnitPrefab.transform.rotation).GetComponent <Unit>();

                        NewUnit.NPCUnitSpawnerID = TasksQueue[0].UnitSpawnerID; //set the NPC unit spawner id for this unit
                        //set the unit faction ID.
                        NewUnit.FactionID = FactionID;
                        NewUnit.CreatedBy = (TaskHolder == TaskHolders.Building) ? RefBuilding : null; //only assign the created by attribute if the unit was created by a building

                        NewUnit.gameObject.GetComponent <NavMeshAgent>().enabled = true;               //enable the nav mesh agent component for the newly created unit

                        //rallypoint for NPC players:
                        //if the new unit must construct a building, send the unit to build.
                        if (TasksQueue[0].TargetBuilding != null && NewUnit.GetComponent <Builder>())
                        {
                            NewUnit.GetComponent <Builder>().SetTargetBuilding(TasksQueue[0].TargetBuilding);
                        }
                        //if the new unit is entitled to collect a resource, send the unit to collect.
                        else if (TasksQueue[0].TargetResource != null && NewUnit.GetComponent <GatherResource>())
                        {
                            NewUnit.GetComponent <GatherResource>().SetTargetResource(TasksQueue[0].TargetResource);
                        }

                        //if the new unit belongs to NPC army.
                        if (TasksQueue[0].TargetArmy != null)
                        {
                            if (TasksQueue[0].TargetArmy.ArmyUnits[TasksQueue[0].ID].ProgressAmount > 0)
                            {
                                //add the unit to the NPC army list.
                                TasksQueue[0].TargetArmy.ArmyUnits[TasksQueue[0].ID].CurrentUnits.Add(NewUnit);

                                NewUnit.GetComponent <Attack>().ArmyUnitID = TasksQueue[0].ID;
                                //Make all enemy units attack on range:
                                NewUnit.GetComponent <Attack>().AttackInRange = true;

                                TasksQueue[0].TargetArmy.ArmyUnits[TasksQueue[0].ID].ProgressAmount--;
                            }
                        }
                    }
                }
                else
                {
                    //if it's a MP game, then ask the server to spawn the unit.
                    //send input action to the input manager
                    InputVars NewInputAction = new InputVars();
                    //mode:
                    NewInputAction.SourceMode = (byte)InputSourceMode.Create;

                    NewInputAction.Source = UnitPrefab.gameObject;
                    NewInputAction.Target = (TaskHolder == TaskHolders.Building) ? RefBuilding.gameObject : null;

                    NewInputAction.InitialPos = GetSpawnPosition(UnitPrefab.FlyingUnit);

                    InputManager.SendInput(NewInputAction);
                }
            }
            else if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.Research)
            {                                                  //if the tasks upgrades certain units' abilities:
                if (GameManager.MultiplayerGame == false)
                {                                              //if this an offline game:
                    LaunchResearchTaskLocal(TasksQueue[0].ID); //launch the task directly
                }
                else
                {
                    LaunchResearchTask(TasksQueue[0].ID);
                }
            }
            else if (TasksList[TasksQueue[0].ID].TaskType == TaskManager.TaskTypes.Destroy)
            { //if this task has a goal to self destroy the building.
                DestroyTaskHolder();
            }
        }