2024-02-08 03:30:36 -06:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2024-02-08 19:38:37 -06:00
|
|
|
using UnityEngine.SceneManagement;
|
2024-02-08 03:30:36 -06:00
|
|
|
|
|
|
|
public class AppleTree : MonoBehaviour
|
|
|
|
{
|
|
|
|
[Header("Set in Inspector")]
|
|
|
|
// Prefab for instantiating apples
|
|
|
|
public GameObject applePrefab;
|
2024-02-08 17:27:45 -06:00
|
|
|
public GameObject rottenApplePrefab;
|
2024-02-08 21:10:54 -06:00
|
|
|
public GameObject goldenApplePrefab;
|
2024-02-08 03:30:36 -06:00
|
|
|
// Speed at which the AppleTree moves
|
|
|
|
public float speed = 1f;
|
|
|
|
// Distance where AppleTree turns around
|
|
|
|
public float leftAndRightEdge = 10f;
|
|
|
|
// Chance that the AppleTree will change directions
|
|
|
|
public float chanceToChangeDirections = 0.1f;
|
|
|
|
// Rate at which Apples will be instantiated
|
|
|
|
public float secondsBetweenAppleDrops = 1f;
|
2024-02-08 17:27:45 -06:00
|
|
|
// Rate at which RottenApples will be instantiated
|
2024-02-08 21:10:54 -06:00
|
|
|
public float secondsBetweenRottenAppleDrops = 2f;
|
|
|
|
// Rate at which RottenApples will be instantiated
|
2024-02-08 21:13:42 -06:00
|
|
|
public float secondsBetweenGoldenAppleDrops = 10f;
|
2024-02-08 21:10:54 -06:00
|
|
|
public bool goldenAppleTime = false;
|
|
|
|
public float goldenAppleTimeCurrent = 0f;
|
|
|
|
public float goldenAppleTimeLimit = 5f;
|
2024-02-08 03:30:36 -06:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
// Dropping apples every second
|
2024-02-08 21:10:54 -06:00
|
|
|
Invoke("DropApple", 1f);
|
2024-02-08 19:38:37 -06:00
|
|
|
if (SceneManager.GetActiveScene().name == "_Scene_2") { return; }
|
2024-02-08 17:27:45 -06:00
|
|
|
// Dropping rotten apples every 3 seconds
|
|
|
|
Invoke("DropRottenApple", 2f);
|
2024-02-08 21:10:54 -06:00
|
|
|
Invoke("DropGoldenApple", 4f);
|
2024-02-08 03:30:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void DropApple() {
|
|
|
|
GameObject apple = Instantiate<GameObject>(applePrefab);
|
|
|
|
apple.transform.position = transform.position;
|
|
|
|
Invoke("DropApple", secondsBetweenAppleDrops);
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:27:45 -06:00
|
|
|
void DropRottenApple() {
|
|
|
|
GameObject rottenApple = Instantiate<GameObject>(rottenApplePrefab);
|
|
|
|
rottenApple.transform.position = transform.position;
|
|
|
|
Invoke("DropRottenApple", secondsBetweenRottenAppleDrops);
|
|
|
|
}
|
|
|
|
|
2024-02-08 21:10:54 -06:00
|
|
|
void DropGoldenApple() {
|
|
|
|
GameObject goldenApple = Instantiate<GameObject>(goldenApplePrefab);
|
|
|
|
goldenApple.transform.position = transform.position;
|
|
|
|
if (goldenAppleTime) {
|
|
|
|
Invoke("DropGoldenApple", secondsBetweenGoldenAppleDrops + goldenAppleTimeLimit);
|
|
|
|
} else {
|
|
|
|
Invoke("DropGoldenApple", secondsBetweenGoldenAppleDrops);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void GoldenAppleTimeStart() {
|
2024-02-08 21:13:42 -06:00
|
|
|
goldenAppleTime = true;
|
|
|
|
secondsBetweenAppleDrops *= 0.25f;
|
2024-02-08 21:10:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public void GoldenAppleTimeEnd() {
|
2024-02-08 21:13:42 -06:00
|
|
|
goldenAppleTime = false;
|
2024-02-08 21:10:54 -06:00
|
|
|
goldenAppleTimeCurrent = 0f;
|
2024-02-08 21:13:42 -06:00
|
|
|
secondsBetweenAppleDrops *= 4;
|
2024-02-08 21:10:54 -06:00
|
|
|
}
|
|
|
|
|
2024-02-08 03:30:36 -06:00
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
// Basic Movement
|
|
|
|
Vector3 pos = transform.position;
|
|
|
|
pos.x += speed * Time.deltaTime;
|
|
|
|
transform.position = pos;
|
|
|
|
// Changing Direction
|
|
|
|
if (pos.x < -leftAndRightEdge) {
|
|
|
|
speed = Mathf.Abs(speed);
|
|
|
|
} else if (pos.x > leftAndRightEdge) {
|
|
|
|
speed = -Mathf.Abs(speed);
|
|
|
|
}
|
2024-02-08 21:10:54 -06:00
|
|
|
|
|
|
|
if (goldenAppleTime) {
|
|
|
|
goldenAppleTimeCurrent += Time.deltaTime;
|
|
|
|
if (goldenAppleTimeCurrent > goldenAppleTimeLimit) {
|
|
|
|
GoldenAppleTimeEnd();
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 03:30:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update exactly 50 times per second
|
|
|
|
void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (Random.value < chanceToChangeDirections) {
|
|
|
|
speed *= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|