Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Assets/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.0] - 2020-10-07
## [0.5.0] - 2020-10-07

### Added
- Tests for game events and their listeners.
- Tests for mutable objects.

### Changed
- **Renamed** event and listener trigger functions (`OnGameEvent` -> `RaiseGameEvent`).
- **Renamed** `GameEvents.Vector2.Vector3GameEvent` to `GameEvents.Vector2.Vector2GameEvent`, fixed typo.
- Made `MutableObjectHandler` more extensible.
- Exposed `GameEvent` and `OnGameEvent` on listeners via properties.
- `MutableObject` instances are now reset using `SceneManager.activeSceneChanged`.

## [0.4.0] - 2020-10-06

### Added
- `MutableObjectHandler` which would automate the process of resetting `MutableObjects`.
Expand Down
4 changes: 3 additions & 1 deletion Assets/Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Available game events:
width="100%" alt="Example usage of Mutable Objects"
/>

Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when a new level loads.
Mutable objects are used for storing and editing data on `ScriptableObject` assets at runtime. This data can be referenced, observed and used as a bridge by various scripts. Mutable objects are useful in situations where `ScriptableObject` data needs to be reset when the [active scene changes](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-activeSceneChanged.html).

Available mutable objects:
- `MutableBool` - encapsulates a `bool` value.
Expand Down Expand Up @@ -104,3 +104,5 @@ public class MutableCustom : MutableObject
}
}
```

[Unity Package Manager]: https://docs.unity3d.com/Packages/com.unity.package-manager-ui@2.0/manual/index.html
4 changes: 2 additions & 2 deletions Assets/Editor/GameEvents/Vector2/Vector2GameEventEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace GameEvents.Vector2
{
[CustomEditor(typeof(Vector3GameEvent))]
[CustomEditor(typeof(Vector2GameEvent))]
public class Vector2GameEventEditor
: ArgumentGameEventEditor<Vector3GameEvent, UnityEngine.Vector2>
: ArgumentGameEventEditor<Vector2GameEvent, UnityEngine.Vector2>
{
protected override UnityEngine.Vector2 DrawArgumentField(UnityEngine.Vector2 value)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Runtime/GameEvents/Game/GameEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void RaiseGameEvent()
Debug.Log($"Raise event: {name}, listener: {listener}");
}

listener.OnGameEvent();
listener.RaiseGameEvent();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void RaiseGameEvent(TArgument argument)
Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}");
}

listener.OnGameEvent(argument);
listener.RaiseGameEvent(argument);
}
}

Expand Down
20 changes: 19 additions & 1 deletion Assets/Runtime/GameEvents/Generic/ArgumentGameEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ public abstract class ArgumentGameEventListener<TGameEvent, TUnityEvent, TArgume
[Tooltip("Called when the listener is triggered with an argument")]
private TUnityEvent onGameEvent = default;

/// <summary>
/// Get or set the underlying GameEvent.
/// </summary>
public TGameEvent GameEvent
{
get => gameEvent;
set => gameEvent = value;
}

/// <summary>
/// Get or set the underlying UnityEvent.
/// </summary>
public TUnityEvent OnGameEvent
{
get => onGameEvent;
set => onGameEvent = value;
}

/// <summary>
/// Trigger this listener with an argument.
/// </summary>
public void OnGameEvent(TArgument argument)
public void RaiseGameEvent(TArgument argument)
{
onGameEvent.Invoke(argument);
}
Expand Down
28 changes: 23 additions & 5 deletions Assets/Runtime/GameEvents/Generic/BaseGameEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ public abstract class BaseGameEventListener<TGameEvent>
[Tooltip("Called when the listener is triggered with an argument")]
private UnityEvent onGameEvent = default;

/// <summary>
/// Get or set the underlying GameEvent.
/// </summary>
public TGameEvent GameEvent
{
get => gameEvent;
set => gameEvent = value;
}

/// <summary>
/// Get or set the underlying UnityEvent.
/// </summary>
public UnityEvent OnGameEvent
{
get => onGameEvent;
set => onGameEvent = value;
}

public void RaiseGameEvent()
{
onGameEvent.Invoke();
}

private void OnEnable()
{
if (gameEvent == null)
Expand All @@ -36,10 +59,5 @@ private void OnDisable()

gameEvent.UnregisterListener(this);
}

public void OnGameEvent()
{
onGameEvent.Invoke();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IArgumentGameEventListener<in TArgument>
/// <summary>
/// Trigger this listener with an argument.
/// </summary>
void OnGameEvent(TArgument argument);
void RaiseGameEvent(TArgument argument);
}
}
2 changes: 1 addition & 1 deletion Assets/Runtime/GameEvents/Generic/IGameEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IGameEventListener
/// <summary>
/// Trigger this listener.
/// </summary>
void OnGameEvent();
void RaiseGameEvent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace GameEvents.Vector2
{
[CreateAssetMenu(fileName = "Vector2GameEvent", menuName = "Game Events/Vector2 Game Event")]
public class Vector3GameEvent : ArgumentGameEvent<UnityEngine.Vector2>
public class Vector2GameEvent : ArgumentGameEvent<UnityEngine.Vector2>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace GameEvents.Vector2
{
[AddComponentMenu("Game Events/Vector2 Game Event Listener")]
public class Vector2GameEventListener
: ArgumentGameEventListener<Vector3GameEvent, Vector2Event, UnityEngine.Vector2>
: ArgumentGameEventListener<Vector2GameEvent, Vector2Event, UnityEngine.Vector2>
{
}
}
54 changes: 39 additions & 15 deletions Assets/Runtime/MutableObjects/MutableObjectHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using MutableObjects.Generic;
using UnityEngine;
Expand All @@ -8,31 +9,54 @@ namespace MutableObjects
{
public static class MutableObjectHandler
{
/// <summary>
/// Default mutable object reset action.
/// </summary>
public static readonly Action<IEnumerable<IMutableObject>> DefaultOnReset =
mutableObjects =>
{
foreach (var mutableObject in mutableObjects)
{
mutableObject.ResetValues();
}
};

/// <summary>
/// Currently used mutable object reset action.
/// </summary>
public static Action<IEnumerable<IMutableObject>> OnReset { get; set; } = DefaultOnReset;

/// <summary>
/// Reset mutable object asset values.
/// </summary>
public static void ResetMutableObjectValues(
IEnumerable<IMutableObject> mutableObjects,
bool excludePersisting = false
)
{
if (excludePersisting)
{
mutableObjects = mutableObjects.Where(mutableObject => !mutableObject.Persisting);
}

OnReset(mutableObjects);
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void RuntimeInit()
{
SetInitialMutableObjectValues();
SceneManager.sceneUnloaded += scene => ResetMutableObjectValues();
SceneManager.activeSceneChanged += ResetMutableObjectValues;
}

private static void SetInitialMutableObjectValues()
{
var mutableObjects = FindMutableObjects();
foreach (var mutableObject in mutableObjects)
{
mutableObject.ResetValues();
}
ResetMutableObjectValues(FindMutableObjects());
}

private static void ResetMutableObjectValues()
private static void ResetMutableObjectValues(Scene curr, Scene next)
{
var mutableObjects = FindMutableObjects()
.Where(mutableObject => !mutableObject.Persisting);

foreach (var mutableObject in mutableObjects)
{
mutableObject.ResetValues();
}
ResetMutableObjectValues(FindMutableObjects(), true);
}

private static IEnumerable<IMutableObject> FindMutableObjects()
Expand Down
8 changes: 8 additions & 0 deletions Assets/Tests.meta

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

8 changes: 8 additions & 0 deletions Assets/Tests/Runtime.meta

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

20 changes: 20 additions & 0 deletions Assets/Tests/Runtime/Chark.UnityScriptableObjects.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Chark.UnityScriptableObjects.Tests",
"references": [
"UnityEngine.TestRunner",
"Chark.UnityScriptableObjects"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

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

3 changes: 3 additions & 0 deletions Assets/Tests/Runtime/GameEvents.meta

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

Loading