mie

mail@pastecode.io avatar
unknown
csharp
4 years ago
5.5 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.SceneManagement;

public class CharacterScript : MonoBehaviour
{    
    public static float jumpForce = 300f;
    public static float speed = 2.5f;
    public static int hp;
    public bool moveRight;    
    public TMPro.TMP_Text coinText;
    public AudioClip coinSound;
    public AudioClip burgerSound;
    public AudioClip waterSound;    
    public Vector2 respawnPoint;
    public PlayerControl playerControl;

    public static int coinCount;

    private int currentLevel;

    float HorizontalMove = 0f;
    bool jump = false;
    bool gameStarted = false;
    bool move;
    bool grounded = false;    
    Rigidbody2D rigidbody;
    Animator anim;
    //CameraControl cameraControl;
    //PlayerControl playerControl;

    //private void Awake()
    //{
    //    playerControl = new PlayerControl();
    //}
    //private void OnEnable()
    //{
    //    playerControl.Enable();
    //}
    //private void OnDisable()
    //{
    //    playerControl.Disable();
    //}
    void Start()
    {
        hp = 3;
        coinCount = 0;
        rigidbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        respawnPoint = transform.position;
        //cameraControl = GameObject.Find("Visual Camera").GetComponent<CameraControl>();
    }

    void Update()
    {
        anim.SetBool("Grounded", grounded);
        anim.SetBool("Move", move);
        if (hp == 0)
        {
            GameOver();
        }
        //float jumpInput = playerControl.PlayerControls.Jump.ReadValue<float>();        
        //if (jumpInput > 0)
        //{
        //if ((grounded == true) && (gameStarted == true))
        //{
        //    jump = true;
        //    grounded = false;
        //    anim.SetTrigger("Jump");
        //}
        //else
        //{
        //    gameStarted = true;
        //    anim.SetTrigger("Start");
        //}
        //}

    }
    public void Jump()
    {
        gameStarted = true;
        if ((grounded == true) && (gameStarted == true))
        {
            jump = true;
            grounded = false;
            anim.SetTrigger("Jump");
        }
        //else
        //{
        //    gameStarted = true;            
        //    //anim.SetTrigger("Start");            
        //}
    }    
    public void Flip()
    {       
        gameStarted = true;
        if (HorizontalMove >= 0)
        {
            transform.localScale = new Vector2(1, 1);
        }
        if (HorizontalMove < 0)
        {
            transform.localScale = new Vector2(-1, 1);
        }
        //moveRight = !moveRight;
        //transform.Rotate(Vector2.up * 180);
    }   
    private void FixedUpdate()
    {
        if (gameStarted == true)
        {
            //rigidbody.velocity = new Vector2(speed, rigidbody.velocity.y);

            if (HorizontalMove == 0)
            {
                move = false;
            }
            else
            {
                move = true;
            }
            HorizontalMove = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
            transform.position = new Vector2(transform.position.x + HorizontalMove, transform.position.y);
        }
        if (jump == true)
        {
            rigidbody.AddForce(new Vector2(0f, jumpForce));
            jump = false;
        }
    }    
    public void GameOver()
    {
        AnalyticsResult analyticsResult = Analytics.CustomEvent("GameOver" + 1);
        Debug.Log("analyticsResult" + analyticsResult);
        currentLevel = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentLevel);               
    }
    public void AddCoin(int coin)
    {
        coinCount = coinCount + coin;
        coinText.text = coinCount.ToString();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        grounded = true;
    }
    private void OnTriggerEnter2D (Collider2D other)
    {
        if (other.CompareTag("coin"))
        {
            AnalyticsResult analyticsResult = Analytics.CustomEvent("Pickup Coin" + new Dictionary<string, object> 
            { { "Level", 1 }, { "Position", other.transform.position.x } });
            AddCoin(1);
            AudioSource.PlayClipAtPoint(coinSound, other.transform.position);
            Destroy(other.gameObject);
        }
        if (other.CompareTag("burger"))
        {
            coinCount += 10;
            coinText.text = coinCount.ToString();
            AudioSource.PlayClipAtPoint(burgerSound, other.transform.position);
            Destroy(other.gameObject);
        }
        if (other.CompareTag("water"))
        {
            coinCount += 15;
            coinText.text = coinCount.ToString();
            AudioSource.PlayClipAtPoint(waterSound, other.transform.position);
            Destroy(other.gameObject);
        }
        if (other.CompareTag("death"))
        {
            hp -= 1;            
            transform.position = respawnPoint;
            AnalyticsResult analyticsResult = Analytics.CustomEvent("Fall Down" + 1);
            Debug.Log("analyticsResult" + analyticsResult);
        }
        if (other.CompareTag("checkpoint"))
        {
            respawnPoint = other.transform.position;
        }
    }
}