Untitled

 avatar
unknown
plain_text
3 years ago
6.3 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

// this is an alternative Player script, with extras.

[RequireComponent(typeof(Rigidbody))]   // This automatically attaches a RIGIDBODY component to anything this script is attached to
public class RollyBallBeta : MonoBehaviour
{
    // PUBLIC variables
    public float playerSpeed = 15;
    public Text scoreText;
    public GameObject collectVFX;
    public float vfxPlayTime;
    // Maybe we need some more variables to track progress...?
    public GameObject[] collectibleObjects;
    public int targetCollectibles;
    public Text collectibleText;
    public Text timeTaken;
    public GameObject exitDoor;


    // PRIVATE variables
    Rigidbody rb;
    Vector3 moveInput;
    int scoreValue;
    Vector3 startPos;
    int collectedCollectibles;
    int maxCollectibles;
    int currentTimeTaken;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        startPos = transform.position;
    }

    void Start()
    {
        SetStartValues();
        DisplayCollectiblesCount();
        StartTimer();
        DisplayTimer();
        GetMaxCollectibles();
        DisplayCollectiblesCount();
    }

    void Update()
    {
        GetInputs();
    }

    void FixedUpdate()
    {
        DoMovement();
    }

    #region Collision Scripts
    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Collectible"))
        {
            int newScore = scoreValue + 1;
            UpdateCollectibles();
            UpdateScoreValue(newScore);
            if (collectVFX != null)
            {
                GameObject VFX = Instantiate(collectVFX, other.transform.position, other.transform.rotation);
                StartCoroutine(DestroyVFX(VFX));
            }
            Destroy(other.gameObject);
        }

        else if (other.CompareTag("Restart"))
        {
            //rb.velocity = Vector3.zero;
            //transform.position = startPos;

            // maybe this would be a better option...?
            RestartLevel();
            Debug.Log("Restart");
        }

        // Your Tags here!

        else if (other.CompareTag("EndDoor"))
        {
            LoadNextLevel();
            
        }

        else
        {
            Debug.Log("I hit a " + other.name + " with the tag " + other.tag + " at pos: " + other.transform.position);
        }
    }
    #endregion

    void SetStartValues()
    {
        UpdateScoreValue(0);
        // maybe we could setup physics here?
        //SetupInitialPhysics();
        // Maybe we could do a timer?
        //ResetTimer();
        //startTimer();
    }

    void GetInputs()
    {
        moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
    }

    void DoMovement()
    {
        rb.AddForce(moveInput * playerSpeed);
    }

    #region Score
    void UpdateScoreValue(int i)
    {
        scoreValue = i;
        WriteScoreText(i);
    }

    void WriteScoreText(int i)
    {
        scoreText.text = i + " points";
    }
    #endregion

    #region Collectibles
    void UpdateCollectibles()
    {
        collectedCollectibles++;
        DisplayCollectiblesCount();
    }

    void GetMaxCollectibles()
    {
        maxCollectibles = collectibleObjects.Count();
    }

    void DisplayCollectiblesCount()
    {
        collectibleText.text = collectedCollectibles.ToString() + " / " + maxCollectibles;
    }

    void DisplayCollectiblesCount(int collected)
    {
        collectibleText.text = collected.ToString() + " / " + targetCollectibles.ToString();
    }
    #endregion

    #region Timer
    void StartTimer()
    {
        InvokeRepeating("UpdateTimer", 1f, 1f);
    }

    void UpdateTimer()
    {
        currentTimeTaken++;
        DisplayTimer();
    }

    void ResetTimer()
    {
        currentTimeTaken=0;
        DisplayTimer();
    }

    void StopTimer()
    {
        CancelInvoke("UpdateTimer");
    }

    void DisplayTimer()
    {
        timeTaken.text = currentTimeTaken.ToString();
    }
    #endregion

    IEnumerator DestroyVFX(GameObject vfx)
    {
        yield return new WaitForSeconds(vfxPlayTime);
        Destroy(vfx);
    }

    void SetupInitialPhysics()  // This METHOD will setup various Physics options; they can all be set in the editor, but if you know what you want you can automate it in script.
    {
        rb.isKinematic = false;                                                 // this is not a kinematic rigidbody, we want physics running
        rb.detectCollisions = true;                                             // yes please, detect collisions!
        rb.useGravity = true;                                                   // yes please, use gravity!
        rb.interpolation = RigidbodyInterpolation.Interpolate;                  // smooths out physics movements to better sync with rendering fps
        rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;   // ContinousSpeculative is more efficient, but may have issues at speed
    }

    #region Level Loading Scripts

    // Methods relating to loading levels.
    // This is getting long enough that it might be worth thinking about turning it into a new CLASS...
    int GetCurrentSceneIndex()
    {
        UnityEngine.SceneManagement.Scene scene = SceneManager.GetActiveScene();
        int level = scene.buildIndex;
        return (level);
    }

    void LoadPreviousLevel()
    {
        int prev = GetCurrentSceneIndex();
        if (prev<0) { prev = 0; }
        SceneManager.LoadScene(prev);
    }

    void LoadNextLevel()
    {
        int next = GetCurrentSceneIndex();
        next++;
        SceneManager.LoadScene(next);
    }

    void LoadSpecificLevel(int level)
    {
        SceneManager.LoadScene(level);
    }

    void RestartLevel()
    {
        SceneManager.LoadScene(GetCurrentSceneIndex());
    }
    #endregion
}
Editor is loading...