浏览代码

Basic Functional Interactions + Refactors

/main
Thomas ICHÉ 5 年前
当前提交
d81d62c3
共有 17 个文件被更改,包括 181 次插入3 次删除
  1. 2
      Runtime/Managers/Manager.cs
  2. 12
      Runtime/StateMachine/SetStateAction.cs
  3. 17
      Runtime/Interactions/BasicInteractive.cs
  4. 24
      Runtime/Interactions/BasicInteractiveUser.cs
  5. 18
      Runtime/Interactions/Interaction.cs
  6. 11
      Runtime/Interactions/Interaction.cs.meta
  7. 43
      Runtime/Interactions/InteractionManager.cs
  8. 39
      Runtime/Interactions/Interactive.cs
  9. 18
      Runtime/Interactions/InteractiveUser.cs
  10. 0
      /Runtime/Interactions.meta
  11. 0
      /Runtime/Interactions/BasicInteractive.cs.meta
  12. 0
      /Runtime/Interactions/BasicInteractiveUser.cs.meta
  13. 0
      /Runtime/Interactions/Interactive.cs.meta
  14. 0
      /Runtime/Interactions/InteractionManager.cs.meta
  15. 0
      /Runtime/Interactions/InteractiveUser.cs.meta

2
Runtime/Managers/Manager.cs


using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

12
Runtime/StateMachine/SetStateAction.cs


{
[NonNullCheck]
public StateMachine StateMachine;
public string State = "State";
public string state
{
get { return m_State; }
set { m_State = value; }
}
[SerializeField]
protected string m_State = "State";
StateMachine.SetState(State);
StateMachine.SetState(m_State);
}
}
}

17
Runtime/Interactions/BasicInteractive.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Interactions
{
public class BasicInteractive : Interactive
{
[Header("Settings")]
public float Distance = 1.0f;
public override bool CanInteract(InteractiveUser user)
{
return Vector3.Distance(user.transform.position, this.transform.position) < Distance;
}
}
}

24
Runtime/Interactions/BasicInteractiveUser.cs


using System.Linq;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Interactions
{
public class BasicInteractiveUser : InteractiveUser
{
public Camera Camera;
public float InteractionAngle = 60.0f;
public override bool CanInteract(Interactive interactive)
{
Vector3 toInteractive = (interactive.transform.position - Camera.transform.position).normalized;
return Mathf.Acos(Vector3.Dot(toInteractive, Camera.transform.forward)) < InteractionAngle;
}
public override Interactive[] SortCandidates(IEnumerable<Interactive> candidates)
{
return candidates.OrderBy(a => Vector3.Distance(a.gameObject.transform.position, this.transform.position)).ToArray();
}
}
}

18
Runtime/Interactions/Interaction.cs


using GameplayIngredients.Actions;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Interactions
{
public class Interaction : ActionBase
{
public InteractiveUser InteractiveUser;
public override void Execute(GameObject instigator = null)
{
if (InteractiveUser != null)
InteractiveUser.Interact();
}
}
}

11
Runtime/Interactions/Interaction.cs.meta


fileFormatVersion: 2
guid: 4401de233d589a14cbba6dffc6dbc8d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

43
Runtime/Interactions/InteractionManager.cs


using System.Collections.Generic;
namespace GameplayIngredients.Interactions
{
public static class InteractionManager
{
static List<Interactive> s_Interactives = new List<Interactive>();
public static void RegisterInteractive(Interactive interactive)
{
s_Interactives.Add(interactive);
}
public static void RemoveInteractive(Interactive interactive)
{
s_Interactives.Remove(interactive);
}
public static void Interact(InteractiveUser user)
{
foreach(var interactive in GetCandidates(user))
{
if (interactive.Interact(user))
break;
}
}
private static Interactive[] GetCandidates(InteractiveUser user)
{
List<Interactive> candidates = new List<Interactive>();
foreach(var interactive in s_Interactives)
{
// Filter interactives at user's reach
if (interactive.CanInteract(user))
candidates.Add(interactive);
}
return user.SortCandidates(candidates.ToArray());
}
}
}

39
Runtime/Interactions/Interactive.cs


using GameplayIngredients.Events;
using NaughtyAttributes;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Interactions
{
public abstract class Interactive : EventBase
{
[Header("Events")]
[SerializeField, ReorderableList]
protected Callable[] OnInteract;
protected virtual void OnEnable()
{
InteractionManager.RegisterInteractive(this);
}
protected virtual void OnDisable()
{
InteractionManager.RemoveInteractive(this);
}
public bool Interact(InteractiveUser user)
{
if (user.CanInteract(this) && CanInteract(user))
{
Callable.Call(OnInteract, user.gameObject);
return true;
}
else
return false;
}
public abstract bool CanInteract(InteractiveUser user);
}
}

18
Runtime/Interactions/InteractiveUser.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GameplayIngredients.Interactions
{
public abstract class InteractiveUser : MonoBehaviour
{
public abstract bool CanInteract(Interactive interactive);
public abstract Interactive[] SortCandidates(IEnumerable<Interactive> candidates);
public void Interact()
{
InteractionManager.Interact(this);
}
}
}

/Runtime/Interactive.meta → /Runtime/Interactions.meta

/Runtime/Interactive/BasicInteractive.cs.meta → /Runtime/Interactions/BasicInteractive.cs.meta

/Runtime/Interactive/BasicInteractiveUser.cs.meta → /Runtime/Interactions/BasicInteractiveUser.cs.meta

/Runtime/Interactive/Interactive.cs.meta → /Runtime/Interactions/Interactive.cs.meta

/Runtime/Interactive/InteractiveManager.cs.meta → /Runtime/Interactions/InteractionManager.cs.meta

/Runtime/Interactive/InteractiveUser.cs.meta → /Runtime/Interactions/InteractiveUser.cs.meta

正在加载...
取消
保存