Damage numbers

This commit is contained in:
Sveske Juice 2024-02-03 16:25:59 -08:00
parent fc295808e9
commit 85cb42fd63
14 changed files with 604 additions and 64 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &6232425507328955236
--- !u!1 &6833538753401515138
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
@ -8,10 +8,10 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 81829978540419969}
- component: {fileID: 3789424986669542967}
- component: {fileID: 3270777934385952829}
- component: {fileID: 3979768280741324332}
- component: {fileID: 4735696322134991437}
- component: {fileID: 2776254023051504893}
- component: {fileID: 2851877176569532699}
- component: {fileID: 4288064990526774250}
m_Layer: 0
m_Name: DamageText
m_TagString: Untagged
@ -19,32 +19,32 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &81829978540419969
--- !u!224 &4735696322134991437
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6232425507328955236}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_GameObject: {fileID: 6833538753401515138}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_Father: {fileID: 3288169701052299107}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -0.72}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 3, y: 2}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!23 &3789424986669542967
--- !u!23 &2776254023051504893
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6232425507328955236}
m_GameObject: {fileID: 6833538753401515138}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
@ -80,13 +80,13 @@ MeshRenderer:
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!114 &3270777934385952829
--- !u!114 &2851877176569532699
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6232425507328955236}
m_GameObject: {fileID: 6833538753401515138}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9541d86e2fd84c1d9990edf0852d74ab, type: 3}
@ -127,8 +127,8 @@ MonoBehaviour:
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 5
m_fontSizeBase: 5
m_fontSize: 8
m_fontSizeBase: 8
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
@ -170,15 +170,15 @@ MonoBehaviour:
_SortingLayerID: 0
_SortingOrder: 0
m_hasFontAssetChanged: 0
m_renderer: {fileID: 3789424986669542967}
m_renderer: {fileID: 2776254023051504893}
m_maskType: 0
--- !u!111 &3979768280741324332
--- !u!111 &4288064990526774250
Animation:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6232425507328955236}
m_GameObject: {fileID: 6833538753401515138}
m_Enabled: 1
serializedVersion: 3
m_Animation: {fileID: 7400000, guid: a6deca1a46fd10e4c966a1a58f998693, type: 2}
@ -188,3 +188,73 @@ Animation:
m_PlayAutomatically: 1
m_AnimatePhysics: 0
m_CullingType: 0
--- !u!1 &8357464303304312906
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3288169701052299107}
- component: {fileID: 2241243433806884705}
m_Layer: 0
m_Name: DamageText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3288169701052299107
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8357464303304312906}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 4735696322134991437}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2241243433806884705
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8357464303304312906}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 01c9b525ae607a844848c8e44062a0f1, type: 3}
m_Name:
m_EditorClassIdentifier:
damageToSize:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 1
value: 1
inSlope: 0.011841985
outSlope: 0.011841985
tangentMode: 0
weightedMode: 0
inWeight: 0
outWeight: 1
- serializedVersion: 3
time: 250
value: 2
inSlope: 0.004031593
outSlope: 0.004031593
tangentMode: 0
weightedMode: 0
inWeight: 0.9989648
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
damageText: {fileID: 2851877176569532699}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9ac131fe76b864945a3aeb678a16f786
guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3
PrefabImporter:
externalObjects: {}
userData:

View File

@ -255,6 +255,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
damageTickDelay: 1
OnHealthZero:
m_PersistentCalls:
m_Calls:
@ -288,6 +289,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
damageTickDelay: 0.25
OnHealthZero:
m_PersistentCalls:
m_Calls: []

View File

@ -1707,6 +1707,53 @@ Transform:
m_CorrespondingSourceObject: {fileID: 320690117028550720, guid: 30e0cc55a67f02d4f92b2677ec4b1511, type: 3}
m_PrefabInstance: {fileID: 869964773}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1206522691
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1206522693}
- component: {fileID: 1206522692}
m_Layer: 0
m_Name: DamageTextManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1206522692
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1206522691}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: aab27275f26e5e244856836f6d17b909, type: 3}
m_Name:
m_EditorClassIdentifier:
textParent: {fileID: 1206522693}
damageTextPrefab: {fileID: 8357464303304312906, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
lifeTime: 3
--- !u!4 &1206522693
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1206522691}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1232208432
PrefabInstance:
m_ObjectHideFlags: 0
@ -3640,6 +3687,66 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1968948229
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalPosition.x
value: -10.853601
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalPosition.y
value: -18.801167
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalPosition.z
value: -0.052307792
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3288169701052299107, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8357464303304312906, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
propertyPath: m_Name
value: DamageText
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 6833538753401515138, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
insertIndex: -1
addedObject: {fileID: 0}
m_SourcePrefab: {fileID: 100100000, guid: 25c9a2db8fc5b7c4f8feebba5c73e7a3, type: 3}
--- !u!1 &2051970329
GameObject:
m_ObjectHideFlags: 0
@ -3934,3 +4041,5 @@ SceneRoots:
- {fileID: 1920006249}
- {fileID: 640312360}
- {fileID: 5796191506433166633}
- {fileID: 1968948229}
- {fileID: 1206522693}

View File

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyAfter : MonoBehaviour
{
[SerializeField]
private float destroyInSeconds = 1.0f;
public void Init(float delay)
{
this.destroyInSeconds = delay;
Destroy(gameObject, delay);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 612e58b28f1e9ab449b1bba4737e2dc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -22,9 +22,9 @@ public class HealthBar : MonoBehaviour
attachedHealth.OnHealthChange.RemoveListener(UpdateHealthBar);
}
private void UpdateHealthBar(int prevHealth, int newHealth)
private void UpdateHealthBar(float prevHealth, float newHealth)
{
float percent = ((float) newHealth) / ((float)attachedHealth.getMaxHealth());
float percent = newHealth / attachedHealth.getMaxHealth();
healthBarImg.fillAmount = percent;
}
}

View File

@ -1,14 +1,23 @@
using UnityEngine;
using UnityEngine.Events;
using TMPro;
using System;
using UnityUtils;
public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
{
[SerializeField] int maxHealth = 100;
public int currentHealth { get; private set; }
[SerializeField] float maxHealth = 100;
[SerializeField] float damageTickDelay = 0.25f;
private float currentDamageTick = 0f;
private float accumulatedDamageInTick = 0f;
public float currentHealth { get; private set; }
public static event Action<Vector3, float> OnHealthChangeAtPos;
public UnityEvent OnHealthZero;
public UnityEvent<int, int> OnHealthChange;
public UnityEvent<float, float> OnHealthChange;
[Header("Squeze Damage")]
[SerializeField]
@ -22,22 +31,36 @@ public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
currentHealth = maxHealth;
}
public int getMaxHealth() {
void Update()
{
if (currentDamageTick < Time.time)
{
if (accumulatedDamageInTick < 1f) return;
OnHealthChangeAtPos?.Invoke(transform.position.Add(y: 2f), accumulatedDamageInTick);
currentDamageTick = Time.time + damageTickDelay;
accumulatedDamageInTick = 0f;
}
}
public float getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(int amount, bool heal = false) {
public void setMaxHealth(float amount, bool heal = false) {
maxHealth = amount;
if (heal)
currentHealth = amount;
}
public void TakeDamage(int damage)
public void TakeDamage(float damage)
{
currentHealth -= damage;
OnHealthChange?.Invoke(currentHealth + damage, currentHealth);
accumulatedDamageInTick += damage;
if (currentHealth <= 0) {
OnHealthZero?.Invoke();
BloodComputeShader.Instance.createBlood(transform.position, (int)(maxHealth *maxHealth * BloodComputeShader.Instance.scoreMult), maxHealth / 25.0f);

View File

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DamageText : MonoBehaviour
{
[SerializeField] AnimationCurve damageToSize;
[SerializeField] TMPro.TextMeshPro damageText;
public void Init(float damage)
{
transform.localScale = Vector2.one * damageToSize.Evaluate(damage);
damageText.text = damage.ToString();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01c9b525ae607a844848c8e44062a0f1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
public class DamageTextManager : MonoBehaviour
{
[SerializeField] Transform textParent;
[SerializeField] GameObject damageTextPrefab;
[SerializeField] float lifeTime = 3f;
private void OnEnable()
{
HealthComponent.OnHealthChangeAtPos += SpawnDamage;
}
private void OnDisable()
{
HealthComponent.OnHealthChangeAtPos -= SpawnDamage;
}
public void SpawnDamage(Vector3 position, float damage)
{
GameObject go = Instantiate(damageTextPrefab, position, Quaternion.identity, textParent);
go.GetComponent<DamageText>().Init(damage);
go.AddComponent<DestroyAfter>().Init(lifeTime);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aab27275f26e5e244856836f6d17b909
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -24,7 +24,7 @@ public class HealthDisplay : MonoBehaviour
healthComponent.OnHealthChange.RemoveListener(UpdateHealth);
}
public void UpdateHealth(int prevHealth, int newHealth)
public void UpdateHealth(float prevHealth, float newHealth)
{
healthText.text = padding + healthComponent.currentHealth;
}