witches
This commit is contained in:
parent
ffb1f1caa1
commit
b21d392ff6
80 changed files with 2888 additions and 221 deletions
|
@ -15,6 +15,7 @@ public abstract class AbstractUnit : MonoBehaviour
|
|||
[field: SerializeField] public bool IsQueen { get; private set; }
|
||||
|
||||
public abstract void TakeDamage(float damage);
|
||||
public abstract void Heal(float heal);
|
||||
|
||||
void Awake()
|
||||
{
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Should be attached to the Arrow, not the skeleton
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class ArrowHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float baseDamage;
|
||||
[SerializeField] private float baseKnockback;
|
||||
private Rigidbody _rigidBody;
|
||||
private bool _fromTeamA;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_rigidBody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Destroy after 8s, in all case
|
||||
Destroy(this.gameObject, 8.0f);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Align with speed
|
||||
if (_rigidBody.linearVelocity.magnitude >= 1f) transform.forward = _rigidBody.linearVelocity.normalized;
|
||||
}
|
||||
|
||||
public void LaunchArrow(Vector3 baseSpeed, bool fromTeamA)
|
||||
{
|
||||
_rigidBody.linearVelocity = baseSpeed;
|
||||
_fromTeamA = fromTeamA;
|
||||
}
|
||||
|
||||
void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
// Can be optimized with tags, but it add dependance beetween teams
|
||||
if (collision.gameObject.TryGetComponent<AbstractUnit>(out AbstractUnit unit))
|
||||
{
|
||||
if (unit is MinecraftUnit && unit.IsTeamA != _fromTeamA) // No friendly fire
|
||||
{
|
||||
MinecraftUnit minecraftUnit = unit as MinecraftUnit;
|
||||
Vector3 knockback = _rigidBody.linearVelocity * baseKnockback;
|
||||
minecraftUnit.StartCoroutine(minecraftUnit.MovementHandler.TakeImpulse(knockback));
|
||||
}
|
||||
unit.TakeDamage(baseDamage);
|
||||
}
|
||||
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
|
@ -22,8 +22,6 @@ public class AttackHandler : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
print("coldown");
|
||||
print(cooldown);
|
||||
InvokeRepeating(nameof(Attack), Random.Range(-cooldown*0.2f, cooldown*0.2f), cooldown);
|
||||
}
|
||||
|
||||
|
@ -34,8 +32,8 @@ public class AttackHandler : MonoBehaviour
|
|||
/// </summary>
|
||||
public virtual bool Attack()
|
||||
{
|
||||
|
||||
Collider[] targets = DetectTargets();
|
||||
bool hasHit = false;
|
||||
foreach (Collider target in targets)
|
||||
{
|
||||
if (!target.CompareTag("Unit")) continue;
|
||||
|
@ -46,6 +44,7 @@ public class AttackHandler : MonoBehaviour
|
|||
if (targetUnit.IsTeamA == _minecraftUnit.IsTeamA) continue;
|
||||
|
||||
targetUnit.TakeDamage(damage);
|
||||
hasHit = true;
|
||||
|
||||
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
|
||||
+ knockbackVerticalForce * Vector3.up;
|
||||
|
@ -57,15 +56,15 @@ public class AttackHandler : MonoBehaviour
|
|||
minecraftTarget.StartCoroutine(minecraftTarget.MovementHandler.TakeImpulse(knockbackVector));
|
||||
}
|
||||
|
||||
// Attack animation
|
||||
if (_minecraftUnit.Animator)
|
||||
{
|
||||
_minecraftUnit.Animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return true;
|
||||
// Attack animation
|
||||
if (_minecraftUnit.Animator && hasHit)
|
||||
{
|
||||
_minecraftUnit.Animator.SetTrigger("Attack");
|
||||
}
|
||||
|
||||
return hasHit;
|
||||
}
|
||||
|
||||
private Collider[] DetectTargets()
|
||||
|
@ -75,7 +74,7 @@ public class AttackHandler : MonoBehaviour
|
|||
switch (attackShape)
|
||||
{
|
||||
case SphereCollider sphere:
|
||||
hitColliders = Physics.OverlapSphere(sphere.transform.position, sphere.radius, sphere.includeLayers);
|
||||
hitColliders = Physics.OverlapSphere(transform.position, sphere.radius, sphere.includeLayers);
|
||||
break;
|
||||
case BoxCollider box:
|
||||
hitColliders = Physics.OverlapBox(box.bounds.center, box.bounds.extents, box.transform.rotation, box.includeLayers);
|
||||
|
|
29
Assets/Scripts/UnitScripts/Attacks/CreeperBomb.cs
Normal file
29
Assets/Scripts/UnitScripts/Attacks/CreeperBomb.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class CreeperBomb : AttackHandler
|
||||
{
|
||||
[SerializeField] private GameObject explodeMesh;
|
||||
[SerializeField] private float exploseMeshTime = 0.5f;
|
||||
|
||||
|
||||
public override bool Attack()
|
||||
{
|
||||
bool hasExploded = base.Attack();
|
||||
if (hasExploded)
|
||||
{
|
||||
_minecraftUnit.HealthHandler.Death();
|
||||
CoroutineManager.Instance.StartCoroutine(ExplodeVisual());
|
||||
Destroy(gameObject);
|
||||
}
|
||||
return hasExploded;
|
||||
}
|
||||
|
||||
private IEnumerator ExplodeVisual()
|
||||
{
|
||||
GameObject explosion = Instantiate(explodeMesh, transform.position, Quaternion.identity);
|
||||
explosion.transform.parent = null;
|
||||
yield return new WaitForSeconds(exploseMeshTime);
|
||||
Destroy(explosion);
|
||||
}
|
||||
}
|
2
Assets/Scripts/UnitScripts/Attacks/CreeperBomb.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/Attacks/CreeperBomb.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a3898c60f9d89bde4973aa3d701f6282
|
8
Assets/Scripts/UnitScripts/Attacks/Projectiles.meta
Normal file
8
Assets/Scripts/UnitScripts/Attacks/Projectiles.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 81db3118949a24b2d83ea142891913b3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
24
Assets/Scripts/UnitScripts/Attacks/Projectiles/Arrow.cs
Normal file
24
Assets/Scripts/UnitScripts/Attacks/Projectiles/Arrow.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class Arrow : ProjectileHandler
|
||||
{
|
||||
[SerializeField] private float baseDamage;
|
||||
[SerializeField] private float baseKnockback;
|
||||
|
||||
void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
// Can be optimized with tags, but it add dependance beetween teams
|
||||
if (collision.gameObject.TryGetComponent<AbstractUnit>(out AbstractUnit unit))
|
||||
{
|
||||
if (unit is MinecraftUnit && unit.IsTeamA != FromTeamA) // No friendly fire
|
||||
{
|
||||
MinecraftUnit minecraftUnit = unit as MinecraftUnit;
|
||||
Vector3 knockback = RigidBody.linearVelocity * baseKnockback;
|
||||
minecraftUnit.StartCoroutine(minecraftUnit.MovementHandler.TakeImpulse(knockback));
|
||||
}
|
||||
unit.TakeDamage(baseDamage);
|
||||
}
|
||||
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 36e1a3cc9e04b444cadcfe2b444d0abc
|
|
@ -1,6 +1,6 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class AttackSkeleton : AttackHandler
|
||||
public class AttackProjectile : AttackHandler
|
||||
{
|
||||
[SerializeField] private GameObject arrowPrefab;
|
||||
[SerializeField] private float arrowBaseSpeed;
|
||||
|
@ -18,7 +18,7 @@ public class AttackSkeleton : AttackHandler
|
|||
if (launchAngle < 0) return false;
|
||||
|
||||
GameObject arrow = Instantiate(arrowPrefab, spawnPos.position, spawnPos.rotation);
|
||||
ArrowHandler arrowHandler = arrow.GetComponent<ArrowHandler>();
|
||||
ProjectileHandler projectileHandler = arrow.GetComponent<ProjectileHandler>();
|
||||
// In target <-> launcher + transform.up basis
|
||||
Vector2 localLaunchVector = arrowBaseSpeed * new Vector2(Mathf.Cos(launchAngle), Mathf.Sin(launchAngle));
|
||||
// Transform it in global basis
|
||||
|
@ -26,7 +26,7 @@ public class AttackSkeleton : AttackHandler
|
|||
Vector3 diffVector = Vector3.ProjectOnPlane(targetUnit.transform.position - spawnPos.position, Vector3.up);
|
||||
|
||||
Vector3 launchVectorNormalized = (localLaunchVector.x * diffVector.normalized + localLaunchVector.y * Vector3.up).normalized;
|
||||
arrowHandler.LaunchArrow(launchVectorNormalized * arrowBaseSpeed, _minecraftUnit.IsTeamA);
|
||||
projectileHandler.LaunchProjectile(launchVectorNormalized * arrowBaseSpeed, _minecraftUnit.IsTeamA);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -36,9 +36,10 @@ public class AttackSkeleton : AttackHandler
|
|||
// Source : https://en.wikipedia.org/wiki/Projectile_motion#Angle_%CE%B8_required_to_hit_coordinate_(x,_y)
|
||||
|
||||
AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit;
|
||||
Vector3 diffVector = Vector3.ProjectOnPlane(targetUnit.transform.position - spawnPos.position, Vector3.up);
|
||||
Vector3 diffVector = targetUnit.transform.position - spawnPos.position;
|
||||
Vector3 projectOnPlane = Vector3.ProjectOnPlane(diffVector, Vector3.up);
|
||||
|
||||
float x = Vector3.ProjectOnPlane(diffVector, Vector3.up).magnitude;
|
||||
float x = Vector3.ProjectOnPlane(projectOnPlane, Vector3.up).magnitude;
|
||||
float y = diffVector.y;
|
||||
float g = Physics.gravity.magnitude;
|
||||
float v = arrowBaseSpeed;
|
|
@ -0,0 +1,38 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public class HealthPotion : ProjectileHandler
|
||||
{
|
||||
[SerializeField] private float healthAdd;
|
||||
[SerializeField] private SphereCollider healthPotionEffectArea;
|
||||
[SerializeField] private GameObject explodeMesh;
|
||||
[SerializeField] private float exploseMeshTime = 0.5f;
|
||||
|
||||
void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
Collider[] targets = Physics.OverlapSphere(transform.position, healthPotionEffectArea.radius, healthPotionEffectArea.includeLayers);
|
||||
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 EnemyHealing
|
||||
if (targetUnit.IsTeamA != FromTeamA) continue;
|
||||
|
||||
targetUnit.Heal(healthAdd);
|
||||
|
||||
}
|
||||
CoroutineManager.Instance.StartCoroutine(ExplodeVisual());
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private IEnumerator ExplodeVisual()
|
||||
{
|
||||
GameObject explosion = Instantiate(explodeMesh, transform.position, Quaternion.identity);
|
||||
explosion.transform.parent = null;
|
||||
yield return new WaitForSeconds(exploseMeshTime);
|
||||
Destroy(explosion);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fd66c851299240c02a3082ade5b6295d
|
|
@ -0,0 +1,37 @@
|
|||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Should be attached to the Arrow, not the skeleton
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class ProjectileHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] protected float _lifeSpan = 8.0f;
|
||||
protected Rigidbody RigidBody;
|
||||
protected bool FromTeamA;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
RigidBody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Destroy after _lifeSpan, in all case
|
||||
Destroy(this.gameObject, _lifeSpan);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Align with speed
|
||||
if (RigidBody.linearVelocity.magnitude >= 1f) transform.forward = RigidBody.linearVelocity.normalized;
|
||||
}
|
||||
|
||||
public void LaunchProjectile(Vector3 baseSpeed, bool fromTeamA)
|
||||
{
|
||||
RigidBody.linearVelocity = baseSpeed;
|
||||
FromTeamA = fromTeamA;
|
||||
}
|
||||
|
||||
}
|
|
@ -44,12 +44,12 @@ public class HealthHandler : MonoBehaviour
|
|||
armor -= armorBoost;
|
||||
}
|
||||
|
||||
public void Death()
|
||||
public void Death(float delay = 0)
|
||||
{
|
||||
DeathSate deathState = _minecraftUnit.AbstractDeath();
|
||||
if (deathState == DeathSate.QueenADead) print("TEAM B WIN GG");
|
||||
if (deathState == DeathSate.QueenBDead) print("TEAM A WIN GG");
|
||||
Destroy(gameObject);
|
||||
Destroy(gameObject, delay);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,4 +29,9 @@ public class MinecraftUnit : AbstractUnit
|
|||
{
|
||||
HealthHandler.TakeDamage(damage);
|
||||
}
|
||||
|
||||
public override void Heal(float heal)
|
||||
{
|
||||
HealthHandler.Heal(heal);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class MovementHandler : MonoBehaviour
|
|||
{
|
||||
[SerializeField] public float speed;
|
||||
[SerializeField] private NavMeshAgent agent;
|
||||
[SerializeField] private Transform defaultMoveTarget;
|
||||
[SerializeField] private bool followEnemy = true;
|
||||
[SerializeField] private float knockbackTime = 1.2f;
|
||||
private float _noNavMeshDeadTime = 6.0f;
|
||||
|
||||
|
@ -59,26 +59,30 @@ public class MovementHandler : MonoBehaviour
|
|||
|
||||
public void UpdateNearest()
|
||||
{
|
||||
TargetUnit = FindNearestEnemy();
|
||||
TargetUnit = FindNearest(followEnemy);
|
||||
}
|
||||
|
||||
public void MoveTowardsNearest()
|
||||
{
|
||||
MoveTowards(TargetUnit.transform.position);
|
||||
}
|
||||
|
||||
AbstractUnit FindNearestEnemy()
|
||||
|
||||
// If findEnemy, return closest ennemy else return closest ally
|
||||
public AbstractUnit FindNearest(bool findEnemy)
|
||||
{
|
||||
List<AbstractUnit> enemies = _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA;
|
||||
// Funny funny double ternary operator.
|
||||
List<AbstractUnit> targets = findEnemy ?
|
||||
_minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamB : GlobalsVariable.AliveUnitsTeamA
|
||||
: _minecraftUnit.IsTeamA ? GlobalsVariable.AliveUnitsTeamA : GlobalsVariable.AliveUnitsTeamB;
|
||||
|
||||
AbstractUnit closestUnit = null;
|
||||
float closestDistance = float.MaxValue;
|
||||
foreach (AbstractUnit enemy in enemies)
|
||||
foreach (AbstractUnit target in targets)
|
||||
{
|
||||
float distanceToEnemy = (enemy.transform.position - transform.position).sqrMagnitude;
|
||||
if (distanceToEnemy < closestDistance)
|
||||
float distanceToEnemy = (target.transform.position - transform.position).sqrMagnitude;
|
||||
if (distanceToEnemy < closestDistance && target != _minecraftUnit)
|
||||
{
|
||||
closestUnit = enemy;
|
||||
closestUnit = target;
|
||||
closestDistance = distanceToEnemy;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue