Script Player, UI Score, Main Menu Scene, UI Gameover, UI PauseMenu
This commit is contained in:
AryPratama 2024-06-10 14:26:43 +07:00
commit d4fe831535
5 changed files with 460 additions and 0 deletions

60
GameController.cs Normal file
View File

@ -0,0 +1,60 @@
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
public static int playerScore = 0;
public static int botScore = 0;
public Text playerScoreText;
public Text botScoreText;
private string lastHitter;
void Start()
{
UpdateScoreUI();
}
public void UpdateScoreUI()
{
playerScoreText.text = playerScore.ToString();
botScoreText.text = botScore.ToString();
}
public void AddScore(string hitter, int points)
{
if (hitter == "Player")
{
playerScore += points;
}
else if (hitter == "Bot")
{
botScore += points;
}
UpdateScoreUI();
}
public void SetLastHitter(string hitter)
{
lastHitter = hitter;
}
public bool LastHitterEquals(string hitter)
{
return lastHitter == hitter;
}
public void ResetBall(Bola bola)
{
bola.ResetBola();
}
public void ResetScores()
{
playerScore = 0;
botScore = 0;
UpdateScoreUI();
}
}

204
GerakanRaket.cs Normal file
View File

@ -0,0 +1,204 @@
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using UnityEngine;
public class GerakanRaket : MonoBehaviour
{
// Variabel untuk Gyro
SerialPort stream = new SerialPort("COM6", 9600);
public string strReceived;
public string[] strData = new string[4];
public string[] strData_received = new string[4];
public float qw, qx, qy, qz;
// Variabel untuk Gerakan Raket
public Transform aimTarget;
public float horizontalSpeed = 0.5f;
public float verticalSpeed = 0.5f;
public float aimSpeed = 1.0f;
public AudioClip hitSoundClip;
private float initialHeight;
private Vector3 initialPosition;
private bool ballMoving = false;
void Start()
{
InitializeSerialPort();
initialHeight = transform.position.y;
initialPosition = transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
ResetPlayerPosition();
return;
}
try
{
if (stream.IsOpen && stream.BytesToRead > 0)
{
strReceived = stream.ReadLine();
strData = strReceived.Split(',');
if (strData.Length == 4)
{
ProcessGyroData();
if (ballMoving) Move();
}
}
}
catch (System.Exception ex)
{
Debug.LogError("Error reading from serial port: " + ex.Message);
ReconnectSerialPort();
}
}
void ProcessGyroData()
{
strData_received[0] = strData[0];
strData_received[1] = strData[1];
strData_received[2] = strData[2];
strData_received[3] = strData[3];
qw = float.Parse(strData_received[0]);
qx = float.Parse(strData_received[1]);
qy = float.Parse(strData_received[2]);
qz = float.Parse(strData_received[3]);
transform.rotation = new Quaternion(-qy, -qz, qx, qw);
}
void Move()
{
MoveHorizontal();
MoveVertical();
MoveAimTarget();
}
void MoveHorizontal()
{
float horizontalMovement = -qx * horizontalSpeed * Time.deltaTime;
Vector3 newPosition = transform.position;
newPosition.x += horizontalMovement;
newPosition.x = Mathf.Clamp(newPosition.x, -3f, 4.6f);
transform.position = newPosition;
}
void MoveVertical()
{
float verticalMovement = -qy * verticalSpeed * Time.deltaTime;
Vector3 newPosition = transform.position;
newPosition.z += verticalMovement;
newPosition.z = Mathf.Clamp(newPosition.z, -6.5f, 1.6f);
transform.position = newPosition;
}
void MoveAimTarget()
{
float aimHorizontalMovement = qz * aimSpeed * Time.deltaTime;
Vector3 aimTargetPosition = aimTarget.position;
aimTargetPosition.x += aimHorizontalMovement;
aimTarget.position = aimTargetPosition;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Bola"))
{
HandleBallCollision(other);
}
}
void HandleBallCollision(Collider ballCollider)
{
ballMoving = true;
Vector3 dir = aimTarget.position - ballCollider.transform.position;
Rigidbody bolaRigidbody = ballCollider.GetComponent<Rigidbody>();
if (!bolaRigidbody.useGravity)
{
bolaRigidbody.useGravity = true;
}
float horizontalMovementDirection = -qz;
if (horizontalMovementDirection > 10)
{
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(1, 5, 1);
}
else if (horizontalMovementDirection < -10)
{
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(-1, 5, 1);
}
else
{
float verticalMovementDirection = -qy;
if (verticalMovementDirection > 0)
{
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 5, 2);
}
else if (verticalMovementDirection < 0)
{
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 7, 0);
}
else
{
bolaRigidbody.velocity = dir.normalized * 7 + new Vector3(0, 6, 0);
}
}
if (hitSoundClip != null)
{
AudioSource.PlayClipAtPoint(hitSoundClip, transform.position);
}
}
public void ResetPlayerPosition()
{
Debug.Log("Resetting Player Position");
transform.position = initialPosition;
ballMoving = false;
}
void OnDestroy()
{
if (stream != null && stream.IsOpen)
{
stream.Close();
}
}
void InitializeSerialPort()
{
try
{
stream.Open();
}
catch (System.Exception ex)
{
Debug.LogError("Error opening serial port: " + ex.Message);
}
}
void ReconnectSerialPort()
{
try
{
if (stream != null && stream.IsOpen)
{
stream.Close();
}
stream.Open();
Debug.Log("Serial port reconnected successfully.");
}
catch (System.Exception ex)
{
Debug.LogError("Error reconnecting serial port: " + ex.Message);
}
}
}

17
MainMenu.cs Normal file
View File

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
public void Play()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void Quit()
{
Application.Quit();
Debug.Log("Player sudah keluar");
}
}

77
OverMenu.cs Normal file
View File

@ -0,0 +1,77 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class OverMenu : MonoBehaviour
{
public GameObject gameOverPanel;
public Text resultText;
public GameController gameController;
private bool gameOver = false;
void Update()
{
if (!gameOver && gameController != null && (GameController.playerScore >= 11 || GameController.botScore >= 11))
{
gameOver = true;
Time.timeScale = 0;
gameOverPanel.SetActive(true);
if (GameController.playerScore >= 11)
{
resultText.text = "You Win!";
}
else
{
resultText.text = "You Lose!";
}
}
}
public void RestartGame()
{
// Kembalikan skor ke nilai awal jika ada
if (gameController != null)
{
gameController.ResetScores();
}
ResetMainScene();
// Kembalikan waktu permainan ke normal
Time.timeScale = 1;
// Muat ulang level saat ini
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void QuitGame()
{
// Kembalikan skor ke nilai awal jika ada
if (gameController != null)
{
gameController.ResetScores();
}
ResetMainScene();
// Kembalikan waktu permainan ke normal
Time.timeScale = 1;
// Kembali ke menu utama (biasanya level sebelumnya)
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
private void ResetMainScene()
{
// Reset posisi objek player jika ada
GameObject player = GameObject.FindWithTag("Player");
if (player != null)
{
player.transform.position = Vector3.zero;
}
}
}

102
PauseMenu.cs Normal file
View File

@ -0,0 +1,102 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PauseMenu : MonoBehaviour
{
bool paused = false;
public GameObject PauseMenuUI;
public GameController gameController;
public Slider horizontalMoveSlider;
public Slider verticalMoveSlider;
public GerakanRaket gerakanRaket;
void Start()
{
LoadSettings();
}
void LoadSettings()
{
float horizontalSpeed = PlayerPrefs.GetFloat("HorizontalSpeed", 1f);
horizontalMoveSlider.value = horizontalSpeed;
gerakanRaket.horizontalSpeed = horizontalSpeed;
float verticalSpeed = PlayerPrefs.GetFloat("VerticalSpeed", 1f);
verticalMoveSlider.value = verticalSpeed;
gerakanRaket.verticalSpeed = verticalSpeed;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
pauseGame();
}
}
public void pauseGame()
{
if (paused)
{
Time.timeScale = 1;
paused = false;
PauseMenuUI.SetActive(false);
}
else
{
Time.timeScale = 0;
paused = true;
PauseMenuUI.SetActive(true);
}
}
public void RestartGame()
{
if (gameController != null)
{
gameController.ResetScores();
}
ResetMainScene();
Time.timeScale = 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void QuitGame()
{
if (gameController != null)
{
gameController.ResetScores();
}
ResetMainScene();
Time.timeScale = 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
private void ResetMainScene()
{
GameObject player = GameObject.FindWithTag("Player");
if (player != null)
{
player.transform.position = Vector3.zero;
}
}
public void UpdateHorizontalMove(float value)
{
gerakanRaket.horizontalSpeed = value;
PlayerPrefs.SetFloat("HorizontalSpeed", value);
}
public void UpdateVerticalMove(float value)
{
gerakanRaket.verticalSpeed = value;
PlayerPrefs.SetFloat("VerticalSpeed", value);
}
}