mirror of
https://github.com/DerTyp7/example-top-down-unity.git
synced 2025-10-30 04:47:09 +01:00
Merge remote-tracking branch 'origin/base-world' into Inventory
This commit is contained in:
@@ -1,20 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Interactable : MonoBehaviour
|
||||
// Represents the base class for all interactable objects in the scene
|
||||
public abstract class Interactable : MonoBehaviour
|
||||
{
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
|
||||
public enum InteractionType {
|
||||
Click,
|
||||
Hold
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
public void interact()
|
||||
{
|
||||
|
||||
float holdTime;
|
||||
Transform playerTransform;
|
||||
Transform interactableTransform;
|
||||
|
||||
public InteractionType interactionType;
|
||||
public float radius = 3f;
|
||||
|
||||
public abstract string GetDescription();
|
||||
public abstract void Interact();
|
||||
|
||||
public void IncreaseHoldTime() => holdTime += Time.deltaTime;
|
||||
public void ResetHoldTime() => holdTime = 0f;
|
||||
|
||||
public float GetHoldTime() => holdTime;
|
||||
|
||||
public bool isInRange()
|
||||
{
|
||||
playerTransform = GameObject.FindGameObjectWithTag("Player").gameObject.transform; // Maybe singleton later?
|
||||
interactableTransform = gameObject.transform;
|
||||
float distance = Vector2.Distance(interactableTransform.position, playerTransform.position);
|
||||
|
||||
if(distance <= radius)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
// Gizmos are only visible in the scene view -> NOT visible IN-GAME (DEBUG Reasons)
|
||||
Gizmos.color = Color.magenta;
|
||||
Gizmos.DrawWireSphere(transform.position, radius);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59a114e9b82af2e49a1e22aab16fd0d5
|
||||
guid: 185b418881f2566459c0cffeb5dfc0bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
|
||||
11
Assets/Scripts/PlayerController.cs
Normal file
11
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
// Handles the player input, such as interactions
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
void Update()
|
||||
{
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/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:
|
||||
69
Assets/Scripts/PlayerInteraction.cs
Normal file
69
Assets/Scripts/PlayerInteraction.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerInteraction : MonoBehaviour
|
||||
{
|
||||
public TMPro.TextMeshProUGUI interactionText;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (!successfulHit)
|
||||
{
|
||||
interactionText.text = "";
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
|
||||
}
|
||||
void HandleInteraction(Interactable interactable)
|
||||
{
|
||||
switch (interactable.interactionType)
|
||||
{
|
||||
case Interactable.InteractionType.Click:
|
||||
if (Input.GetButtonDown("Interact"))
|
||||
{
|
||||
interactable.Interact();
|
||||
}
|
||||
break;
|
||||
case Interactable.InteractionType.Hold:
|
||||
if (Input.GetButton("Interact"))
|
||||
{
|
||||
interactable.IncreaseHoldTime();
|
||||
|
||||
if(interactable.GetHoldTime() > 1f)
|
||||
{
|
||||
interactable.Interact();
|
||||
interactable.ResetHoldTime();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
interactable.ResetHoldTime();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new System.Exception("Unsupported type of interactable");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerInteraction.cs.meta
Normal file
11
Assets/Scripts/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/PlayerMovement.cs
Normal file
48
Assets/Scripts/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/PlayerMovement.cs.meta
Normal file
11
Assets/Scripts/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:
|
||||
21
Assets/Scripts/TreeInteraction.cs
Normal file
21
Assets/Scripts/TreeInteraction.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TreeInteraction : Interactable
|
||||
{
|
||||
public override string GetDescription()
|
||||
{
|
||||
if (isInRange())
|
||||
return "Baum muss schreien";
|
||||
else
|
||||
return "Tree is not in range";
|
||||
}
|
||||
public override void Interact()
|
||||
{
|
||||
if (isInRange())
|
||||
Debug.Log("AaaaaaaaaaAaAAaAaAAaAaAaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
|
||||
else
|
||||
Debug.Log("Tree is not in range");
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/TreeInteraction.cs.meta
Normal file
11
Assets/Scripts/TreeInteraction.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c68a9987090906346b5bc7622cb96556
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user