Repaired hole minigame
This commit is contained in:
@@ -1,46 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class DraggableObject : MonoBehaviour
|
||||
{
|
||||
[Header("Přetahování")]
|
||||
public float dragSmoothness = 15f;
|
||||
|
||||
[Header("Vizuální zpětná vazba")]
|
||||
public SpriteRenderer spriteRenderer;
|
||||
public Renderer Renderer;
|
||||
public Color normalColor = Color.white;
|
||||
public Color dragColor = new Color(1f, 1f, 0.5f);
|
||||
public float scaleOnDrag = 1.15f;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
private Rigidbody rb;
|
||||
private Camera mainCamera;
|
||||
private bool isDragging = false;
|
||||
private Vector3 targetPosition;
|
||||
private Vector3 originalScale;
|
||||
private bool hasBeenScored = false;
|
||||
private Plane _dragPlane;
|
||||
private Collider col;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
col = GetComponent<Collider>();
|
||||
mainCamera = Camera.main;
|
||||
originalScale = transform.localScale;
|
||||
if (spriteRenderer == null)
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
if (Renderer == null)
|
||||
Renderer = GetComponent<Renderer>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb.gravityScale = 0f;
|
||||
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
|
||||
rb.useGravity = false;
|
||||
rb.constraints = RigidbodyConstraints.FreezeRotation;
|
||||
targetPosition = transform.position;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
HandleInput();
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (isDragging)
|
||||
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * dragSmoothness);
|
||||
{
|
||||
Vector3 newPos = Vector3.Lerp(rb.position, targetPosition, Time.fixedDeltaTime * dragSmoothness);
|
||||
rb.MovePosition(newPos);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleInput()
|
||||
@@ -48,13 +57,13 @@ public class DraggableObject : MonoBehaviour
|
||||
if (Input.touchCount > 0)
|
||||
{
|
||||
Touch touch = Input.GetTouch(0);
|
||||
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
|
||||
|
||||
if (touch.phase == TouchPhase.Began)
|
||||
TryStartDrag(worldPos);
|
||||
TryStartDrag(touch.position);
|
||||
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
|
||||
{
|
||||
if (isDragging) targetPosition = worldPos;
|
||||
if (isDragging) UpdateDrag(touch.position);
|
||||
}
|
||||
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
||||
{
|
||||
@@ -63,49 +72,70 @@ public class DraggableObject : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
TryStartDrag(worldPos);
|
||||
TryStartDrag(Input.mousePosition);
|
||||
else if (Input.GetMouseButton(0) && isDragging)
|
||||
targetPosition = worldPos;
|
||||
UpdateDrag(Input.mousePosition);
|
||||
else if (Input.GetMouseButtonUp(0) && isDragging)
|
||||
EndDrag();
|
||||
}
|
||||
}
|
||||
|
||||
void TryStartDrag(Vector3 worldPos)
|
||||
void TryStartDrag(Vector3 screenPos)
|
||||
{
|
||||
if (GetComponent<Collider2D>().OverlapPoint(worldPos))
|
||||
StartDrag(worldPos);
|
||||
}
|
||||
Debug.Log("Trying to start drag at: " + screenPos);
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPos);
|
||||
RaycastHit hit;
|
||||
|
||||
void StartDrag(Vector3 worldPos)
|
||||
{
|
||||
isDragging = true;
|
||||
rb.linearVelocity = Vector2.zero;
|
||||
targetPosition = worldPos;
|
||||
transform.localScale = originalScale * scaleOnDrag;
|
||||
if (spriteRenderer != null)
|
||||
if (Physics.Raycast(ray, out hit))
|
||||
{
|
||||
spriteRenderer.color = dragColor;
|
||||
spriteRenderer.sortingOrder = 10;
|
||||
if (hit.collider == col)
|
||||
{
|
||||
StartDrag(hit.point);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StartDrag(Vector3 hitPoint)
|
||||
{
|
||||
Debug.Log("Started dragging at: " + hitPoint);
|
||||
isDragging = true;
|
||||
rb.isKinematic = false;
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
_dragPlane = new Plane(-mainCamera.transform.forward, hitPoint);
|
||||
targetPosition = hitPoint;
|
||||
transform.localScale = originalScale * scaleOnDrag;
|
||||
if (Renderer != null)
|
||||
{
|
||||
Renderer.material.color = dragColor;
|
||||
}
|
||||
}
|
||||
void UpdateDrag(Vector3 screenPos)
|
||||
{
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPos);
|
||||
float distance;
|
||||
if (_dragPlane.Raycast(ray, out distance))
|
||||
{
|
||||
targetPosition = ray.GetPoint(distance);
|
||||
}
|
||||
}
|
||||
void EndDrag()
|
||||
{
|
||||
Debug.Log("Ended dragging");
|
||||
isDragging = false;
|
||||
rb.isKinematic = true;
|
||||
transform.localScale = originalScale;
|
||||
if (spriteRenderer != null)
|
||||
if (Renderer != null)
|
||||
{
|
||||
spriteRenderer.color = normalColor;
|
||||
spriteRenderer.sortingOrder = 0;
|
||||
Renderer.material.color = normalColor;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnScored()
|
||||
{
|
||||
Debug.Log("Object scored");
|
||||
if (hasBeenScored) return;
|
||||
hasBeenScored = true;
|
||||
isDragging = false;
|
||||
@@ -123,8 +153,8 @@ public class DraggableObject : MonoBehaviour
|
||||
elapsed += Time.deltaTime;
|
||||
float t = elapsed / duration;
|
||||
transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
|
||||
if (spriteRenderer != null)
|
||||
spriteRenderer.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
|
||||
if (Renderer != null)
|
||||
Renderer.material.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CapsuleCollider))]
|
||||
public class Hole : MonoBehaviour
|
||||
{
|
||||
[Header("Nastavení")]
|
||||
@@ -10,13 +11,9 @@ public class Hole : MonoBehaviour
|
||||
[Tooltip("Síla vtahování itemu k díře")]
|
||||
public float pullForce = 4f;
|
||||
|
||||
[Header("Pohyb díry (volitelné)")]
|
||||
public bool hasMovement = false;
|
||||
public float moveSpeed = 2f;
|
||||
public Vector2 moveRange = new Vector2(1.5f, 0f);
|
||||
|
||||
[Header("Vizuál")]
|
||||
public SpriteRenderer glowRenderer;
|
||||
public Renderer glowRenderer;
|
||||
|
||||
private Vector3 startPosition;
|
||||
private bool isGlowing = false;
|
||||
@@ -35,27 +32,24 @@ public class Hole : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (hasMovement)
|
||||
{
|
||||
float x = startPosition.x + Mathf.Sin(Time.time * moveSpeed) * moveRange.x;
|
||||
float y = startPosition.y + Mathf.Cos(Time.time * moveSpeed * 0.7f) * moveRange.y;
|
||||
transform.position = new Vector3(x, y, transform.position.z);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void OnTriggerStay2D(Collider2D other)
|
||||
void OnTriggerStay(Collider other)
|
||||
{
|
||||
Debug.Log($"Trigger stay with: {other.gameObject.name}");
|
||||
DraggableObject draggable = other.GetComponent<DraggableObject>();
|
||||
if (draggable == null) return;
|
||||
|
||||
float dist = Vector2.Distance(transform.position, other.transform.position);
|
||||
Debug.Log($"Draggable object detected: {other.gameObject.name}");
|
||||
float dist = Vector3.Distance(transform.position, other.transform.position);
|
||||
|
||||
|
||||
Rigidbody2D rb = other.GetComponent<Rigidbody2D>();
|
||||
Rigidbody rb = other.GetComponent<Rigidbody>();
|
||||
if (rb != null)
|
||||
{
|
||||
Vector2 dir = ((Vector2)transform.position - rb.position).normalized;
|
||||
rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode2D.Impulse);
|
||||
Debug.Log($"Rigidbody detected: {other.gameObject.name}, distance: {dist}");
|
||||
Vector3 dir = (transform.position - rb.position).normalized;
|
||||
rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
|
||||
@@ -66,18 +60,21 @@ public class Hole : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerEnter2D(Collider2D other)
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Debug.Log($"Collider entered: {other.gameObject.name}");
|
||||
if (other.GetComponent<DraggableObject>() != null) SetGlow(true);
|
||||
}
|
||||
|
||||
void OnTriggerExit2D(Collider2D other)
|
||||
void OnTriggerExit(Collider other)
|
||||
{
|
||||
Debug.Log($"Collider exited: {other.gameObject.name}");
|
||||
if (other.GetComponent<DraggableObject>() != null) SetGlow(false);
|
||||
}
|
||||
|
||||
void SetGlow(bool active)
|
||||
{
|
||||
Debug.Log($"SetGlow: {active}");
|
||||
isGlowing = active;
|
||||
if (glowRenderer == null) return;
|
||||
glowRenderer.enabled = active;
|
||||
@@ -89,8 +86,8 @@ public class Hole : MonoBehaviour
|
||||
while (isGlowing && glowRenderer != null)
|
||||
{
|
||||
float t = Mathf.PingPong(Time.time * 3f, 1f);
|
||||
Color c = glowRenderer.color;
|
||||
glowRenderer.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t));
|
||||
Color c = glowRenderer.sharedMaterial.color;
|
||||
glowRenderer.sharedMaterial.color = new Color(c.r, c.g, c.b, Mathf.Lerp(0.3f, 0.9f, t));
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using GeoSus.Client;
|
||||
|
||||
public class LevelManager : MonoBehaviour
|
||||
public class LevelManager : MonoBehaviour, ITask
|
||||
{
|
||||
public static LevelManager Instance;
|
||||
|
||||
@@ -14,11 +16,15 @@ public class LevelManager : MonoBehaviour
|
||||
|
||||
private int scoredCount = 0;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else Destroy(gameObject);
|
||||
}
|
||||
public string TaskID { get; set; }
|
||||
|
||||
public TaskType TaskType { get; set; }
|
||||
public string TaskName { get; set; }
|
||||
|
||||
public Position TaskLocation { get; set; }
|
||||
|
||||
public bool IsCompleted { get; private set; } = false;
|
||||
protected Action<ITask> OnCompleted;
|
||||
|
||||
public void RegisterItem()
|
||||
{
|
||||
@@ -38,4 +44,25 @@ public class LevelManager : MonoBehaviour
|
||||
|
||||
public int GetScoredCount() => scoredCount;
|
||||
public int GetTotalCount() => itemsToScore;
|
||||
|
||||
public void Initialize(Action<ITask> onCompleted)
|
||||
{
|
||||
OnCompleted = onCompleted;
|
||||
|
||||
IsCompleted = false;
|
||||
ResetCounter();
|
||||
}
|
||||
|
||||
public void ExitTask(Action<ITask> onExit)
|
||||
{
|
||||
onExit?.Invoke(this);
|
||||
}
|
||||
|
||||
public void Complete()
|
||||
{
|
||||
if (IsCompleted) return;
|
||||
|
||||
IsCompleted = true;
|
||||
OnCompleted?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,11 @@ using System.Collections.Generic;
|
||||
public class ObjectSpawner : MonoBehaviour
|
||||
{
|
||||
public static ObjectSpawner Instance;
|
||||
[SerializeField]
|
||||
private List<GameObject> spawnedHoles = new List<GameObject>();
|
||||
|
||||
[Header("Prefaby")]
|
||||
public GameObject[] objectPrefabs;
|
||||
public GameObject holePrefab;
|
||||
|
||||
[Header("Počty")]
|
||||
[Tooltip("Kolik předmětů spawnovat")]
|
||||
@@ -28,12 +29,9 @@ public class ObjectSpawner : MonoBehaviour
|
||||
public float minY = -5f;
|
||||
public float maxY = 4f;
|
||||
|
||||
[Header("Rodiče pro přehlednost (volitelné)")]
|
||||
public Transform objectParent;
|
||||
public Transform holeParent;
|
||||
|
||||
private List<GameObject> spawnedObjects = new List<GameObject>();
|
||||
private List<GameObject> spawnedHoles = new List<GameObject>();
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@@ -57,41 +55,42 @@ public class ObjectSpawner : MonoBehaviour
|
||||
LevelManager.Instance.ResetCounter();
|
||||
}
|
||||
|
||||
SpawnHoles();
|
||||
//SpawnHoles();
|
||||
SpawnObjects();
|
||||
}
|
||||
|
||||
void SpawnHoles()
|
||||
{
|
||||
for (int i = 0; i < holeCount; i++)
|
||||
{
|
||||
Vector2 pos = RandomPos(1f);
|
||||
GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent);
|
||||
|
||||
Hole h = hole.GetComponent<Hole>();
|
||||
if (h != null && holesMove)
|
||||
{
|
||||
h.hasMovement = true;
|
||||
h.moveSpeed = holeMoveSpeed;
|
||||
h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f);
|
||||
}
|
||||
|
||||
spawnedHoles.Add(hole);
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnObjects()
|
||||
//void SpawnHoles()
|
||||
//{
|
||||
// for (int i = 0; i < holeCount; i++)
|
||||
// {
|
||||
// Vector2 pos = RandomPos(1f);
|
||||
// GameObject hole = Instantiate(holePrefab, pos, Quaternion.identity, holeParent);
|
||||
|
||||
// Hole h = hole.GetComponent<Hole>();
|
||||
// if (h != null && holesMove)
|
||||
// {
|
||||
// h.hasMovement = true;
|
||||
// h.moveSpeed = holeMoveSpeed;
|
||||
// h.moveRange = new Vector2(Random.Range(0.8f, 1.8f), 0f);
|
||||
// }
|
||||
|
||||
// spawnedHoles.Add(hole);
|
||||
// }
|
||||
//}
|
||||
|
||||
public void SpawnObjects()
|
||||
{
|
||||
Debug.Log($"Spawning {objectCount} objects...");
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
GameObject prefab = objectPrefabs[Random.Range(0, objectPrefabs.Length)];
|
||||
Vector2 pos = RandomPos(0.5f);
|
||||
GameObject obj = Instantiate(prefab, pos, Quaternion.identity, objectParent);
|
||||
Vector3 pos = RandomPos(0.5f);
|
||||
GameObject obj = Instantiate(prefab, pos, Quaternion.identity);
|
||||
|
||||
// Náhodná barva
|
||||
SpriteRenderer sr = obj.GetComponent<SpriteRenderer>();
|
||||
Renderer sr = obj.GetComponent<Renderer>();
|
||||
if (sr != null)
|
||||
sr.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f);
|
||||
sr.sharedMaterial.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f);
|
||||
|
||||
spawnedObjects.Add(obj);
|
||||
}
|
||||
@@ -100,14 +99,17 @@ public class ObjectSpawner : MonoBehaviour
|
||||
public void Clear()
|
||||
{
|
||||
foreach (var o in spawnedObjects) if (o != null) Destroy(o);
|
||||
foreach (var h in spawnedHoles) if (h != null) Destroy(h);
|
||||
spawnedObjects.Clear();
|
||||
//foreach (var h in spawnedHoles) if (h != null) Destroy(h);
|
||||
//spawnedObjects.Clear();
|
||||
spawnedHoles.Clear();
|
||||
}
|
||||
|
||||
Vector2 RandomPos(float margin) =>
|
||||
new Vector2(
|
||||
Vector3 RandomPos(float margin)
|
||||
{
|
||||
return new Vector3(
|
||||
Random.Range(minX + margin, maxX - margin),
|
||||
Random.Range(minY + margin, maxY - margin)
|
||||
Random.Range(minY + margin, maxY - margin),
|
||||
0.5f
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user