things
This commit is contained in:
parent
a17810ffeb
commit
4e91f448c9
826 changed files with 66 additions and 8 deletions
66
Assets/Scripts/UnitScripts/AbstractUnit.cs
Normal file
66
Assets/Scripts/UnitScripts/AbstractUnit.cs
Normal file
|
@ -0,0 +1,66 @@
|
|||
using UnityEngine;
|
||||
|
||||
public enum DeathSate
|
||||
{
|
||||
NotImportant = 0,
|
||||
QueenADead = 1,
|
||||
QueenBDead = 2,
|
||||
|
||||
}
|
||||
// For compatibility with the other team units, only contains things that need to be in common
|
||||
public abstract class AbstractUnit : MonoBehaviour
|
||||
{
|
||||
public float price;
|
||||
[field: SerializeField] public bool IsTeamA { get; private set; }
|
||||
|
||||
[field: SerializeField]
|
||||
protected bool _isQueen;
|
||||
|
||||
public virtual bool IsQueen
|
||||
{
|
||||
get => _isQueen;
|
||||
set => SetQueen(value);
|
||||
}
|
||||
|
||||
protected virtual void SetQueen(bool isQueen)
|
||||
{
|
||||
_isQueen = isQueen;
|
||||
}
|
||||
|
||||
public abstract void TakeDamage(float damage);
|
||||
public abstract void Heal(float heal);
|
||||
public abstract void AddArmor(float armor);
|
||||
public abstract void RemoveArmor(float armor);
|
||||
public abstract void StartFight();
|
||||
|
||||
protected void Awake()
|
||||
{
|
||||
|
||||
if (IsTeamA)
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamA.Add(this);
|
||||
if (IsQueen) GlobalsVariable.QueenA = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Add(this);
|
||||
if (IsQueen) GlobalsVariable.QueenB = this;
|
||||
}
|
||||
}
|
||||
|
||||
public DeathSate AbstractDeath()
|
||||
{
|
||||
if (IsTeamA)
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamA.Remove(this);
|
||||
if (IsQueen) return DeathSate.QueenADead;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.AliveUnitsTeamB.Remove(this);
|
||||
if (IsQueen) return DeathSate.QueenBDead;
|
||||
}
|
||||
return DeathSate.NotImportant;
|
||||
}
|
||||
|
||||
}
|
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
|
8
Assets/Scripts/UnitScripts/Attacks.meta
Normal file
8
Assets/Scripts/UnitScripts/Attacks.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bbb6d2297b3261d3a8edbd488757550a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
92
Assets/Scripts/UnitScripts/Attacks/AttackHandler.cs
Normal file
92
Assets/Scripts/UnitScripts/Attacks/AttackHandler.cs
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
2
Assets/Scripts/UnitScripts/Attacks/AttackHandler.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/Attacks/AttackHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eb03fb8c097f016f09d345ce200c3f41
|
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:
|
25
Assets/Scripts/UnitScripts/Attacks/Projectiles/Arrow.cs
Normal file
25
Assets/Scripts/UnitScripts/Attacks/Projectiles/Arrow.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 36e1a3cc9e04b444cadcfe2b444d0abc
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6fa6d262ce5ee0761b28bd18f6bcd247
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fd66c851299240c02a3082ade5b6295d
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9aa6ff4d0673f53389daff2a5ba3c45f
|
8
Assets/Scripts/UnitScripts/Bars.meta
Normal file
8
Assets/Scripts/UnitScripts/Bars.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6a9e2d1e9e9f8774395ab8c8b44e8dba
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
23
Assets/Scripts/UnitScripts/Bars/HealthBars.cs
Normal file
23
Assets/Scripts/UnitScripts/Bars/HealthBars.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class HealthBar : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private HealthHandler healthHandler;
|
||||
[SerializeField] private Slider healthSlider;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
healthSlider.maxValue = healthHandler.MaxHealth;
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
healthSlider.value = healthHandler.CurrentHealth;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void LateUpdate()
|
||||
{
|
||||
transform.LookAt(Camera.main.transform);
|
||||
}
|
||||
}
|
2
Assets/Scripts/UnitScripts/Bars/HealthBars.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/Bars/HealthBars.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: da56039456f5041c88cca65308f23ca0
|
21
Assets/Scripts/UnitScripts/Bars/ManaBars.cs
Normal file
21
Assets/Scripts/UnitScripts/Bars/ManaBars.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ManaBars : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private BaseCapacity capacity;
|
||||
[SerializeField] private Slider manaSlider;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
manaSlider.maxValue = capacity.MaxMana;
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
manaSlider.value = capacity.Mana;
|
||||
}
|
||||
// Child of healthbar, rotation is handled by healthbar
|
||||
|
||||
|
||||
}
|
2
Assets/Scripts/UnitScripts/Bars/ManaBars.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/Bars/ManaBars.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bc83878e8ee5d68b2b4f5622c7100b52
|
8
Assets/Scripts/UnitScripts/BehaviorState.meta
Normal file
8
Assets/Scripts/UnitScripts/BehaviorState.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9d27c54e796078de08e2819b80f8cbcd
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
[RequireComponent(typeof(MovementHandler))]
|
||||
public abstract class AbstractBehaviour : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float pathFps = 1.0f;
|
||||
[SerializeField] protected float distanceGoal = 0.0f;
|
||||
|
||||
protected abstract void MoveAction();
|
||||
|
||||
protected MinecraftUnit CurrentMinecraftUnit;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
CurrentMinecraftUnit = GetComponent<MinecraftUnit>();
|
||||
StartCoroutine(pathUpdate());
|
||||
}
|
||||
|
||||
private IEnumerator pathUpdate()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
MoveAction();
|
||||
yield return new WaitForSeconds(1.0f/pathFps);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bca22bdff905462e49dd5f0d224452c6
|
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class DefensiveBehaviour : AbstractBehaviour
|
||||
{
|
||||
protected override void MoveAction()
|
||||
{
|
||||
if (CurrentMinecraftUnit.IsTeamA)
|
||||
{
|
||||
if (GlobalsVariable.QueenA != null) return;
|
||||
CurrentMinecraftUnit.MovementHandler.UpdateNearestFrom(GlobalsVariable.QueenA.transform);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GlobalsVariable.QueenB != null) return;
|
||||
CurrentMinecraftUnit.MovementHandler.UpdateNearestFrom(GlobalsVariable.QueenB.transform);
|
||||
}
|
||||
|
||||
Vector3 targetPos = CurrentMinecraftUnit.MovementHandler.TargetUnit.transform.position;
|
||||
Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal;
|
||||
CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4d7eb557429d0c73b8e60870d6370ff6
|
26
Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs
Normal file
26
Assets/Scripts/UnitScripts/BehaviorState/NeutralBehaviour.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class NeutralBehaviour : AbstractBehaviour
|
||||
{
|
||||
protected override void MoveAction()
|
||||
{
|
||||
if (CurrentMinecraftUnit.IsTeamA)
|
||||
{
|
||||
if (GlobalsVariable.AliveUnitsTeamB.Count == 0) return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GlobalsVariable.AliveUnitsTeamA.Count == 0) return;
|
||||
}
|
||||
CurrentMinecraftUnit.MovementHandler.UpdateNearest();
|
||||
AbstractUnit targetUnit = CurrentMinecraftUnit.MovementHandler.TargetUnit;
|
||||
if (targetUnit == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Vector3 targetPos = targetUnit.transform.position;
|
||||
Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal;
|
||||
CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 794f919ac24609c05b9c690aaab19146
|
|
@ -0,0 +1,22 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class OffensiveBehaviour : AbstractBehaviour
|
||||
{
|
||||
protected override void MoveAction()
|
||||
{
|
||||
if (CurrentMinecraftUnit.IsTeamA)
|
||||
{
|
||||
if (GlobalsVariable.QueenB == null) return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (GlobalsVariable.QueenA == null) return;
|
||||
}
|
||||
|
||||
CurrentMinecraftUnit.MovementHandler.TargetUnit = GlobalsVariable.QueenB;
|
||||
Vector3 targetPos = CurrentMinecraftUnit.MovementHandler.TargetUnit.transform.position;
|
||||
Vector3 goalPos = targetPos + (transform.position - targetPos).normalized * distanceGoal;
|
||||
CurrentMinecraftUnit.MovementHandler.MoveTowards(goalPos);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 02c5dcee842138a4ab3b5db250170c31
|
8
Assets/Scripts/UnitScripts/Capacities.meta
Normal file
8
Assets/Scripts/UnitScripts/Capacities.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d76dce105c83372aebd5f8d8f33ee08b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
40
Assets/Scripts/UnitScripts/Capacities/BaseCapacity.cs
Normal file
40
Assets/Scripts/UnitScripts/Capacities/BaseCapacity.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
public class BaseCapacity : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField] public float MaxMana { get; private set; }
|
||||
[field: SerializeField] public float Mana { get; private set; }
|
||||
[SerializeField] private float manaCost;
|
||||
protected AbstractUnit _unit;
|
||||
|
||||
|
||||
|
||||
// Called every frame
|
||||
protected virtual bool CapacityCall()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public void AddMana(float manaAdd)
|
||||
{
|
||||
Mana = Mathf.Min(Mana + manaAdd, MaxMana);
|
||||
}
|
||||
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
_unit = GetComponent<AbstractUnit>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Mana >= manaCost)
|
||||
{
|
||||
bool capacityLaunched = CapacityCall();
|
||||
if (capacityLaunched) Mana -= manaCost;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: adbf6613bee8e8088b3389c62d61b845
|
33
Assets/Scripts/UnitScripts/Capacities/GolemDefense.cs
Normal file
33
Assets/Scripts/UnitScripts/Capacities/GolemDefense.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class GolemDefense : BaseCapacity
|
||||
{
|
||||
[SerializeField] private float armorGain;
|
||||
[SerializeField] private float buffTime;
|
||||
[SerializeField] private SphereCollider buffArea;
|
||||
|
||||
protected override bool CapacityCall()
|
||||
{
|
||||
Collider[] hitColliders = Physics.OverlapSphere(transform.position, buffArea.radius, buffArea.includeLayers);
|
||||
foreach (Collider target in hitColliders)
|
||||
{
|
||||
if (!target.CompareTag("CurrentMinecraftUnit")) continue;
|
||||
AbstractUnit targetUnit = target.GetComponent<AbstractUnit>();
|
||||
if (targetUnit.IsTeamA == _unit.IsTeamA)
|
||||
{
|
||||
CoroutineManager.Instance.StartCoroutine(AddThenRemoveArmor(targetUnit));
|
||||
}
|
||||
}
|
||||
return hitColliders.Length > 0;
|
||||
}
|
||||
|
||||
private IEnumerator AddThenRemoveArmor(AbstractUnit targetUnit)
|
||||
{
|
||||
targetUnit.AddArmor(armorGain);
|
||||
yield return new WaitForSeconds(buffTime);
|
||||
targetUnit.RemoveArmor(armorGain);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1ca640aad27b1da9d87fdc4302aa0cd3
|
23
Assets/Scripts/UnitScripts/Capacities/TripleAttack.cs
Normal file
23
Assets/Scripts/UnitScripts/Capacities/TripleAttack.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class TripleAttack : BaseCapacity
|
||||
{
|
||||
protected override bool CapacityCall()
|
||||
{
|
||||
MinecraftUnit minecraftUnit = _unit as MinecraftUnit;
|
||||
StartCoroutine(TripleAttackRoutine(minecraftUnit));
|
||||
return true;
|
||||
}
|
||||
|
||||
private IEnumerator TripleAttackRoutine(MinecraftUnit minecraftUnit)
|
||||
{
|
||||
minecraftUnit.AttackHandler.Attack();
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
minecraftUnit.AttackHandler.Attack();
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
minecraftUnit.AttackHandler.Attack();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b0344a53e7b8b8f30b008023599beb65
|
14
Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs
Normal file
14
Assets/Scripts/UnitScripts/Capacities/WitchSummon.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class WitchSummon : BaseCapacity
|
||||
{
|
||||
[SerializeField] private GameObject summonUnit;
|
||||
|
||||
protected override bool CapacityCall()
|
||||
{
|
||||
GameObject unit = Instantiate(summonUnit, transform.position, Quaternion.identity);
|
||||
AbstractUnit unitScript = unit.GetComponent<AbstractUnit>();
|
||||
unitScript.StartFight();
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1c3c6b3848aa5f3cd9b73630ddae811e
|
22
Assets/Scripts/UnitScripts/Capacities/ZombieSpeedBoost.cs
Normal file
22
Assets/Scripts/UnitScripts/Capacities/ZombieSpeedBoost.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ZombieSpeedBoost : BaseCapacity
|
||||
{
|
||||
[SerializeField] float timeToBoost;
|
||||
[SerializeField] float boost;
|
||||
protected override bool CapacityCall()
|
||||
{
|
||||
MinecraftUnit minecraftUnit = _unit as MinecraftUnit;
|
||||
StartCoroutine(AddThenRemoveSpeed(minecraftUnit));
|
||||
return true;
|
||||
}
|
||||
|
||||
private IEnumerator AddThenRemoveSpeed(MinecraftUnit minecraftUnit)
|
||||
{
|
||||
// Possibility float imprecision issues
|
||||
minecraftUnit.MovementHandler.speed *= boost;
|
||||
yield return new WaitForSeconds(timeToBoost);
|
||||
minecraftUnit.MovementHandler.speed /= boost;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c3b089e9358a7b3adbe2f521fcfbc8c2
|
34
Assets/Scripts/UnitScripts/GlobalsVariable.cs
Normal file
34
Assets/Scripts/UnitScripts/GlobalsVariable.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public static class GlobalsVariable
|
||||
{
|
||||
|
||||
public static int money=100;
|
||||
|
||||
public static List<AbstractUnit> AliveUnitsTeamA = new List<AbstractUnit>();
|
||||
public static List<AbstractUnit> AliveUnitsTeamB = new List<AbstractUnit>();
|
||||
|
||||
public static AbstractUnit QueenA;
|
||||
public static AbstractUnit QueenB;
|
||||
|
||||
public static Dictionary<string, int> prices = new Dictionary<string, int>()
|
||||
{
|
||||
{ "Zombie",1 },
|
||||
{ "Squelette",2 },
|
||||
{ "Creeper",3 },
|
||||
{ "Sorciere",3 },
|
||||
{ "Golem",8 }
|
||||
};
|
||||
|
||||
public static void Pay(int X)
|
||||
{
|
||||
money -= X;
|
||||
}
|
||||
|
||||
public static void Gain(int Y)
|
||||
{
|
||||
money += Y;
|
||||
}
|
||||
}
|
2
Assets/Scripts/UnitScripts/GlobalsVariable.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/GlobalsVariable.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: aff398f48f2c36187b8027e20ad9dc16
|
68
Assets/Scripts/UnitScripts/HealthHandler.cs
Normal file
68
Assets/Scripts/UnitScripts/HealthHandler.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
public class HealthHandler : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField] public float MaxHealth{ get; private set; }
|
||||
[field: SerializeField] public float CurrentHealth{ get; private set; }
|
||||
[field: SerializeField] public float Armor{ get; private set; }
|
||||
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
_minecraftUnit = GetComponent<MinecraftUnit>();
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
Debug.Assert(damage >= 0, "Damage cannot be negative, use Heal if you want to heal");
|
||||
CurrentHealth -= Mathf.Max(0, damage-Armor);
|
||||
_minecraftUnit.Capacity.AddMana(damage);
|
||||
if (CurrentHealth <= 0) Death();
|
||||
}
|
||||
|
||||
public void Heal(float value)
|
||||
{
|
||||
Debug.Assert(value >= 0, "value can't be less than zero");
|
||||
CurrentHealth = Mathf.Min(CurrentHealth + value, MaxHealth);
|
||||
}
|
||||
|
||||
public void AddArmor(float armorBoost)
|
||||
{
|
||||
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use RemoveArmor instead");
|
||||
Armor += armorBoost;
|
||||
}
|
||||
|
||||
public void RemoveArmor(float armorBoost)
|
||||
{
|
||||
Debug.Assert(armorBoost >= 0, "armorBoost can't be less than zero, use AddArmor instead");
|
||||
Armor -= armorBoost;
|
||||
}
|
||||
|
||||
public void Death(float delay = 0)
|
||||
{
|
||||
DeathSate deathState = _minecraftUnit.AbstractDeath();
|
||||
|
||||
if (deathState == DeathSate.NotImportant)
|
||||
{
|
||||
Destroy(gameObject, delay);
|
||||
return;
|
||||
}
|
||||
|
||||
if (deathState == DeathSate.QueenBDead)
|
||||
{
|
||||
print("get good, reload current scene");
|
||||
BasedGameManager.Instance.ReloadLevel();
|
||||
}
|
||||
|
||||
if (deathState == DeathSate.QueenADead)
|
||||
{
|
||||
print("GG going to next scene");
|
||||
BasedGameManager.Instance.GoNextLevel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
2
Assets/Scripts/UnitScripts/HealthHandler.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/HealthHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8511709ac7cfc1d05bee202fa8b8b095
|
85
Assets/Scripts/UnitScripts/MinecraftUnit.cs
Normal file
85
Assets/Scripts/UnitScripts/MinecraftUnit.cs
Normal file
|
@ -0,0 +1,85 @@
|
|||
using System.Runtime.CompilerServices;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
[RequireComponent(typeof(Rigidbody), typeof(HealthHandler), typeof(AttackHandler))]
|
||||
[RequireComponent(typeof(MovementHandler), typeof(BaseCapacity))]
|
||||
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; }
|
||||
[field: SerializeField] public BaseCapacity Capacity { get; private set; }
|
||||
// Not required
|
||||
[field: SerializeField] public Animator Animator { get; private set; }
|
||||
|
||||
|
||||
|
||||
protected override void SetQueen(bool isQueen)
|
||||
{
|
||||
_isQueen = isQueen;
|
||||
transform.Find("Crown").gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
new void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
if (IsQueen)
|
||||
{
|
||||
transform.Find("Crown").gameObject.SetActive(true);
|
||||
if (IsTeamA)
|
||||
{
|
||||
GlobalsVariable.QueenA = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
GlobalsVariable.QueenB = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Abstract implementation for compatibility with other team
|
||||
|
||||
public override void TakeDamage(float damage)
|
||||
{
|
||||
HealthHandler.TakeDamage(damage);
|
||||
}
|
||||
|
||||
public override void Heal(float heal)
|
||||
{
|
||||
HealthHandler.Heal(heal);
|
||||
}
|
||||
|
||||
public override void AddArmor(float armor)
|
||||
{
|
||||
HealthHandler.AddArmor(armor);
|
||||
}
|
||||
|
||||
public override void RemoveArmor(float armor)
|
||||
{
|
||||
HealthHandler.RemoveArmor(armor);
|
||||
}
|
||||
|
||||
public override void StartFight()
|
||||
{
|
||||
Component[] components = GetComponents<Component>();
|
||||
|
||||
foreach (Component component in components)
|
||||
{
|
||||
if (component is MonoBehaviour monoBehaviour)
|
||||
{
|
||||
monoBehaviour.enabled = true;
|
||||
}
|
||||
|
||||
if (component is NavMeshAgent agent)
|
||||
{
|
||||
agent.enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
2
Assets/Scripts/UnitScripts/MinecraftUnit.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/MinecraftUnit.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9815bed5dc151c8c6a271899ed1b7c4f
|
131
Assets/Scripts/UnitScripts/MovementHandler.cs
Normal file
131
Assets/Scripts/UnitScripts/MovementHandler.cs
Normal file
|
@ -0,0 +1,131 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[RequireComponent(typeof(MinecraftUnit))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class MovementHandler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] public float speed;
|
||||
[SerializeField] private NavMeshAgent agent;
|
||||
[SerializeField] private bool followEnemy = true;
|
||||
[SerializeField] private float knockbackTime = 1.2f;
|
||||
[SerializeField] private float _noNavMeshDeadTime = 3.0f;
|
||||
|
||||
[HideInInspector] public AbstractUnit TargetUnit {get; set; }
|
||||
|
||||
private MinecraftUnit _minecraftUnit;
|
||||
private Rigidbody _rigidbody;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_minecraftUnit = GetComponent<MinecraftUnit>();
|
||||
_rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
agent.speed = speed;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Mathf.Abs(agent.speed - speed) < 0.01f) agent.speed = speed;
|
||||
}
|
||||
|
||||
public void ChangeSpeed(float newSpeed)
|
||||
{
|
||||
speed = newSpeed;
|
||||
}
|
||||
|
||||
public void StopMoving()
|
||||
{
|
||||
agent.speed = 0;
|
||||
}
|
||||
|
||||
public void ResumeMoving()
|
||||
{
|
||||
agent.speed = speed;
|
||||
}
|
||||
|
||||
public void MoveTowards(Vector3 destination)
|
||||
{
|
||||
if (agent.enabled) agent.SetDestination(destination);
|
||||
}
|
||||
|
||||
public void UpdateNearest()
|
||||
{
|
||||
TargetUnit = FindNearest(followEnemy);
|
||||
}
|
||||
|
||||
public void UpdateNearestFrom(Transform transform)
|
||||
{
|
||||
TargetUnit = FindNearestFromTransform(true, transform);
|
||||
}
|
||||
|
||||
|
||||
// If findEnemy, return closest ennemy else return closest ally
|
||||
private AbstractUnit FindNearestFromTransform(bool findEnemy, Transform from)
|
||||
{
|
||||
// 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 target in targets)
|
||||
{
|
||||
float distanceToEnemy = (target.transform.position - from.position).sqrMagnitude;
|
||||
if (distanceToEnemy < closestDistance && target != _minecraftUnit)
|
||||
{
|
||||
closestUnit = target;
|
||||
closestDistance = distanceToEnemy;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestUnit == null)
|
||||
{
|
||||
print("What");
|
||||
print(targets.Count);
|
||||
print(targets);
|
||||
}
|
||||
|
||||
return closestUnit;
|
||||
}
|
||||
|
||||
private AbstractUnit FindNearest(bool findEnemy)
|
||||
{
|
||||
return FindNearestFromTransform(findEnemy, transform);
|
||||
}
|
||||
|
||||
public IEnumerator TakeImpulse(Vector3 impulse)
|
||||
{
|
||||
// Unity navmesh, can't handle physics (it rewrite velocity). So we deactivate it when applying force.
|
||||
agent.enabled = false;
|
||||
_rigidbody.AddForce(impulse, ForceMode.Impulse);
|
||||
yield return new WaitForSeconds(knockbackTime);
|
||||
|
||||
float noSurfaceTime = 0.0f;
|
||||
|
||||
// Make sure to be on the navmesh surface before reactivating agent
|
||||
while (_rigidbody != null && !NavMesh.SamplePosition(_rigidbody.position, out _, 1.0f, NavMesh.AllAreas))
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
noSurfaceTime += 0.5f;
|
||||
|
||||
// Die if exited navMesh for to long
|
||||
if (noSurfaceTime > _noNavMeshDeadTime)
|
||||
{
|
||||
_minecraftUnit.HealthHandler.Death();
|
||||
yield break;
|
||||
}
|
||||
|
||||
}
|
||||
if (agent != null) agent.enabled = true;
|
||||
}
|
||||
}
|
2
Assets/Scripts/UnitScripts/MovementHandler.cs.meta
Normal file
2
Assets/Scripts/UnitScripts/MovementHandler.cs.meta
Normal file
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: aa245c9f3dff7f6b9888b009a1023628
|
Loading…
Add table
Add a link
Reference in a new issue