Walls + Start Arrow

This commit is contained in:
Crizomb 2025-01-14 13:11:00 +01:00
parent 7da33be977
commit 33467f6cc4
71 changed files with 4255 additions and 81 deletions

View file

@ -0,0 +1,37 @@
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;
public Rigidbody _rigidBody;
void Awake()
{
_rigidBody = GetComponent<Rigidbody>();
}
public void LaunchArrow(Vector3 baseSpeed)
{
_rigidBody.linearVelocity = baseSpeed;
}
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)
{
MinecraftUnit minecraftUnit = unit as MinecraftUnit;
Vector3 knockback = _rigidBody.linearVelocity * baseKnockback;
minecraftUnit.StartCoroutine(minecraftUnit.MovementHandler.TakeImpulse(knockback));
}
unit.TakeDamage(baseDamage);
}
}
}

View file

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

View file

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
[RequireComponent(typeof(MinecraftUnit))]
public class AttackHandler : MonoBehaviour
{
[SerializeField] protected float damage;
[SerializeField] protected float cooldown;
[SerializeField] protected Collider attackShape;
[SerializeField] protected float knockbackHorizontalForce;
[SerializeField] protected float knockbackVerticalForce;
protected 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;
}
/// <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 virtual 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,18 @@
using UnityEngine;
public class AttackSkeleton : AttackHandler
{
[SerializeField] private GameObject arrowPrefab;
[SerializeField] private float arrowBaseSpeed;
[SerializeField] private Transform spawnPos;
public override bool Attack()
{
if (_timer > 0) return false;
GameObject arrow = Instantiate(arrowPrefab, spawnPos.position, spawnPos.rotation);
ArrowHandler arrowHandler = arrow.GetComponent<ArrowHandler>();
arrowHandler.LaunchArrow(arrowBaseSpeed * spawnPos.forward);
return true;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6fa6d262ce5ee0761b28bd18f6bcd247