AbstractUnit for cross team compatibility

+ New folder in Scripts
This commit is contained in:
Crizomb 2025-01-09 11:44:24 +01:00
parent 385d8669b1
commit f8e85d7134
18 changed files with 112 additions and 80 deletions

View file

@ -0,0 +1,10 @@
using UnityEngine;
public abstract class AbstractUnit : MonoBehaviour
{
public float price;
[field: SerializeField] public bool IsTeamA { get; private set; }
public abstract bool Attack();
public abstract void TakeDamage(float damage);
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c34c1a29dc0657256b674f37fe8466d9

View file

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
[RequireComponent(typeof(MinecraftUnit))]
public class AttackHandler : MonoBehaviour
{
[SerializeField] private float damage;
[SerializeField] private float cooldown;
[SerializeField] private Collider attackShape;
[SerializeField] private float knockbackHorizontalForce;
[SerializeField] private float knockbackVerticalForce;
private float _timer;
private MinecraftUnit _minecraftUnit;
void Awake()
{
_minecraftUnit = GetComponent<MinecraftUnit>();
}
void Start()
{
// Random to avoid too much synchronicity
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
}
void Update()
{
_timer = _timer - Time.deltaTime;
Attack();
}
/// <summary>
/// Launch an Attack, and return true if it's possible to attack
/// see what Units are in the attackShape, apply damage and knockback to those unit if they're ennemies
/// </summary>
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
AbstractUnit targetUnit = target.GetComponent<AbstractUnit>();
// No friendly fire
if (targetUnit.IsTeamA == _minecraftUnit.IsTeamA) continue;
targetUnit.TakeDamage(damage);
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
+ knockbackVerticalForce * Vector3.up;
// Knockback logic specific to MinecraftUnit (can't force other team to do our weird impl)
if (targetUnit is MinecraftUnit)
{
MinecraftUnit minecraftTarget = (MinecraftUnit)targetUnit;
minecraftTarget.StartCoroutine(minecraftTarget.MovementHandler.TakeImpulse(knockbackVector));
}
}
_timer = cooldown + Random.Range(-cooldown*0.2f, cooldown*0.2f);
return true;
}
private Collider[] DetectTargets()
{
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;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: eb03fb8c097f016f09d345ce200c3f41

View file

@ -0,0 +1,29 @@
using UnityEngine;
using UnityEngine.InputSystem;
public class CameraMouvement : MonoBehaviour
{
[SerializeField] private float moveSpeed = 5f;
private Vector2 _moveInput;
public void HandleCameraMovement(InputAction.CallbackContext context)
{
_moveInput = Vector2.zero;
if (context.phase == InputActionPhase.Performed)
{
_moveInput = context.ReadValue<Vector2>();
}
}
// 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()
{
var mouvement = moveSpeed * Time.deltaTime * (new Vector3(_moveInput.y, 0, -_moveInput.x));
transform.Translate(mouvement, Space.World);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9c8ad113b549ec9a0ac0bedf3875c19e

View file

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public static class GlobalsVariable
{
public static List<MinecraftUnit> AliveUnitsTeamA = new List<MinecraftUnit>();
public static List<MinecraftUnit> AliveUnitsTeamB = new List<MinecraftUnit>();
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aff398f48f2c36187b8027e20ad9dc16

View file

@ -0,0 +1,62 @@
using UnityEngine;
[RequireComponent(typeof(MinecraftUnit))]
public class HealthHandler : MonoBehaviour
{
[SerializeField] private float maxHealth;
[SerializeField] private float currentHealth;
[SerializeField] private float armor;
private MinecraftUnit _minecraftUnit;
public void Awake()
{
_minecraftUnit = GetComponent<MinecraftUnit>();
}
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);
if (currentHealth <= 0) Death();
}
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;
}
public void Death()
{
print("you dead");
if (_minecraftUnit.IsTeamA)
{
GlobalsVariable.AliveUnitsTeamA.Remove(_minecraftUnit);
}
else
{
GlobalsVariable.AliveUnitsTeamB.Remove(_minecraftUnit);
}
Destroy(gameObject);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8511709ac7cfc1d05bee202fa8b8b095

View file

@ -0,0 +1,48 @@
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
[DisallowMultipleComponent]
[RequireComponent(typeof(Rigidbody))]
public class MinecraftUnit : AbstractUnit
{
[field: SerializeField] public Rigidbody Body { get; private set; }
[field: SerializeField] public HealthHandler HealthHandler { get; private set; }
[field: SerializeField] public AttackHandler AttackHandler { get; private set; }
[field: SerializeField] public MovementHandler MovementHandler { get; private set; }
void OnValidate()
{
Debug.Assert(Body != null);
Debug.Assert(HealthHandler != null);
Debug.Assert(AttackHandler != null);
Debug.Assert(MovementHandler != null);
}
void Awake()
{
if (IsTeamA)
{
GlobalsVariable.AliveUnitsTeamA.Add(this);
}
else
{
GlobalsVariable.AliveUnitsTeamB.Add(this);
}
}
// Abstract implementation for compatibility with other team
public override bool Attack()
{
return AttackHandler.Attack();
}
public override void TakeDamage(float damage)
{
HealthHandler.TakeDamage(damage);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9815bed5dc151c8c6a271899ed1b7c4f

View file

@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem.iOS;
[RequireComponent(typeof(MinecraftUnit))]
[RequireComponent(typeof(Rigidbody))]
public class MovementHandler : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private NavMeshAgent agent;
[SerializeField] private Transform defaultMoveTarget;
private MinecraftUnit _minecraftUnit;
private Rigidbody _rigidbody;
void Awake()
{
_minecraftUnit = GetComponent<MinecraftUnit>();
_rigidbody = GetComponent<Rigidbody>();
}
public void ChangeSpeed(float newSpeed)
{
speed = newSpeed;
}
public void StopMoving()
{
agent.speed = 0;
}
public void ResumeMoving()
{
agent.speed = speed;
}
public void MoveTowards(Vector3 destination)
{
if (agent.enabled) agent.SetDestination(destination);
}
public void MoveTowardsNearest()
{
MoveTowards(FindNearestEnemy().transform.position);
}
MinecraftUnit FindNearestEnemy()
{
List<MinecraftUnit> enemies = _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
MinecraftUnit closestMinecraftUnit = null;
float closestDistance = float.MaxValue;
foreach (MinecraftUnit enemy in enemies)
{
float distanceToEnemy = (enemy.transform.position - transform.position).sqrMagnitude;
if (distanceToEnemy < closestDistance)
{
closestMinecraftUnit = enemy;
closestDistance = distanceToEnemy;
}
}
return closestMinecraftUnit;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//MoveTowards(defaultMoveTarget.position);
MoveTowardsNearest();
}
}
public IEnumerator TakeImpulse(Vector3 impulse)
{
// Unity navmesh, can't handle physics (it rewrite velocity). So we deactivate it when applying force.
agent.enabled = false;
_rigidbody.AddForce(impulse, ForceMode.Impulse);
yield return new WaitForSeconds(0.5f);
float noSurfaceTime = 0.0f;
// Make sure to be on the navmesh surface before reactivating agent
while (_rigidbody != null && !NavMesh.SamplePosition(_rigidbody.position, out _, 1.0f, NavMesh.AllAreas))
{
yield return new WaitForSeconds(0.5f);
noSurfaceTime += 0.5f;
// Die if exited navMesh for to long
if (noSurfaceTime > 6.0f)
{
_minecraftUnit.HealthHandler.Death();
yield break;
}
}
if (agent != null) agent.enabled = true;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: aa245c9f3dff7f6b9888b009a1023628