Interaction

This commit is contained in:
janis
2022-02-14 17:12:18 +01:00
parent 352581713f
commit cd3d015226
336 changed files with 59574 additions and 332 deletions

View File

@@ -0,0 +1,50 @@
using UnityEngine;
// Represents the base class for all interactable objects in the scene
public abstract class Interactable : MonoBehaviour
{
public enum InteractionType {
Click,
Hold
}
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);
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3367b6118f93acb46b35bdeef787e45d
guid: 185b418881f2566459c0cffeb5dfc0bd
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,12 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
Base Class for all objects with interaction
*/
public class InteractableObject : MonoBehaviour
{
}

View 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()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 194170eed9e58d74584e7e11747aea0b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
interactionText.text = interactable.GetDescription();
successfulHit = true;
}
}
if (!successfulHit)
{
interactionText.text = "";
}
}
void HandleInteractionText() // interaction text has to follow mouse cursor
{
}
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");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c17c7c66da95ea747b93edf1da56ba16
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -2,25 +2,37 @@ 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;
[SerializeField] Rigidbody2D rb;
[SerializeField] Animator animator;
Rigidbody2D rb;
Animator animator;
Vector2 movement;
private void Update()
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);
}
private void FixedUpdate()
void FixedUpdate()
{
if (rb != null)
{

View File

@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TreeInteraction : Interactable
{
public override string GetDescription() => "Baum muss schreie";
public override void Interact()
{
if(isInRange())
Debug.Log("AaaaaaaaaaAaAAaAaAAaAaAaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c68a9987090906346b5bc7622cb96556
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: