ublic class MobSpawner : MonoBehaviour
p
{
public GameObject Slime; // Full Code Credit : Ahanaf
public Transform spawnPoint;
public Button spawnButton;
public float spawnDelay = 0.5f;
rivate List<GameObject> activeSlimes = new List<GameObject>();
p
private bool isSpawning = false;
rivate int currentWave = 1;
p
public int slimeIncrement = 2;
oid Update()
v
{
CleanupSlimeList();
if (activeSlimes.Count == 0 && !isSpawning && spawnButton != null && !spawnButton.interactable)
{
spawnButton.interactable = true;
}
{
currentWave++;
StartCoroutine(SpawnSlimesWithDelay());
}
}
public void OnSpawnButtonPressed()
{
if (spawnButton != null)
spawnButton.interactable = false;
StartCoroutine(SpawnSlimesWithDelay());
}
IEnumerator SpawnSlimesWithDelay()
{
isSpawning = true;
int slimeCountThisWave = baseSlimeCount + slimeIncrement * (currentWave - 1);
f or (int i = 0; i < slimeCountThisWave; i++)
{
Vector3 spawnPos = spawnPoint != null ? spawnPoint.position : Vector3.zero;
GameObject slime = Instantiate(Slime, spawnPos, Quaternion.identity);
activeSlimes.Add(slime);
yield return new WaitForSeconds(spawnDelay);
}
isSpawning = false;
}
oid CleanupSlimeList()
v
{
activeSlimes.RemoveAll(slime => slime == null);
}
}
ublic class MovementScript : MonoBehaviour
p
{
public GameObject Mob;
ublic Vector3 UpDirection = Vector3.up;
p
public Vector3 DownDirection = Vector3.down;
public Vector3 LeftDirection = Vector3.left;
public Vector3 RightDirection = Vector3.right;
public float moveSpeed = 5f;
private Vector3 moveDirection = Vector3.zero;
oid Update()
v
{
if (moveDirection != Vector3.zero)
{
Mob.transform.position += moveDirection.normalized * moveSpeed * Time.deltaTime;
}
}
rivate void OnTriggerEnter2D(Collider2D other)
p
{
if (other.CompareTag("Upwards"))
{
moveDirection += UpDirection;
}
else if (other.CompareTag("Downwards"))
{
moveDirection += DownDirection;
}
else if (other.CompareTag("Leftwards"))
{
moveDirection += LeftDirection;
}
else if (other.CompareTag("Rightwards"))
{
moveDirection += RightDirection;
}
if (other.CompareTag("RemoveEnd"))
{
Destroy(Mob);
}
}
rivate void OnTriggerExit2D(Collider2D other)
p
{
if (other.CompareTag("Upwards"))
{
moveDirection -= UpDirection;
}
else if (other.CompareTag("Downwards"))
{
moveDirection -= DownDirection;
}
else if (other.CompareTag("Leftwards"))
{
moveDirection -= LeftDirection;
}
else if (other.CompareTag("Rightwards"))
{
moveDirection -= RightDirection;
}
}
}
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 50f;
private float currentHealth;
oid Start()
v
{
currentHealth = maxHealth;
}
ublic void TakeDamage(float amount)
p
{
currentHealth -= amount;
if (currentHealth <= 0)
{
Destroy(gameObject);
}
}
}
public class TowerStats : MonoBehaviour
{
ublic float damage = 10f;
p
public float range = 2f;
public float attackRate = 1f; // Time between attacks in seconds
private float attackTimer = 0f;
oid Update()
v
{
attackTimer -= Time.deltaTime;
if (attackTimer <= 0f)
{
GameObject target = FindNearestEnemy();
if (target != null)
{
Attack(target);
attackTimer = attackRate;
}
}
}
ameObject FindNearestEnemy()
G
{
Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, range);
float closestDistance = float.MaxValue;
GameObject closestEnemy = null;
f oreach (Collider2D hit in hits)
{
if (hit.CompareTag("Enemy"))
{
float dist = Vector2.Distance(transform.position, hit.transform.position);
if (dist < closestDistance)
{
closestDistance = dist;
closestEnemy = hit.gameObject;
}
}
}
return closestEnemy;
}
oid Attack(GameObject enemy)
v
{
EnemyHealth eh = enemy.GetComponent<EnemyHealth>();
if (eh != null)
{
eh.TakeDamage(damage);
}
}
oid OnDrawGizmosSelected()
v
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, range);
}
}
ublic class WarriorTowerPlacer : MonoBehaviour
p
{
public GameObject warriorTowerPrefab; // Assign in Inspector
public Vector2 placementPosition = new Vector2(0, 0); // Coordinate to place the tower
[ Header("Warrior Stats")]
public float damage = 10f;
public float range = 2f;
oid Update()
v
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
PlaceWarriorTower();
}
}
oid PlaceWarriorTower()
v
{
if (warriorTowerPrefab != null)
{
GameObject tower = Instantiate(warriorTowerPrefab, placementPosition, Quaternion.identity);
// Try to assign damage and range if the tower has a TowerStats component
TowerStats stats = tower.GetComponent<TowerStats>();
if (stats != null)
{
stats.damage = damage;
stats.range = range;
}
else
{
Debug.LogWarning("The Warrior Tower prefab is missing a TowerStats component!");
}
}
else
{
Debug.LogWarning("Warrior Tower Prefab is not assigned!");
}
}
}