apple-picker/Assets/Scripts/Basket.cs

66 lines
2.5 KiB
C#
Raw Normal View History

2024-02-08 03:30:36 -06:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2024-02-08 19:15:28 -06:00
using UnityEngine.SceneManagement;
2024-02-08 03:30:36 -06:00
public class Basket : MonoBehaviour
{
[Header("Set Dynamically")]
public Text scoreGT;
// Start is called before the first frame update
void Start()
{
2024-02-08 19:15:28 -06:00
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
2024-02-08 03:30:36 -06:00
// 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()
{
2024-02-08 19:15:28 -06:00
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
2024-02-08 03:30:36 -06:00
// 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 OnCollisionEnter(Collision coll) {
// Find out what hit this basket
GameObject collidedWith = coll.gameObject;
switch (collidedWith.tag) {
case "Apple":
Destroy(collidedWith);
2024-02-08 19:15:28 -06:00
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;
case "RottenApple":
Destroy(collidedWith);
2024-02-08 19:15:28 -06:00
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
ApplePicker apScript = Camera.main.GetComponent<ApplePicker>();
apScript.RottenAppleDestroyed();
break;
default:
break;
}
2024-02-08 03:30:36 -06:00
}
}