Pickup Pizza und so
This commit is contained in:
140
3d Prototyp/Assets/Pickupper.cs
Normal file
140
3d Prototyp/Assets/Pickupper.cs
Normal file
@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using Utility;
|
||||
|
||||
public class Pickupper : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Character _character;
|
||||
|
||||
[SerializeField, Tooltip("Der Radius in dem Gottfried Gegenstände aufheben kann."), Min(0.0f)]
|
||||
private float _pickupRadius = 1.5f;
|
||||
|
||||
/// <summary>
|
||||
/// Der Radius in dem Gottfried Gegenstände aufheben kann.
|
||||
/// </summary>
|
||||
public float PickupRadius => _pickupRadius;
|
||||
|
||||
[SerializeField]
|
||||
private CapsuleCollider _pickupCollider;
|
||||
|
||||
[SerializeField]
|
||||
private List<Pickupable> _pickupablesInRange = new();
|
||||
|
||||
public IReadOnlyList<Pickupable> PickupablesInRange => _pickupablesInRange;
|
||||
|
||||
private int _selectedActionIndex = 0;
|
||||
|
||||
public bool CanSelectPrevious => _selectedActionIndex > 0;
|
||||
public bool CanSelectNext => _selectedActionIndex < PickupablesInRange.Count - 1;
|
||||
|
||||
public Pickupable Selected => PickupablesInRange[_selectedActionIndex];
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.LeftArrow))
|
||||
{
|
||||
SelectPreviousAction();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.RightArrow))
|
||||
{
|
||||
SelectNextAction();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
PickUp();
|
||||
}
|
||||
}
|
||||
|
||||
private void PickUp()
|
||||
{
|
||||
if (PickupablesInRange.Count == 0)
|
||||
return;
|
||||
|
||||
Pickupable pickupable = Selected;
|
||||
|
||||
UnhighlightPickupable(pickupable);
|
||||
_character.PickupItem(pickupable);
|
||||
}
|
||||
|
||||
public void SelectPreviousAction()
|
||||
{
|
||||
_selectedActionIndex--;
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
public void SelectNextAction()
|
||||
{
|
||||
_selectedActionIndex++;
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
private void UpdateSelection()
|
||||
{
|
||||
if (_pickupablesInRange.Count > 0)
|
||||
{
|
||||
_selectedActionIndex = Math.Clamp(_selectedActionIndex, 0, PickupablesInRange.Count - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
_selectedActionIndex = 0;
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
foreach (Pickupable pickupable in _pickupablesInRange)
|
||||
{
|
||||
pickupable.SetSelected(index == _selectedActionIndex);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Pickupable pickupable = other.gameObject.GetComponent<Pickupable>();
|
||||
|
||||
if (pickupable)
|
||||
{
|
||||
HighlightPickupable(pickupable);
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerExit(Collider other)
|
||||
{
|
||||
Pickupable pickupable = other.gameObject.GetComponent<Pickupable>();
|
||||
|
||||
if (pickupable)
|
||||
{
|
||||
UnhighlightPickupable(pickupable);
|
||||
}
|
||||
}
|
||||
|
||||
private void HighlightPickupable(Pickupable pickupable)
|
||||
{
|
||||
if (!_pickupablesInRange.Contains(pickupable))
|
||||
_pickupablesInRange.Add(pickupable);
|
||||
|
||||
pickupable.Highlight(true);
|
||||
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
private void UnhighlightPickupable(Pickupable pickupable)
|
||||
{
|
||||
pickupable.Highlight(false);
|
||||
_pickupablesInRange.Remove(pickupable);
|
||||
UpdateSelection();
|
||||
}
|
||||
|
||||
// Wird aufgerufen, wenn ein Wert im Editor geändert wird
|
||||
void OnValidate()
|
||||
{
|
||||
if (_pickupCollider)
|
||||
{
|
||||
_pickupCollider.radius = _pickupRadius;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user