Move container logic to use inheritence and move logic around.
Also fix the clear counter spawning items, next I'm going to implament the logic for placing food onto them instead.
This commit is contained in:
parent
234e00cb9b
commit
44837c6c13
21 changed files with 1478 additions and 397 deletions
42
Assets/Scripts/BaseCounter.cs
Normal file
42
Assets/Scripts/BaseCounter.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class BaseCounter : MonoBehaviour, IKitchenObjectParent
|
||||
{
|
||||
|
||||
[SerializeField] private Transform counterTopPoint;
|
||||
|
||||
private KitchenObject kitchenObject;
|
||||
|
||||
public virtual void Interact(Player player)
|
||||
{
|
||||
Debug.LogError("BaseCounter.Interact();");
|
||||
}
|
||||
|
||||
public Transform GetKitchenObjectFollowTransform()
|
||||
{
|
||||
return counterTopPoint;
|
||||
}
|
||||
|
||||
public void SetKitchenObject(KitchenObject kitchenObject)
|
||||
{
|
||||
this.kitchenObject = kitchenObject;
|
||||
}
|
||||
|
||||
public KitchenObject GetKitchenObject()
|
||||
{
|
||||
return kitchenObject;
|
||||
}
|
||||
|
||||
public void ClearKitchenObject()
|
||||
{
|
||||
kitchenObject = null;
|
||||
}
|
||||
|
||||
public bool HasKitchenObject()
|
||||
{
|
||||
return kitchenObject != null;
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/BaseCounter.cs.meta
Normal file
11
Assets/Scripts/BaseCounter.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9e9dc0c79ee97d84e9fe3b4eb090bd76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -2,52 +2,15 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ClearCounter : MonoBehaviour, IKitchenObjectParent
|
||||
public class ClearCounter : BaseCounter
|
||||
{
|
||||
|
||||
|
||||
[SerializeField] private KitchenObjectSO kitchenObjectSO;
|
||||
[SerializeField] private Transform counterTopPoint;
|
||||
|
||||
private KitchenObject kitchenObject;
|
||||
|
||||
public void Interact(Player player)
|
||||
public override void Interact(Player player)
|
||||
{
|
||||
if (kitchenObject == null)
|
||||
{
|
||||
Transform kitchenObjectTransform = Instantiate(kitchenObjectSO.prefab, counterTopPoint);
|
||||
kitchenObjectTransform.GetComponent<KitchenObject>().SetKitchenObjectParent(this);
|
||||
} else
|
||||
{
|
||||
// Give object to player
|
||||
kitchenObject.SetKitchenObjectParent(player);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Transform GetKitchenObjectFollowTransform()
|
||||
{
|
||||
return counterTopPoint;
|
||||
}
|
||||
|
||||
public void SetKitchenObject(KitchenObject kitchenObject)
|
||||
{
|
||||
this.kitchenObject = kitchenObject;
|
||||
}
|
||||
|
||||
public KitchenObject GetKitchenObject()
|
||||
{
|
||||
return kitchenObject;
|
||||
}
|
||||
|
||||
public void ClearKitchenObject()
|
||||
{
|
||||
kitchenObject = null;
|
||||
}
|
||||
|
||||
public bool HasKitchenObject()
|
||||
{
|
||||
return kitchenObject != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
24
Assets/Scripts/ContainerCounter.cs
Normal file
24
Assets/Scripts/ContainerCounter.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ContainerCounter : BaseCounter
|
||||
{
|
||||
|
||||
public event EventHandler OnPlayerGrabbedObject;
|
||||
|
||||
[SerializeField] private KitchenObjectSO kitchenObjectSO;
|
||||
|
||||
public override void Interact(Player player)
|
||||
{
|
||||
if (!HasKitchenObject())
|
||||
{
|
||||
Transform kitchenObjectTransform = Instantiate(kitchenObjectSO.prefab);
|
||||
kitchenObjectTransform.GetComponent<KitchenObject>().SetKitchenObjectParent(player);
|
||||
|
||||
OnPlayerGrabbedObject?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/ContainerCounter.cs.meta
Normal file
11
Assets/Scripts/ContainerCounter.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 85e5fb2507b46314682f5d827a1eb8eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
28
Assets/Scripts/ContainerCounterVisual.cs
Normal file
28
Assets/Scripts/ContainerCounterVisual.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ContainerCounterVisual : MonoBehaviour
|
||||
{
|
||||
|
||||
private const string OPEN_CLOSE = "OpenClose";
|
||||
|
||||
[SerializeField] private ContainerCounter containerCounter;
|
||||
|
||||
private Animator animator;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
containerCounter.OnPlayerGrabbedObject += ContainerCounter_OnPlayerGrabbedObject;
|
||||
}
|
||||
|
||||
private void ContainerCounter_OnPlayerGrabbedObject(object sender, System.EventArgs e)
|
||||
{
|
||||
animator.SetTrigger(OPEN_CLOSE);
|
||||
}
|
||||
}
|
11
Assets/Scripts/ContainerCounterVisual.cs.meta
Normal file
11
Assets/Scripts/ContainerCounterVisual.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ef4e04faf28bf8f4c9d9b6e814e5549d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -12,7 +12,7 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
public event EventHandler<OnSelectedCounterChangedEventArgs> OnSelectedcounterChanged;
|
||||
public class OnSelectedCounterChangedEventArgs : EventArgs
|
||||
{
|
||||
public ClearCounter selectedCounter;
|
||||
public BaseCounter selectedCounter;
|
||||
}
|
||||
|
||||
[SerializeField] private float moveSpeed = 7f;
|
||||
|
@ -22,7 +22,7 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
|
||||
private bool isWalking;
|
||||
private Vector3 lastInteractDir;
|
||||
private ClearCounter selectedCounter;
|
||||
private BaseCounter selectedCounter;
|
||||
private KitchenObject kitchenObject;
|
||||
|
||||
private void Start()
|
||||
|
@ -67,7 +67,7 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
|
||||
Vector3 moveDir = new Vector3(inputVector.x, 0f, inputVector.y);
|
||||
|
||||
// Kepp a constantly updating record of the last move direction.
|
||||
// Kepp a consistently updating record of the last move direction.
|
||||
// This means you can interact without holding down a direction.
|
||||
if (moveDir != Vector3.zero)
|
||||
{
|
||||
|
@ -75,21 +75,25 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
}
|
||||
|
||||
float interactDistance = 2f;
|
||||
// fire a raycast forward to search for objects in the countersLayerMask and output result to raycastHit
|
||||
if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
|
||||
{
|
||||
if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
|
||||
// if the raycast hits an object of type BaseCounter
|
||||
if (raycastHit.transform.TryGetComponent(out BaseCounter baseCounter))
|
||||
{
|
||||
// Has Clear Counter
|
||||
if (clearCounter != selectedCounter)
|
||||
// if selectedCounter is not equal to baseCounter update it
|
||||
if (baseCounter != selectedCounter)
|
||||
{
|
||||
SetSelectedCounter(clearCounter);
|
||||
SetSelectedCounter(baseCounter);
|
||||
}
|
||||
}
|
||||
// else if no BaseCounter was hit and selectedCounter isn't null then set it to null
|
||||
else if (selectedCounter != null)
|
||||
{
|
||||
SetSelectedCounter(null);
|
||||
}
|
||||
}
|
||||
// else if no BaseCounter was hit and selectedCounter isn't null then set it to null
|
||||
else if (selectedCounter != null)
|
||||
{
|
||||
SetSelectedCounter(null);
|
||||
|
@ -109,47 +113,57 @@ public class Player : MonoBehaviour, IKitchenObjectParent
|
|||
float moveDistance = moveSpeed * Time.deltaTime;
|
||||
float playerRadius = .7f;
|
||||
float playerHeight = 2f;
|
||||
// fire a CapsuleCast (A 3D raycast in the shape of a capsule) to see if there is an object in the way of the player movement.
|
||||
bool canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDir, moveDistance);
|
||||
|
||||
// if the raycast hit something then this canMove is false and we attempt to move on eithery X or Z
|
||||
if (!canMove)
|
||||
{
|
||||
// Cannot move towards moveDir
|
||||
// cannot move towards moveDir
|
||||
|
||||
// Attempt only X move
|
||||
// attempt only X move
|
||||
Vector3 moveDirX = new Vector3(moveDir.x, 0, 0).normalized;
|
||||
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirX, moveDistance);
|
||||
|
||||
// if able to move on X then change moveDir to X
|
||||
if (canMove)
|
||||
{
|
||||
moveDir = moveDirX;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Cannot move on X so attempt to move on Z
|
||||
// cannot move on X so attempt to move on Z
|
||||
Vector3 moveDirZ = new Vector3(0, 0, moveDir.z).normalized;
|
||||
canMove = !Physics.CapsuleCast(transform.position, transform.position + Vector3.up * playerHeight, playerRadius, moveDirZ, moveDistance);
|
||||
|
||||
// if able to move on Z then change moveDir to Z
|
||||
if (canMove)
|
||||
{
|
||||
moveDir = moveDirZ;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Cannot move at all
|
||||
// cannot move at all so canMove is still false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (canMove)
|
||||
{
|
||||
// add the time ajusted movement vector to current position which keep a consistent speed no matter the FPS
|
||||
transform.position += moveDir * moveDistance;
|
||||
}
|
||||
|
||||
// set isWalking to true if moveDir is not (0,0,0)
|
||||
isWalking = moveDir != Vector3.zero;
|
||||
|
||||
float rotateSpeed = 10f;
|
||||
// smothly rotate towards moveDir with a Slerp smoothed operation.
|
||||
// Slerp is for like rotations while Lerp is for positions
|
||||
transform.forward = Vector3.Slerp(transform.forward, moveDir, Time.deltaTime * rotateSpeed);
|
||||
}
|
||||
|
||||
private void SetSelectedCounter(ClearCounter selectedCounter)
|
||||
private void SetSelectedCounter(BaseCounter selectedCounter)
|
||||
{
|
||||
this.selectedCounter = selectedCounter;
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ using UnityEngine;
|
|||
public class SelectedCounterVisual : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private ClearCounter clearCounter;
|
||||
[SerializeField] private GameObject visualGameObject;
|
||||
[SerializeField] private BaseCounter baseCounter;
|
||||
[SerializeField] private GameObject[] visualGameObjectArray;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
@ -15,7 +15,7 @@ public class SelectedCounterVisual : MonoBehaviour
|
|||
|
||||
private void Player_OnSelectedcounterChanged(object sender, Player.OnSelectedCounterChangedEventArgs e)
|
||||
{
|
||||
if (e.selectedCounter == clearCounter)
|
||||
if (e.selectedCounter == baseCounter)
|
||||
{
|
||||
Show();
|
||||
} else
|
||||
|
@ -25,12 +25,18 @@ public class SelectedCounterVisual : MonoBehaviour
|
|||
}
|
||||
|
||||
private void Show()
|
||||
{
|
||||
visualGameObject.SetActive(true);
|
||||
{
|
||||
foreach (GameObject visualGameObject in visualGameObjectArray)
|
||||
{
|
||||
visualGameObject.SetActive(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void Hide()
|
||||
{
|
||||
visualGameObject.SetActive(false);
|
||||
foreach (GameObject visualGameObject in visualGameObjectArray)
|
||||
{
|
||||
visualGameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue