92 lines
3.5 KiB
C#
92 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class Basket : MonoBehaviour
|
|
{
|
|
[Header("Set Dynamically")]
|
|
public Text scoreGT;
|
|
float botSpeed = 15f;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
|
|
// Find a reference to the ScoreCounter GameObject
|
|
GameObject scoreGO = GameObject.Find("ScoreCounter");
|
|
// Get the Text Component of that GameObject
|
|
scoreGT = scoreGO.GetComponent<Text>();
|
|
// Set the starting number of points to 0
|
|
scoreGT.text = "0";
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (SceneManager.GetActiveScene().name == "_Scene_2") { GetBotInput(); }
|
|
else { GetPlayerInput(); }
|
|
}
|
|
|
|
void GetPlayerInput() {
|
|
// Get the current screen position of the mouse from Input
|
|
Vector3 mousePos2D = Input.mousePosition;
|
|
// The Camera's z position sets how far to push the mouse into 3D
|
|
mousePos2D.z = -Camera.main.transform.position.z;
|
|
// Convert the point from 2D screen space into 3D game world space
|
|
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
|
|
// Move the x position of this Basket to the x position of the Mouse
|
|
Vector3 pos = this.transform.position;
|
|
pos.x = mousePos3D.x;
|
|
this.transform.position = pos;
|
|
}
|
|
|
|
void GetBotInput() {
|
|
float basketHeight = this.transform.position.y;
|
|
float currentMin = 999;
|
|
float directionToGo = this.transform.position.x;
|
|
GameObject[] tAppleArray = GameObject.FindGameObjectsWithTag("Apple");
|
|
foreach (GameObject tGO in tAppleArray) {
|
|
if (tGO.transform.position.y - basketHeight < currentMin) {
|
|
currentMin = tGO.transform.position.y - basketHeight;
|
|
if (tGO.transform.position.x > this.transform.position.x) { directionToGo = 1; }
|
|
else { directionToGo = -1; }
|
|
}
|
|
}
|
|
Vector3 pos = transform.position;
|
|
pos.x += botSpeed * Time.deltaTime * directionToGo;
|
|
transform.position = pos;
|
|
}
|
|
|
|
void OnCollisionEnter(Collision coll) {
|
|
// Find out what hit this basket
|
|
GameObject collidedWith = coll.gameObject;
|
|
ApplePicker apScript = Camera.main.GetComponent<ApplePicker>();
|
|
switch (collidedWith.tag) {
|
|
case "RottenApple":
|
|
Destroy(collidedWith);
|
|
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
|
|
apScript.RottenAppleDestroyed();
|
|
break;
|
|
case "GoldenApple":
|
|
Destroy(collidedWith);
|
|
apScript.GoldenAppleDestroyed();
|
|
break;
|
|
case "Apple":
|
|
Destroy(collidedWith);
|
|
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
|
|
// Parse the text of the scoreGT into an int
|
|
int score = int.Parse(scoreGT.text);
|
|
// Add points for catching the apple
|
|
score += 100;
|
|
// Convert the score back to a string and display it
|
|
scoreGT.text = score.ToString();
|
|
// Track the high score
|
|
if (score > HighScore.score) { HighScore.score = score; }
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|