This commit is contained in:
Crizomb 2025-01-29 00:59:22 +01:00
parent a17810ffeb
commit 4e91f448c9
826 changed files with 66 additions and 8 deletions

View file

@ -0,0 +1,92 @@
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 MinecraftUnit _minecraftUnit;
void Awake()
{
_minecraftUnit = GetComponent<MinecraftUnit>();
}
void Start()
{
InvokeRepeating(nameof(Attack), Random.Range(-cooldown*0.2f, cooldown*0.2f), cooldown);
}
/// <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()
{
Collider[] targets = DetectTargets();
bool hasHit = false;
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);
_minecraftUnit.Capacity.AddMana(damage);
hasHit = true;
Vector3 knockbackVector = knockbackHorizontalForce * (target.transform.position - transform.position).normalized
+ knockbackVerticalForce * Vector3.up;
// logic specific if targetUnit is Unit
if (targetUnit is MinecraftUnit)
{
MinecraftUnit minecraftTarget = (MinecraftUnit)targetUnit;
minecraftTarget.StartCoroutine(minecraftTarget.MovementHandler.TakeImpulse(knockbackVector));
}
}
// Attack animation
if (_minecraftUnit.Animator && hasHit)
{
_minecraftUnit.Animator.SetTrigger("Attack");
}
return hasHit;
}
private Collider[] DetectTargets()
{
Collider[] hitColliders;
switch (attackShape)
{
case SphereCollider sphere:
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);
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 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);
}
}

View file

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

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 81db3118949a24b2d83ea142891913b3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,25 @@
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);
_minecraftUnitOrigin.Capacity.AddMana(baseDamage);
}
Destroy(this.gameObject);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 36e1a3cc9e04b444cadcfe2b444d0abc

View file

@ -0,0 +1,70 @@
using UnityEngine;
public class AttackProjectile : AttackHandler
{
[SerializeField] private GameObject arrowPrefab;
[SerializeField] private float arrowBaseSpeed;
[SerializeField] private Transform spawnPos;
[SerializeField] private bool directShot;
public override bool Attack()
{
// If no target (target is dead an destroyed)
if (!_minecraftUnit.MovementHandler.TargetUnit)
{
if (_minecraftUnit.IsTeamA)
{
if (GlobalsVariable.AliveUnitsTeamB.Count == 0) return false;
}
else
{
if (GlobalsVariable.AliveUnitsTeamA.Count == 0) return false;
}
}
float launchAngle = findLaunchAngle();
//print(launchAngle);
// If target not reachable
if (launchAngle < 0) return false;
GameObject arrow = Instantiate(arrowPrefab, spawnPos.position, spawnPos.rotation);
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
AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit;
Vector3 diffVector = Vector3.ProjectOnPlane(targetUnit.transform.position - spawnPos.position, Vector3.up);
Vector3 launchVectorNormalized = (localLaunchVector.x * diffVector.normalized + localLaunchVector.y * Vector3.up).normalized;
projectileHandler.LaunchProjectile(launchVectorNormalized * arrowBaseSpeed, _minecraftUnit.IsTeamA, _minecraftUnit);
return true;
}
private float findLaunchAngle()
{
// Source : https://en.wikipedia.org/wiki/Projectile_motion#Angle_%CE%B8_required_to_hit_coordinate_(x,_y)
AbstractUnit targetUnit = _minecraftUnit.MovementHandler.TargetUnit;
if (targetUnit == null) return -1f;
Vector3 diffVector = targetUnit.transform.position - spawnPos.position;
Vector3 projectOnPlane = Vector3.ProjectOnPlane(diffVector, Vector3.up);
float x = Vector3.ProjectOnPlane(projectOnPlane, Vector3.up).magnitude;
float y = diffVector.y;
float g = Physics.gravity.magnitude;
float v = arrowBaseSpeed;
float v_sqr = v * v;
//print("x : " + x);
float inside_sqrt_root = v_sqr*v_sqr - g*(g*x*x + 2*y*v_sqr);
if (inside_sqrt_root < 0.0f) return -1f; // Can't reach target
// directShot is the smallest angle, undirectShot shot is the biggest angle
float numerator = directShot ? v_sqr - Mathf.Sqrt(inside_sqrt_root) : v_sqr + Mathf.Sqrt(inside_sqrt_root);
float inside_arctan = numerator / (g * x);
return Mathf.Atan(inside_arctan);
}
}

View file

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

View file

@ -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);
_minecraftUnitOrigin.Capacity.AddMana(healthAdd*0.1f);
}
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);
}
}

View file

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

View file

@ -0,0 +1,40 @@
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;
protected MinecraftUnit _minecraftUnitOrigin;
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, MinecraftUnit minecraftUnit)
{
RigidBody.linearVelocity = baseSpeed;
FromTeamA = fromTeamA;
_minecraftUnitOrigin = minecraftUnit;
}
}

View file

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