Commit 964e4754 authored by Martin Mihálik's avatar Martin Mihálik 🔥
Browse files

Damage System

parent d52edaf6
......@@ -12,6 +12,7 @@ GameObject:
- component: {fileID: 9034781248440383825}
- component: {fileID: 9034781248440383854}
- component: {fileID: 9034781248440383855}
- component: {fileID: 4637414488442095651}
m_Layer: 10
m_Name: Explosion
m_TagString: Untagged
......@@ -114,3 +115,16 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_MaxScale: 0
m_GrowthRate: 0
--- !u!114 &4637414488442095651
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9034781248440383852}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0f51ba1a63d0af9448dc4136820d75b5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Damage: 1
......@@ -11,8 +11,9 @@ GameObject:
- component: {fileID: 82881188109033726}
- component: {fileID: 1678648180425802822}
- component: {fileID: 82881188109033697}
- component: {fileID: 82881188109033699}
- component: {fileID: 82881188109033698}
- component: {fileID: 82881188109033699}
- component: {fileID: -8143619350437993802}
m_Layer: 9
m_Name: Meteor
m_TagString: Untagged
......@@ -121,22 +122,6 @@ PolygonCollider2D:
- {x: 0.27674294, y: -0.122409254}
- {x: 0.29, y: -0.10128546}
- {x: 0.2787619, y: 0.15690452}
--- !u!114 &82881188109033699
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 82881188109033727}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a08ab749ab9ce6478905ab3509cbb04, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Direction: {x: 0, y: 0}
m_Speed: 0
m_ExplosionPrefab: {fileID: 9034781248440383852, guid: c84a1d0620e6bee4099ba5078530b2bc, type: 3}
m_RubblePrefab: {fileID: 4362927315812235910, guid: b70e9174981d32c40861eb95d6d1b357, type: 3}
--- !u!96 &82881188109033698
TrailRenderer:
serializedVersion: 2
......@@ -244,3 +229,32 @@ TrailRenderer:
m_MinVertexDistance: 0.1
m_Autodestruct: 1
m_Emitting: 1
--- !u!114 &82881188109033699
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 82881188109033727}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a08ab749ab9ce6478905ab3509cbb04, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Direction: {x: 0, y: 0}
m_Speed: 0
m_ExplosionPrefab: {fileID: 9034781248440383852, guid: c84a1d0620e6bee4099ba5078530b2bc, type: 3}
m_RubblePrefab: {fileID: 4362927315812235910, guid: b70e9174981d32c40861eb95d6d1b357, type: 3}
--- !u!114 &-8143619350437993802
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 82881188109033727}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0f51ba1a63d0af9448dc4136820d75b5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Damage: 1
......@@ -138,6 +138,7 @@ GameObject:
- component: {fileID: 512397505}
- component: {fileID: 512397506}
- component: {fileID: 512397507}
- component: {fileID: 512397508}
m_Layer: 7
m_Name: Player
m_TagString: Untagged
......@@ -311,6 +312,34 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 00d89d77021225147b7c047536cdfad8, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &512397508
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 512397500}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 01e9ce25e2a99d149a174ad4098919ae, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Health: 1
m_OnDeath:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 512397506}
m_TargetAssemblyTypeName: PlayerControls, Assembly-CSharp
m_MethodName: OnDeath
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
--- !u!1 &519420028
GameObject:
m_ObjectHideFlags: 0
......
fileFormatVersion: 2
guid: d0bfefb16e5482147a8843ab6bce74fb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
public class DamageDealer : MonoBehaviour
{
private static long nextId;
private static long AcquireId()
{
return nextId++;
}
[SerializeField]
private uint m_Damage;
public uint Damage { get => m_Damage; }
public long Id { get; set; } = -1;
private void Start()
{
if (Id == -1)
{
Id = AcquireId();
}
}
}
fileFormatVersion: 2
guid: 0f51ba1a63d0af9448dc4136820d75b5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class DamageTaker : MonoBehaviour
{
[SerializeField] private uint m_Health;
public uint Health { get => m_Health; }
public bool Dead
{
get => Health == 0;
}
[SerializeField] private UnityEvent m_OnDeath;
private HashSet<long> m_DamageTaken;
private void Start()
{
m_DamageTaken = new HashSet<long>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (Dead)
{
return;
}
var dealer = collision.gameObject.GetComponent<DamageDealer>();
if (dealer == null)
{
return;
}
if (m_DamageTaken.Contains(dealer.Id))
{
return;
}
if (m_Health <= dealer.Damage)
{
m_Health = 0;
m_OnDeath.Invoke();
}
else
{
m_Health -= dealer.Damage;
m_DamageTaken.Add(dealer.Id);
}
}
}
fileFormatVersion: 2
guid: 01e9ce25e2a99d149a174ad4098919ae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -10,11 +10,13 @@ public class MeteorController : MonoBehaviour
private SpriteRenderer m_Sprite;
private Collider2D m_Collider;
private DamageDealer m_Dealer;
private void Start()
{
m_Sprite = GetComponent<SpriteRenderer>();
m_Collider = GetComponent<Collider2D>();
m_Dealer = GetComponent<DamageDealer>();
m_Direction.Normalize();
}
......@@ -55,7 +57,7 @@ public class MeteorController : MonoBehaviour
private void Explode()
{
SpawnExplosion(gameObject.transform.position, 2f, 4f, gameObject.transform.parent);
SpawnExplosion(gameObject.transform.position, 2f, 4f, gameObject.transform.parent, m_Dealer);
SpawnRubble(3, gameObject.transform.position, m_Collider.bounds.extents, 300f, gameObject.transform.parent);
Disappear();
......@@ -67,7 +69,7 @@ public class MeteorController : MonoBehaviour
m_Collider.enabled = false;
}
private void SpawnExplosion(Vector2 position, float maxScale, float growthRate, Transform parent)
private void SpawnExplosion(Vector2 position, float maxScale, float growthRate, Transform parent, DamageDealer sharedDealer)
{
var explosion = Instantiate(m_ExplosionPrefab, position, Quaternion.identity, parent);
......@@ -75,6 +77,10 @@ public class MeteorController : MonoBehaviour
controller.m_MaxScale = maxScale;
controller.m_GrowthRate = growthRate;
var dealer = explosion.GetComponent<DamageDealer>();
dealer.Id = sharedDealer.Id;
}
private void SpawnRubble(uint count, Vector2 basePosition, Vector2 bbox, float force, Transform parent)
......
......@@ -13,4 +13,9 @@ public class PlayerControls : MonoBehaviour
{
m_Controller.Move(Input.GetAxis("Horizontal"), Input.GetButton("Jump"));
}
public void OnDeath()
{
Debug.Log("Dead");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment