Skip to content

Instantly share code, notes, and snippets.

@satomi-i-k
Last active October 3, 2023 08:59
Show Gist options
  • Save satomi-i-k/b6bbb38d837abed2d4d5238f2fc06f9b to your computer and use it in GitHub Desktop.
Save satomi-i-k/b6bbb38d837abed2d4d5238f2fc06f9b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
public class Raq_GameManager : MonoBehaviour
{
public Transform rubberball;
public Transform raq_player;
public Transform raq_opponent;
public Animator opponent_anim;
public Animator countDown;
public Animator swipeLabel;
public Transform servePos;
public Transform player_servePos;
public Transform player_receivePos;
[SerializeField]
GameObject nextLabel;
[SerializeField]
GameObject nextText;
[SerializeField]
GameObject outLabel;
[SerializeField]
GameObject Player;
[SerializeField]
GameObject Opponent;
[HideInInspector]
public bool playerServe = true;
[HideInInspector]
public bool befServe;
public int playerPoints;
public int opponentPoints;
RubberBall ballScript;
void Start()
{
playerPoints = 0;
opponentPoints = 0;
nextLabel.SetActive(false);
outLabel.SetActive(false);
playerServe = true;
befServe = true;
Set_Position();
Serve();
ballScript = rubberball.GetComponent<RubberBall>();
}
public void Set_Position()
{
if (befServe == false)
return;
rubberball.position = servePos.position;
rubberball.GetComponent<Rigidbody>().velocity = Vector3.zero;
Player.SetActive(true);
Opponent.SetActive(true);
if (playerServe)
{
raq_player.position = player_servePos.position;
raq_opponent.position = player_receivePos.position;
}
else
{
raq_opponent.position = player_servePos.position;
raq_player.position = player_receivePos.position;
}
}
public void Serve()
{
if (playerServe)
{
swipeLabel.SetTrigger("Play");
}
else
{
StartCoroutine(Opponent_Serve());
}
}
IEnumerator Opponent_Serve()
{
Raq_opponent opponentScript = raq_opponent.GetComponent<Raq_opponent>();
countDown.SetTrigger("Countdown");
yield return new WaitForSeconds(3f);
StartCoroutine(opponentScript.Raq_JustHit());
opponent_anim.SetTrigger("Hit right");
yield return new WaitForSeconds(0.28f);
opponentScript.Raq_HitBall(true, servePos);
befServe = false;
ballScript.after_hit = true;
Invoke(nameof(Cancel_Afthit), ballScript.timelag);
}
void Cancel_Afthit()
{
ballScript.after_hit = false;
}
public void CourtTriggered(bool front_col)
{
befServe = true;
outLabel.SetActive(true);
if (front_col)
{
if (ballScript.GetLastHit())
{
WinPoint();
}
else
{
LosePoint();
}
}
else
{
if (ballScript.GetLastHit())
{
LosePoint();
}
else
{
WinPoint();
}
}
if (playerPoints >= 5)
{
nextLabel.SetActive(true);
nextText.GetComponent<UnityEngine.UI.Text>().text = "YOU WIN!!";
StartCoroutine(NextMatch());
}
else if (opponentPoints >= 5)
{
nextLabel.SetActive(true);
nextText.GetComponent<UnityEngine.UI.Text>().text = "YOU LOSE";
StartCoroutine(NextMatch());
}
else
{
StartCoroutine(NextGame());
}
}
IEnumerator NextGame()
{
yield return new WaitForSeconds(4.0f);
Player.SetActive(false);
Opponent.SetActive(false);
nextLabel.SetActive(true);
nextText.GetComponent<UnityEngine.UI.Text>().text = "Player:" + playerPoints + " Opponent:" + opponentPoints;
yield return new WaitForSeconds(6.0f);
outLabel.SetActive(false);
nextLabel.SetActive(false);
playerServe = !playerServe;
Set_Position();
Serve();
}
//add a point to the opponent score
void LosePoint()
{
opponentPoints++;
}
//add a point to the player score
void WinPoint()
{
playerPoints++;
}
IEnumerator NextMatch()
{
yield return new WaitForSeconds(4.0f);
SceneManager.LoadScene("ContinueScene");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment