示例#1
0
    private Transform GenButton(float pos, BuildingTypeScriptableObject buildingType)
    {
        Transform xform = Instantiate(buildingSelect, transform);

        xform.gameObject.SetActive(true);
        xform.Find("image").GetComponent <Image>().sprite = buildingType != null ? buildingType.sprite : arrowSprite;
        xform.GetComponent <Button>().onClick.AddListener(() => {
            BuildingManager.Instance.SelectBuildingType(buildingType);
            UpdateSelectedBuildingType(buildingType);
        });
        if (buildingType != null)
        {
            _buildingButtons[buildingType] = xform;
        }
        MouseEnterExitEvents mouseEnterExitEvents = xform.GetComponent <MouseEnterExitEvents>();

        mouseEnterExitEvents.OnMouseEnter += (object sender, EventArgs eventArgs) => {
            if (buildingType != null)
            {
                TooltipUI.Instance.ShowMsg(buildingType.nameString + "\n" + buildingType.GetProductionCostsString());
            }
            else
            {
                TooltipUI.Instance.ShowMsg("Arrow");
            }
        };
        mouseEnterExitEvents.OnMouseExit += (object sender, EventArgs eventArgs) => {
            TooltipUI.Instance.Hide();
        };
        RectTransform rect = xform.GetComponent <RectTransform>();

        rect.anchoredPosition = new Vector3(pos, 0, 0);
        return(xform);
    }
示例#2
0
    public static BuildingUnderConstruction Create(Vector3 position, BuildingTypeScriptableObject buildingType)
    {
        Transform xform = Instantiate(GameAssets.Instance.buildingUnderConstructionPrefab, position, Quaternion.identity);
        BuildingUnderConstruction buildingUnderConstruction = xform.GetComponent <BuildingUnderConstruction>();

        buildingUnderConstruction.Setup(buildingType);
        return(buildingUnderConstruction);
    }
 public void SelectBuildingType(BuildingTypeScriptableObject buildingType)
 {
     _activeBuildingType = buildingType;
     OnActiveBuildingTypeChanged?.Invoke(this,
                                         new OnActiveBuildingTypeChangedEventArgs {
         buildingType = buildingType
     }
                                         );
 }
示例#4
0
 private void Awake()
 {
     _buildingType    = GetComponent <BuildingTypeRef>().buildingType;
     _health          = GetComponent <HealthSystem>();
     _health.OnDeath += OnDeath;
     _health.SetMaxHealth(_buildingType.maxHealth);
     _health.OnDamaged += OnDamage;
     _health.OnHealed  += OnHealed;
     HideDemolishButton();
     HideRepairButton();
 }
 public override void Awake()
 {
     base.Awake();
     _buildingTypes = Resources.Load <BuildingTypeListScriptableObject>(typeof(BuildingTypeListScriptableObject).Name);
     if (_buildingTypes == null)
     {
         Debug.Log("*** Building Types List not found");
         return;
     }
     _activeBuildingType = null;
     _HQ.GetComponent <HealthSystem>().OnDeath += HQDied;
 }
示例#6
0
    private void Setup(BuildingTypeScriptableObject buildingType)
    {
        _constructionTime  = buildingType.constructionTime;
        _constructionTimer = _constructionTime;
        _buildingType      = buildingType;
        _image.sprite      = buildingType.sprite;
        BoxCollider2D buildingBox = buildingType.prefab.GetComponent <BoxCollider2D>();

        _box.size   = buildingBox.size;
        _box.offset = buildingBox.offset;
        Instantiate(_buildingConstructionParticles, transform.position, Quaternion.identity);
    }
示例#7
0
 private void UpdateSelectedBuildingType(BuildingTypeScriptableObject current)
 {
     _arrowButton.Find("selected").gameObject.SetActive(true);
     foreach (BuildingTypeScriptableObject buildingType in _buildingButtons.Keys)
     {
         Transform xform = _buildingButtons[buildingType];
         xform.Find("selected").gameObject.SetActive(buildingType == current);
         if (buildingType == current)
         {
             _arrowButton.Find("selected").gameObject.SetActive(false);
         }
     }
 }
    private bool CanSpawn(BuildingTypeScriptableObject buildingType, Vector3 pos, out string errMsg)
    {
        BoxCollider2D box = buildingType.prefab.GetComponent <BoxCollider2D>();

        Collider2D[] colliders = Physics2D.OverlapBoxAll((Vector2)pos + box.offset, box.size, 0);
        if (colliders.Length != 0)
        {
            errMsg = "Area not clear";
            return(false);
        }
        // Look to see if there are any other factories of this type too close
        colliders = Physics2D.OverlapCircleAll(pos, buildingType.minSeparation);
        foreach (Collider2D collider in colliders)
        {
            BuildingTypeRef buildingTypeRef = collider.GetComponent <BuildingTypeRef>();
            if (buildingTypeRef != null)
            {
                if (buildingTypeRef.buildingType == buildingType)
                {
                    errMsg = "Sorry, there's another factory too closeby";
                    return(false);
                }
            }
        }
        // Make sure there are at least some resources (else this factory would be useless)
        if (buildingType.hasResourceGeneratorData)
        {
            ResourceGeneratorData resourceGeneratorData = buildingType.resourceGeneratorData;
            int nearbyResources = ResourceGenerator.GetNearbyResources(resourceGeneratorData, pos);
            if (nearbyResources == 0)
            {
                errMsg = "No resources nearby";
                return(false);
            }
        }
        // Make sure there is at least one other building nearby (no isolationists!)
        colliders = Physics2D.OverlapCircleAll(pos, maxSeparation);
        foreach (Collider2D collider in colliders)
        {
            BuildingTypeRef buildingTypeRef = collider.GetComponent <BuildingTypeRef>();
            if (buildingTypeRef != null)
            {
                errMsg = "";
                return(true);
            }
        }
        errMsg = "Sorry, that's way too far from another building";
        return(false);
    }