225 lines
5.7 KiB
C#
225 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class Shooter : MonoBehaviour
|
|
{
|
|
public PlayerType playerType;
|
|
public NetPlayer player;
|
|
public static Shooter master;
|
|
public static Shooter client;
|
|
|
|
public void Toggle(bool val)
|
|
{
|
|
if (val)
|
|
{
|
|
this.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
this.enabled = false;
|
|
line.SetActive(false);
|
|
}
|
|
}
|
|
public bool canShoot;
|
|
public float speed = 25f;
|
|
|
|
public Transform nextBubblePosition;
|
|
public GameObject currentBubble;
|
|
public GameObject nextBubble;
|
|
public GameObject bottomShootPoint;
|
|
public RectTransform shootingPoint;
|
|
public Button swapButton;
|
|
|
|
private Vector2 lookDirection;
|
|
private float lookAngle;
|
|
public GameObject line;
|
|
private GameObject limit;
|
|
private LineRenderer lineRenderer;
|
|
private Vector2 gizmosPoint;
|
|
|
|
public void Awake()
|
|
{
|
|
limit = GameObject.FindGameObjectWithTag("Limit");
|
|
if(playerType == PlayerType.Master){
|
|
master = this;
|
|
}else if(playerType == PlayerType.Client){
|
|
client= this;
|
|
}
|
|
//lineRenderer = line.GetComponent<LineRenderer>();
|
|
}
|
|
|
|
private void Start() {
|
|
swapButton.onClick.AddListener(SwapBubblesNetwork);
|
|
}
|
|
public void Update()
|
|
{
|
|
|
|
lookDirection = Input.mousePosition - shootingPoint.position;
|
|
if (pointerInside)
|
|
{
|
|
lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
|
|
line.SetActive(true);
|
|
line.transform.position = transform.position;
|
|
line.transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);
|
|
}
|
|
else
|
|
{
|
|
line.SetActive(false);
|
|
}
|
|
|
|
return;
|
|
if (GameManager.instance.gameState == "play")
|
|
{
|
|
gizmosPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
|
|
lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
|
|
|
|
if (Input.GetMouseButton(0)
|
|
&& (Camera.main.ScreenToWorldPoint(Input.mousePosition).y > bottomShootPoint.transform.position.y)
|
|
&& (Camera.main.ScreenToWorldPoint(Input.mousePosition).y < limit.transform.position.y))
|
|
{
|
|
line.transform.position = transform.position;
|
|
line.transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90);
|
|
|
|
if (LevelManager.instance != null
|
|
&& LevelManager.instance.GetBubbleAreaChildCount() > 0)
|
|
{
|
|
line.SetActive(true);
|
|
|
|
//cast a ray between transform position and mouse position
|
|
//CastRay(transform.position, gizmosPoint);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
line.SetActive(false);
|
|
}
|
|
|
|
if (canShoot
|
|
&& Input.GetMouseButtonUp(0)
|
|
&& (Camera.main.ScreenToWorldPoint(Input.mousePosition).y > bottomShootPoint.transform.position.y)
|
|
&& (Camera.main.ScreenToWorldPoint(Input.mousePosition).y < limit.transform.position.y))
|
|
{
|
|
canShoot = false;
|
|
Shoot();
|
|
}
|
|
}
|
|
}
|
|
bool pointerInside;
|
|
public void OnPointerEnter(BaseEventData e)
|
|
{
|
|
pointerInside = true;
|
|
}
|
|
|
|
public void OnPointerExit(BaseEventData e)
|
|
{
|
|
pointerInside = false;
|
|
}
|
|
|
|
public void OnPointerUp(BaseEventData e)
|
|
{
|
|
player.RequestShoot(lookAngle);
|
|
}
|
|
|
|
public void Shoot(float _lookAngle){
|
|
if(!canShoot){return;}
|
|
lookAngle = _lookAngle;
|
|
canShoot = false;
|
|
Shoot();
|
|
}
|
|
|
|
public void Shoot()
|
|
{
|
|
if (currentBubble == null) CreateNextBubble();
|
|
ScoreManager.GetInstance().AddThrows();
|
|
AudioManager.instance.PlaySound("shoot");
|
|
transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90f);
|
|
currentBubble.transform.rotation = transform.rotation;
|
|
currentBubble.GetComponent<CircleCollider2D>().enabled = true;
|
|
Rigidbody2D rb = currentBubble.GetComponent<Rigidbody2D>();
|
|
rb.AddForce(currentBubble.transform.up * speed, ForceMode2D.Impulse);
|
|
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
|
|
rb.gravityScale = 0;
|
|
currentBubble = null;
|
|
}
|
|
void SwapBubblesNetwork(){
|
|
player.RequestSwapBubbles();
|
|
}
|
|
public void SwapBubbles()
|
|
{
|
|
List<GameObject> bubblesInScene = LevelManager.instance.bubblesInScene;
|
|
if (bubblesInScene.Count < 1) return;
|
|
|
|
currentBubble.transform.position = nextBubblePosition.position;
|
|
nextBubble.transform.position = transform.position;
|
|
GameObject temp = currentBubble;
|
|
currentBubble = nextBubble;
|
|
nextBubble = temp;
|
|
}
|
|
|
|
public void CreateNewBubbles()
|
|
{
|
|
if (nextBubble != null)
|
|
Destroy(nextBubble);
|
|
|
|
if (currentBubble != null)
|
|
Destroy(currentBubble);
|
|
|
|
nextBubble = null;
|
|
currentBubble = null;
|
|
CreateNextBubble();
|
|
canShoot = true;
|
|
}
|
|
|
|
public void CreateNextBubble()
|
|
{
|
|
List<GameObject> bubblesInScene = LevelManager.instance.bubblesInScene;
|
|
List<string> colors = LevelManager.instance.colorsInScene;
|
|
|
|
if (bubblesInScene.Count < 1) return;
|
|
|
|
if (nextBubble == null)
|
|
{
|
|
nextBubble = InstantiateNewBubble(bubblesInScene);
|
|
}
|
|
else
|
|
{
|
|
// if (!colors.Contains(nextBubble.GetComponent<Bubble>().bubbleColor.ToString()))
|
|
// {
|
|
// Destroy(nextBubble);
|
|
// nextBubble = InstantiateNewBubble(bubblesInScene);
|
|
// }
|
|
}
|
|
|
|
if (currentBubble == null)
|
|
{
|
|
currentBubble = nextBubble;
|
|
currentBubble.transform.position = transform.position;
|
|
nextBubble = InstantiateNewBubble(bubblesInScene);
|
|
}
|
|
}
|
|
|
|
private GameObject InstantiateNewBubble(List<GameObject> bubblesInScene)
|
|
{
|
|
if (bubblesInScene.Count > 0)
|
|
{
|
|
GameObject newBubble = Instantiate(bubblesInScene[Random.Range(0, bubblesInScene.Count)], new Vector3(0, -100, 0), Quaternion.identity);
|
|
NetworkServer.Spawn(newBubble);
|
|
newBubble.transform.position = nextBubblePosition.position;
|
|
newBubble.GetComponent<Bubble>().isFixed = false;
|
|
newBubble.GetComponent<CircleCollider2D>().enabled = false;
|
|
Rigidbody2D rb2d = newBubble.AddComponent(typeof(Rigidbody2D)) as Rigidbody2D;
|
|
rb2d.gravityScale = 0f;
|
|
return newBubble;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|