0% found this document useful (0 votes)
1 views

Program Code

The document contains multiple C# scripts for a game, including a MobSpawner that spawns slimes, a MovementScript for controlling a mob's movement, an EnemyHealth script for managing enemy health, a TowerStats script for tower behavior, and a WarriorTowerPlacer for placing towers in the game. Each script includes essential functionalities such as spawning, movement, health management, attacking enemies, and placing towers with specified stats. The scripts utilize Unity's MonoBehaviour and coroutine features for game mechanics.

Uploaded by

theimpossbleissa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Program Code

The document contains multiple C# scripts for a game, including a MobSpawner that spawns slimes, a MovementScript for controlling a mob's movement, an EnemyHealth script for managing enemy health, a TowerStats script for tower behavior, and a WarriorTowerPlacer for placing towers in the game. Each script includes essential functionalities such as spawning, movement, health management, attacking enemies, and placing towers with specified stats. The scripts utilize Unity's MonoBehaviour and coroutine features for game mechanics.

Uploaded by

theimpossbleissa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

‭ 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();‬

i‭f (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());‬
‭}‬

I‭Enumerator 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;‬

i‭f (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;‬

i‭f (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)‬


‭{‬
i‭f (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!");‬
‭}‬
‭}‬
‭}‬

You might also like