Initial Commit
This commit is contained in:
148
Assets/Scripts/PlayerController.cs
Normal file
148
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] InputActionReference moveAction, jumpAction, lookAction, selectAction, discardAction;
|
||||
[SerializeField] float moveSpeed, lookSpeed, minLookAngle, maxLookAngle, interactionRange, throwForce;
|
||||
[SerializeField] float jumpForce;
|
||||
[SerializeField] CharacterController charCon;
|
||||
[SerializeField] Camera theCam;
|
||||
[SerializeField] Transform holdPoint;
|
||||
[SerializeField] LayerMask whatIsStock, whatIsShelf;
|
||||
private float ySpeed, horiRot, vertRot, currentThrowPower;
|
||||
private StockObject heldPickup;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (moveAction != null)
|
||||
moveAction.action.Enable();
|
||||
if (jumpAction != null)
|
||||
jumpAction.action.Enable();
|
||||
if (lookAction != null)
|
||||
lookAction.action.Enable();
|
||||
if (selectAction != null)
|
||||
selectAction.action.Enable();
|
||||
if (discardAction != null)
|
||||
discardAction.action.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (moveAction != null)
|
||||
moveAction.action.Disable();
|
||||
if (jumpAction != null)
|
||||
jumpAction.action.Disable();
|
||||
if (lookAction != null)
|
||||
lookAction.action.Disable();
|
||||
if (selectAction != null)
|
||||
selectAction.action.Disable();
|
||||
if (discardAction != null)
|
||||
discardAction.action.Disable();
|
||||
}
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
Vector2 lookInput = lookAction.action.ReadValue<Vector2>();
|
||||
horiRot += lookInput.x * Time.deltaTime * lookSpeed;
|
||||
transform.rotation = Quaternion.Euler(0f, horiRot, 0f);
|
||||
vertRot -= lookInput.y * Time.deltaTime * lookSpeed;
|
||||
vertRot = Mathf.Clamp(vertRot, minLookAngle, maxLookAngle);
|
||||
theCam.transform.localRotation = Quaternion.Euler(vertRot, 0f, 0f);
|
||||
|
||||
|
||||
|
||||
// Safety check to prevent errors if references are missing in the Inspector
|
||||
if (moveAction == null || charCon == null) return;
|
||||
|
||||
Vector2 moveInput = moveAction.action.ReadValue<Vector2>();
|
||||
//Debug.Log(moveInput);
|
||||
//transform.position = transform.position + new Vector3(moveInput.x * Time.deltaTime * moveSpeed, 0f, moveInput.y * Time.deltaTime * moveSpeed);
|
||||
//Vector3 moveAmount = new Vector3(moveInput.x, 0f, moveInput.y);
|
||||
Vector3 vertMove = transform.forward * moveInput.y;
|
||||
Vector3 horiMove = transform.right * moveInput.x;
|
||||
Vector3 moveAmount = vertMove + horiMove;
|
||||
moveAmount = moveAmount.normalized * moveSpeed;
|
||||
if (charCon.isGrounded)
|
||||
{
|
||||
ySpeed = 0;
|
||||
if (jumpAction.action.WasPressedThisFrame())
|
||||
ySpeed = jumpForce;
|
||||
}
|
||||
ySpeed = ySpeed + (Physics.gravity.y * Time.deltaTime);
|
||||
|
||||
moveAmount.y = ySpeed;
|
||||
charCon.Move(moveAmount * Time.deltaTime);
|
||||
|
||||
//check for pickup
|
||||
Ray ray = theCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
|
||||
RaycastHit hit;
|
||||
if (heldPickup == null)
|
||||
{
|
||||
if(selectAction.action.WasPressedThisFrame())
|
||||
{
|
||||
if (Physics.Raycast(ray, out hit, interactionRange, whatIsStock))
|
||||
{
|
||||
//Debug.Log("Hit " + hit.collider.name);
|
||||
heldPickup = hit.collider.GetComponent<StockObject>();
|
||||
heldPickup.transform.SetParent(holdPoint);
|
||||
heldPickup.Pickup();
|
||||
}
|
||||
}
|
||||
if (discardAction.action.WasPressedThisFrame())
|
||||
{
|
||||
if (Physics.Raycast(ray, out hit, interactionRange, whatIsShelf))
|
||||
{
|
||||
heldPickup = hit.collider.GetComponent<ShelfSpaceController>().Getstock();
|
||||
if (heldPickup != null)
|
||||
{
|
||||
heldPickup.transform.SetParent(holdPoint);
|
||||
heldPickup.Pickup();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (heldPickup != null)
|
||||
{
|
||||
if(selectAction.action.WasPressedThisFrame())
|
||||
{
|
||||
if (Physics.Raycast(ray, out hit, interactionRange, whatIsShelf))
|
||||
{
|
||||
//heldPickup.MakePlaced();
|
||||
//heldPickup.transform.SetParent(hit.transform);
|
||||
//heldPickup = null;
|
||||
hit.collider.GetComponent<ShelfSpaceController>().PlaceStock(heldPickup);
|
||||
if(heldPickup.isPlaced)
|
||||
heldPickup = null;
|
||||
}
|
||||
}
|
||||
// While the button is held, increase the current power up to the max throwForce
|
||||
if (discardAction.action.IsPressed())
|
||||
{
|
||||
currentThrowPower = Mathf.Min(currentThrowPower + (throwForce * Time.deltaTime), throwForce);
|
||||
}
|
||||
|
||||
// When the button is released, either throw or drop
|
||||
if (discardAction.action.WasReleasedThisFrame())
|
||||
{
|
||||
heldPickup.Release();
|
||||
|
||||
// If power is above a small threshold (not just a quick click), apply force
|
||||
if (currentThrowPower > throwForce * 0.1f)
|
||||
{
|
||||
heldPickup.theRB.AddForce(theCam.transform.forward * currentThrowPower, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
heldPickup = null;
|
||||
currentThrowPower = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/PlayerController.cs.meta
Normal file
2
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 379eace162d998c42b5c291a03ac8da1
|
||||
41
Assets/Scripts/ShelfSpaceController.cs
Normal file
41
Assets/Scripts/ShelfSpaceController.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class ShelfSpaceController : MonoBehaviour
|
||||
{
|
||||
public StockInfo info;
|
||||
public int amountOnShelf;
|
||||
public List<StockObject> objectsOnShelf;
|
||||
public void PlaceStock(StockObject objectToPlace)
|
||||
{
|
||||
if (objectToPlace == null) return;
|
||||
|
||||
// If the shelf is empty, initialize it with the item's info
|
||||
//if (amountOnShelf == 0)
|
||||
if(objectsOnShelf.Count == 0)
|
||||
{
|
||||
info = objectToPlace.info;
|
||||
}
|
||||
// Otherwise, verify that the item matches the shelf's assigned stock type
|
||||
else if (info == null || info.name != objectToPlace.info.name)
|
||||
{
|
||||
return; // Name mismatch or info missing, exit early
|
||||
}
|
||||
|
||||
// Parent the object to the shelf and trigger the placement logic
|
||||
objectToPlace.transform.SetParent(transform);
|
||||
objectToPlace.MakePlaced();
|
||||
//amountOnShelf++;
|
||||
objectsOnShelf.Add(objectToPlace);
|
||||
}
|
||||
public StockObject Getstock()
|
||||
{
|
||||
StockObject stockToReturn = null;
|
||||
if(objectsOnShelf.Count > 0)
|
||||
{
|
||||
stockToReturn = objectsOnShelf[objectsOnShelf.Count - 1];
|
||||
objectsOnShelf.RemoveAt(objectsOnShelf.Count - 1);
|
||||
}
|
||||
return stockToReturn;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/ShelfSpaceController.cs.meta
Normal file
2
Assets/Scripts/ShelfSpaceController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2c0d8b7a9a872e428143f5845dcc36a
|
||||
21
Assets/Scripts/StockInfo.cs
Normal file
21
Assets/Scripts/StockInfo.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class StockInfo
|
||||
{
|
||||
public string name;
|
||||
public enum StockType
|
||||
{
|
||||
Produce,
|
||||
Dairy,
|
||||
Meat,
|
||||
Bakery,
|
||||
Frozen,
|
||||
Cereal,
|
||||
bigDrink,
|
||||
chipsTube,
|
||||
fruit,
|
||||
fruitLarge
|
||||
}
|
||||
public StockType typeOfStock;
|
||||
}
|
||||
2
Assets/Scripts/StockInfo.cs.meta
Normal file
2
Assets/Scripts/StockInfo.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 264f84116bc7a424da7380846673a013
|
||||
46
Assets/Scripts/StockObject.cs
Normal file
46
Assets/Scripts/StockObject.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class StockObject : MonoBehaviour
|
||||
{
|
||||
public StockInfo info;
|
||||
[SerializeField] float moveSpeed;
|
||||
public bool isPlaced;
|
||||
public Rigidbody theRB;
|
||||
private Collider theCol;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
TryGetComponent(out theRB);
|
||||
TryGetComponent(out theCol);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(isPlaced)
|
||||
{
|
||||
transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.zero, moveSpeed * Time.deltaTime);
|
||||
transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.identity, moveSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
public void Pickup()
|
||||
{
|
||||
theRB.isKinematic = true;
|
||||
transform.localPosition = Vector3.zero;
|
||||
transform.localRotation = Quaternion.identity;
|
||||
isPlaced = false;
|
||||
theCol.enabled = false;
|
||||
}
|
||||
public void MakePlaced()
|
||||
{
|
||||
theRB.isKinematic = true;
|
||||
isPlaced = true;
|
||||
theCol.enabled = false;
|
||||
}
|
||||
public void Release()
|
||||
{
|
||||
transform.SetParent(null);
|
||||
theRB.isKinematic = false;
|
||||
theCol.enabled = true;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/StockObject.cs.meta
Normal file
2
Assets/Scripts/StockObject.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 059d0866a711d164b82c99461d440c06
|
||||
Reference in New Issue
Block a user