Skip to content

Commit 52715d5

Browse files
authored
Death loop, Serializable Dictionary & Spawner fixes (#20)
* Changes Made + Persistent Inventory Items Saving + Bag Refactored from SO to Serializable Class + Custom Json Serializer for SOs using Addressable + Small Utils Edits and Bug Fixes * Changes Made + Lighting regenerated for game scene + Small modifications and added burst project settings files * Refactored Functions in to states (just renames) * Updated Unity version to v2022 LTS * checkpoint * checkpoint, refactored StateStatus enum to a bool IsEnabled * added refactored and tested new state transitions * pooling abstraction implemented, moved SerializableVector3.cs to Data package to fix core dependency compile error * implemented spawner to hit effect on weapons * Changes made - Added serialized dictionary as an attribute to Generic Dictionary - More improvements to Spawner * checkpoint * finished #16 * small DeSpawn fix for Spawner.cs
1 parent 3bf132e commit 52715d5

File tree

22 files changed

+455
-48
lines changed

22 files changed

+455
-48
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Damage.Main;
2+
3+
namespace Character.Damage
4+
{
5+
public class DefaultDeath : Death
6+
{
7+
public override void Apply(DamageData damage)
8+
{
9+
Destroy(damage.Damagable.Obj);
10+
}
11+
}
12+
}

Packages/com.ekaka.character/Runtime/Damage/DefaultDeath.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System.Dynamic;
2+
using Core.Utils;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Editor.Core
7+
{
8+
[CustomPropertyDrawer(typeof(SerializedDictionaryAttribute), true)]
9+
public class SerializedDictionaryPropertyDrawer : PropertyDrawer
10+
{
11+
private Rect _position;
12+
13+
private bool _foldout;
14+
15+
private readonly float _foldoutPadding = 15f;
16+
17+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
18+
{
19+
_position = position;
20+
21+
_position.height = EditorGUIUtility.singleLineHeight;
22+
23+
_foldout = EditorGUI.Foldout(_position, _foldout, new GUIContent(property.displayName, property.tooltip));
24+
25+
if (_foldout)
26+
{
27+
_position.x += _foldoutPadding;
28+
29+
_position.width -= _foldoutPadding;
30+
31+
NewLine();
32+
33+
DrawSerializedDictionary(property);
34+
35+
property.serializedObject.ApplyModifiedProperties();
36+
}
37+
}
38+
39+
private void DrawSerializedDictionary(SerializedProperty dictProperty)
40+
{
41+
SerializedProperty pairListProperty = dictProperty.FindPropertyRelative(BaseEditor.SerializedListName);
42+
43+
EditorGUI.LabelField(MovedRect(0f, .4f), "Keys", EditorStyles.boldLabel);
44+
45+
EditorGUI.LabelField(MovedRect(.45f, .4f), "Values", EditorStyles.boldLabel);
46+
47+
if (GUI.Button(MovedRect(.9f, .1f), new GUIContent("+", "Add")))
48+
{
49+
pairListProperty.InsertArrayElementAtIndex(pairListProperty.arraySize);
50+
}
51+
52+
NewLine(1.25f);
53+
54+
int index = 0;
55+
56+
foreach (SerializedProperty property in pairListProperty)
57+
{
58+
SerializedProperty keyProperty = property.FindPropertyRelative(BaseEditor.KeyName);
59+
60+
EditorGUI.PropertyField(MovedRect(0f, .4f), keyProperty, GUIContent.none);
61+
62+
SerializedProperty valueProperty = property.FindPropertyRelative(BaseEditor.ValueName);
63+
64+
EditorGUI.PropertyField(MovedRect(.45f, .4f), valueProperty, GUIContent.none);
65+
66+
if (GUI.Button(MovedRect(.9f, .1f), new GUIContent("-", "Remove")))
67+
{
68+
pairListProperty.DeleteArrayElementAtIndex(index);
69+
}
70+
71+
NewLine();
72+
73+
index++;
74+
}
75+
}
76+
77+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
78+
{
79+
return _foldout ? _position.y : _position.height;
80+
}
81+
82+
private Rect MovedRect(float normalizedX = 1f, float normalizedWidth = 1f)
83+
{
84+
Rect rect = _position;
85+
86+
rect.x += normalizedX * _position.width;
87+
88+
rect.width *= normalizedWidth;
89+
90+
return rect;
91+
}
92+
93+
private void NewLine(float normalizedLineHeight = 1.15f)
94+
{
95+
_position.y += _position.height * normalizedLineHeight;
96+
}
97+
}
98+
}

Packages/com.ekaka.core/Editor/SerializedDictionaryPropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.ekaka.core/Runtime/Game/GameManager.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public enum GameState
2424
Play,
2525
//when game is paused - still in game scene
2626
Pause,
27+
//when game is over (Player is dead) - still in game scene
28+
GameOver,
2729
//onApplicationQuit (maybe use for dispose/garbage collection)
2830
Quitting
2931
}
@@ -80,6 +82,8 @@ private void InvokeGameStateChanged(GameState state)
8082

8183
public GameState State { get; private set; } = GameState.Initializing;
8284

85+
public bool InGame => State == GameState.Play || State == GameState.Pause || State == GameState.GameOver;
86+
8387
private void Start()
8488
{
8589
Initialize();
@@ -127,9 +131,15 @@ public void StartGame(bool continued)
127131
}
128132

129133
//load persistent data first
130-
private void ContinueGame()
134+
private void ContinueGame(bool reload = false)
131135
{
132-
//implement
136+
//change to loading until scene loads async
137+
ChangeGameState(GameState.Loading);
138+
139+
Debug.Log("Loading Game...");
140+
141+
//load game scene and call onSceneLoaded
142+
Utils.Utils.LoadScene(GameScene, NewGameStarted, reload);
133143
}
134144

135145
private void StartNewGame()
@@ -163,24 +173,35 @@ public void ExitGame()
163173
//leave/unload game scene and load to Landing scene
164174
public void ExitToMainMenu()
165175
{
166-
switch (State)
176+
if (InGame)
167177
{
168-
case GameState.Play: case GameState.Pause:
169-
170-
//change to loading until scene loads async
171-
ChangeGameState(GameState.Loading);
178+
//change to loading until scene loads async
179+
ChangeGameState(GameState.Loading);
172180

173-
Debug.Log($"exiting {nameof(GameScene)}...");
174-
175-
//load landing scene and call onSceneLoaded
176-
Utils.Utils.LoadScene(LandingScene, GameExited);
181+
Debug.Log($"exiting {nameof(GameScene)}...");
177182

178-
break;
183+
//load landing scene and call onSceneLoaded
184+
Utils.Utils.LoadScene(LandingScene, GameExited);
185+
}
186+
187+
else
188+
{
189+
Debug.LogError($"can't exit {nameof(GameScene)} when {nameof(GameState)} is not an {nameof(InGame)} {State}");
190+
}
191+
}
192+
193+
public void TryAgain()
194+
{
195+
if (!InGame)
196+
{
197+
Debug.LogError("Can't Not in Game.");
179198

180-
default:
181-
Debug.LogError($"can't exit {nameof(GameScene)} when {nameof(GameState)} is {State}");
182-
return;
199+
return;
183200
}
201+
202+
GameExited();
203+
204+
ContinueGame(true);
184205
}
185206

186207
//called once landing scene is loaded
@@ -208,6 +229,20 @@ public void PauseGame()
208229

209230
ChangeGameState(GameState.Pause);
210231
}
232+
233+
public void GameOver()
234+
{
235+
if (!InGame)
236+
{
237+
Debug.LogWarning($"can't {nameof(GameOver)} when {nameof(GameState)} is {State}");
238+
239+
return;
240+
}
241+
242+
Time.timeScale = 0f;
243+
244+
ChangeGameState(GameState.GameOver);
245+
}
211246

212247
public void ResumeGame()
213248
{

Packages/com.ekaka.core/Runtime/Utils/GenericDictionary.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,26 @@ public TValue this[TKey key]
5151
}
5252

5353
public bool IsReadOnly { get; set; }
54-
55-
public void OnBeforeSerialize() { }
54+
55+
public void OnBeforeSerialize()
56+
{
57+
if (serializedList.GroupBy(p => p.Key).Any(g => g.Count() > 1))
58+
{
59+
Debug.LogError("Can't serialize duplicate keys into dictionary");
60+
}
61+
}
5662

5763
public void OnAfterDeserialize()
5864
{
5965
_dictionary.Clear();
6066

61-
serializedList.ForEach(pair => _dictionary.Add(pair.Key, pair.Value));
67+
serializedList.ForEach(pair =>
68+
{
69+
if (_dictionary.ContainsKey(pair.Key))
70+
Debug.LogError($"Can't serialize duplicate key {pair.Key}");
71+
else
72+
_dictionary.Add(pair.Key, pair.Value);
73+
});
6274
}
6375

6476
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => _dictionary.GetEnumerator();

Packages/com.ekaka.core/Runtime/Utils/Pooling/Spawner.cs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
21
using System.Collections;
3-
using Core.Game;
42
using UnityEngine;
53
using UnityEngine.Pool;
64

@@ -41,37 +39,63 @@ public T Spawn(Vector3 position, Quaternion rotation, Transform parent)
4139

4240
return instance;
4341
}
42+
43+
public T Spawn(Vector3 position, Quaternion rotation, Transform parent, float timeout)
44+
{
45+
T instance = Spawn(position, rotation, parent);
46+
47+
DeSpawn(instance, timeout);
48+
49+
return instance;
50+
}
4451

4552
public void DeSpawn(T instance)
4653
{
47-
Pool.Release(instance);
54+
// Check if already DeSpawned.
55+
if (instance != null)
56+
{
57+
Pool.Release(instance);
58+
}
4859
}
4960

50-
public void DeSpawn(T instance, float t)
61+
public void DeSpawn(T instance, float timeout)
5162
{
52-
GameManager.Instance.StartCoroutine(WaitAndDeSpawn(instance, t));
63+
StartCoroutine(WaitAndDeSpawn(instance, timeout));
5364
}
5465

55-
private IEnumerator WaitAndDeSpawn(T instance, float t)
66+
private IEnumerator WaitAndDeSpawn(T instance, float timeout)
5667
{
57-
yield return new WaitForSeconds(t);
68+
yield return new WaitForSeconds(timeout);
5869

59-
_pool?.Release(instance);
70+
DeSpawn(instance);
6071
}
61-
62-
protected void Renew(T instance)
72+
73+
private void Renew(T instance)
6374
{
6475
instance.Renew();
6576
}
6677

67-
protected void Release(T instance)
78+
private void Release(T instance)
6879
{
6980
instance.Release();
7081
}
7182

72-
protected void Retire(T instance)
83+
private void Retire(T instance)
7384
{
7485
instance.Retire();
7586
}
87+
88+
public void Clear()
89+
{
90+
Pool.Clear();
91+
}
92+
93+
private void OnApplicationQuit()
94+
{
95+
if (_pool != null && _pool.CountActive > 0)
96+
{
97+
Pool.Dispose();
98+
}
99+
}
76100
}
77101
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace Core.Utils
7+
{
8+
[AttributeUsage(AttributeTargets.Field)]
9+
public class SerializedDictionaryAttribute : PropertyAttribute
10+
{
11+
public SerializedDictionaryAttribute()
12+
{
13+
14+
}
15+
}
16+
}

Packages/com.ekaka.core/Runtime/Utils/SerializedDictionaryAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)