Initial Commit
This commit is contained in:
35
Assets/Scripts/AttackPoint.cs
Normal file
35
Assets/Scripts/AttackPoint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class AttackPoint : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int maxAttackers;
|
||||
private int curAttackers;
|
||||
private bool isFull;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
public bool CheckIfFull()
|
||||
{
|
||||
if(curAttackers >= maxAttackers)
|
||||
isFull = true;
|
||||
else
|
||||
isFull = false;
|
||||
return isFull;
|
||||
}
|
||||
public void AddAttacker()
|
||||
{
|
||||
curAttackers++;
|
||||
}
|
||||
public void RemoveAttacker()
|
||||
{
|
||||
curAttackers--;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AttackPoint.cs.meta
Normal file
2
Assets/Scripts/AttackPoint.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f8b74343418b9e49aa2bd66989e1654
|
||||
37
Assets/Scripts/Building.cs
Normal file
37
Assets/Scripts/Building.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Building : MonoBehaviour
|
||||
{
|
||||
public float maxHealth;
|
||||
private float curHealth;
|
||||
private Slider healthBar;
|
||||
void Awake()
|
||||
{
|
||||
healthBar = GetComponentInChildren<Slider>();
|
||||
}
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
curHealth = maxHealth;
|
||||
healthBar.maxValue = maxHealth;
|
||||
healthBar.value = curHealth;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
public void TakeDamage(float damageToTake)
|
||||
{
|
||||
curHealth -= damageToTake;
|
||||
if(curHealth <= 0)
|
||||
gameObject.SetActive(false);
|
||||
healthBar.value = curHealth;
|
||||
}
|
||||
public float GetHealth()
|
||||
{
|
||||
return curHealth;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Building.cs.meta
Normal file
2
Assets/Scripts/Building.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23ebf1feafb4aa845bff0746469fb9ec
|
||||
92
Assets/Scripts/EnemyController.cs
Normal file
92
Assets/Scripts/EnemyController.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class EnemyController : MonoBehaviour
|
||||
{
|
||||
public float moveSpeed;
|
||||
public float targetRange;
|
||||
public float attackRange;
|
||||
public float attackSpeed;
|
||||
public float attackDamage;
|
||||
public float health;
|
||||
public Transform defaultTarget;
|
||||
public Transform target;
|
||||
private NavMeshAgent navMeshAgent;
|
||||
private Animator animator;
|
||||
private SphereCollider targetCollider;
|
||||
private float attackCooldown;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
navMeshAgent = GetComponent<NavMeshAgent>();
|
||||
animator = GetComponentInChildren<Animator>();
|
||||
targetCollider = GetComponentInChildren<SphereCollider>();
|
||||
navMeshAgent.speed = moveSpeed;
|
||||
targetCollider.radius = targetRange;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
MoveToDefault();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(target != null && !target.gameObject.activeInHierarchy)
|
||||
{
|
||||
target = null;
|
||||
MoveToDefault();
|
||||
}
|
||||
|
||||
if(target != null)
|
||||
Attack();
|
||||
|
||||
animator.SetFloat("Speed", navMeshAgent.velocity.magnitude);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if(target != null) return;
|
||||
if(!other.CompareTag("Gate")) return;
|
||||
|
||||
AttackPoint attackPoint = other.GetComponentInChildren<AttackPoint>();
|
||||
if(attackPoint != null)
|
||||
{
|
||||
target = attackPoint.transform;
|
||||
navMeshAgent.stoppingDistance = attackRange;
|
||||
navMeshAgent.destination = attackPoint.transform.position;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveToDefault()
|
||||
{
|
||||
navMeshAgent.stoppingDistance = 0f;
|
||||
if(defaultTarget != null)
|
||||
navMeshAgent.destination = defaultTarget.position;
|
||||
}
|
||||
|
||||
private void Attack()
|
||||
{
|
||||
attackCooldown -= Time.deltaTime;
|
||||
if(Vector3.Distance(target.position, transform.position) < attackRange && attackCooldown <= 0f)
|
||||
{
|
||||
transform.LookAt(target.position);
|
||||
animator.SetTrigger("Attack");
|
||||
attackCooldown = 1f / attackSpeed;
|
||||
target.GetComponent<AttackPoint>().AddAttacker();
|
||||
target.GetComponentInParent<Building>().TakeDamage(attackDamage);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
// Target range — green if priority target is assigned, red if not
|
||||
Gizmos.color = target != null ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, targetRange);
|
||||
|
||||
// Attack range — green if target is within attack range, red if not
|
||||
bool inAttackRange = target != null && Vector3.Distance(target.position, transform.position) < attackRange;
|
||||
Gizmos.color = inAttackRange ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, attackRange);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/EnemyController.cs.meta
Normal file
2
Assets/Scripts/EnemyController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a19abd9de0411d40bf12f005040fb23
|
||||
16
Assets/Scripts/EnemyDetector.cs
Normal file
16
Assets/Scripts/EnemyDetector.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyDetector : MonoBehaviour
|
||||
{
|
||||
private EnemyController enemyController;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
enemyController = GetComponentInParent<EnemyController>();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
enemyController.OnDetectorTriggerEnter(other);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/EnemyDetector.cs.meta
Normal file
2
Assets/Scripts/EnemyDetector.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 137b6d86a1dcfa94b84dee4c8c248fab
|
||||
28
Assets/Scripts/SimpleEnemySpawner.cs
Normal file
28
Assets/Scripts/SimpleEnemySpawner.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SimpleEnemySpawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private EnemyController enemyToSpawn;
|
||||
[SerializeField] private Transform spawnPoint;
|
||||
[SerializeField] private float timeBetweenSpawns;
|
||||
[SerializeField] private int amountToSpawn;
|
||||
private float spawnCounter;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
spawnCounter = timeBetweenSpawns;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
spawnCounter -= Time.deltaTime;
|
||||
if(spawnCounter <= 0f)
|
||||
{
|
||||
spawnCounter = timeBetweenSpawns;
|
||||
Instantiate(enemyToSpawn, spawnPoint.position, spawnPoint.rotation);
|
||||
amountToSpawn--;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/SimpleEnemySpawner.cs.meta
Normal file
2
Assets/Scripts/SimpleEnemySpawner.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d054caeeff053f94683ce391a2be170c
|
||||
Reference in New Issue
Block a user