AbstractUnit for cross team compatibility
+ New folder in Scripts
This commit is contained in:
parent
385d8669b1
commit
f8e85d7134
18 changed files with 112 additions and 80 deletions
|
@ -179,12 +179,12 @@ MonoBehaviour:
|
|||
m_Script: {fileID: 11500000, guid: 9815bed5dc151c8c6a271899ed1b7c4f, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
<Body>k__BackingField: {fileID: 3280843376750909586}
|
||||
<Health>k__BackingField: {fileID: 8213395333053285225}
|
||||
<Attack>k__BackingField: {fileID: 8908285060348773052}
|
||||
<Move>k__BackingField: {fileID: 6587310683195830629}
|
||||
<IsTeamA>k__BackingField: 1
|
||||
price: 0
|
||||
<IsTeamA>k__BackingField: 1
|
||||
<Body>k__BackingField: {fileID: 3280843376750909586}
|
||||
<HealthHandler>k__BackingField: {fileID: 8213395333053285225}
|
||||
<AttackHandler>k__BackingField: {fileID: 8908285060348773052}
|
||||
<MovementHandler>k__BackingField: {fileID: 6587310683195830629}
|
||||
--- !u!114 &8213395333053285225
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
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>();
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
8
Assets/Scripts/UnitScripts.meta
Normal file
8
Assets/Scripts/UnitScripts.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8a05c17988617d46a9233519a2dfbf3c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
Assets/Scripts/UnitScripts/AbstractUnit.cs
Normal file
10
Assets/Scripts/UnitScripts/AbstractUnit.cs
Normal 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);
|
||||
|
||||
}
|
2
Assets/Scripts/UnitScripts/AbstractUnit.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/AbstractUnit.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c34c1a29dc0657256b674f37fe8466d9
|
|
@ -4,7 +4,7 @@ using UnityEngine;
|
|||
using UnityEngine.Serialization;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
public class AttackHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float damage;
|
||||
|
@ -14,11 +14,11 @@ public class AttackHandler : MonoBehaviour
|
|||
[SerializeField] private float knockbackVerticalForce;
|
||||
|
||||
private float _timer;
|
||||
private Unit _unit;
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
_minecraftUnit = GetComponent<MinecraftUnit>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
|
@ -46,16 +46,22 @@ public class AttackHandler : MonoBehaviour
|
|||
{
|
||||
if (!target.CompareTag("Unit")) continue;
|
||||
// GetComponent is expensive in performance, optimize here if it's slow
|
||||
Unit targetUnit = target.GetComponent<Unit>();
|
||||
AbstractUnit targetUnit = target.GetComponent<AbstractUnit>();
|
||||
|
||||
// No friendly fire
|
||||
if (targetUnit.IsTeamA == _unit.IsTeamA) continue;
|
||||
if (targetUnit.IsTeamA == _minecraftUnit.IsTeamA) continue;
|
||||
|
||||
targetUnit.TakeDamage(damage);
|
||||
|
||||
targetUnit.Health.TakeDamage(damage);
|
||||
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
|
||||
+ knockbackVerticalForce * Vector3.up;
|
||||
|
||||
targetUnit.StartCoroutine(targetUnit.Move.TakeImpulse(knockbackVector));
|
||||
// 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;
|
10
Assets/Scripts/UnitScripts/GlobalsVariable.cs
Normal file
10
Assets/Scripts/UnitScripts/GlobalsVariable.cs
Normal 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>();
|
||||
|
||||
}
|
|
@ -1,17 +1,17 @@
|
|||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
public class HealthHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float maxHealth;
|
||||
[SerializeField] private float currentHealth;
|
||||
[SerializeField] private float armor;
|
||||
|
||||
private Unit _unit;
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
_minecraftUnit = GetComponent<MinecraftUnit>();
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
|
@ -47,13 +47,13 @@ public class HealthHandler : MonoBehaviour
|
|||
public void Death()
|
||||
{
|
||||
print("you dead");
|
||||
if (_unit.IsTeamA)
|
||||
if (_minecraftUnit.IsTeamA)
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamA.Remove(_unit);
|
||||
GlobalsVariable.AliveUnitsTeamA.Remove(_minecraftUnit);
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Remove(_unit);
|
||||
GlobalsVariable.AliveUnitsTeamB.Remove(_minecraftUnit);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
48
Assets/Scripts/UnitScripts/MinecraftUnit.cs
Normal file
48
Assets/Scripts/UnitScripts/MinecraftUnit.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ using UnityEngine;
|
|||
using UnityEngine.AI;
|
||||
using UnityEngine.InputSystem.iOS;
|
||||
|
||||
[RequireComponent(typeof(Unit))]
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class MovementHandler : MonoBehaviour
|
||||
{
|
||||
|
@ -14,12 +14,12 @@ public class MovementHandler : MonoBehaviour
|
|||
[SerializeField] private NavMeshAgent agent;
|
||||
[SerializeField] private Transform defaultMoveTarget;
|
||||
|
||||
private Unit _unit;
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_unit = GetComponent<Unit>();
|
||||
_minecraftUnit = GetComponent<MinecraftUnit>();
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
|
@ -48,23 +48,23 @@ public class MovementHandler : MonoBehaviour
|
|||
MoveTowards(FindNearestEnemy().transform.position);
|
||||
}
|
||||
|
||||
Unit FindNearestEnemy()
|
||||
MinecraftUnit FindNearestEnemy()
|
||||
{
|
||||
List<Unit> enemies = _unit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
|
||||
List<MinecraftUnit> enemies = _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
|
||||
|
||||
Unit closestUnit = null;
|
||||
MinecraftUnit closestMinecraftUnit = null;
|
||||
float closestDistance = float.MaxValue;
|
||||
foreach (Unit enemy in enemies)
|
||||
foreach (MinecraftUnit enemy in enemies)
|
||||
{
|
||||
float distanceToEnemy = (enemy.transform.position - transform.position).sqrMagnitude;
|
||||
if (distanceToEnemy < closestDistance)
|
||||
{
|
||||
closestUnit = enemy;
|
||||
closestMinecraftUnit = enemy;
|
||||
closestDistance = distanceToEnemy;
|
||||
}
|
||||
}
|
||||
|
||||
return closestUnit;
|
||||
return closestMinecraftUnit;
|
||||
}
|
||||
|
||||
void Update()
|
||||
|
@ -94,7 +94,7 @@ public class MovementHandler : MonoBehaviour
|
|||
// Die if exited navMesh for to long
|
||||
if (noSurfaceTime > 6.0f)
|
||||
{
|
||||
_unit.Health.Death();
|
||||
_minecraftUnit.HealthHandler.Death();
|
||||
yield break;
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue