45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
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;
|
|
}
|
|
}
|
|
|
|
}
|