Add selected clear sounter highlight
This commit is contained in:
parent
5772da3965
commit
989648c00b
18 changed files with 352 additions and 127 deletions
|
@ -1,44 +1,46 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
|
||||
public static Player Instance { get; private set; }
|
||||
|
||||
public event EventHandler<OnSelectedCounterChangedEventArgs> OnSelectedcounterChanged;
|
||||
public class OnSelectedCounterChangedEventArgs : EventArgs
|
||||
{
|
||||
public ClearCounter selectedCounter;
|
||||
}
|
||||
|
||||
[SerializeField] private float moveSpeed = 7f;
|
||||
[SerializeField] private GameInput gameInput;
|
||||
[SerializeField] private LayerMask countersLayerMask;
|
||||
|
||||
private bool isWalking;
|
||||
private Vector3 lastInteractDir;
|
||||
private ClearCounter selectedCounter;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
gameInput.OnInteractAction += GameInput_OnInteractAction;
|
||||
gameInput.OnInteractAction += GameInput_OnInteractAction;
|
||||
}
|
||||
|
||||
private void GameInput_OnInteractAction(object sender, System.EventArgs e)
|
||||
{
|
||||
Vector2 inputVector = gameInput.GetMovementVectorNormalized();
|
||||
|
||||
Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
|
||||
|
||||
// Kepp a constantly updating record of the last move direction.
|
||||
// This means you can interact without holding down a direction.
|
||||
if (moveDir != Vector3.zero)
|
||||
if (selectedCounter != null)
|
||||
{
|
||||
lastInteractDir = moveDir;
|
||||
selectedCounter.Interact();
|
||||
}
|
||||
}
|
||||
|
||||
float interactDistance = 2f;
|
||||
if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
|
||||
{
|
||||
// Has Clear Counter
|
||||
clearCounter.Interact();
|
||||
}
|
||||
Debug.LogError("There is more than one player instance!");
|
||||
}
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
@ -71,12 +73,24 @@ public class Player : MonoBehaviour
|
|||
float interactDistance = 2f;
|
||||
if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
|
||||
{
|
||||
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
|
||||
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
|
||||
{
|
||||
// Has Clear Counter
|
||||
//clearCounter.Interact();
|
||||
if (clearCounter != selectedCounter)
|
||||
{
|
||||
SetSelectedCounter(clearCounter);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SetSelectedCounter(null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SetSelectedCounter(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -131,4 +145,14 @@ public class Player : MonoBehaviour
|
|||
transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);
|
||||
}
|
||||
|
||||
private void SetSelectedCounter(ClearCounter selectedCounter)
|
||||
{
|
||||
this.selectedCounter = selectedCounter;
|
||||
|
||||
OnSelectedcounterChanged?.Invoke(this, new OnSelectedCounterChangedEventArgs
|
||||
{
|
||||
selectedCounter = selectedCounter,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue