Beispiel #1
0
    public static void CreateStructure(string buildingName, RTSAgent constructingAgent, Rect playingArea)
    {
        Vector2d buildPoint       = new Vector2d(constructingAgent.transform.position.x, constructingAgent.transform.position.z + 10);
        RTSAgent buildingTemplate = GameResourceManager.GetAgentTemplate(buildingName);

        if (buildingTemplate.MyAgentType == AgentType.Building && buildingTemplate.GetComponent <Structure>())
        {
            // check that the Player has the resources available before allowing them to create a new structure
            if (!_cachedCommander.CachedResourceManager.CheckResources(buildingTemplate))
            {
                Debug.Log("Not enough resources!");
            }
            else
            {
                tempObject = Object.Instantiate(buildingTemplate.gameObject, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
                if (tempObject)
                {
                    _findingPlacement = true;
                    SetTransparentMaterial(tempObject, GameResourceManager.AllowedMaterial);
                    tempObject.gameObject.name = buildingName;

                    tempStructure = tempObject.GetComponent <Structure>();
                    if (tempStructure.StructureType == StructureType.Wall)
                    {
                        // walls require a little help since they are click and drag
                        _constructingWall       = true;
                        tempStructure.IsOverlay = true;
                        WallPositioningHelper.Setup();
                    }

                    tempStructureBody = tempObject.GetComponent <UnityLSBody>().InternalBody;

                    // structure size is 2 times the size of halfwidth & halfheight
                    tempStructure.BuildSizeLow  = (tempStructureBody.HalfWidth.CeilToInt() * 2);
                    tempStructure.BuildSizeHigh = (tempStructureBody.HalfLength.CeilToInt() * 2);

                    cachedAgent = constructingAgent;

                    tempStructure.gameObject.transform.position = Positioning.GetSnappedPosition(buildPoint.ToVector3());
                }
            }
        }
    }
    //this should be in the controller...
    private void LoadRTSAgents(JsonTextReader reader)
    {
        if (reader == null)
        {
            return;
        }
        RTSAgents agents = GetComponentInChildren <RTSAgents>();
        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;
                    // need to create unit via commander controller...
                    GameObject newObject = Instantiate(GameResourceManager.GetAgentTemplate(type).gameObject);
                    RTSAgent   agent     = newObject.GetComponent <RTSAgent>();
                    agent.name = agent.name.Replace("(Clone)", "").Trim();
                    agent.LoadDetails(reader);
                    agent.transform.parent = agents.transform;
                    agent.SetCommander(this);
                    agent.SetTeamColor();
                }
            }
            else if (reader.TokenType == JsonToken.EndArray)
            {
                return;
            }
        }
    }