/** * <summary> * Select a given resource * </summary> * * <param name="resource">resource</param> * * <returns> * void * </returns> */ public static void SelectResource(GameObject resource) { ResourcesManagerBehaviour.UnselectResource(); // Unselect units if (UnitsManagerBehaviour.SelectedUnits != null) { UnitsManagerBehaviour.UnselectGameObjects(); } ResourceBehaviour resourceBehaviour = resource.GetComponent <ResourceBehaviour>(); //resourceBehaviour.SetSelect(true); // Show info component panel Self.infoComponentPanel.SetActive(true); Self.infoComponentPanel .transform .Find("Single") .gameObject .SetActive(true); ResourceUI resourceUI = resourceBehaviour.GetUI() .GetComponent <ResourceUI>(); InfoComponentPanelUI infoComponentPanelUI = Self.infoComponentPanel.GetComponent <InfoComponentPanelUI>(); infoComponentPanelUI.SetName(resource.name); infoComponentPanelUI.SetHealth(resourceBehaviour.GetHealth(), resourceBehaviour.GetMaxHealth()); infoComponentPanelUI.SetIcon(resourceUI.GetIcon()); // Set selected resource ResourcesManagerBehaviour.SelectedResource = resource; }
/** * <summary> * @implementation ISelectable * * On selected * </summary> * * <returns> * void * </returns> */ public virtual void OnSelected() { // Stop selection through the UI. if (ComponentPanelsUI.OnUI) { return; } // Unselect units if (UnitsManagerBehaviour.SelectedUnits != null) { UnitsManagerBehaviour.UnselectGameObjects(); } // Unselect resource if (ResourcesManagerBehaviour.SelectedResource != null) { ResourcesManagerBehaviour.UnselectResource(); } this.UI.SetActive(true); GameObject health = this.UI.transform.Find("Health").gameObject; health.SetActive(true); // Show building action component this.buildingActionComponent.SetActive(true); }
/** * <summary> * Unselect and set the slot inactive * </summary> * * <returns> * void * </returns> */ public void Unselect() { if (BuilderActionComponentUI.ActiveSlot == null) { return; } this.outline.effectColor = this.defaultColor; GameObject previewGameObject = BuilderActionComponentUI.ActiveSlot.GetComponent <BuildingSlotUI>().GetPreview(); previewGameObject.transform.localPosition = Vector3.zero; BuilderActionComponentUI.PreviewFollow = false; BuilderActionComponentUI.ActiveSlot = null; // For better alignment, may change in the future? this.preview.GetComponent <BuildingPreviewBehaviour>().TurnRenderer(false); MouseBehaviour.Mode = MouseMode.Default; // Show all visible renderers and hide placement (buildings, and resources) BuildingsManagerBehaviour.TurnRenderers(true); ResourcesManagerBehaviour.TurnRenderers(true); BuildingsManagerBehaviour.TurnPlacementIndicator(false); ResourcesManagerBehaviour.TurnPlacementIndicator(false); this.select = false; }
/** * <summary> * Instantiate and eventually place the building * </summary> * * <param name="position"/> * * <returns> * void * </returns> */ public virtual void Place(Vector3 position) { foreach (GameObject selectedUnit in UnitsManagerBehaviour.SelectedUnits) { if (selectedUnit.GetComponent <BuilderBehaviour>()) { GameObject building = MonoBehaviour.Instantiate( Resources.Load(this.structurePath) as GameObject ); // Substract / reduce resources foreach (ResourceRequirement resourceRequirement in this.resourceRequirements) { ResourceType resourceRequirementType = resourceRequirement.GetType(); float value = ResourcesManagerBehaviour.Wood.GetValue() - resourceRequirement.GetAmount(); ResourcesManagerBehaviour.SetResource(resourceRequirementType, value); } building.transform.position = position; BuilderBehaviour builderBehaviour = selectedUnit.GetComponent <BuilderBehaviour>(); builderBehaviour.StartConstruction(building); } } // Hide the requirement UI this.HideRequirementsUI(); // Rebuild navmesh navigations NavMeshSurfacesManager.BuildNavMesh(); }
private void Awake() { ResourcesManagerBehaviour.Visibles = new List <GameObject>(); ResourcesManagerBehaviour.Resources = new List <GameObject>(); ResourcesManagerBehaviour.Wood = new WoodResource(); ResourcesManagerBehaviour.Food = new FoodResource(); ResourcesManagerBehaviour.Self = this; }
/** * <summary> * Select a unit * </summary> * * <returns> * void * </returns> */ public static void Select(GameObject unit) { if (unit.GetComponent <UnitBehaviour>() == null) { throw new Exception(unit.name + " " + "is not a unit type"); } // Clear previous units UnitsManagerBehaviour.UnselectGameObjects(); // Unselect resource if (ResourcesManagerBehaviour.SelectedResource != null) { ResourcesManagerBehaviour.UnselectResource(); } UnitBehaviour unitBehaviour = unit.GetComponent <UnitBehaviour>(); if (unitBehaviour is ISelectableUnit) { UnitsManagerBehaviour.SelectedUnits.Add(unit); unitBehaviour.SetSelect(true); Self.infoComponentPanel.SetActive(true); // Show info component panel Self.infoComponentPanel .transform .Find("Single") .gameObject .SetActive(true); // Hide multiple Self.infoComponentPanel .transform .Find("Multiple") .gameObject .SetActive(false); UnitUI unitUI = unitBehaviour.GetUI().GetComponent <UnitUI>(); InfoComponentPanelUI infoComponentPanelUI = Self.infoComponentPanel.GetComponent <InfoComponentPanelUI>(); infoComponentPanelUI.SetName(unit.name); infoComponentPanelUI.SetHealth(unitBehaviour.GetHealth(), unitBehaviour.GetMaxHealth()); infoComponentPanelUI.SetIcon(unitUI.GetIcon()); // Show action component panel Self.actionComponentPanel.SetActive(true); } }
/** * <summary> * On game object destroyed * </summary> * * <returns> * void * </returns> */ private void OnDestroy() { if (NavMeshSurfacesManager.IsInitialized()) { NavMeshSurfacesManager.BuildNavMesh(); } if (!this && ResourcesManagerBehaviour.SelectedResource == this.gameObject) { ResourcesManagerBehaviour.UnselectResource(); } // On destroy removes the resource from visible list. ResourcesManagerBehaviour.Visibles.RemoveAll(active => active.GetInstanceID() == this.gameObject.GetInstanceID()); ResourcesManagerBehaviour.Resources.RemoveAll(active => active.GetInstanceID() == this.gameObject.GetInstanceID()); }
/** * <summary> * Select a given building * </summary> * * <param name="building"></param> * * <returns> * void * </returns> */ public static void SelectBuilding(GameObject building) { // Unselect previous building BuildingsManagerBehaviour.UnselectBuilding(); // Unselect units if (UnitsManagerBehaviour.SelectedUnits != null) { UnitsManagerBehaviour.UnselectGameObjects(); } // Unselect previous resource if (ResourcesManagerBehaviour.SelectedResource != null) { ResourcesManagerBehaviour.UnselectResource(); } BuildingBehaviour buildingBehaviour = building.GetComponent <BuildingBehaviour>(); buildingBehaviour.SetSelect(true); // Show info component panel Self.infoComponentPanel.SetActive(true); Self.infoComponentPanel .transform .Find("Single") .gameObject .SetActive(true); BuildingUI buildingUI = buildingBehaviour.GetUI() .GetComponent <BuildingUI>(); InfoComponentPanelUI infoComponentPanelUI = Self.infoComponentPanel.GetComponent <InfoComponentPanelUI>(); infoComponentPanelUI.SetName(building.name); infoComponentPanelUI.SetHealth(buildingBehaviour.GetHealth(), buildingBehaviour.GetMaxHealth()); infoComponentPanelUI.SetIcon(buildingUI.GetIcon()); // Show action component panel Self.actionComponentPanel.SetActive(true); // Set selected building BuildingsManagerBehaviour.SelectedBuilding = building; }
/** * <summary> * Update health bar every frame, if the resource * is being gathered. * </summary> */ private IEnumerator CheckHealth() { while (true) { while (this.IsGathering()) { if (this.IsDrained()) { yield break; } ResourcesManagerBehaviour.UpdateSelectedResourceInfo(); yield return(null); } yield return(null); } }
/** * <summary> * Validate resources before placing the building * </summary> */ public bool Validate() { bool isValidated = false; // Show requirements foreach (ResourceRequirement requirement in this.resourceRequirements) { if (requirement.GetType() == ResourceType.Wood) { // Show wood requirement and validate the value int amount = (int)requirement.GetAmount(); // Validation isValidated = ResourcesManagerBehaviour.ValidateResourceRequirement(ResourceType.Wood, amount); } } return(isValidated); }
private IEnumerator GenerateFood() { while (true) { float timer = 0f; int maxSeconds = 1; while (timer < maxSeconds) { timer = timer + Time.deltaTime; yield return(null); } // Generate ResourcesManagerBehaviour.AddResource(ResourceType.Food, 1); Debug.Log("Building : Farm : Generate"); yield return(null); } }
/** * <summary> * Select and set the slot active * </summary> * * <returns> * void * </returns> */ public void Select() { this.outline.effectColor = new Color((255 / 255), (0f / 255f), (0f / 255f), 1); BuilderActionComponentUI.PreviewFollow = true; BuilderActionComponentUI.ActiveSlot = this.gameObject; // For better alignment, may change in the future? this.preview.GetComponent <BuildingPreviewBehaviour>().TurnRenderer(false); MouseBehaviour.Mode = MouseMode.Build; // Hide all visible renderers and show placement (buildings, and resources) BuildingsManagerBehaviour.TurnRenderers(false); ResourcesManagerBehaviour.TurnRenderers(false); BuildingsManagerBehaviour.TurnPlacementIndicator(true); ResourcesManagerBehaviour.TurnPlacementIndicator(true); this.select = true; }
/** * <summary> * On mouse exit * </summary> * * <returns> * void * </returns> */ //private void OnMouseExit() //{ // this.timer = 0f; // if (MouseBehaviour.Mode == MouseMode.OverResource) // { // MouseBehaviour.Mode = MouseMode.Default; // } //} /** * <summary> * @implementation ISelectable * * On selected * </summary> * * <returns> * void * </returns> */ //public virtual void OnSelected() //{ // // Stop selection through the UI. // if (ComponentPanelsUI.OnUI) // { // return; // } // // Unselect units // if (UnitsManagerBehaviour.SelectedUnits != null) // { // UnitsManagerBehaviour.UnselectGameObjects(); // } // // Unselect building // if (BuildingsManagerBehaviour.SelectedBuilding != null) // { // BuildingsManagerBehaviour.UnselectBuilding(); // } // this.UI.SetActive(true); //} /** * <summary> * @implementation ISelectable * * On unselected * </summary> * * <returns> * void * </returns> */ //public virtual void OnUnselected () //{ // this.UI.SetActive(false); //} /** * <summary> * While the builder is gathering the resource * </summary> * * <returns> * IEnumerator null * </returns> */ private IEnumerator CheckResourceGathering() { while (true) { // While the resource is not completely drained while (!this.IsDrained()) { // While the resource is not being gathered by // the builder while (!this.gathering) { //this.UI.SetActive(false); yield return(null); } // Show the UI. this.UI.SetActive(true); // Decrease the health float drainSpeed = 10f; float drainRate = drainSpeed * Time.deltaTime; float health = this.health - drainRate; this.SetHealth(health); ResourcesManagerBehaviour.AddResource(ResourceType.Wood, drainRate); if (this.health < 0f) { this.SetDrain(true); } yield return(null); } // The resource is drained. this.OnCompleted(); yield break; } }
/** * <summary> * On unit selected * </summary> */ public virtual void OnSelected() { // Stop selection through the UI. if (ComponentPanelsUI.OnUI) { return; } // Unselect resources if (ResourcesManagerBehaviour.SelectedResource != null) { ResourcesManagerBehaviour.UnselectResource(); } // Unselect building if (BuildingsManagerBehaviour.SelectedBuilding != null) { BuildingsManagerBehaviour.UnselectBuilding(); } this.UI.SetActive(true); }
/** * <summary> * On left mouse up * </summary> * * <returns> * void * </returns> */ private void OnLeftMouseUp() { // Stop scrolling, if the mouse is over UI or in build mode. if (!this.drag) { return; } // Is pointer up on the component panels UI, stop the selection if (ComponentPanelsUI.OnUI) { this.drag = false; this.HideSelectionArea(); return; } RectTransform areaRect = this.area.GetComponent <RectTransform>(); // Set final mouse position this.mousePosition.SetFinalPosition(this.transform.position); Bounds cameraViewportBounds = MouseScreenBehaviour.GetViewportBounds( this.mousePosition.GetInitialPosition(), this.mousePosition.GetFinalPosition() ); ResourcesManagerBehaviour.UnselectResource(); BuildingsManagerBehaviour.UnselectBuilding(); UnitsManagerBehaviour.UnselectGameObjects(); this.mouseSelectionBehaviour.SelectGameObjects(UnitsManagerBehaviour.Visibles, cameraViewportBounds); this.drag = false; this.HideSelectionArea(); }
/** * <summary> * @overload * * Select a group of units * </summary> * * <returns> * void * </returns> */ public static void Select(List <GameObject> units) { // Select a single unit, if the list has only one unit if (units.Count == 1) { UnitsManagerBehaviour.Select(units[0]); return; } // Validate unit game objects foreach (GameObject unit in units) { if (unit.GetComponent <UnitBehaviour>() == null) { throw new Exception(unit.name + " " + "is not a unit type"); } } // Clear previous units UnitsManagerBehaviour.UnselectGameObjects(); // Unselect resource if (ResourcesManagerBehaviour.SelectedResource != null) { ResourcesManagerBehaviour.UnselectResource(); } // Add unit to the global list foreach (GameObject unit in units) { UnitBehaviour unitBehaviour = unit.GetComponent <UnitBehaviour>(); if (unitBehaviour is ISelectableUnit) { UnitsManagerBehaviour.SelectedUnits.Add(unit); } } // Set selection foreach (GameObject unit in UnitsManagerBehaviour.SelectedUnits) { UnitBehaviour unitBehaviour = unit.GetComponent <UnitBehaviour>(); unitBehaviour.SetSelect(true); } // Show multiple unit icons if (UnitsManagerBehaviour.SelectedUnits.Count > 1) { Self.infoComponentPanel.SetActive(true); // Show info component panel Self.infoComponentPanel .transform .Find("Single") .gameObject .SetActive(false); // Hide multiple Self.infoComponentPanel .transform .Find("Multiple") .gameObject .SetActive(true); // Remove previous multiple selection slots Self.RemoveMultipleSelectionSlots(); // Generate new multiple selection slots Self.GenerateMultipleSelectionUnitSlots(); // Hide action component panel Self.actionComponentPanel.SetActive(false); } }
// Use this for initialization private void Start() { ResourcesManagerBehaviour.SetResource(ResourceType.Wood, 0f); ResourcesManagerBehaviour.SetResource(ResourceType.Food, 0f); }