init: version 1
This commit is contained in:
28
Assets/Prefabs/Sensor/Scripts/Activateable.cs
Normal file
28
Assets/Prefabs/Sensor/Scripts/Activateable.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Activateable is a base class for objects that can
|
||||
/// be activated or deactivated by a sensor.
|
||||
/// </summary>
|
||||
public abstract class Activateable : MonoBehaviour
|
||||
{
|
||||
[Header("Activateable Configuration")]
|
||||
public bool inverted;
|
||||
public bool isActive;
|
||||
|
||||
protected abstract void OnActivation();
|
||||
protected abstract void OnDeactivation();
|
||||
|
||||
public void SetActivation(bool state)
|
||||
{
|
||||
isActive = inverted ? !state : state;
|
||||
if (isActive) OnActivation();
|
||||
else OnDeactivation();
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
if (isActive) OnActivation();
|
||||
else OnDeactivation();
|
||||
}
|
||||
}
|
||||
3
Assets/Prefabs/Sensor/Scripts/Activateable.cs.meta
Normal file
3
Assets/Prefabs/Sensor/Scripts/Activateable.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c81f590d7854b14932f4d61d140d93a
|
||||
timeCreated: 1751737003
|
||||
23
Assets/Prefabs/Sensor/Scripts/ColorChangeController.cs
Normal file
23
Assets/Prefabs/Sensor/Scripts/ColorChangeController.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// ColorChangeController updates the color of a sprite
|
||||
/// based on its activation state.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
public class ColorChangeController : Activateable
|
||||
{
|
||||
[Header("Configuration")]
|
||||
public Color activatedColor = new(148 / 255f, 209 / 255f, 124 / 255f);
|
||||
public Color deactivatedColor = new(219 / 255f, 219 / 255f, 219 / 255f);
|
||||
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
}
|
||||
|
||||
protected override void OnActivation() => _spriteRenderer.color = activatedColor;
|
||||
protected override void OnDeactivation() => _spriteRenderer.color = deactivatedColor;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e48fb98a5b7b173439a0f0fa0f074b4c
|
||||
47
Assets/Prefabs/Sensor/Scripts/MergerController.cs
Normal file
47
Assets/Prefabs/Sensor/Scripts/MergerController.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// MergerController is a sensor that activates or deactivates
|
||||
/// when multiple connections are active.
|
||||
/// </summary>
|
||||
public class MergerController : Activateable
|
||||
{
|
||||
[Header("Configuration")]
|
||||
public Color activatedColor = new(148 / 255f, 209 / 255f, 124 / 255f);
|
||||
public Color deactivatedColor = new(219 / 255f, 219 / 255f, 219 / 255f);
|
||||
public int connectionCount = 2;
|
||||
public Activateable[] activateables;
|
||||
|
||||
private int _activeConnectionCount = 0;
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
}
|
||||
|
||||
protected override void OnActivation()
|
||||
{
|
||||
_activeConnectionCount++;
|
||||
if (_activeConnectionCount < connectionCount) return;
|
||||
|
||||
_spriteRenderer.color = activatedColor;
|
||||
foreach (Activateable activatable in activateables)
|
||||
{
|
||||
activatable.SetActivation(true);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDeactivation()
|
||||
{
|
||||
_activeConnectionCount = math.max(0, _activeConnectionCount - 1);
|
||||
if (_activeConnectionCount >= connectionCount) return;
|
||||
|
||||
_spriteRenderer.color = deactivatedColor;
|
||||
foreach (Activateable activatable in activateables)
|
||||
{
|
||||
activatable.SetActivation(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Prefabs/Sensor/Scripts/MergerController.cs.meta
Normal file
2
Assets/Prefabs/Sensor/Scripts/MergerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88142265365e0ad3f8bf0b014c0f9401
|
||||
83
Assets/Prefabs/Sensor/Scripts/SensorController.cs
Normal file
83
Assets/Prefabs/Sensor/Scripts/SensorController.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
/// <summary>
|
||||
/// SensorController is a sensor that activates or deactivates
|
||||
/// based on collisions with a cube or player.
|
||||
/// </summary>
|
||||
public class SensorController : MonoBehaviour
|
||||
{
|
||||
private int _collisionEnterCount;
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
private Coroutine _activationCoroutine;
|
||||
|
||||
[Header("Configuration")]
|
||||
public Color activatedColor = new (44 / 255f, 127 / 255f, 33/ 255f);
|
||||
public Color deactivatedColor = new (218 / 255f, 65 / 255f, 60 / 255f);
|
||||
public float activationDelay = 0.2f;
|
||||
|
||||
public bool requiresCube;
|
||||
public int requiredCubeOriginMap = -1;
|
||||
|
||||
public Activateable[] activateables;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
_spriteRenderer.color = deactivatedColor;
|
||||
}
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (!IsMatch(collision)) return;
|
||||
_collisionEnterCount++;
|
||||
if (_collisionEnterCount != 1) return;
|
||||
|
||||
_activationCoroutine ??= StartCoroutine(ActivateAfterDelay());
|
||||
}
|
||||
|
||||
private void OnCollisionExit2D(Collision2D collision)
|
||||
{
|
||||
if (!IsMatch(collision)) return;
|
||||
_collisionEnterCount--;
|
||||
if (_collisionEnterCount != 0) return;
|
||||
|
||||
if (_activationCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_activationCoroutine);
|
||||
_activationCoroutine = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_spriteRenderer.color = deactivatedColor;
|
||||
foreach (Activateable activateable in activateables)
|
||||
{
|
||||
activateable.SetActivation(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsMatch(Collision2D collision)
|
||||
{
|
||||
if (requiresCube)
|
||||
{
|
||||
if (!collision.gameObject.CompareTag("Cube")) return false;
|
||||
var originMap = collision.gameObject.GetComponent<CubeController>().originMap;
|
||||
if (requiredCubeOriginMap != -1 && originMap != requiredCubeOriginMap) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private IEnumerator ActivateAfterDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(activationDelay);
|
||||
_activationCoroutine = null;
|
||||
|
||||
_spriteRenderer.color = activatedColor;
|
||||
foreach (Activateable activateable in activateables)
|
||||
{
|
||||
activateable.SetActivation(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Prefabs/Sensor/Scripts/SensorController.cs.meta
Normal file
2
Assets/Prefabs/Sensor/Scripts/SensorController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62fa21a751f6cedee9ac7f5910595add
|
||||
Reference in New Issue
Block a user