Add ability for player to get hit by enemies
This commit is contained in:
parent
62b981f440
commit
aa104db4bb
@ -140,8 +140,11 @@ public class Gobler : Enemy {
|
|||||||
case State.FinalizeAttack:
|
case State.FinalizeAttack:
|
||||||
Collider2D[] hits = Physics2D.OverlapCircleAll(MyPos + Vector2.down, 4);
|
Collider2D[] hits = Physics2D.OverlapCircleAll(MyPos + Vector2.down, 4);
|
||||||
foreach (var hit in hits) {
|
foreach (var hit in hits) {
|
||||||
if (!hit.CompareTag("PlayerHurtBox") || !hit.CompareTag("SuperSpecialGem")) continue;
|
if (hit.CompareTag("PlayerHurtBox"))
|
||||||
|
Player.Combat.Damage(BaseDamage, PlayerPos - MyPos, 20);
|
||||||
|
else if (hit.CompareTag("SuperSpecialGem"))
|
||||||
Debug.Log($"{hit.tag} hit");
|
Debug.Log($"{hit.tag} hit");
|
||||||
|
|
||||||
//GameObject parent = hit.transform.parent?.gameObject;
|
//GameObject parent = hit.transform.parent?.gameObject;
|
||||||
//Vector2 knockbakDirection = ActiveClass.HitBoxDraw.Directional ? PrevDirection : parent.transform.position - transform.position;
|
//Vector2 knockbakDirection = ActiveClass.HitBoxDraw.Directional ? PrevDirection : parent.transform.position - transform.position;
|
||||||
//EnemySpawnerData.EnemyMap[parent].Damage(ActiveClass.HitBoxDraw.Damage, knockbakDirection, ActiveClass.HitBoxDraw.Knockback);
|
//EnemySpawnerData.EnemyMap[parent].Damage(ActiveClass.HitBoxDraw.Damage, knockbakDirection, ActiveClass.HitBoxDraw.Knockback);
|
||||||
|
|||||||
@ -0,0 +1,44 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
using Unity.VisualScripting;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
public class PlayerCombatHandler : AttackerAndDamageable {
|
||||||
|
|
||||||
|
public bool TakingDamage;
|
||||||
|
|
||||||
|
public PlayerCombatHandler() {
|
||||||
|
BaseDamage = 10;
|
||||||
|
AttackPreparationTime = 0.5f;
|
||||||
|
AttackCooldownDuration = 1;
|
||||||
|
|
||||||
|
BaseHealth = 100;
|
||||||
|
CurrentHealth = BaseHealth;
|
||||||
|
InvincibilityDuration = 0.11f;
|
||||||
|
FrameFreezeDuration = 0.3f;
|
||||||
|
IsKnockable = true;
|
||||||
|
|
||||||
|
OnTakeDamage += (damage, dir) => TakingDamage = true;
|
||||||
|
|
||||||
|
foreach (var material in Player.Instance.GetComponentsInChildren<SpriteRenderer>().Select(x => x.material).ToList()) {
|
||||||
|
switch (material.name.Split(' ')[0]) {
|
||||||
|
case "DamageFlashMat": Player.Instance.MaterialColorOverlay = material; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ApplyKnockback() {
|
||||||
|
if (IsInvincible) {
|
||||||
|
var velocity = DirectionOfDamage * Knockback * 10 * InvincibilityLeft;
|
||||||
|
var isWalkable = NavMeshUtils.IsWalkable(Player.Instance.Rigidbody.transform.position, DirectionOfDamage);
|
||||||
|
Player.Instance.Rigidbody.linearVelocity = isWalkable ? velocity : Vector2.zero;
|
||||||
|
//Rigidbody.linearVelocity = velocity;
|
||||||
|
Player.Instance.MaterialColorOverlay.SetFloat("_FlashAmount", InvincibilityLeft / InvincibilityDuration);
|
||||||
|
} else {
|
||||||
|
TakingDamage = false;
|
||||||
|
Player.Instance.MaterialColorOverlay.SetFloat("_FlashAmount", 0);
|
||||||
|
Player.Instance.Rigidbody.linearVelocity = Vector2.zero;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,54 +0,0 @@
|
|||||||
using UnityEngine;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
public class PlayerHitHandler {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PlayerHealth {
|
|
||||||
public int MaxHealth { get; private set; }
|
|
||||||
public int CurrentHealth { get; private set; }
|
|
||||||
public bool IsDead => CurrentHealth <= 0;
|
|
||||||
|
|
||||||
public bool IsInvincible => Time.time < invincibleUntil;
|
|
||||||
private float invincibleUntil = 0f;
|
|
||||||
|
|
||||||
private float invincibilityDuration = 1f; // seconds
|
|
||||||
|
|
||||||
public event Action<int, Vector2> OnTakeDamage;
|
|
||||||
public event Action OnDeath;
|
|
||||||
public event Action<int> OnHeal;
|
|
||||||
|
|
||||||
public PlayerHealth(int maxHealth, float invincibilityDuration = 1f) {
|
|
||||||
MaxHealth = maxHealth;
|
|
||||||
CurrentHealth = maxHealth;
|
|
||||||
this.invincibilityDuration = invincibilityDuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void TakeDamage(int amount, Vector2 hitDirection) {
|
|
||||||
if (IsDead || IsInvincible) return;
|
|
||||||
|
|
||||||
CurrentHealth -= amount;
|
|
||||||
CurrentHealth = Mathf.Max(CurrentHealth, 0);
|
|
||||||
invincibleUntil = Time.time + invincibilityDuration;
|
|
||||||
|
|
||||||
OnTakeDamage?.Invoke(amount, hitDirection);
|
|
||||||
|
|
||||||
if (CurrentHealth == 0) {
|
|
||||||
OnDeath?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Heal(int amount) {
|
|
||||||
if (IsDead) return;
|
|
||||||
|
|
||||||
CurrentHealth += amount;
|
|
||||||
CurrentHealth = Mathf.Min(CurrentHealth, MaxHealth);
|
|
||||||
OnHeal?.Invoke(amount);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Reset() {
|
|
||||||
CurrentHealth = MaxHealth;
|
|
||||||
invincibleUntil = 0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,10 +2,12 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
using UnityEngine.EventSystems;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
using static UnityEditor.PlayerSettings;
|
||||||
using static UnityEngine.EventSystems.EventTrigger;
|
using static UnityEngine.EventSystems.EventTrigger;
|
||||||
|
|
||||||
[SelectionBase]
|
[SelectionBase]
|
||||||
@ -15,10 +17,11 @@ public class Player : MonoBehaviour {
|
|||||||
[Header("Asset/Prefab")]
|
[Header("Asset/Prefab")]
|
||||||
[SerializeField] public BuilderManager Builder;
|
[SerializeField] public BuilderManager Builder;
|
||||||
|
|
||||||
[Header("Movement")]
|
[Header("References")]
|
||||||
[SerializeField] private Rigidbody2D Rigidbody;
|
[SerializeField] public Rigidbody2D Rigidbody;
|
||||||
[SerializeField] public Animator Animator;
|
[SerializeField] public Animator Animator;
|
||||||
[SerializeField] private SpriteRenderer Renderer;
|
[SerializeField] public SpriteRenderer Renderer;
|
||||||
|
[SerializeField] public Material MaterialColorOverlay;
|
||||||
|
|
||||||
[Header("Character Class")]
|
[Header("Character Class")]
|
||||||
[SerializeField] private ParticleSystem Aura;
|
[SerializeField] private ParticleSystem Aura;
|
||||||
@ -58,7 +61,9 @@ public class Player : MonoBehaviour {
|
|||||||
[HideInInspector] public GameObjectPool VfxAirJumpPool;
|
[HideInInspector] public GameObjectPool VfxAirJumpPool;
|
||||||
|
|
||||||
private BoxCollider2D[] BoxColliders;
|
private BoxCollider2D[] BoxColliders;
|
||||||
private bool LockMovement;
|
public bool LockMovement;
|
||||||
|
|
||||||
|
[SerializeField] public PlayerCombatHandler Combat { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
public Vector2 PrevDirection = Vector2.zero;
|
public Vector2 PrevDirection = Vector2.zero;
|
||||||
@ -88,6 +93,8 @@ public class Player : MonoBehaviour {
|
|||||||
BoxColliders = GetComponentsInChildren<BoxCollider2D>();
|
BoxColliders = GetComponentsInChildren<BoxCollider2D>();
|
||||||
StaminaSliderHud.maxValue = StaminaMax;
|
StaminaSliderHud.maxValue = StaminaMax;
|
||||||
SetClass(1);
|
SetClass(1);
|
||||||
|
|
||||||
|
Combat = new PlayerCombatHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -198,6 +205,11 @@ public class Player : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void MovementUpdate() {
|
private void MovementUpdate() {
|
||||||
|
if (Combat.TakingDamage) {
|
||||||
|
Combat.ApplyKnockback();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var movement = (MoveDirection * MoveSpeed) / MoveSpeedDampener;
|
var movement = (MoveDirection * MoveSpeed) / MoveSpeedDampener;
|
||||||
if (IsDashing) {
|
if (IsDashing) {
|
||||||
if (DashSpeed < (Jump.Active ? 0.4f : 0.2f)) {
|
if (DashSpeed < (Jump.Active ? 0.4f : 0.2f)) {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user