HandleMovement + Basic NavMesh
This commit is contained in:
parent
d4a32d9593
commit
47a59a5440
18 changed files with 3104 additions and 52 deletions
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
public class AttackHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float damage;
|
||||
|
@ -10,6 +11,12 @@ public class AttackHandler : MonoBehaviour
|
|||
[SerializeField] private float knockback;
|
||||
|
||||
private float _timer;
|
||||
private Unit _unit;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
@ -23,7 +30,7 @@ public class AttackHandler : MonoBehaviour
|
|||
|
||||
/// <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
|
||||
/// see what Units are in the attackShape, apply damage and knockback to those unit if they're ennemies
|
||||
/// </summary>
|
||||
public bool Attack()
|
||||
{
|
||||
|
@ -35,6 +42,8 @@ public class AttackHandler : MonoBehaviour
|
|||
if (!target.CompareTag("Unit")) continue;
|
||||
// GetComponent is expensive in performance, optimize here if it's slow
|
||||
Unit unit = target.GetComponent<Unit>();
|
||||
// No friendly fire
|
||||
if (unit.IsTeamA == _unit.IsTeamA) continue;
|
||||
unit.Health.TakeDamage(damage);
|
||||
Vector3 knockbackVector = knockback * (target.transform.position - transform.position).normalized;
|
||||
unit.Body.AddForce(knockbackVector, ForceMode.Impulse);
|
||||
|
|
10
Assets/Scripts/GlobalsVariable.cs
Normal file
10
Assets/Scripts/GlobalsVariable.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public static class GlobalsVariable
|
||||
{
|
||||
public static List<Unit> AliveUnitsTeamA = new List<Unit>();
|
||||
public static List<Unit> AliveUnitsTeamB = new List<Unit>();
|
||||
|
||||
}
|
2
Assets/Scripts/GlobalsVariable.cs.meta
Normal file
2
Assets/Scripts/GlobalsVariable.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: aff398f48f2c36187b8027e20ad9dc16
|
|
@ -1,10 +1,18 @@
|
|||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
public class HealthHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float maxHealth;
|
||||
[SerializeField] private float currentHealth;
|
||||
[SerializeField] private float armor;
|
||||
|
||||
private Unit _unit;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
|
@ -39,6 +47,14 @@ public class HealthHandler : MonoBehaviour
|
|||
public void Death()
|
||||
{
|
||||
print("you dead");
|
||||
if (_unit.IsTeamA)
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamA.Remove(_unit);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Remove(_unit);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
65
Assets/Scripts/MovementHandler.cs
Normal file
65
Assets/Scripts/MovementHandler.cs
Normal file
|
@ -0,0 +1,65 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
public class MovementHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float speed;
|
||||
[SerializeField] private NavMeshAgent agent;
|
||||
|
||||
[SerializeField] private Transform defaultMoveTarget;
|
||||
|
||||
private Unit _unit;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
}
|
||||
|
||||
public void ChangeSpeed(float newSpeed)
|
||||
{
|
||||
speed = newSpeed;
|
||||
}
|
||||
|
||||
public void StopMoving()
|
||||
{
|
||||
agent.speed = 0;
|
||||
}
|
||||
|
||||
public void ResumeMoving()
|
||||
{
|
||||
agent.speed = speed;
|
||||
}
|
||||
|
||||
public void MoveTowards(Vector3 destination)
|
||||
{
|
||||
agent.SetDestination(destination);
|
||||
}
|
||||
|
||||
public void MoveTowardsNearest()
|
||||
{
|
||||
MoveTowards(FindNearestEnemy().transform.position);
|
||||
}
|
||||
|
||||
Unit FindNearestEnemy()
|
||||
{
|
||||
List<Unit> enemies = _unit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
|
||||
return enemies
|
||||
.Aggregate((nearest, current) =>
|
||||
(current.transform.position - transform.position).sqrMagnitude <
|
||||
(nearest.transform.position - transform.position).sqrMagnitude
|
||||
? current
|
||||
: nearest);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
//MoveTowards(defaultMoveTarget.position);
|
||||
MoveTowardsNearest();
|
||||
}
|
||||
}
|
||||
}
|
2
Assets/Scripts/MovementHandler.cs.meta
Normal file
2
Assets/Scripts/MovementHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: aa245c9f3dff7f6b9888b009a1023628
|
|
@ -1,19 +1,41 @@
|
|||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class Unit : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField] public Rigidbody Body { get; private set; }
|
||||
[field: SerializeField] public HealthHandler Health { get; private set; }
|
||||
[field: SerializeField] public AttackHandler Attack { get; private set; }
|
||||
[field: SerializeField] public MovementHandler Move { get; private set; }
|
||||
|
||||
[field: SerializeField] public bool IsTeamA { get; private set; }
|
||||
|
||||
[SerializeField] private int price;
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
void OnValidate()
|
||||
{
|
||||
Debug.Assert(Body != null);
|
||||
Debug.Assert(Health != null);
|
||||
Debug.Assert(Attack != null);
|
||||
Debug.Assert(Move != null);
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
||||
if (IsTeamA)
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamA.Add(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Add(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue