Last active
April 5, 2025 18:52
-
-
Save ysalihtuncel/904c842b442b8eeee657ee41424520ac to your computer and use it in GitHub Desktop.
Door
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
public class Door : MonoBehaviour, ISwitchable | |
{ | |
private bool isActivated; | |
public bool IsActivated => isActivated; | |
public Switch switcher; | |
//Local angles for the door to open and close | |
public float openAngle = 90f, closeAngle = 0f; | |
public float duration = 1f; | |
void Start() | |
{ | |
switcher.SetClient(this); | |
} | |
public void Activate() | |
{ | |
Open(); | |
isActivated = true; | |
} | |
public void Deactivate() | |
{ | |
Close(); | |
isActivated = false; | |
} | |
public void Open() | |
{ | |
Debug.Log("Door opened!"); | |
StopAllCoroutines(); | |
StartCoroutine(DoorAction(openAngle)); | |
} | |
public void Close() | |
{ | |
Debug.Log("Door closed!"); | |
StopAllCoroutines(); | |
StartCoroutine(DoorAction(closeAngle)); | |
} | |
IEnumerator DoorAction(float angle) | |
{ | |
float t = 0; | |
Vector3 startRot = transform.eulerAngles; | |
Vector3 endRot = new Vector3(0, angle, 0); | |
while (t < 1) | |
{ | |
t += Time.deltaTime / duration; | |
transform.eulerAngles = Vector3.Lerp(startRot, endRot, t); | |
yield return null; | |
} | |
transform.eulerAngles = endRot; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public interface ISwitchable | |
{ | |
public bool IsActivated { get; } | |
public void Activate(); | |
public void Deactivate(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Switch : MonoBehaviour | |
{ | |
ISwitchable client; | |
public void SetClient(ISwitchable client) | |
{ | |
this.client = client; | |
} | |
public void Toggle() | |
{ | |
if (client.IsActivated) | |
{ | |
client.Deactivate(); | |
} | |
else | |
{ | |
client.Activate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment