button and symbol events, PuzzleDetail log type

This commit is contained in:
2022-11-21 20:36:41 +01:00
parent dbcb9c0a24
commit 4aa429d87d
6 changed files with 140 additions and 54 deletions

View File

@@ -1,53 +1,103 @@
using NaughtyAttributes;
using System;
using NaughtyAttributes;
using UnityEngine;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
namespace EscapeRoomEngine.Desert.Runtime
{
public enum ButtonEventType
{
Pressed, Released, Activated, Deactivated
}
public static class ButtonEventExtensions
{
public static string Description(this ButtonEventType type, Button button)
{
var action = type switch
{
ButtonEventType.Pressed => "pressed",
ButtonEventType.Released => "released",
ButtonEventType.Activated => "activated",
ButtonEventType.Deactivated => "deactivated",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
return $"{button} was {action}";
}
}
public delegate void ButtonEventHandler(Button source, ButtonEventType e);
public class Button : MonoBehaviour
{
public bool Active
public event ButtonEventHandler ButtonEvent;
[ShowNativeProperty]
private bool Active
{
get => _active;
private set
set
{
var previous = _active;
_active = value;
Pressed = Pressed; // check whether is still pressed
if (previous != _active)
{
OnButtonEvent(_active ? ButtonEventType.Activated : ButtonEventType.Deactivated);
}
}
}
public virtual bool Pressed
[ShowNativeProperty]
private bool Pressed
{
get => _pressed;
protected set
set
{
var previous = _pressed;
_pressed = Active && value;
if (previous != _pressed)
{
OnButtonEvent(_pressed ? ButtonEventType.Pressed : ButtonEventType.Released);
}
}
}
private bool _active, _pressed;
private bool _active = true, _pressed;
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Enable()
protected virtual void Awake()
{
Active = true;
ButtonEvent += (_, type) =>
{
if (type == ButtonEventType.Deactivated)
{
Pressed = false; // release button if it is deactivated
}
};
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Disable()
private void OnButtonEvent(ButtonEventType type)
{
Active = false;
Logger.Log(type.Description(this), LogType.PuzzleDetail);
ButtonEvent?.Invoke(this, type);
}
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Press()
{
Pressed = true;
}
#region Debug Buttons
[Button(enabledMode: EButtonEnableMode.Playmode)]
public void Release()
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Enable() => Active = true;
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Disable() => Active = false;
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Press() => Pressed = true;
[Button(enabledMode: EButtonEnableMode.Playmode)] public void Release() => Pressed = false;
#endregion
public override string ToString()
{
Pressed = false;
return name;
}
}
}

View File

@@ -1,18 +1,50 @@
using System;
using UnityEngine;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;
namespace EscapeRoomEngine.Desert.Runtime.Puzzle_A
{
public enum SymbolEventType
{
Activated, Deactivated
}
public static class SymbolEventExtensions
{
public static string Description(this SymbolEventType type, Symbol symbol)
{
var action = type switch
{
SymbolEventType.Activated => "activated",
SymbolEventType.Deactivated => "deactivated",
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
return $"{symbol} was {action}";
}
}
public delegate void SymbolEventHandler(SymbolEventType e);
[RequireComponent(typeof(Emission))]
public class Symbol : MonoBehaviour
{
public int symbolNumber;
public float anglePosition;
public event SymbolEventHandler SymbolEvent;
public bool Active
{
get => _emission.active;
set
{
_emission.active = value;
var previous = _emission.active;
_emission.active = value;
if (previous != value)
{
OnSymbolEvent(value ? SymbolEventType.Activated : SymbolEventType.Deactivated);
}
}
}
@@ -27,5 +59,17 @@ namespace EscapeRoomEngine.Desert.Runtime.Puzzle_A
{
Active = false;
}
private void OnSymbolEvent(SymbolEventType type)
{
Logger.Log(type.Description(this), LogType.PuzzleDetail);
SymbolEvent?.Invoke(type);
}
public override string ToString()
{
return name;
}
}
}

View File

@@ -9,29 +9,27 @@ namespace EscapeRoomEngine.Desert.Runtime.Puzzle_A
private static readonly int FresnelColor = Shader.PropertyToID("_FresnelColor");
private static readonly int Color = Shader.PropertyToID("_Color");
public int symbolNumber;
public EngineTheme theme;
public override bool Pressed
{
get => base.Pressed;
protected set
{
base.Pressed = value;
var color =
Pressed ? theme.activeColor
: Active ? theme.puzzleColor
: theme.solvedColor;
_material.SetColor(FresnelColor, color);
_material.SetColor(Color, color);
}
}
private Material _material;
private void Start()
{
_material = GetComponent<MeshRenderer>().material;
ButtonEvent += (_, type) =>
{
var color = type switch
{
ButtonEventType.Pressed => theme.activeColor,
ButtonEventType.Deactivated => theme.solvedColor,
_ => theme.puzzleColor
};
_material.SetColor(FresnelColor, color);
_material.SetColor(Color, color);
};
}
}
}