Beispiel #1
0
 public void SetActiveBuildingType(BuildingTypeSo buildingType)
 {
     _activeBuildingType = buildingType;
     OnActiveBuildingTypeChange?.Invoke(this,
                                        new OnActiveBuildingTypeChangeEventArgs {
         activeBuildingType = _activeBuildingType
     });
 }
Beispiel #2
0
 private void SetBuildingType(BuildingTypeSo buildingType)
 {
     _buildingType                    = buildingType;
     _constructionTimer               = buildingType.constructionTimerMax;
     _boxCollider2D.offset            = buildingType.prefab.GetComponent <BoxCollider2D>().offset;
     _boxCollider2D.size              = buildingType.prefab.GetComponent <BoxCollider2D>().size;
     _spriteRenderer.sprite           = buildingType.sprite;
     _buildingTypeHolder.buildingType = buildingType;
 }
Beispiel #3
0
    public static BuildingConstruction Create(Vector3 position, BuildingTypeSo buildingType)
    {
        var pfBuildingConstruction        = GameAssets.Instance.pfBuildingConstruction;
        var buildingConstructionTransform = Instantiate(pfBuildingConstruction, position, Quaternion.identity);
        var buildingConstruction          = buildingConstructionTransform.GetComponent <BuildingConstruction>();

        buildingConstruction.SetBuildingType(buildingType);
        return(buildingConstruction);
    }
Beispiel #4
0
    private void Start()
    {
        _buildingType = GetComponent <BuildingTypeHolder>().buildingType;
        _healthSystem = GetComponent <HealthSystem>();
        _healthSystem.SetHealthAmountMax(_buildingType.healthAmountMax, true);

        _healthSystem.OnDied    += HealthSystemOnDied;
        _healthSystem.OnDamaged += HealthSystemOnDamaged;
        _healthSystem.OnHealed  += HealthSystemOnHealed;
    }
Beispiel #5
0
    private static void AssignMouseEvents(Transform buttonTransform, BuildingTypeSo buildingType)
    {
        var mouseEnterExitEvents = buttonTransform.GetComponent <MouseEnterExitEvents>();

        mouseEnterExitEvents.OnMouseEnter += (sender, e) =>
        {
            TooltipUI.Instance.Show($"{buildingType.nameString}\n{buildingType.GetConstructionResourceCostString()}");
        };
        mouseEnterExitEvents.OnMouseExit += (sender, e) => { TooltipUI.Instance.Hide(); };
    }
Beispiel #6
0
    private bool CanSpawnBuilding(BuildingTypeSo buildingType, Vector2 position, out string errorMessage)
    {
        var boxCollider2d = buildingType.prefab.GetComponent <BoxCollider2D>();

        var  colliders2dArray = Physics2D.OverlapBoxAll(position + boxCollider2d.offset, boxCollider2d.size, 0);
        bool isAreaClear      = colliders2dArray.Length == 0;

        if (!isAreaClear)
        {
            errorMessage = "Area is not clear!";
            return(false);
        }

        colliders2dArray = Physics2D.OverlapCircleAll(position, buildingType.minConstructionRadius);
        foreach (var collider2D in colliders2dArray)
        {
            //Colliders inside the construction radius
            var buildingTypeHolder = collider2D.GetComponent <BuildingTypeHolder>();
            if (buildingTypeHolder && buildingTypeHolder.buildingType == buildingType)
            {
                //There's already building type within the construction radius
                errorMessage = "Too close to another building of the same type!";
                return(false);
            }
        }

        if (buildingType.hasResourceGeneratorData)
        {
            var resourceGeneratorData = buildingType.resourceGeneratorData;
            int nearbyResourceAmount  = ResourceGenerator.GetNearbyResourceAmount(resourceGeneratorData, position);

            if (nearbyResourceAmount == 0)
            {
                errorMessage = "There are no nearby Resource Nodes!";
                return(false);
            }
        }

        colliders2dArray = Physics2D.OverlapCircleAll(position, maxConstructionRadius);
        foreach (var collider2D in colliders2dArray)
        {
            //Colliders inside the construction radius
            var buildingTypeHolder = collider2D.GetComponent <BuildingTypeHolder>();
            if (buildingTypeHolder)
            {
                //It's not too far from other buildings on the map
                errorMessage = "";
                return(true);
            }
        }

        errorMessage = "Too far from any other building!";
        return(false);
    }
Beispiel #7
0
 private void HandleSelectedButton(BuildingTypeSo buildingType)
 {
     if (BuildingManager.Instance.GetActiveBuildingType() == buildingType)
     {
         BuildingManager.Instance.SetActiveBuildingType(null);
         UpdateActiveBuildingTypeButton();
     }
     else
     {
         BuildingManager.Instance.SetActiveBuildingType(buildingType);
         UpdateActiveBuildingTypeButton();
     }
 }
Beispiel #8
0
 private void Awake()
 {
     _demolishButton = transform.Find("button").GetComponent <Button>();
     _demolishButton.onClick.AddListener(() =>
     {
         BuildingTypeSo buildingType = building.GetComponent <BuildingTypeHolder>().buildingType;
         foreach (var resourceAmount in buildingType.constructionResourceCostArray)
         {
             ResourceManager.Instance.AddResource(resourceAmount.resourceType, Mathf.FloorToInt(resourceAmount.amount * 0.6f));
         }
         Destroy(building.gameObject);
     });
 }
Beispiel #9
0
 private void Awake()
 {
     Instance            = this;
     _buildingTypeList   = Resources.Load <BuildingTypeListSo>(nameof(BuildingTypeListSo));
     _activeBuildingType = null;
 }