public class EntityScriptableObject : ScriptableObject
{
[SerializeField] private EntityType _type;
[SerializeField] private string _displayName;
[SerializeField] private string _description;
[SerializeField] private string _raycastFeedbackText;
[SerializeField] private GameObject _prefab;
public EntityType Type => _type;
public string DisplayName => _displayName;
public string Description => _description;
public string RaycastFeedbackText => _raycastFeedbackText;
public GameObject Prefab => _prefab;
}
public class ItemScriptableObject : EntityScriptableObject
{
[SerializeField] private Sprite _sprite;
public Sprite Sprite => _sprite;
}
public class DocumentScriptableObject : EntityScriptableObject
{
[SerializeField] private string _text;
public string Text => _text;
}
public class Entity : MonoBehaviour
{
[field: SerializeField]
public virtual EntityScriptableObject ScriptableObject
{
get;
private set;
}
public void Interact()
{
Transform interactionPoint;
switch (ScriptableObject.Type)
{
case EntityType.Key:
Player.Inventory.Add(this as Item);
Destroy(gameObject);
break;
case EntityType.VideoTape:
Player.Inventory.Add(this as Item);
Destroy(gameObject);
break;
case EntityType.Note:
Player.Journal.Add(this as Document);
Destroy(gameObject);
break;
case EntityType.Painting:
GameManager.ChangeTypeOfControll(TypesOfControl.InspectionControll);
ObjectInspectorController.Instance.enabled = true;
ObjectInspectorController.Instance.Initialize(this);
break;
case EntityType.TV:
GameManager.PauseGame();
interactionPoint = InteractionPoints.Instance.TV.transform;
InteractionController.Instance.InteractionPoint = interactionPoint;
InteractionController.Instance.CurrentInteraction = InteractionType.TV;
InteractionController.Instance.MoveToInteractionPoint();
InteractionController.Instance.OnStopInteraction += StopInteraction;
UIController.Instance.OpenInventory();
PlayerController.Instance.enabled = false;
break;
case EntityType.Door:
DoorController door = gameObject.GetComponent<DoorController>();
if (door.IsOpen)
door.CloseDoor();
else
door.OpenDoor();
break;
case EntityType.SafeDigital:
GameManager.PauseGame();
GameManager.ChangeTypeOfControll(TypesOfControl.InteractionControl);
interactionPoint = InteractionPoints.Instance.SafeDigital.transform;
InteractionController.Instance.InteractionPoint = interactionPoint;
InteractionController.Instance.CurrentInteraction = InteractionType.SafeNumeric;
InteractionController.Instance.MoveToInteractionPoint();
InteractionController.Instance.OnStopInteraction += StopInteraction;
PlayerController.Instance.enabled = false;
CameraController.Instance.enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
gameObject.GetComponentInChildren<SafeDoor>().enabled = true;
break;
case EntityType.SafePadlock:
GameManager.PauseGame();
GameManager.ChangeTypeOfControll(TypesOfControl.InteractionControl);
interactionPoint = InteractionPoints.Instance.SafePadlock.transform;
InteractionController.Instance.InteractionPoint = interactionPoint;
InteractionController.Instance.CurrentInteraction = InteractionType.SafePadlock;
InteractionController.Instance.MoveToInteractionPoint();
InteractionController.Instance.OnStopInteraction += StopInteraction;
PlayerController.Instance.enabled = false;
CameraController.Instance.enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
gameObject.GetComponentInChildren<Padlock>().enabled = true;
break;
default:
break;
}
}
public void StopInteraction()
{
switch (ScriptableObject.Type)
{
case EntityType.Key:
break;
case EntityType.VideoTape:
break;
case EntityType.Note:
break;
case EntityType.Painting:
break;
case EntityType.TV:
InteractionController.Instance.OnStopInteraction -= StopInteraction;
InteractionController.Instance.ReturnToInteractionPosition();
InteractionController.Instance.CurrentInteraction = InteractionType.None;
GameManager.ResumeGame();
break;
case EntityType.Door:
break;
case EntityType.SafeDigital:
gameObject.GetComponentInChildren<SafeDoor>().enabled = false;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
PlayerController.Instance.enabled = true;
CameraController.Instance.enabled = true;
InteractionController.Instance.OnStopInteraction -= StopInteraction;
InteractionController.Instance.CurrentInteraction = InteractionType.None;
InteractionController.Instance.ReturnToInteractionPosition();
GameManager.ChangeTypeOfControll(TypesOfControl.PlayerControl);
GameManager.ResumeGame();
break;
case EntityType.SafePadlock:
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
PlayerController.Instance.enabled = true;
CameraController.Instance.enabled = true;
InteractionController.Instance.OnStopInteraction -= StopInteraction;
InteractionController.Instance.CurrentInteraction = InteractionType.None;
InteractionController.Instance.ReturnToInteractionPosition();
GameManager.ChangeTypeOfControll(TypesOfControl.PlayerControl);
GameManager.ResumeGame();
break;
default:
break;
}
}
}
public class Item : Entity
{
[SerializeField] private ItemScriptableObject _scriptableObject;
public override EntityScriptableObject ScriptableObject => _scriptableObject;
public void Use()
{
switch (_scriptableObject.Type)
{
case EntityType.Key:
Player.Inventory.Remove(this);
Debug.Log("Key used.");
break;
case EntityType.VideoTape:
if (InteractionController.Instance.CurrentInteraction != InteractionType.TV)
return;
Player.Inventory.Remove(this);
Debug.Log("Tape inserted.");
break;
case EntityType.Note:
break;
case EntityType.Painting:
break;
case EntityType.TV:
break;
case EntityType.Door:
break;
case EntityType.SafeDigital:
break;
case EntityType.SafePadlock:
break;
default:
break;
}
}
}
public class Document : Entity
{
[SerializeField] private DocumentScriptableObject _scriptableObject;
public override EntityScriptableObject ScriptableObject => _scriptableObject;
public void Read()
{
switch (ScriptableObject.Type)
{
case EntityType.Key:
break;
case EntityType.VideoTape:
break;
case EntityType.Note:
DocumentInspectorController.Instance.enabled = true;
DocumentInspectorController.Instance.Initialize(_scriptableObject.Text);
break;
case EntityType.Painting:
break;
case EntityType.TV:
break;
case EntityType.Door:
break;
case EntityType.SafeDigital:
break;
case EntityType.SafePadlock:
break;
default:
break;
}
}
}
Я избавился от наследования классов ScriptableObject, так что теперь иерархию наследования имеют только объекты Entity, Item и Document.
Метод Interact в наследниках переопределяю так как по задумке объект одного и того же типа (например ключ) может как быть предметом, и его можно будет подобрать и использовать, так может и просто лежать где-нибудь, но подобрать его уже нельзя, хотя всё ещё можно как-либо взаимодействовать, например осмотреть.