Quelques trucs pour structuer
This commit is contained in:
parent
af46b49095
commit
1165e50bb5
15 changed files with 245 additions and 2 deletions
8
Assets/Scripts.meta
Normal file
8
Assets/Scripts.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: e5378d644a3d2139389083d95ed259ce
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
62
Assets/Scripts/AttackHandler.cs
Normal file
62
Assets/Scripts/AttackHandler.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AttackHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float damage;
|
||||
[SerializeField] private float cooldown;
|
||||
[SerializeField] private Collider attackShape;
|
||||
|
||||
private float _timer;
|
||||
|
||||
void Start()
|
||||
{
|
||||
_timer = cooldown;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
_timer = Mathf.Max(_timer - Time.deltaTime, 0f);
|
||||
}
|
||||
|
||||
public bool Attack()
|
||||
{
|
||||
if (_timer >= 0) return false;
|
||||
Collider[] targets = DetectTargets();
|
||||
foreach (Collider target in targets)
|
||||
{
|
||||
if (!target.CompareTag("Unit")) continue;
|
||||
// GetComponent is expensive in performance, optimize here if it's slow
|
||||
Unit unit = target.GetComponent<Unit>();
|
||||
unit.healthHandler.TakeDamage(damage);
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Collider[] DetectTargets()
|
||||
{
|
||||
// Make sure to manager layers for better performance
|
||||
|
||||
List<Unit> targets = new List<Unit>();
|
||||
Collider[] hitColliders;
|
||||
|
||||
switch (attackShape)
|
||||
{
|
||||
case SphereCollider sphere:
|
||||
hitColliders = Physics.OverlapSphere(sphere.transform.position, sphere.radius, sphere.includeLayers);
|
||||
break;
|
||||
case BoxCollider box:
|
||||
hitColliders = Physics.OverlapBox(box.bounds.center, box.bounds.extents, box.transform.rotation, box.includeLayers);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Only sphere or box are supported");
|
||||
}
|
||||
|
||||
return hitColliders;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
2
Assets/Scripts/AttackHandler.cs.meta
Normal file
2
Assets/Scripts/AttackHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eb03fb8c097f016f09d345ce200c3f41
|
38
Assets/Scripts/HealthHandler.cs
Normal file
38
Assets/Scripts/HealthHandler.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class HealthHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float maxHealth;
|
||||
[SerializeField] private float currentHealth;
|
||||
[SerializeField] private float armor;
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
Debug.Assert(damage >= 0, "Damage cannot be negative, use Heal if you want to heal");
|
||||
currentHealth -= Mathf.Max(0, damage-armor);
|
||||
}
|
||||
|
||||
public void Heal(float value)
|
||||
{
|
||||
Debug.Assert(value >= 0, "value can't be less than zero");
|
||||
currentHealth = Mathf.Min(currentHealth + value, maxHealth);
|
||||
}
|
||||
|
||||
public float GetArmor()
|
||||
{
|
||||
return armor;
|
||||
}
|
||||
|
||||
public void EquipArmor(float armorBoost)
|
||||
{
|
||||
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use UnEquipArmor instead");
|
||||
armor += armorBoost;
|
||||
}
|
||||
|
||||
public void UnEquipArmor(float armorBoost)
|
||||
{
|
||||
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use EquipArmor instead");
|
||||
armor -= armorBoost;
|
||||
}
|
||||
|
||||
}
|
2
Assets/Scripts/HealthHandler.cs.meta
Normal file
2
Assets/Scripts/HealthHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8511709ac7cfc1d05bee202fa8b8b095
|
20
Assets/Scripts/Unit.cs
Normal file
20
Assets/Scripts/Unit.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class Unit : MonoBehaviour
|
||||
{
|
||||
[SerializeField] public HealthHandler healthHandler;
|
||||
[SerializeField] public AttackHandler attackHandler;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Null safety enjoyers things
|
||||
Debug.Assert(healthHandler != null);
|
||||
Debug.Assert(attackHandler != null);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
2
Assets/Scripts/Unit.cs.meta
Normal file
2
Assets/Scripts/Unit.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9815bed5dc151c8c6a271899ed1b7c4f
|
Loading…
Add table
Add a link
Reference in a new issue