2024-04-04 12:16:48 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-04-07 00:09:53 +02:00
|
|
|
using Data;
|
|
|
|
using Interaction;
|
2024-04-07 12:39:05 +02:00
|
|
|
using TMPro.Examples;
|
2024-04-04 12:16:48 +02:00
|
|
|
using UnityEngine;
|
2024-04-07 00:09:53 +02:00
|
|
|
using UnityEngine.Events;
|
2024-04-04 12:16:48 +02:00
|
|
|
using UnityEngine.EventSystems;
|
2024-04-07 00:58:57 +02:00
|
|
|
using UnityEngine.InputSystem;
|
2024-04-07 00:09:53 +02:00
|
|
|
using UnityEngine.Serialization;
|
|
|
|
using Utility;
|
2024-04-04 12:16:48 +02:00
|
|
|
|
|
|
|
public class Character : MonoBehaviour
|
|
|
|
{
|
|
|
|
[Header("Movement")]
|
|
|
|
public float moveSpeed = 2f;
|
|
|
|
public float groundDrag = 1f;
|
|
|
|
|
|
|
|
private float horizontalInput, verticalInput;
|
|
|
|
private Vector3 moveDirection;
|
|
|
|
private Rigidbody rb;
|
|
|
|
|
|
|
|
public float speed = 2f;
|
|
|
|
|
2024-04-05 18:37:40 +02:00
|
|
|
[Header("Turning")]
|
|
|
|
public float turningRate = 30f;
|
|
|
|
public Quaternion _targetRotation = Quaternion.identity;
|
|
|
|
|
2024-04-04 12:16:48 +02:00
|
|
|
[Header("Keybindings")]
|
|
|
|
public KeyCode jumpKey = KeyCode.Space;
|
|
|
|
|
|
|
|
public float jumpForce;
|
|
|
|
public float jumpCooldown;
|
|
|
|
bool readyToJump = true;
|
|
|
|
|
|
|
|
[Header("Groud Check")]
|
|
|
|
public float playerHeight;
|
|
|
|
public LayerMask whatIsGround;
|
|
|
|
public bool grounded;
|
|
|
|
|
2024-04-06 18:08:54 +02:00
|
|
|
[Header("Hold item")]
|
|
|
|
[SerializeField] private Transform _hand;
|
2024-04-07 00:09:53 +02:00
|
|
|
[FormerlySerializedAs("_carriedItem")] [SerializeField] private GameObject _carriedItemModel;
|
|
|
|
[SerializeField] private PickupInteractible _carriedInteractible;
|
|
|
|
[FormerlySerializedAs("_throwAsideForce")] [SerializeField, Tooltip("Die Kraft mit der der Gegenstand fallen gelassen wird.")]
|
|
|
|
private float _dropItemForce = 3.0f;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private GottfriedData _data;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private AudioSource _audioSource;
|
|
|
|
|
|
|
|
public bool CarriesItem => _carriedInteractible;
|
|
|
|
|
|
|
|
public PickupInteractible CarriedItem => _carriedInteractible;
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-04 12:16:48 +02:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = gameObject.GetComponent<Rigidbody>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.2f, whatIsGround);
|
|
|
|
|
|
|
|
SpeedControl();
|
|
|
|
|
2024-04-05 18:37:40 +02:00
|
|
|
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
|
|
|
|
|
2024-04-04 12:16:48 +02:00
|
|
|
if (grounded)
|
|
|
|
{
|
|
|
|
rb.drag = groundDrag;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb.drag = 0.3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
public void PickupItem(PickupInteractible itemToPickup)
|
2024-04-06 18:08:54 +02:00
|
|
|
{
|
2024-04-07 00:14:00 +02:00
|
|
|
if (_carriedInteractible)
|
2024-04-06 18:08:54 +02:00
|
|
|
{
|
2024-04-07 00:09:53 +02:00
|
|
|
_carriedInteractible.gameObject.SetActive(true);
|
|
|
|
_carriedInteractible.transform.parent = null;
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
_carriedInteractible.transform.position = _carriedItemModel.transform.position;
|
|
|
|
_carriedInteractible.transform.rotation = _carriedItemModel.transform.rotation;
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
Rigidbody rigidbody = _carriedInteractible.GetComponent<Rigidbody>();
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
rigidbody.AddForce(Random.insideUnitSphere * _dropItemForce, ForceMode.Impulse);
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
Destroy(_carriedItemModel);
|
2024-04-06 18:08:54 +02:00
|
|
|
|
2024-04-07 00:09:53 +02:00
|
|
|
_carriedItemModel = null;
|
|
|
|
_carriedInteractible = null;
|
2024-04-06 18:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (itemToPickup)
|
|
|
|
{
|
|
|
|
GameObject model = Instantiate(itemToPickup.Model, _hand);
|
|
|
|
|
|
|
|
itemToPickup.transform.parent = model.transform;
|
|
|
|
itemToPickup.gameObject.SetActive(false);
|
|
|
|
|
2024-04-07 00:14:00 +02:00
|
|
|
_carriedItemModel = model;
|
2024-04-07 00:09:53 +02:00
|
|
|
_carriedInteractible = itemToPickup;
|
2024-04-06 18:08:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 12:16:48 +02:00
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
|
|
|
MovePlayer();
|
|
|
|
}
|
|
|
|
|
2024-04-07 00:58:57 +02:00
|
|
|
public void DoMove(InputAction.CallbackContext context)
|
2024-04-04 12:16:48 +02:00
|
|
|
{
|
2024-04-07 00:58:57 +02:00
|
|
|
Vector2 movement = context.ReadValue<Vector2>();
|
2024-04-04 12:16:48 +02:00
|
|
|
|
2024-04-07 00:58:57 +02:00
|
|
|
horizontalInput = movement.x;
|
|
|
|
verticalInput = movement.y;
|
|
|
|
|
2024-04-05 18:37:40 +02:00
|
|
|
if(horizontalInput != 0f || verticalInput != 0f)
|
|
|
|
{
|
|
|
|
_targetRotation = Quaternion.Euler(new Vector3(0f, Mathf.Atan2(horizontalInput, verticalInput) * Mathf.Rad2Deg, 0f));
|
|
|
|
}
|
2024-04-07 00:58:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DoJump(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (!context.performed || !readyToJump || !grounded)
|
|
|
|
return;
|
2024-04-05 18:37:40 +02:00
|
|
|
|
2024-04-07 00:58:57 +02:00
|
|
|
Jump();
|
2024-04-04 12:16:48 +02:00
|
|
|
|
2024-04-07 00:58:57 +02:00
|
|
|
Invoke(nameof(ResetJump), jumpCooldown);
|
|
|
|
}
|
2024-04-04 12:16:48 +02:00
|
|
|
|
2024-04-07 00:58:57 +02:00
|
|
|
public void DoDropItem(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (!context.performed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PickupItem(null);
|
2024-04-04 12:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void MovePlayer()
|
|
|
|
{
|
|
|
|
moveDirection = new Vector3(0, 0, verticalInput) + new Vector3(horizontalInput, 0, 0);
|
2024-04-07 00:58:57 +02:00
|
|
|
|
2024-04-04 12:16:48 +02:00
|
|
|
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SpeedControl()
|
|
|
|
{
|
|
|
|
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
|
|
|
|
|
|
|
|
if (flatVel.magnitude > moveSpeed)
|
|
|
|
{
|
|
|
|
Vector3 limitedVel = flatVel.normalized * moveSpeed;
|
|
|
|
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Jump()
|
|
|
|
{
|
|
|
|
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
|
|
|
|
|
|
|
|
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetJump()
|
|
|
|
{
|
|
|
|
readyToJump = true;
|
|
|
|
}
|
2024-04-07 00:09:53 +02:00
|
|
|
|
|
|
|
public void GiveItemTo(Developer developer)
|
|
|
|
{
|
|
|
|
Debug.Log($"Gebe {_carriedInteractible.Name} an {developer.Name}");
|
2024-04-07 00:58:57 +02:00
|
|
|
|
2024-04-07 12:39:05 +02:00
|
|
|
PickupInteractible item = _carriedInteractible;
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
return;
|
2024-04-07 00:09:53 +02:00
|
|
|
|
2024-04-07 12:39:05 +02:00
|
|
|
switch (item.ItemType)
|
|
|
|
{
|
|
|
|
case ItemType.Coffee:
|
|
|
|
developer.GiveDrink(0.4, WantedConsumable.Drink);
|
|
|
|
break;
|
|
|
|
case ItemType.Mate:
|
|
|
|
developer.GiveDrink(0.4, WantedConsumable.Mate);
|
|
|
|
break;
|
|
|
|
case ItemType.Pizza:
|
|
|
|
developer.GiveFood(0.25, WantedConsumable.Pizza);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item.UsesLeft--;
|
|
|
|
|
|
|
|
if (item.UsesLeft <= 0)
|
|
|
|
{
|
|
|
|
PickupItem(null);
|
2024-04-07 00:58:57 +02:00
|
|
|
|
2024-04-07 12:39:05 +02:00
|
|
|
Destroy(item.gameObject);
|
|
|
|
}
|
2024-04-07 00:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SayItsImpossible()
|
|
|
|
{
|
|
|
|
_audioSource.PlayOneShot(_data.VoiceSayItsImpossible.GetRandomElement());
|
|
|
|
}
|
2024-04-04 12:16:48 +02:00
|
|
|
}
|