public bool CheckResources(RTSAgent agent) { bool validResources = true; foreach (KeyValuePair <ResourceType, int> entry in agent.resourceCost) { if (entry.Value > 0) { switch (entry.Key.ToString()) { case "Provision": if ((entry.Value + GetResourceAmount(entry.Key)) >= GetResourceLimit(entry.Key)) { validResources = false; Debug.Log("not enough supplies!"); } break; default: if (entry.Value > GetResourceAmount(entry.Key)) { validResources = false; Debug.Log("not enough _resources!"); } break; } ; } ; } return(validResources); }
public void CancelBuildingPlacement() { findingPlacement = false; Destroy(tempBuilding.gameObject); tempBuilding = null; tempCreator = null; }
public void CreateBuilding(RTSAgent agent, string buildingName) { Vector2d buildPoint = new Vector2d(agent.transform.position.x, agent.transform.position.z + 10); if (cachedCommander) { //cleanup later... CreateBuilding(buildingName, buildPoint, agent, agent.GetPlayerArea()); } }
private void DrawActions(string[] actions) { GUIStyle buttons = new GUIStyle(); buttons.hover.background = buttonHover; buttons.active.background = buttonClick; GUI.skin.button = buttons; int numActions = actions.Length; //define the area to draw the actions inside GUI.BeginGroup(new Rect(BUILD_IMAGE_WIDTH, 0, ORDERS_BAR_WIDTH, buildAreaHeight)); //draw scroll bar for the list of actions if need be if (numActions >= MaxNumRows(buildAreaHeight)) { DrawSlider(buildAreaHeight, numActions / 2.0f); } //display possible actions as buttons and handle the button click for each for (int i = 0; i < numActions; i++) { int column = i % 2; int row = i / 2; Rect pos = GetButtonPos(row, column); Texture2D action = GameResourceManager.GetBuildImage(actions[i]); if (action) { //create the button and handle the click of that button if (GUI.Button(pos, action)) { RTSAgent agent = Selector.MainSelectedAgent as RTSAgent; if (agent) { PlayClick(); if (agent.MyAgentType == AgentType.Unit && agent.GetAbility <Construct>() && !ConstructionHandler.IsFindingBuildingLocation()) { ConstructionHandler.CreateStructure(actions[i], agent, agent.GetPlayerArea()); } else if (agent.MyAgentType == AgentType.Building && !agent.GetAbility <Structure>().NeedsConstruction && agent.GetAbility <Spawner>()) { // send spawn command Command spawnCom = new Command(AbilityDataItem.FindInterfacer("Spawner").ListenInputID); spawnCom.Add <DefaultData>(new DefaultData(DataType.String, actions[i])); UserInputHelper.SendCommand(spawnCom); } } } } } GUI.EndGroup(); }
public RTSAgent GetAgent() { if (this.Prefab != null) { RTSAgent agent = this.Prefab.GetComponent <RTSAgent>(); if (agent) { return(agent); } } return(null); }
private bool ConvoyInPosition(RTSAgent agent) { if (!agent) { return(false); } float closeEnough = 3.0f; Vector3d agentPos = agent.Body.Position3d; bool xInPos = agentPos.x > destination.x - closeEnough && agentPos.x < destination.x + closeEnough; bool zInPos = agentPos.z > destination.z - closeEnough && agentPos.z < destination.z + closeEnough; return(xInPos && zInPos); }
public int SortDegreeFromAgentType(AgentType agentType) { RTSAgent agent = GetAgent(); if (agent == null) { return(-1); } if (agentType == agent.MyAgentType) { return(1); } return(0); }
private void DrawOrdersBar() { if (Selector.MainSelectedAgent.GetAbility <Structure>() && Selector.MainSelectedAgent.GetAbility <Structure>().UnderConstruction()) { return; } GUI.skin = ordersSkin; GUI.BeginGroup(new Rect(Screen.width - ORDERS_BAR_WIDTH - BUILD_IMAGE_WIDTH, RESOURCE_BAR_HEIGHT, ORDERS_BAR_WIDTH + BUILD_IMAGE_WIDTH, Screen.height - RESOURCE_BAR_HEIGHT)); GUI.Box(new Rect(BUILD_IMAGE_WIDTH + SCROLL_BAR_WIDTH, 0, ORDERS_BAR_WIDTH, Screen.height - RESOURCE_BAR_HEIGHT), ""); string selectionName = ""; RTSAgent selectedAgent = Selector.MainSelectedAgent as RTSAgent; selectionName = selectedAgent.GetComponent <RTSAgent>().objectName; if (selectedAgent.IsOwnedBy(cachedCommander.CachedController)) { // reset slider value if the selected object has changed if (lastSelection && lastSelection != Selector.MainSelectedAgent) { sliderValue = 0.0f; } if (selectedAgent.MyAgentType == AgentType.Unit && selectedAgent.GetAbility <Construct>()) { DrawActions(selectedAgent.GetAbility <Construct>().GetBuildActions()); } else if (selectedAgent.MyAgentType == AgentType.Building && selectedAgent.GetAbility <Spawner>()) { DrawActions(selectedAgent.GetAbility <Spawner>().GetSpawnActions()); } // store the current selection lastSelection = selectedAgent; if (lastSelection.MyAgentType == AgentType.Building) { if (lastSelection.GetAbility <Spawner>()) { DrawBuildQueue(lastSelection.GetAbility <Spawner>().getBuildQueueValues(), lastSelection.GetAbility <Spawner>().getBuildPercentage()); } DrawStandardBuildingOptions(lastSelection as RTSAgent); } } if (!selectionName.Equals("")) { int leftPos = BUILD_IMAGE_WIDTH + SCROLL_BAR_WIDTH / 2; int topPos = buildAreaHeight + BUTTON_SPACING; GUI.Label(new Rect(leftPos, topPos, ORDERS_BAR_WIDTH, SELECTION_NAME_HEIGHT), selectionName); } GUI.EndGroup(); }
public static void Reset() { _findingPlacement = false; if (_constructingWall) { _constructingWall = false; WallPositioningHelper.Reset(); } //temp structure no longer required Object.Destroy(tempObject); tempStructure = null; tempStructureBody = null; cachedAgent = null; // remove temporary structure from grid GridBuilder.Reset(); }
public void CreateBuilding(string buildingName, Vector2d buildPoint, RTSAgent creator, Rect playingArea) { GameObject newBuilding = Instantiate(ResourceManager.GetAgentTemplate(buildingName).gameObject); tempBuilding = newBuilding.GetComponent <RTSAgent>(); if (tempBuilding.MyAgentType == AgentType.Building) { tempBuilding.name = buildingName; tempBuilding.gameObject.transform.position = buildPoint.ToVector3(); tempCreator = creator; findingPlacement = true; tempBuilding.SetTransparentMaterial(notAllowedMaterial, true); } else { Destroy(newBuilding); } }
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()); } } } }
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(ResourceManager.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(); agent.SetTeamColor(); if (agent.GetAbility <Structure>().UnderConstruction()) { agent.SetTransparentMaterial(CachedBuilderManager.allowedMaterial, true); } } } else if (reader.TokenType == JsonToken.EndArray) { return; } } }
public void RemoveResources(RTSAgent agent) { foreach (KeyValuePair <ResourceType, int> entry in agent.resourceCost) { if (entry.Value > 0) { switch (entry.Key.ToString()) { case "Provision": AddResource(entry.Key, entry.Value); break; default: RemoveResource(entry.Key, entry.Value); break; } ; } ; } }
private void DrawStandardOptions(RTSAgent agent) { GUIStyle buttons = new GUIStyle(); buttons.hover.background = smallButtonHover; buttons.active.background = smallButtonClick; GUI.skin.button = buttons; int leftPos = BUILD_IMAGE_WIDTH + SCROLL_BAR_WIDTH + BUTTON_SPACING; int topPos = buildAreaHeight - BUILD_IMAGE_HEIGHT / 2; int width = BUILD_IMAGE_WIDTH / 2; int height = BUILD_IMAGE_HEIGHT / 2; if (cachedCommander.GetController().SelectedAgents.Count == 1 && GUI.Button(new Rect(leftPos, topPos, width, height), agent.destroyImage)) { PlayClick(); agent.Die(); } if (agent.GetAbility <Rally>() && agent.GetAbility <Rally>().hasSpawnPoint() && !agent.GetAbility <Structure>().NeedsConstruction) { leftPos += width + BUTTON_SPACING; if (GUI.Button(new Rect(leftPos, topPos, width, height), agent.GetAbility <Rally>().rallyPointImage)) { PlayClick(); if (activeCursorState != CursorState.RallyPoint) { agent.GetAbility <Rally>().SetFlagState(FlagState.SettingFlag); SetCursorState(CursorState.RallyPoint); SetCursorLock(true); } else { agent.GetAbility <Rally>().SetFlagState(FlagState.FlagSet); SetCursorLock(false); SetCursorState(CursorState.Select); } } } }
private void DrawStandardBuildingOptions(RTSAgent building) { GUIStyle buttons = new GUIStyle(); buttons.hover.background = smallButtonHover; buttons.active.background = smallButtonClick; GUI.skin.button = buttons; int leftPos = BUILD_IMAGE_WIDTH + SCROLL_BAR_WIDTH + BUTTON_SPACING; int topPos = buildAreaHeight - BUILD_IMAGE_HEIGHT / 2; int width = BUILD_IMAGE_WIDTH / 2; int height = BUILD_IMAGE_HEIGHT / 2; if (GUI.Button(new Rect(leftPos, topPos, width, height), building.GetAbility <Structure>().sellImage)) { PlayClick(); building.GetAbility <Structure>().Sell(); } if (building.GetAbility <Spawner>() != null && building.GetAbility <Spawner>().hasSpawnPoint()) { leftPos += width + BUTTON_SPACING; if (GUI.Button(new Rect(leftPos, topPos, width, height), building.GetAbility <Spawner>().rallyPointImage)) { PlayClick(); if (activeCursorState != CursorState.RallyPoint && previousCursorState != CursorState.RallyPoint) { building.GetAbility <Spawner>().SetFlagState(FlagState.SettingFlag); SetCursorState(CursorState.RallyPoint); } else { // dirty hack to ensure toggle between RallyPoint and not works ... building.GetAbility <Spawner>().SetFlagState(FlagState.FlagSet); SetCursorState(CursorState.PanRight); SetCursorState(CursorState.Select); } } } }
// move to build ability? public void StartConstruction() { findingPlacement = false; Vector2d buildPoint = new Vector2d(tempBuilding.transform.position.x, tempBuilding.transform.position.z); RTSAgent newBuilding = cachedCommander.CachedController.CreateAgent(tempBuilding.gameObject.name, buildPoint, Vector2d.right) as RTSAgent; Destroy(tempBuilding.gameObject); newBuilding.SetState(AnimState.Building); newBuilding.RestoreMaterials(); newBuilding.SetPlayingArea(tempCreator.GetPlayerArea()); newBuilding.GetAbility <Health>().HealthAmount = FixedMath.Create(0); newBuilding.SetCommander(); // send build command Command buildCom = new Command(AbilityDataItem.FindInterfacer("Construct").ListenInputID); buildCom.Add <DefaultData>(new DefaultData(DataType.UShort, newBuilding.GlobalID)); UserInputHelper.SendCommand(buildCom); newBuilding.GetAbility <Structure>().StartConstruction(); // check that the Player has the resources available before allowing them to create a new Unit / Building cachedCommander.RemoveResource(ResourceType.Gold, newBuilding.cost); }
public void Setup(RTSAgent agent) { TrackedAgent = agent; GameObject.DontDestroyOnLoad(gameObject); this.gameObject.name = agent.ToString(); }