first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using OTG.Lab06.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.Combat
|
||||
{
|
||||
public sealed class Enemy : MonoBehaviour, IDamageable
|
||||
{
|
||||
[SerializeField] private float health = 80f;
|
||||
[SerializeField] private GameObject deathEffectPrefab;
|
||||
|
||||
private bool isAlive = true;
|
||||
|
||||
public Transform Transform => transform;
|
||||
public bool IsAlive => isAlive;
|
||||
|
||||
public void TakeDamage(float amount)
|
||||
{
|
||||
if (!isAlive)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
health -= Mathf.Max(0f, amount);
|
||||
FloatingDamageManager.Show(amount, transform.position + Vector3.up * 2.1f);
|
||||
|
||||
if (health <= 0f)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
isAlive = false;
|
||||
if (deathEffectPrefab != null)
|
||||
{
|
||||
Instantiate(deathEffectPrefab, transform.position + Vector3.up * 0.6f, Quaternion.identity);
|
||||
}
|
||||
|
||||
Destroy(gameObject, 0.05f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 843f0415e5497d8ebb5efafe243655cc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OTG.Lab06.Combat
|
||||
{
|
||||
public sealed class FloatingDamage : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Text label;
|
||||
[SerializeField] private float lifetime = 1.1f;
|
||||
[SerializeField] private float riseSpeed = 1.2f;
|
||||
|
||||
private float remaining;
|
||||
|
||||
public void Initialize(float amount)
|
||||
{
|
||||
if (label == null)
|
||||
{
|
||||
label = GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
label.text = Mathf.RoundToInt(amount).ToString();
|
||||
remaining = lifetime;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.position += Vector3.up * (riseSpeed * Time.deltaTime);
|
||||
if (Camera.main != null)
|
||||
{
|
||||
transform.rotation = Quaternion.LookRotation(transform.position - Camera.main.transform.position, Vector3.up);
|
||||
}
|
||||
|
||||
remaining -= Time.deltaTime;
|
||||
float alpha = Mathf.Clamp01(remaining / lifetime);
|
||||
if (label != null)
|
||||
{
|
||||
Color color = label.color;
|
||||
color.a = alpha;
|
||||
label.color = color;
|
||||
}
|
||||
|
||||
if (remaining <= 0f)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33319da925118fecb85b8ee57a280c93
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.Combat
|
||||
{
|
||||
public sealed class FloatingDamageManager : MonoBehaviour
|
||||
{
|
||||
private static FloatingDamageManager instance;
|
||||
|
||||
[SerializeField] private FloatingDamage floatingDamagePrefab;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public static void Show(float amount, Vector3 position)
|
||||
{
|
||||
if (instance == null || instance.floatingDamagePrefab == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FloatingDamage damage = Instantiate(instance.floatingDamagePrefab, position, Quaternion.identity);
|
||||
damage.Initialize(amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72e42db6e80bea56d9317fe7e537f134
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user