mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-30 12:57:08 +01:00
Merge branch 'main' into Tools
This commit is contained in:
11
Assets/Scripts/Player/Player.cs
Normal file
11
Assets/Scripts/Player/Player.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public void Die()
|
||||
{
|
||||
Debug.Log("----- PLAYER DIED!!! -----");
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/Player.cs.meta
Normal file
11
Assets/Scripts/Player/Player.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ee4c18f008d122488cbb74c779ef5c4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
Assets/Scripts/Player/PlayerController.cs
Normal file
18
Assets/Scripts/Player/PlayerController.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// Handles the player input, such as interactions
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
GameObject calendar;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetButtonDown("Calendar") && calendar != null)
|
||||
{
|
||||
calendar.SetActive(!calendar.activeSelf);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/PlayerController.cs.meta
Normal file
11
Assets/Scripts/Player/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 194170eed9e58d74584e7e11747aea0b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
105
Assets/Scripts/Player/PlayerInteraction.cs
Normal file
105
Assets/Scripts/Player/PlayerInteraction.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.UI;
|
||||
public class PlayerInteraction : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
TextMeshProUGUI interactionText;
|
||||
|
||||
[SerializeField]
|
||||
Image interactionProgressImg;
|
||||
|
||||
void Update()
|
||||
{
|
||||
bool successfulHit = false;
|
||||
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
|
||||
|
||||
if(hit.collider != null)
|
||||
{
|
||||
Interactable interactable = hit.collider.gameObject.GetComponent<Interactable>();
|
||||
|
||||
if(interactable != null)
|
||||
{
|
||||
// Debug.Log("Target Position: " + hit.collider.gameObject.transform.position);
|
||||
HandleInteraction(interactable);
|
||||
successfulHit = true;
|
||||
HandleInteractionText(interactable);
|
||||
HandleInteractionProgress(interactable);
|
||||
}
|
||||
}
|
||||
|
||||
if (!successfulHit)
|
||||
{
|
||||
interactionText.text = "";
|
||||
interactionProgressImg.fillAmount = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HandleInteractionProgress(Interactable interactable)
|
||||
{
|
||||
interactionProgressImg.fillAmount = interactable.GetHoldTime() / interactable.GetHoldDuration();
|
||||
}
|
||||
|
||||
void HandleInteractionText(Interactable interactable)
|
||||
{
|
||||
interactionText.text = interactable.GetDescription();
|
||||
interactionText.transform.position = new Vector3(Input.mousePosition.x + interactionText.rectTransform.sizeDelta.x / 2 + 20, Input.mousePosition.y - 5, Input.mousePosition.z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <c>HandleInteraction</c> <strong>handles the player interaction based on the Interactable.InteractionType.</strong>
|
||||
/// </summary>
|
||||
/// <param name="interactable"></param>
|
||||
void HandleInteraction(Interactable interactable)
|
||||
{
|
||||
switch (interactable.interactionType)
|
||||
{
|
||||
case Interactable.InteractionType.Click:
|
||||
if (Input.GetButtonDown("Interact") && interactable.isInRange())
|
||||
{
|
||||
interactable.Interact();
|
||||
}
|
||||
break;
|
||||
case Interactable.InteractionType.Hold:
|
||||
if (Input.GetButton("Interact") && interactable.isInRange())
|
||||
{
|
||||
interactable.IncreaseHoldTime();
|
||||
|
||||
if(interactable.GetHoldTime() > interactable.GetHoldDuration())
|
||||
{
|
||||
interactable.Interact();
|
||||
interactable.ResetHoldTime();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
interactable.ResetHoldTime();
|
||||
}
|
||||
break;
|
||||
case Interactable.InteractionType.Harvest:
|
||||
Harvestable harvestable = interactable.GetComponent<Harvestable>();
|
||||
ToolItem toolItem = (ToolItem)GetComponent<InventoryController>().selectedItem;
|
||||
|
||||
bool isCorrectTool = (toolItem != null && toolItem.toolType == harvestable.toolType);
|
||||
|
||||
|
||||
|
||||
if (Input.GetButton("Interact") && interactable.isInRange() && isCorrectTool)
|
||||
{
|
||||
harvestable.IncreaseHarvestTime();
|
||||
|
||||
if (harvestable.GetHarvestTime() >= harvestable.GetHarvestDuration())
|
||||
{
|
||||
harvestable.Interact();
|
||||
harvestable.ResetHarvestTime();
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new System.Exception("Unsupported type of interactable");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/PlayerInteraction.cs.meta
Normal file
11
Assets/Scripts/Player/PlayerInteraction.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c17c7c66da95ea747b93edf1da56ba16
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
48
Assets/Scripts/Player/PlayerMovement.cs
Normal file
48
Assets/Scripts/Player/PlayerMovement.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// Handles the player's movement, such as running, walking, sneaking
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
// Speed multiplier of the movement
|
||||
[SerializeField] float moveSpeed = 5f;
|
||||
|
||||
|
||||
Rigidbody2D rb;
|
||||
Animator animator;
|
||||
Vector2 movement;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
movement.x = Input.GetAxisRaw("Horizontal");
|
||||
movement.y = Input.GetAxisRaw("Vertical");
|
||||
|
||||
// Normalize, so the player is not faster by moving diagonally
|
||||
movement.Normalize();
|
||||
|
||||
animator.SetFloat("Horizontal", movement.x);
|
||||
animator.SetFloat("Vertical", movement.y);
|
||||
animator.SetFloat("Speed", movement.sqrMagnitude);
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (rb != null)
|
||||
{
|
||||
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("No Rigidbody2D found in PlayerMovement.cs");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Player/PlayerMovement.cs.meta
Normal file
11
Assets/Scripts/Player/PlayerMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5eabb8b018254b445a53ca18ab08171b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user