// sets all values for TurretController script using turretData, adds "click-icon-to-buy" function, changes visual cues to see if turret is bought private void SetValues(GameObject turret) { TurretController script = turret.GetComponent <TurretController>(); // script setup script.turretData = turretData; script.UpdateVariables(); Transform newShootpoint = new GameObject("Shootpoint").transform; newShootpoint.SetParent(turret.transform); script.shootPoint = newShootpoint; script.shootPoint.localPosition = new Vector2(turretData.shootpointOffsetX, 0); script.targetTag = "Enemy"; script.bullet = turretData.bullet; script.enabled = false; turret.transform.localPosition = Vector2.zero; // moving turret to middle of tower block SpriteRenderer rend = turret.GetComponent <SpriteRenderer>(); // setting up sprite (half transparent, sorting order) rend.sprite = turretData.artwork; rend.color = new Color(1, 1, 1, 0.5f); rend.sortingOrder = towerCount + 1; turret.GetComponent <BoxCollider2D>().size = new Vector2(1, 1); turret.GetComponent <BoxCollider2D>().isTrigger = true; TurretButton turrButScript = turret.GetComponent <TurretButton>(); // setting up fake turret button ("click-icon-to-buy" without using UI buttons) turrButScript.values = values; turrButScript.towerPlacement = this; turrButScript.index = towerCount; }
private void SpawnTowerOrTurret(float[] chances) { float current = UnityEngine.Random.Range(0f, 1f); if (current <= chances[0]) // build tower { if (towerCount < 3 && gold >= tower.cost[towerCount]) { gold -= tower.cost[towerCount]; GameObject newTower = new GameObject("Enemy Tower" + towerCount, typeof(SpriteRenderer)); // tower creation newTower.GetComponent <SpriteRenderer>().sprite = tower.artwork; newTower.transform.position = new Vector3(towerBase.transform.position.x, tower.yPlacement[towerCount], 0); newTower.transform.SetParent(towerBase.gameObject.transform); towerCount++; } } else if (current <= chances[0] + chances[1]) // build turret (if tower exists) { if (gold >= turretData.price && towerBase.transform.childCount > turretCount && turretCount < 3) { gold -= turretData.price; GameObject turret = new GameObject("Enemy turret", typeof(Animator), typeof(SpriteRenderer), typeof(BoxCollider2D), typeof(TurretController)); // turret creation turret.transform.SetParent(towerBase.transform.GetChild(turretCount)); TurretController script = turret.GetComponent <TurretController>(); // setting up TurretController script using turretData script.turretData = turretData; script.UpdateVariables(); Transform newShootpoint = new GameObject("Shootpoint").transform; newShootpoint.SetParent(turret.transform); script.shootPoint = newShootpoint; script.shootPoint.localPosition = new Vector2(-turretData.shootpointOffsetX, 0); script.targetTag = "Friendly"; script.bullet = bullet; turret.transform.localPosition = Vector2.zero; // moving turret to middle of tower block SpriteRenderer rend = turret.GetComponent <SpriteRenderer>(); // setting up sprite (half transparent, sorting order) rend.sprite = turretData.artwork; rend.sortingOrder = towerCount + 1; rend.flipX = true; turret.GetComponent <BoxCollider2D>().size = new Vector2(1, 1); turret.GetComponent <BoxCollider2D>().isTrigger = true; turretCount++; } else if (gold >= turretData.price) // replace existing turrets with new ones { TurretController[] tc = towerBase.GetComponentsInChildren <TurretController>(); foreach (var controller in tc) { if (!controller.turretData.Equals(this.turretData)) { gold -= turretData.price; gold += controller.turretData.price; controller.turretData = turretData; controller.UpdateVariables(); return; } } } } else { return; } }