コード例 #1
0
 private void Awake()
 {
     m_boxCollider2D        = GetComponent <BoxCollider2D>();
     m_spriteRenderer       = transform.Find("Sprite").GetComponent <SpriteRenderer>();
     m_buildingTypeholder   = GetComponent <BuildingTypeHolder>();
     m_constructionMaterial = m_spriteRenderer.material;
     PlayBuildParticles(transform.position);
 }
コード例 #2
0
        private bool CanSpawnBuilding(BuildingTypeSO buildingType, Vector3 position, out string errorMessage)
        {
            BoxCollider2D boxCollider2D = buildingType.prefab.GetComponent <BoxCollider2D>();

            Collider2D[] collider2DArray = Physics2D.OverlapBoxAll(position + (Vector3)boxCollider2D.offset, boxCollider2D.size, 0f);

            bool isAreaClear = collider2DArray.Length == 0;

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

            collider2DArray = Physics2D.OverlapCircleAll(position, buildingType.minConstructionRadius);

            foreach (Collider2D collider in collider2DArray)
            {
                // Colliders inside construction radius:
                BuildingTypeHolder buildingTypeHolder = collider.GetComponent <BuildingTypeHolder>();

                // Have a building and it is same type, then we can't build here:
                if (buildingTypeHolder != null)
                {
                    if (buildingTypeHolder.buildingType == buildingType)
                    {
                        errorMessage = "To close to another building of the same type!";
                        return(false);
                    }
                }
            }

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

                if (nearbyResourceAmount == 0)
                {
                    errorMessage = "No nearby resources!";
                    return(false);
                }
            }

            // Check to see if there is a building within the maxConstructionRadius:
            collider2DArray = Physics2D.OverlapCircleAll(position, m_maxConstructionRadius);

            foreach (Collider2D collider in collider2DArray)
            {
                // Colliders inside construction radius:
                BuildingTypeHolder buildingTypeHolder = collider.GetComponent <BuildingTypeHolder>();

                // Have a building so it is okay to build:
                if (buildingTypeHolder != null)
                {
                    errorMessage = "";
                    return(true);
                }
            }

            // To far away from another building, can't build:
            errorMessage = "Too far from from any other building!";
            return(false);
        }