Skip to content

Commit

Permalink
Merge pull request #1 from eviltwo/interactions
Browse files Browse the repository at this point in the history
Interactions
  • Loading branch information
eviltwo authored Mar 4, 2024
2 parents fa427cf + d25b9ef commit df0e021
Show file tree
Hide file tree
Showing 27 changed files with 5,663 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/ActionGameCore/Assets/Interactions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/ActionGameCore/Assets/Interactions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [0.4.0] - 2024-03-04
### Added
- Add intaract system, event and button.
7 changes: 7 additions & 0 deletions src/ActionGameCore/Assets/Interactions/CHANGELOG.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/ActionGameCore/Assets/Interactions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Interactions
このパッケージは、カメラの中心にあるオブジェクトを検知し、プレイヤーが操作できるようにします。(例:電気のスイッチや棚の引き出し)

また、オブジェクトを見た時に"Eキーで開ける"などの文字を出せる仕組みも実装予定です。


# 注意
新InputSystemに対応しており、旧InputManagerは未対応です。

# インポート
UPMを使用して必要なパッケージをインポートできます。
1. InputSystemパッケージをインポートします。
1. このInteractionsパッケージをインポートします。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/Interactions
```

# 初期設定
1. InputActionAssetを作成し、その中に"Interact"というActionを作ります。
- キーボードの"E"キーを割り当てます。
1. メニューの"GameObject > Create Empty"で空オブジェクトを作成し、 `InteractRaycaster``InteractSystem` コンポーネントをアタッチします。
1. `InteractSystem` のInteractActionReferencesに"Interact"を設定します。

# 3Dボタン
1. CubeなどのColliderが付いているオブジェクトを作成し、 `Button3D` コンポーネントをアタッチします。
- InteractEventに任意の関数を設定します。
- 例えば、CubeのGameObjectのSetActiveを設定します。
1. ゲームを再生し、Cubeを見ながら"E"を押します。Cubeが消えるなど意図通りの関数が実行されたら完了です。

# Change log
[CHANGELOG.md](CHANGELOG.md)
7 changes: 7 additions & 0 deletions src/ActionGameCore/Assets/Interactions/README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/ActionGameCore/Assets/Interactions/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/ActionGameCore/Assets/Interactions/Scripts/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Interactions
{
/// <summary>
/// Interact object finder for FPS game.
/// </summary>
public class InteractCameraRaycaster : InteractObjectFinder
{
[SerializeField]
public Camera Camera = null;

[SerializeField]
public float MaxDistance = 10.0f;

[SerializeField]
public LayerMask LayerMask = -1;

private readonly RaycastHit[] _hitBuffer = new RaycastHit[50];

private class DistanceComparer : IComparer<RaycastHit>
{
public int Compare(RaycastHit x, RaycastHit y)
{
return x.distance.CompareTo(y.distance);
}
}

private readonly DistanceComparer _distanceComparer = new DistanceComparer();

private void Reset()
{
Camera = Camera.main;
}

public override void Find(List<GameObject> resultAppendList)
{
if (Camera == null)
{
return;
}

var ray = new Ray(Camera.transform.position, Camera.transform.forward);
var dist = Camera.farClipPlane;
var hitCount = Physics.RaycastNonAlloc(ray, _hitBuffer, dist, LayerMask);

if (hitCount > 1)
{
Array.Sort(_hitBuffer, 0, hitCount, _distanceComparer);
}

for (int i = 0; i < hitCount; i++)
{
var hit = _hitBuffer[i];
resultAppendList.Add(hit.collider.gameObject);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using UnityEngine;

namespace Interactions
{
public abstract class InteractObjectFinder : MonoBehaviour
{
protected virtual void OnEnable()
{
InteractObjectFinderManager.Add(this);
}

protected virtual void OnDisable()
{
InteractObjectFinderManager.Remove(this);
}

public abstract void Find(List<GameObject> resultAppendList);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace Interactions
{
public static class InteractObjectFinderManager
{
private static readonly List<InteractObjectFinder> _finders = new List<InteractObjectFinder>();

public static void Add(InteractObjectFinder objectFinder)
{
_finders.Add(objectFinder);
}

public static void Remove(InteractObjectFinder objectFinder)
{
_finders.Remove(objectFinder);
}

public static IReadOnlyList<InteractObjectFinder> GetObjectFinders()
{
return _finders;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Interactions
{
public interface IInteractHandler
{
string InputActionNameFilter { get; }

void OnInteract(InteractEventData eventData);
}

public class InteractEventData
{
public string InputActionName { get; set; }
public IInteractHandler InteractedObject { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#if SUPPORT_INPUTSYSTEM
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;

namespace Interactions
{
public class InteractSystem : MonoBehaviour
{
[SerializeField]
private InputActionReference[] _interactActionReferences = null;

[Serializable]
public class ButtonInteractEvent : UnityEvent<InteractEventData> { }

[SerializeField]
public ButtonInteractEvent InteractEvent;

private List<GameObject> _findBuffer = new List<GameObject>();

private List<IInteractHandler> _pointerOverInteractables = new List<IInteractHandler>();

public IReadOnlyList<IInteractHandler> PointerOverInteractables => _pointerOverInteractables;

private void Update()
{
_findBuffer.Clear();
var finders = InteractObjectFinderManager.GetObjectFinders();
var finderCount = finders.Count;
for (int i = 0; i < finderCount; i++)
{
var finder = finders[i];
finder.Find(_findBuffer);
}

_pointerOverInteractables.Clear();
if (_findBuffer.Count > 0)
{
var result = _findBuffer[0];
result.gameObject.GetComponents(_pointerOverInteractables);
}

var actionCount = _interactActionReferences.Length;
for (int i = 0; i < actionCount; i++)
{
var action = _interactActionReferences[i]?.action;
if (action != null && action.WasPressedThisFrame())
{
var handlerCount = _pointerOverInteractables.Count;
for (int j = 0; j < handlerCount; j++)
{
var interactable = _pointerOverInteractables[j];
var filter = interactable.InputActionNameFilter;
if (string.IsNullOrEmpty(filter) || filter == action.name)
{
var eventData = new InteractEventData
{
InputActionName = action.name,
InteractedObject = interactable,
};
interactable.OnInteract(eventData);
InteractEvent?.Invoke(eventData);
}
}
}
}
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using UnityEngine;
using UnityEngine.Events;

namespace Interactions
{
public class Button3D : MonoBehaviour, IInteractHandler
{
[SerializeField]
public string InteractActionNameFilter = "Interact";

public string InputActionNameFilter => InteractActionNameFilter;

[Serializable]
public class ButtonInteractEvent : UnityEvent<InteractEventData> { }

[SerializeField]
public ButtonInteractEvent InteractEvent;

public void OnInteract(InteractEventData eventData)
{
InteractEvent?.Invoke(eventData);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit df0e021

Please sign in to comment.