squeze damage from rope

This commit is contained in:
Sveske Juice 2024-02-02 16:14:34 -08:00
parent e87e5c76b7
commit cca6af8109
10 changed files with 271 additions and 45 deletions

View File

@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 347555805}
- component: {fileID: 347555804}
- component: {fileID: 347555803}
- component: {fileID: 347555807}
m_Layer: 0
m_Name: EnemyTest (3)
m_TagString: Untagged
@ -271,6 +272,39 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &347555807
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 347555802}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1fdfc885f1a69704b893825cf9e616ba, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
OnHealthZero:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 347555807}
m_TargetAssemblyTypeName: HealthComponent, Assembly-CSharp
m_MethodName: SimpleKill
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
OnHealthChange:
m_PersistentCalls:
m_Calls: []
minThreshold: 1
squezeDamageScalor: 1
--- !u!1 &418427021
GameObject:
m_ObjectHideFlags: 0
@ -283,6 +317,7 @@ GameObject:
- component: {fileID: 418427024}
- component: {fileID: 418427023}
- component: {fileID: 418427022}
- component: {fileID: 418427026}
m_Layer: 0
m_Name: EnemyTest (4)
m_TagString: Untagged
@ -419,6 +454,39 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &418427026
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 418427021}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1fdfc885f1a69704b893825cf9e616ba, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
OnHealthZero:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 418427026}
m_TargetAssemblyTypeName: HealthComponent, Assembly-CSharp
m_MethodName: SimpleKill
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
OnHealthChange:
m_PersistentCalls:
m_Calls: []
minThreshold: 1
squezeDamageScalor: 1
--- !u!1 &437176298
GameObject:
m_ObjectHideFlags: 0
@ -431,6 +499,7 @@ GameObject:
- component: {fileID: 437176300}
- component: {fileID: 437176299}
- component: {fileID: 437176302}
- component: {fileID: 437176303}
m_Layer: 0
m_Name: EnemyTest (2)
m_TagString: Untagged
@ -567,6 +636,39 @@ Rigidbody2D:
m_SleepingMode: 1
m_CollisionDetection: 1
m_Constraints: 0
--- !u!114 &437176303
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 437176298}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1fdfc885f1a69704b893825cf9e616ba, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
OnHealthZero:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 437176303}
m_TargetAssemblyTypeName: HealthComponent, Assembly-CSharp
m_MethodName: SimpleKill
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
OnHealthChange:
m_PersistentCalls:
m_Calls: []
minThreshold: 1
squezeDamageScalor: 1
--- !u!1 &442848047
GameObject:
m_ObjectHideFlags: 0
@ -578,6 +680,7 @@ GameObject:
- component: {fileID: 442848048}
- component: {fileID: 442848050}
- component: {fileID: 442848049}
- component: {fileID: 442848051}
m_Layer: 5
m_Name: Health
m_TagString: Untagged
@ -701,6 +804,21 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 442848047}
m_CullTransparentMesh: 1
--- !u!114 &442848051
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 442848047}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6ea817cc94396c24199c7d5b0f34e889, type: 3}
m_Name:
m_EditorClassIdentifier:
padding: 'Health: '
healthText: {fileID: 442848049}
healthComponent: {fileID: 1451580751}
--- !u!1 &484915623
GameObject:
m_ObjectHideFlags: 0
@ -919,8 +1037,26 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
currentHealth: 0
healthText: {fileID: 442848049}
OnHealthZero:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1451580751}
m_TargetAssemblyTypeName: HealthComponent, Assembly-CSharp
m_MethodName: SimpleKill
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
OnHealthChange:
m_PersistentCalls:
m_Calls: []
minThreshold: 1
squezeDamageScalor: 0
--- !u!114 &1451580752
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,46 @@
using UnityEngine;
using UnityEngine.Events;
using TMPro;
public class HealthComponent : MonoBehaviour, ISquezeDamageReceiver
{
[SerializeField] int maxHealth = 100;
public int currentHealth { get; private set; }
public UnityEvent OnHealthZero;
public UnityEvent<int, int> OnHealthChange;
[Header("Squeze Damage")]
[SerializeField]
float minThreshold = 1f;
[SerializeField]
float squezeDamageScalor = 1f;
void Awake()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
OnHealthChange?.Invoke(currentHealth + damage, currentHealth);
if (currentHealth <= 0)
OnHealthZero?.Invoke();
}
public void TakeSquezeDamage(float squezeDamage)
{
Debug.Log($"Taking {squezeDamage}");
if (squezeDamage < minThreshold) return;
TakeDamage((int) Mathf.Round(squezeDamage * squezeDamageScalor));
}
public void SimpleKill()
{
Destroy(gameObject);
}
}

View File

@ -1,37 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerHP : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public TextMeshProUGUI healthText;
void Start()
{
currentHealth = maxHealth;
}
public void Update()
{
if (currentHealth <= 0)
{
// TODO: Game Over
}
if (healthText != null && currentHealth > 0)
{
healthText.text = "Health: " + currentHealth;
}
else if (healthText != null && currentHealth <= 0)
healthText.text = "Health: 0";
if (Input.GetKeyDown(KeyCode.Space))
TakeDamage(5);
}
public void TakeDamage(float damage)
{
currentHealth -= (int)damage;
}
}

View File

@ -0,0 +1,4 @@
public interface ISquezeDamageReceiver
{
public void TakeSquezeDamage(float squezeDamage);
}

View File

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

View File

@ -51,10 +51,10 @@ public class RopeSimulator : MonoBehaviour
public Vector2 testPos;
Vector2 prevStartPos;
Rope rope;
Dictionary<Collider2D, float> colliderToSquezeForce = new();
private void Start()
{
//rope = new RopeBuilder()
@ -100,6 +100,8 @@ public class RopeSimulator : MonoBehaviour
private void Update()
{
colliderToSquezeForce.Clear();
rope.points.First().position = start.position;
rope.points.Last().position = end.position;
@ -128,7 +130,14 @@ public class RopeSimulator : MonoBehaviour
start.gameObject.GetComponent<Rigidbody2D>().AddForce(pullDirection * overshoot * pullForce);
}
prevStartPos = start.position;
// Handle squeze kills
foreach (var collider in colliderToSquezeForce)
{
ISquezeDamageReceiver squezeDamageReceiver = collider.Key.GetComponent<ISquezeDamageReceiver>();
if (squezeDamageReceiver == null) continue;
squezeDamageReceiver.TakeSquezeDamage(collider.Value);
}
}
private void OnDrawGizmos()
@ -216,10 +225,17 @@ public class RopeSimulator : MonoBehaviour
Collider2D hitCollider = Physics2D.OverlapCircle(p.position, ropeRadius, staticColliderMask);
if (hitCollider == null) return;
// Register the squeze force this rope particle is squezing the collider
Vector2 resolvedPos = hitCollider.ClosestPoint(p.position);
Vector2 penetrationDir = (resolvedPos - p.position).normalized;
Vector2 finalPos = resolvedPos - penetrationDir * ropeRadius;
Vector2 penetration = resolvedPos - p.position;
Vector2 finalPos = resolvedPos - penetration.normalized * ropeRadius;
float squezeForce;
if (!colliderToSquezeForce.TryGetValue(hitCollider, out squezeForce))
colliderToSquezeForce.Add(hitCollider, squezeForce + penetration.magnitude);
else
colliderToSquezeForce[hitCollider] = squezeForce + penetration.magnitude;
p.position = finalPos;
}

8
Assets/Scripts/UI.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8b645cb8aa203484aaefb2b6f86e696c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class HealthDisplay : MonoBehaviour
{
[SerializeField] string padding = "Health: ";
[SerializeField] TextMeshProUGUI healthText;
[SerializeField] HealthComponent healthComponent;
private void Start()
{
healthText.text = padding + healthComponent.currentHealth;
}
private void OnEnable()
{
healthComponent.OnHealthChange.AddListener(UpdateHealth);
}
private void OnDisable()
{
healthComponent.OnHealthChange.RemoveListener(UpdateHealth);
}
public void UpdateHealth(int prevHealth, int newHealth)
{
healthText.text = padding + healthComponent.currentHealth;
}
}

View File

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