private static void DestroyAgentBuffer(DeactivationData data)
        {
            RTSAgent agent = data.Agent;

            if (agent.IsActive == false)
            {
                return;
            }
            bool immediate = data.Immediate;

            agent.Deactivate(immediate);
            ChangeController(agent, null);

            //Pool if the agent is registered
            ushort agentCodeID;

            if (agent.TypeIndex != UNREGISTERED_TYPE_INDEX)
            {
                // if (CodeIndexMap.TryGetValue(agent.MyAgentCode, out agentCodeID))
                // {
                agentCodeID = GameResourceManager.GetAgentCodeIndex(agent.MyAgentCode);
                if (agentCodeID.IsNotNull())
                {
                    TypeAgentsActive[agentCodeID][agent.TypeIndex] = false;
                }
            }
        }
        /// <summary>
        /// Create an uninitialized RTSAgent
        /// </summary>
        /// <returns>The raw agent.</returns>
        /// <param name="agentCode">Agent code.</param>
        /// <param name="isBare">If set to <c>true</c> is bare.</param>
        public static RTSAgent CreateRawAgent(string agentCode, Vector2d startPosition = default(Vector2d), Vector2d startRotation = default(Vector2d))
        {
            if (!GameResourceManager.IsValidAgentCode(agentCode))
            {
                throw new System.ArgumentException(string.Format("Agent code '{0}' not found.", agentCode));
            }
            FastStack <RTSAgent> cache    = CachedAgents[agentCode];
            RTSAgent             curAgent = null;

            if (cache.IsNotNull() && cache.Count > 0)
            {
                curAgent = cache.Pop();
                ushort agentCodeID = GameResourceManager.GetAgentCodeIndex(agentCode);
                Debug.Log(curAgent.TypeIndex);
                TypeAgentsActive[agentCodeID][curAgent.TypeIndex] = true;
            }
            else
            {
                IAgentData interfacer = GameResourceManager.AgentCodeInterfacerMap[agentCode];

                Vector3    pos = startPosition.ToVector3();
                Quaternion rot = new Quaternion(0, startRotation.y, 0, startRotation.x);

                curAgent = GameObject.Instantiate(GameResourceManager.GetAgentTemplate(agentCode).gameObject, pos, rot).GetComponent <RTSAgent>();
                curAgent.Setup(interfacer);

                RegisterRawAgent(curAgent);
            }
            return(curAgent);
        }
Exemple #3
0
        void SetupConfigs()
        {
            IUnitConfigDataProvider database;

            //todo guard
            if (LSDatabaseManager.TryGetDatabase(out database))
            {
                ConfigElementData = database.UnitConfigElementData;
                ConfigElementMap  = new Dictionary <string, UnitConfigElementDataItem> ();
                for (int i = 0; i < ConfigElementData.Length; i++)
                {
                    var item = ConfigElementData [i];
                    ConfigElementMap.Add(item.Name, item);
                }
                ConfigData = database.UnitConfigData;
                for (int i = 0; i < ConfigData.Length; i++)
                {
                    IUnitConfigDataItem item  = ConfigData [i];
                    RTSAgent            agent = GameResourceManager.GetAgentTemplate(item.Target);
                    for (int j = 0; j < item.Stats.Length; j++)
                    {
                        Stat stat = item.Stats [j];
                        //todo guard
                        var       element   = ConfigElementMap [stat.ConfigElement];
                        Component component = agent.GetComponent(element.ComponentType);
                        SetField(component, element.Field, stat.Value);
                    }
                }
            }
        }
Exemple #4
0
        internal static void Setup()
        {
            DefaultMessageRaiser.EarlySetup();

            LSDatabaseManager.Setup();
            Command.Setup();

            GridManager.Setup();

            InputCodeManager.Setup();
            AbilityDataItem.Setup();

            GameResourceManager.Setup();
            //AgentController.Setup ();

            ProjectileManager.Setup();
            EffectManager.Setup();

            PhysicsManager.Setup();
            ClientManager.Setup();

            Time.fixedDeltaTime   = DeltaTimeF;
            Time.maximumDeltaTime = Time.fixedDeltaTime * 2;


            DefaultMessageRaiser.LateSetup();
            if (onSetup != null)
            {
                onSetup();
            }
        }
Exemple #5
0
        public void CreateUnit(string unitName)
        {
            GameObject unit       = GameResourceManager.GetAgentTemplate(unitName).gameObject;
            RTSAgent   unitObject = unit.GetComponent <RTSAgent>();

            // check that the Player has the resources available before allowing them to create a new Unit / Building
            if (Agent.GetCommander() && unitObject)
            {
                if (Agent.GetCommander().CachedResourceManager.CheckResources(unitObject))
                {
                    Agent.GetCommander().CachedResourceManager.RemoveResources(unitObject);
                    spawnQueue.Enqueue(unitName);
                }
                else
                {
                    //    Debug.Log("not enough resources!");
                }
            }
        }
        protected override void OnLateApply()
        {
            foreach (EnvironmentBodyInfo info in EnvironmentBodies)
            {
                info.Body.Initialize(info.Position, info.Rotation);
            }

            foreach (EnvironmentObject obj in EnvironmentObjects)
            {
                obj.LateInitialize();
            }
            var environmentController = AgentControllerHelper.Instance.GetInstanceManager(AgentControllerHelper.Instance.EnvironmentController);

            foreach (var agentInfo in EnvironmentAgents)
            {
                var agent = agentInfo.Agent;
                agentInfo.Agent.Setup(GameResourceManager.GetAgentInterfacer(agentInfo.AgentCode));
                environmentController.InitializeAgent(agent, agentInfo.Position.ToVector2d(), agentInfo.Rotation);
                agentInfo.Agent.Body.HeightPos = agentInfo.Position.z;
                agentInfo.Agent.TypeIndex      = AgentController.UNREGISTERED_TYPE_INDEX;
            }
        }
        private static void LoadTerrain(JsonTextReader reader)
        {
            if (reader == null)
            {
                return;
            }
            Vector3    position = new Vector3(0, 0, 0), scale = new Vector3(1, 1, 1);
            Quaternion rotation = new Quaternion(0, 0, 0, 0);

            while (reader.Read())
            {
                if (reader.Value != null)
                {
                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        if ((string)reader.Value == "Position")
                        {
                            position = LoadVector(reader);
                        }
                        else if ((string)reader.Value == "Rotation")
                        {
                            rotation = LoadQuaternion(reader);
                        }
                        else if ((string)reader.Value == "Scale")
                        {
                            scale = LoadVector(reader);
                        }
                    }
                }
                else if (reader.TokenType == JsonToken.EndObject)
                {
                    GameObject ground = (GameObject)GameObject.Instantiate(GameResourceManager.GetWorldObject("Ground"), position, rotation);
                    ground.transform.localScale = scale;
                    ground.name = ground.name.Replace("(Clone)", "").Trim();
                    return;
                }
            }
        }
        public static void RegisterRawAgent(RTSAgent agent)
        {
            var             agentCodeID = GameResourceManager.GetAgentCodeIndex(agent.MyAgentCode);
            FastList <bool> typeActive;

            if (!AgentController.TypeAgentsActive.TryGetValue(agentCodeID, out typeActive))
            {
                typeActive = new FastList <bool>();
                TypeAgentsActive.Add(agentCodeID, typeActive);
            }
            FastList <RTSAgent> typeAgents;

            if (!TypeAgents.TryGetValue(agentCodeID, out typeAgents))
            {
                typeAgents = new FastList <RTSAgent>();
                TypeAgents.Add(agentCodeID, typeAgents);
            }

            //TypeIndex of ushort.MaxValue means that this agent isn't registered for the pool
            agent.TypeIndex = (ushort)(typeAgents.Count);
            typeAgents.Add(agent);
            typeActive.Add(true);
        }
        //private static void LoadResources(JsonTextReader reader)
        //{
        //    if (reader == null)
        //    {
        //        return;
        //    }
        //    string currValue = "", type = "";
        //    while (reader.Read())
        //    {
        //        if (reader.Value != null)
        //        {
        //            if (reader.TokenType == JsonToken.PropertyName)
        //            {
        //                currValue = (string)reader.Value;
        //            }
        //            else if (currValue == "Type")
        //            {
        //                type = (string)reader.Value;
        //                GameObject newObject = (GameObject)GameObject.Instantiate(ResourceManager.GetWorldObject(type));
        //                Resource resource = newObject.GetComponent<Resource>();
        //                resource.name = resource.name.Replace("(Clone)", "").Trim();
        //                resource.LoadDetails(reader);
        //            }
        //        }
        //        else if (reader.TokenType == JsonToken.EndArray)
        //        {
        //            return;
        //        }
        //    }
        //}

        private static void LoadPlayers(JsonTextReader reader)
        {
            if (reader == null)
            {
                return;
            }

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    //need to create via agentcontrollerhelper....
                    GameObject     newObject = (GameObject)GameObject.Instantiate(GameResourceManager.GetCommanderObject());
                    AgentCommander commander = newObject.GetComponent <AgentCommander>();
                    commander.name = commander.name.Replace("(Clone)", "").Trim();
                    commander.LoadDetails(reader);
                }
                else if (reader.TokenType == JsonToken.EndArray)
                {
                    return;
                }
            }
        }
        public void CreateCommander()
        {
            if (Commander != null)
            {
                Debug.LogError("A commander called '" + Commander.gameObject.name + "' already exists for '" + this.ToString() + "'.");
            }
            if (!UnityEngine.Object.FindObjectOfType <RTSGameManager>())
            {
                Debug.LogError("A game manager has not been initialized!");
            }

            //load from ls db
            GameObject commanderObject = GameObject.Instantiate(GameResourceManager.GetCommanderObject(), UnityEngine.Object.FindObjectOfType <RTSGameManager>().GetComponentInChildren <AgentCommanders>().transform);

            commanderObject.name = this.ControllerName;

            AgentCommander commanderClone = commanderObject.GetComponent <AgentCommander>();

            //change to user's selected username
            commanderClone.username = this.ControllerName;
            commanderClone.SetController(this);

            if (PlayerManager.ContainsController(this))
            {
                commanderClone.human = true;
            }

            //come up with better way to set selected commander to the current commander
            if (this == PlayerManager.MainController)
            {
                PlayerManager.SelectPlayer(commanderClone.username, 0, this.ControllerID, this.PlayerIndex);
            }

            _commander = commanderClone;
            BehaviourHelperManager.InitializeOnDemand(_commander);
        }
 public void CalculateCurrentHealth(float lowSplit, float highSplit)
 {
     if (Agent.MyAgentType == AgentType.Unit || Agent.MyAgentType == AgentType.Building)
     {
         healthPercentage = (float)cachedHealth.HealthAmount / (float)cachedHealth.MaxHealth;
         if (healthPercentage > highSplit)
         {
             healthStyle.normal.background = GameResourceManager.HealthyTexture;
         }
         else if (healthPercentage > lowSplit)
         {
             healthStyle.normal.background = GameResourceManager.DamagedTexture;
         }
         else
         {
             healthStyle.normal.background = GameResourceManager.CriticalTexture;
         }
     }
     else if (Agent.MyAgentType == AgentType.Resource)
     {
         healthPercentage = Agent.GetAbility <ResourceDeposit>().AmountLeft / (float)Agent.GetAbility <ResourceDeposit>().Capacity;
         healthStyle.normal.background = GameResourceManager.GetResourceHealthBar(Agent.GetAbility <ResourceDeposit>().ResourceType);
     }
 }
 protected void DrawSelectionBox(Rect selectBox)
 {
     GUI.Box(selectBox, "");
     CalculateCurrentHealth(0.35f, 0.65f);
     DrawHealthBar(selectBox, "");
     if (cachedHarvest)
     {
         long currentLoad = cachedHarvest.GetCurrentLoad();
         if (currentLoad > 0)
         {
             float     percentFull = currentLoad / (float)cachedHarvest.Capacity;
             float     maxHeight   = selectBox.height - 4;
             float     height      = maxHeight * percentFull;
             float     leftPos     = selectBox.x + selectBox.width - 7;
             float     topPos      = selectBox.y + 2 + (maxHeight - height);
             float     width       = 5;
             Texture2D resourceBar = GameResourceManager.GetResourceHealthBar(cachedHarvest.HarvestType);
             if (resourceBar)
             {
                 GUI.DrawTexture(new Rect(leftPos, topPos, width, height), resourceBar);
             }
         }
     }
 }