2025-07-14 19:30:52 +05:00
|
|
|
using System.Collections.Generic;
|
2025-07-11 15:42:48 +05:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class ShieldController : MonoBehaviour
|
|
|
|
{
|
2025-07-14 19:30:52 +05:00
|
|
|
public float followSpeed = 20f;
|
|
|
|
public float pushForce = 10f;
|
|
|
|
public LayerMask pushableLayers;
|
2025-07-11 15:42:48 +05:00
|
|
|
|
|
|
|
private Rigidbody2D rb;
|
|
|
|
private Camera mainCam;
|
|
|
|
|
2025-07-14 19:30:52 +05:00
|
|
|
private HashSet<GameObject> hitEnemies = new HashSet<GameObject>();
|
|
|
|
|
2025-07-11 15:42:48 +05:00
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = GetComponent<Rigidbody2D>();
|
|
|
|
mainCam = Camera.main;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
2025-07-14 19:30:52 +05:00
|
|
|
if (BalloonGameManager.IsGameOver) return;
|
|
|
|
|
|
|
|
HandleMovement();
|
|
|
|
DestroyOffscreenEnemies();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleMovement()
|
|
|
|
{
|
|
|
|
if (Input.GetMouseButton(0))
|
2025-07-11 15:42:48 +05:00
|
|
|
{
|
|
|
|
Vector2 touchPosition = mainCam.ScreenToWorldPoint(Input.mousePosition);
|
|
|
|
Vector2 direction = (touchPosition - rb.position);
|
|
|
|
rb.velocity = direction * followSpeed;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2025-07-14 19:30:52 +05:00
|
|
|
rb.velocity = Vector2.zero;
|
2025-07-11 15:42:48 +05:00
|
|
|
}
|
2025-07-14 19:30:52 +05:00
|
|
|
|
|
|
|
// Clamp shield inside the camera
|
|
|
|
Vector3 viewPos = mainCam.WorldToViewportPoint(transform.position);
|
|
|
|
viewPos.x = Mathf.Clamp01(viewPos.x);
|
|
|
|
viewPos.y = Mathf.Clamp01(viewPos.y);
|
|
|
|
transform.position = mainCam.ViewportToWorldPoint(viewPos);
|
2025-07-11 15:42:48 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
|
|
|
{
|
|
|
|
if (((1 << collision.gameObject.layer) & pushableLayers) != 0)
|
|
|
|
{
|
|
|
|
Rigidbody2D otherRb = collision.rigidbody;
|
|
|
|
if (otherRb != null && otherRb != rb)
|
|
|
|
{
|
|
|
|
Vector2 forceDir = collision.transform.position - transform.position;
|
|
|
|
forceDir.Normalize();
|
|
|
|
otherRb.AddForce(forceDir * pushForce, ForceMode2D.Impulse);
|
2025-07-14 19:30:52 +05:00
|
|
|
|
|
|
|
// Add to list of hit enemies
|
|
|
|
if (!hitEnemies.Contains(collision.gameObject))
|
|
|
|
{
|
|
|
|
hitEnemies.Add(collision.gameObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyOffscreenEnemies()
|
|
|
|
{
|
|
|
|
float buffer = 1f; // World units to extend camera bounds (adjust as needed)
|
|
|
|
List<GameObject> toRemove = new List<GameObject>();
|
|
|
|
|
|
|
|
foreach (GameObject enemy in hitEnemies)
|
|
|
|
{
|
|
|
|
if (enemy == null) continue;
|
|
|
|
|
|
|
|
Vector3 enemyPos = enemy.transform.position;
|
|
|
|
Vector3 camPos = mainCam.transform.position;
|
|
|
|
|
|
|
|
float camHeight = 2f * mainCam.orthographicSize;
|
|
|
|
float camWidth = camHeight * mainCam.aspect;
|
|
|
|
|
|
|
|
float left = camPos.x - camWidth / 2f - buffer;
|
|
|
|
float right = camPos.x + camWidth / 2f + buffer;
|
|
|
|
float bottom = camPos.y - camHeight / 2f - buffer;
|
|
|
|
float top = camPos.y + camHeight / 2f + buffer;
|
|
|
|
|
|
|
|
if (enemyPos.x < left || enemyPos.x > right ||
|
|
|
|
enemyPos.y < bottom || enemyPos.y > top)
|
|
|
|
{
|
|
|
|
Destroy(enemy);
|
|
|
|
toRemove.Add(enemy);
|
2025-07-11 15:42:48 +05:00
|
|
|
}
|
|
|
|
}
|
2025-07-14 19:30:52 +05:00
|
|
|
|
|
|
|
foreach (GameObject e in toRemove)
|
|
|
|
{
|
|
|
|
hitEnemies.Remove(e);
|
|
|
|
}
|
2025-07-11 15:42:48 +05:00
|
|
|
}
|
|
|
|
}
|