interactable

This commit is contained in:
Janis
2023-03-04 15:53:47 +01:00
parent ae0b21f9e9
commit 5f08865a2b
17 changed files with 506 additions and 357 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a9f7ee3f33be6934bb41385f9e552691
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -10,10 +10,9 @@ public enum FieldState
public class Field : Building
{
public Crop TESTCROP;
public Crop crop;
public SpriteRenderer currentCropSprite;
public SpriteRenderer currentCropSpriteRenderer;
public int daysSincePlanted;
public bool isWatered = false;
public FieldState state = FieldState.EMPTY;
@@ -22,9 +21,20 @@ public class Field : Building
private void Start()
{
TimeManager.OnDayChanged += AddDay;
//! DEBUG
OnPlace();
}
private void SetSprite(Sprite sprite)
{
if (currentCropSpriteRenderer)
{
currentCropSpriteRenderer.sprite = sprite;
currentCropSpriteRenderer.drawMode = SpriteDrawMode.Tiled;
currentCropSpriteRenderer.size = new Vector2(10, 10); // TODO: Make this dynamic
}
}
private void AddDay()
{
if (crop && isPlaced && state != FieldState.DEAD)
@@ -34,7 +44,7 @@ public class Field : Building
else
{
daysSincePlanted++;
currentCropSprite.sprite = crop.sprites[daysSincePlanted];
SetSprite(crop.sprites[daysSincePlanted]);
}
}
@@ -45,12 +55,12 @@ public class Field : Building
daysSincePlanted = 0;
state = FieldState.GROWING;
crop = newCrop;
SetSprite(crop.sprites[0]);
}
public override void OnPlace()
{
daysSincePlanted = 0;
state = FieldState.GROWING;
Plant(TESTCROP);
Plant(null);
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d700e4905ee3fd2408f52a4e3ecb48e8
guid: 53c8af14dae8dbe4e95cda372289f89e
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,14 @@
// Interactable object which lets the player plant & harvest a seed
using UnityEngine;
public class FieldController : Interactable
{
public Field field;
public Crop testCrop;
public override void OnInteract()
{
Debug.Log("Interacting with field");
field.Plant(testCrop);
}
}

View File

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

View File

@@ -0,0 +1,53 @@
// Base class for interactable object in the top-down game
using UnityEngine;
public abstract class Interactable : MonoBehaviour
{
[SerializeField]
[Range(0f, 10f)]
private float radius = 2f; // radius of interaction
[SerializeField]
private string interactText = "Interact"; // text to display when player is in range
[SerializeField]
private string interactTextOutOfRange = "Out of range"; // text to display when player is out of range
public string GetInteractText(GameObject interactor = null)
{
if (interactor != null && IsInRange(interactor))
{
return interactText;
}
else { return interactTextOutOfRange; }
}
public abstract void OnInteract();
public float GetRadius() { return radius; }
public void Interact(GameObject interactor)
{
// check if in range
if (!IsInRange(interactor))
{
Debug.Log("Out of range");
return;
}
Debug.Log("Interacting with " + transform.name);
OnInteract();
}
public bool IsInRange(GameObject interactor)
{
return Vector2.Distance(transform.position, interactor.transform.position) <= radius;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, radius);
}
}

View File

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

View File

@@ -6,15 +6,30 @@ using System;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
[SerializeField]
LayerMask interactableLayer; // Layermask for interactable objects
// Update is called once per frame
void Update()
{
// Interactable
if (Input.GetKeyDown(KeyCode.E))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit;
hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, interactableLayer);
if (hit.collider != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
interactable.Interact(gameObject);
}
}
}
}
}

View File

@@ -40,7 +40,7 @@ public class TimeManager : MonoBehaviour
// Increase minute
Minute++;
OnMinuteChanged?.Invoke();
if (Minute >= 60)
if (Minute >= 59)
{
// Increase hour
Hour++;

View File

@@ -0,0 +1,53 @@
// Text which is left from the cursor
using UnityEngine;
using TMPro;
public class CursorText : MonoBehaviour
{
private TextMeshProUGUI textComponent;
private string text = "";
private GameObject player;
private void Start()
{
textComponent = GetComponent<TextMeshProUGUI>();
player = GameObject.FindGameObjectWithTag("Player");
}
public void SetText(string newText)
{
text = newText;
this.textComponent.SetText(text);
}
private void Update()
{
if (text.Length > 0)
{
// Set gameobject left from cursor
Vector3 mousePos = Input.mousePosition;
mousePos.x += 125;
transform.position = mousePos;
}
// if hovering over interactable object
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit;
hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, LayerMask.GetMask("Interactable"));
if (hit.collider != null)
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
SetText(interactable.GetInteractText(player));
}
}
else
{
SetText("");
}
}
}

View File

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