conflict resolved
This commit is contained in:
170
Assets/Scripts/hod_veci_do_diry/DraggableObject.cs
Normal file
170
Assets/Scripts/hod_veci_do_diry/DraggableObject.cs
Normal file
@@ -0,0 +1,170 @@
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
[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 Renderer Renderer;
|
||||
public Color normalColor = Color.white;
|
||||
public Color dragColor = new Color(1f, 1f, 0.5f);
|
||||
public float scaleOnDrag = 1.15f;
|
||||
|
||||
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<Rigidbody>();
|
||||
col = GetComponent<Collider>();
|
||||
mainCamera = Camera.main;
|
||||
originalScale = transform.localScale;
|
||||
if (Renderer == null)
|
||||
Renderer = GetComponent<Renderer>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
rb.useGravity = true;
|
||||
rb.constraints = RigidbodyConstraints.FreezeRotation;
|
||||
targetPosition = transform.position;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
HandleInput();
|
||||
}
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (isDragging)
|
||||
{
|
||||
Vector3 newPos = Vector3.Lerp(rb.position, targetPosition, Time.fixedDeltaTime * dragSmoothness);
|
||||
rb.MovePosition(newPos);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HandleInput()
|
||||
{
|
||||
if (Input.touchCount > 0)
|
||||
{
|
||||
Touch touch = Input.GetTouch(0);
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10f));
|
||||
|
||||
if (touch.phase == TouchPhase.Began)
|
||||
TryStartDrag(touch.position);
|
||||
else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
|
||||
{
|
||||
if (isDragging) UpdateDrag(touch.position);
|
||||
}
|
||||
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
||||
{
|
||||
if (isDragging) EndDrag();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Vector3 worldPos = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10f));
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
TryStartDrag(Input.mousePosition);
|
||||
}
|
||||
else if (Input.GetMouseButton(0) && isDragging)
|
||||
UpdateDrag(Input.mousePosition);
|
||||
else if (Input.GetMouseButtonUp(0) && isDragging)
|
||||
EndDrag();
|
||||
}
|
||||
}
|
||||
|
||||
void TryStartDrag(Vector3 screenPos)
|
||||
{
|
||||
Debug.Log("Trying to start drag at: " + screenPos);
|
||||
Ray ray = mainCamera.ScreenPointToRay(screenPos);
|
||||
RaycastHit hit;
|
||||
|
||||
if (Physics.Raycast(ray, out hit))
|
||||
{
|
||||
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 = new Vector3(this.transform.position.x, hitPoint.y, hitPoint.z);
|
||||
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))
|
||||
{
|
||||
Vector3 hitPoint = ray.GetPoint(distance);
|
||||
targetPosition = new Vector3(this.transform.position.x, hitPoint.y, hitPoint.z);
|
||||
}
|
||||
}
|
||||
void EndDrag()
|
||||
{
|
||||
Debug.Log("Ended dragging");
|
||||
isDragging = false;
|
||||
rb.isKinematic = true;
|
||||
transform.localScale = originalScale;
|
||||
if (Renderer != null)
|
||||
{
|
||||
Renderer.material.color = normalColor;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnScored()
|
||||
{
|
||||
Debug.Log("Object scored");
|
||||
if (hasBeenScored) return;
|
||||
hasBeenScored = true;
|
||||
isDragging = false;
|
||||
StartCoroutine(SinkIntoHole());
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator SinkIntoHole()
|
||||
{
|
||||
float duration = 0.35f;
|
||||
float elapsed = 0f;
|
||||
Vector3 startScale = transform.localScale;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = elapsed / duration;
|
||||
transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
|
||||
if (Renderer != null)
|
||||
Renderer.material.color = new Color(normalColor.r, normalColor.g, normalColor.b, 1f - t);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
gameObject.SetActive(false);
|
||||
LevelManager.Instance?.RegisterItem();
|
||||
}
|
||||
}
|
||||
108
Assets/Scripts/hod_veci_do_diry/Hole.cs
Normal file
108
Assets/Scripts/hod_veci_do_diry/Hole.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CapsuleCollider))]
|
||||
|
||||
public class Hole : MonoBehaviour
|
||||
{
|
||||
[Header("Nastavení")]
|
||||
[Tooltip("Poloměr zachycení – do kolika jednotek od středu se item 'vtáhne'")]
|
||||
public float catchRadius = 0.6f;
|
||||
|
||||
[Tooltip("Síla vtahování itemu k díře")]
|
||||
public float pullForce = 4f;
|
||||
|
||||
[Header("Vizuál")]
|
||||
public Renderer glowRenderer;
|
||||
|
||||
|
||||
private Vector3 startPosition;
|
||||
private bool isGlowing = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
startPosition = transform.position;
|
||||
|
||||
CircleCollider2D col = GetComponent<CircleCollider2D>();
|
||||
if (col != null)
|
||||
{
|
||||
col.isTrigger = true;
|
||||
col.radius = catchRadius;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnTriggerStay(Collider other)
|
||||
{
|
||||
Debug.Log($"Trigger stay with: {other.gameObject.name}");
|
||||
DraggableObject draggable = other.GetComponent<DraggableObject>();
|
||||
if (draggable == null) return;
|
||||
Debug.Log($"Draggable object detected: {other.gameObject.name}");
|
||||
float dist = Vector3.Distance(transform.position, other.transform.position);
|
||||
|
||||
|
||||
Rigidbody rb = other.GetComponent<Rigidbody>();
|
||||
if (rb != null)
|
||||
{
|
||||
Debug.Log($"Rigidbody detected: {other.gameObject.name}, distance: {dist}");
|
||||
Vector3 dir = (transform.position - rb.position).normalized;
|
||||
rb.AddForce(dir * pullForce * Time.fixedDeltaTime, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
|
||||
if (dist < catchRadius * 0.4f)
|
||||
{
|
||||
draggable.OnScored();
|
||||
SetGlow(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Debug.Log($"Collider entered: {other.gameObject.name}");
|
||||
if (other.GetComponent<DraggableObject>() != null) SetGlow(true);
|
||||
}
|
||||
|
||||
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;
|
||||
if (active) StartCoroutine(PulseGlow());
|
||||
}
|
||||
|
||||
IEnumerator PulseGlow()
|
||||
{
|
||||
while (isGlowing && glowRenderer != null)
|
||||
{
|
||||
float t = Mathf.PingPong(Time.time * 3f, 1f);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawWireSphere(transform.position, catchRadius);
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(transform.position, catchRadius * 0.4f);
|
||||
}
|
||||
}
|
||||
70
Assets/Scripts/hod_veci_do_diry/LevelManager.cs
Normal file
70
Assets/Scripts/hod_veci_do_diry/LevelManager.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using System;
|
||||
|
||||
public class LevelManager : MonoBehaviour, ITask
|
||||
{
|
||||
public static LevelManager Instance;
|
||||
|
||||
[Header("Nastavení levelu")]
|
||||
[Tooltip("Kolik itemů musí hráč trefit pro splnění levelu")]
|
||||
|
||||
public int itemsToScore = 3;
|
||||
|
||||
[Header("Event – vyvolá se po trefení všech itemů")]
|
||||
public UnityEvent OnAllItemsScored;
|
||||
|
||||
private int scoredCount = 0;
|
||||
|
||||
public string TaskID { get; set; }
|
||||
|
||||
public TaskType TaskType { get; set; }
|
||||
public string TaskName { get; set; }
|
||||
|
||||
public (double, double) TaskLocation { get; set; }
|
||||
|
||||
public bool IsCompleted { get; private set; } = false;
|
||||
protected Action<ITask> OnCompleted;
|
||||
|
||||
|
||||
public void RegisterItem()
|
||||
{
|
||||
scoredCount++;
|
||||
Debug.Log($"Trefeno: {scoredCount} / {itemsToScore}");
|
||||
|
||||
if (scoredCount >= itemsToScore)
|
||||
{
|
||||
OnAllItemsScored?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetCounter()
|
||||
{
|
||||
scoredCount = 0;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
117
Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs
Normal file
117
Assets/Scripts/hod_veci_do_diry/ObjectSpawner.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using UnityEngine;
|
||||
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;
|
||||
|
||||
[Header("Počty")]
|
||||
[Tooltip("Kolik předmětů spawnovat")]
|
||||
public int objectCount = 3;
|
||||
[Tooltip("Kolik děr spawnovat")]
|
||||
public int holeCount = 1;
|
||||
|
||||
[Header("Pohyb děr")]
|
||||
public bool holesMove = false;
|
||||
public float holeMoveSpeed = 2f;
|
||||
|
||||
[Header("Spawn hranice (odpovídají kameře)")]
|
||||
|
||||
public float minX = 1f;
|
||||
public float maxX = 1f;
|
||||
public float minY = 0f;
|
||||
public float maxY = 1f;
|
||||
|
||||
|
||||
private List<GameObject> spawnedObjects = new List<GameObject>();
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance == null) Instance = this;
|
||||
else Destroy(gameObject);
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Spawn();
|
||||
}
|
||||
|
||||
public void Spawn()
|
||||
{
|
||||
Clear();
|
||||
|
||||
// LevelManager na aktuální počet
|
||||
if (LevelManager.Instance != null)
|
||||
{
|
||||
LevelManager.Instance.itemsToScore = objectCount;
|
||||
LevelManager.Instance.ResetCounter();
|
||||
}
|
||||
|
||||
//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);
|
||||
// }
|
||||
//}
|
||||
|
||||
public void SpawnObjects()
|
||||
{
|
||||
Debug.Log($"Spawning {objectCount} objects...");
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
GameObject prefab = objectPrefabs[Random.Range(0, objectPrefabs.Length)];
|
||||
Vector3 pos = RandomPos(0.5f);
|
||||
GameObject obj = Instantiate(prefab, pos, Quaternion.identity);
|
||||
|
||||
// Náhodná barva
|
||||
Renderer sr = obj.GetComponent<Renderer>();
|
||||
if (sr != null)
|
||||
sr.sharedMaterial.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.9f, 1f);
|
||||
|
||||
spawnedObjects.Add(obj);
|
||||
}
|
||||
Time.timeScale = 1f;
|
||||
}
|
||||
|
||||
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();
|
||||
spawnedHoles.Clear();
|
||||
}
|
||||
|
||||
Vector3 RandomPos(float margin)
|
||||
{
|
||||
return new Vector3(
|
||||
Random.Range(-3, -4),
|
||||
-0.3f,
|
||||
Random.Range(-2f, 1.5f)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user