Add selected clear sounter highlight
This commit is contained in:
parent
5772da3965
commit
989648c00b
18 changed files with 352 additions and 127 deletions
|
@ -1,10 +1,8 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ClearCounter : MonoBehaviour
|
||||
{
|
||||
|
||||
|
||||
public void Interact()
|
||||
{
|
||||
Debug.Log("Interacted!");
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameInput : MonoBehaviour
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerAnimator : MonoBehaviour
|
||||
|
|
34
Assets/Scripts/SelectedCounterVisual.cs
Normal file
34
Assets/Scripts/SelectedCounterVisual.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class SelectedCounterVisual : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private ClearCounter clearCounter;
|
||||
[SerializeField] private GameObject visualGameObject;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Player.Instance.OnSelectedcounterChanged += Player_OnSelectedcounterChanged;
|
||||
}
|
||||
|
||||
private void Player_OnSelectedcounterChanged(object sender, Player.OnSelectedCounterChangedEventArgs e)
|
||||
{
|
||||
if (e.selectedCounter == clearCounter)
|
||||
{
|
||||
Show();
|
||||
} else
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
|
||||
private void Show()
|
||||
{
|
||||
visualGameObject.SetActive(true);
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
visualGameObject.SetActive(false);
|
||||
}
|
||||
}
|
11
Assets/Scripts/SelectedCounterVisual.cs.meta
Normal file
11
Assets/Scripts/SelectedCounterVisual.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6b8e4c74ab1ad4b4a8c3676f33904e52
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in a new issue