Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Add Stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Kassout committed Apr 27, 2023
1 parent 971805f commit c5785ba
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 13 deletions.
142 changes: 141 additions & 1 deletion PlatformerController/Assets/Scenes/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9d79b2959a674ed4f945c385cef9f274, type: 3}
m_Name:
m_EditorClassIdentifier:
afterImagePrefab: {fileID: 5126111244721906233, guid: 5bd283a986ea91f4dbd908494386f1e0, type: 3}
--- !u!1 &107527214
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -289,6 +288,96 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &123344697
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 123344698}
- component: {fileID: 123344699}
m_Layer: 7
m_Name: Stats
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &123344698
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 123344697}
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: 1113032911}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &123344699
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 123344697}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ff83a06e0ca9354409f2325264470f70, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 30
--- !u!1 &128362230
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 128362231}
- component: {fileID: 128362232}
m_Layer: 9
m_Name: Stats
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &128362231
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 128362230}
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: 522243858}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &128362232
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 128362230}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ff83a06e0ca9354409f2325264470f70, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 100
--- !u!1 &169740279
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -624,6 +713,7 @@ Transform:
- {fileID: 1171421165}
- {fileID: 1394428076}
- {fileID: 625480764}
- {fileID: 128362231}
m_Father: {fileID: 1973051728}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -769,6 +859,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2f8a23b857b56f6499436fc25f6427ff, type: 3}
m_Name:
m_EditorClassIdentifier:
maxKnockBackTime: 0.2
--- !u!61 &625480766
BoxCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -889,6 +980,51 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0057e294e862fb84dac3ef092b364419, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &826421162
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 826421163}
- component: {fileID: 826421164}
m_Layer: 7
m_Name: Stats
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &826421163
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 826421162}
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: 2018358762}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &826421164
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 826421162}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ff83a06e0ca9354409f2325264470f70, type: 3}
m_Name:
m_EditorClassIdentifier:
maxHealth: 50
--- !u!1 &908733560
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1061,6 +1197,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2f8a23b857b56f6499436fc25f6427ff, type: 3}
m_Name:
m_EditorClassIdentifier:
maxKnockBackTime: 0.2
--- !u!61 &1002651841
BoxCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1138,6 +1275,7 @@ Transform:
- {fileID: 1368672505}
- {fileID: 256802740}
- {fileID: 1828052443}
- {fileID: 123344698}
m_Father: {fileID: 1387436680127814067}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -51309,6 +51447,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2f8a23b857b56f6499436fc25f6427ff, type: 3}
m_Name:
m_EditorClassIdentifier:
maxKnockBackTime: 0.2
--- !u!61 &1828052445
BoxCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -51839,6 +51978,7 @@ Transform:
- {fileID: 657241008}
- {fileID: 197339787}
- {fileID: 1002651839}
- {fileID: 826421163}
m_Father: {fileID: 7021417624037721779}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down
10 changes: 6 additions & 4 deletions PlatformerController/Assets/Scripts/Core/Components/Combat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

public class Combat : CoreComponent, IDamageable, IKnockBackable
{
[SerializeField] private float maxKnockBackTime = 0.2f;

private bool _isKnockBackActive;
private float _knockBackStartTime;

public void LogicUpdate()
public override void LogicUpdate()
{
CheckKnockBack();
}

public void Damage(float damage)
public void Damage(float amount)
{
Debug.Log(core.transform.parent.name + " got hit.");
core.Stats.DecreaseHealth(amount);
}

public void KnockBack(Vector2 angle, float strength, int direction)
Expand All @@ -25,7 +27,7 @@ public void KnockBack(Vector2 angle, float strength, int direction)

private void CheckKnockBack()
{
if (_isKnockBackActive && core.Movement.CurrentVelocity.y <= 0.01f && core.CollisionSenses.Ground)
if (_isKnockBackActive && ((core.Movement.CurrentVelocity.y <= 0.01f && core.CollisionSenses.Ground) || Time.time >= _knockBackStartTime + maxKnockBackTime))
{
_isKnockBackActive = false;
core.Movement.CanSetVelocity = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using UnityEngine;

public class CoreComponent : MonoBehaviour
public abstract class CoreComponent : MonoBehaviour, ILogicUpdate
{
protected Core core;

Expand All @@ -12,5 +12,9 @@ protected virtual void Awake()
{
Debug.LogError("There is no Core on the parent.");
}

core.AddComponent(this);
}

public virtual void LogicUpdate() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override void Awake()
CanSetVelocity = true;
}

public void LogicUpdate()
public override void LogicUpdate()
{
CurrentVelocity = Rigidbody.velocity;
}
Expand Down
30 changes: 30 additions & 0 deletions PlatformerController/Assets/Scripts/Core/Components/Stats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;

public class Stats : CoreComponent
{
[SerializeField] private float maxHealth;

private float _currentHealth;

protected override void Awake()
{
base.Awake();

_currentHealth = maxHealth;
}

public void DecreaseHealth(float amount)
{
_currentHealth -= amount;

if (_currentHealth <= 0f)
{
_currentHealth = 0f;
}
}

public void IncreaseHealth(float amount)
{
_currentHealth += Mathf.Clamp(_currentHealth + amount, 0, maxHealth);
}
}
11 changes: 11 additions & 0 deletions PlatformerController/Assets/Scripts/Core/Components/Stats.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions PlatformerController/Assets/Scripts/Core/Core.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine;

public class Core : MonoBehaviour
Expand All @@ -20,20 +21,40 @@ public Combat Combat
private set => _combat = value;
}

public Stats Stats
{
get => GenericNotImplementedError<Stats>.TryGet(_stats, transform.parent.name);
private set => _stats = value;
}

private Movement _movement;
private CollisionSenses _collisionSenses;
private Combat _combat;
private Stats _stats;

private readonly List<ILogicUpdate> _components = new List<ILogicUpdate>();

private void Awake()
{
_movement = GetComponentInChildren<Movement>();
_collisionSenses = GetComponentInChildren<CollisionSenses>();
_combat = GetComponentInChildren<Combat>();
Movement = GetComponentInChildren<Movement>();
CollisionSenses = GetComponentInChildren<CollisionSenses>();
Combat = GetComponentInChildren<Combat>();
Stats = GetComponentInChildren<Stats>();
}

public void LogicUpdate()
{
Movement.LogicUpdate();
Combat.LogicUpdate();
foreach (ILogicUpdate component in _components)
{
component.LogicUpdate();
}
}

public void AddComponent(ILogicUpdate component)
{
if (!_components.Contains(component))
{
_components.Add(component);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public interface IDamageable
{
public void Damage(float damage);
public void Damage(float amount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface ILogicUpdate
{
public void LogicUpdate();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5785ba

Please sign in to comment.