Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3.  
  4. public class Character : Unit
  5. {
  6.     [SerializeField]
  7.     private float speed = 3.0F;
  8.  
  9.     [SerializeField]
  10.     private int lives = 3;
  11.  
  12.     public int Lives
  13.     {
  14.         get { return lives; }
  15.         set
  16.         {
  17.             if (value < 3) lives = value;
  18.             livesBar.Refresh();
  19.         }
  20.     }
  21.     private Lives livesBar;
  22.  
  23.     [SerializeField]
  24.     private float jumpForce = 15.0F;
  25.  
  26.     [SerializeField]
  27.     private int score = 0;
  28.  
  29.     [SerializeField]
  30.     public AudioClip CoinSound;
  31.  
  32.     new private Rigidbody2D rigidbody;
  33.     private Animator animator;
  34.     private SpriteRenderer sprite;
  35.  
  36.     public bool canMove;
  37.     public bool isGrounded = false;
  38.  
  39.     public Transform groundCheck;
  40.     private float groundRadius = 0.2F;
  41.     public LayerMask whatIsGround;
  42.  
  43.     private CharState State
  44.     {
  45.         get { return (CharState)animator.GetInteger("State"); }
  46.         set { animator.SetInteger("State", (int)value); }
  47.     }
  48.  
  49.     private void Awake()
  50.     {
  51.         livesBar = FindObjectOfType<Lives>();
  52.         rigidbody = GetComponent<Rigidbody2D>();
  53.         animator = GetComponent<Animator>();
  54.         sprite = GetComponent<SpriteRenderer>();
  55.     }
  56.  
  57.     private void FixedUpdate()
  58.     {
  59.         isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
  60.         if (!isGrounded)
  61.             return;
  62.     }
  63.  
  64.     private void Update()
  65.     {
  66.         if (!canMove)
  67.             return;
  68.  
  69.         State = CharState.idle;
  70.  
  71.         if (Input.GetButton("Horizontal")) Run();
  72.         if (isGrounded && Input.GetButtonDown("Jump"))
  73.             Jump();
  74.         if (Input.GetButton("Fire1"))
  75.             Attack();
  76.  
  77.         if (lives <= 0)
  78.             SceneManager.LoadScene("level");
  79.     }
  80.  
  81.     private void Run()
  82.     {
  83.         Vector3 direction = transform.right * Input.GetAxis("Horizontal");
  84.         transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
  85.  
  86.         sprite.flipX = direction.x < 0.0F;
  87.         State = CharState.walk;
  88.     }
  89.  
  90.     private void Jump()
  91.     {
  92.         State = CharState.jump;
  93.         rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
  94.     }
  95.  
  96.     private void Attack()
  97.     {
  98.         State = CharState.attack;
  99.     }
  100.  
  101.     public override void ReceiveDamage()
  102.     {
  103.         if (State != CharState.attack)
  104.             Lives--;
  105.     }
  106.  
  107.     private void OnCollisionEnter2D(Collision2D collision)
  108.     {
  109.         if (collision.gameObject.tag == "Finish")
  110.             SceneManager.LoadScene("start_level");
  111.  
  112.         if (collision.transform.tag == "MovingPlatform")
  113.         {
  114.             transform.parent = collision.transform;
  115.         }
  116.     }
  117.  
  118.     void OnCollisionExit2D(Collision2D collision)
  119.     {
  120.         if (collision.transform.tag == "Ground")
  121.         {
  122.             transform.parent = collision.transform;
  123.         }
  124.     }
  125.  
  126.     private void OnTriggerEnter2D(Collider2D collision)
  127.     {
  128.         if (collision.tag == "Coin")
  129.         {
  130.             score++;
  131.             AudioSource.PlayClipAtPoint(CoinSound, transform.position);
  132.             Destroy(collision.gameObject);
  133.         }
  134.  
  135.         if (collision.tag == "Gem")
  136.         {
  137.             score += 10;
  138.             AudioSource.PlayClipAtPoint(CoinSound, transform.position);
  139.             Destroy(collision.gameObject);
  140.         }
  141.  
  142.         if (collision.tag == "DiePlace")
  143.             Lives--;    }
  144.  
  145.     void OnGUI()
  146.     {
  147.         GUI.Box(new Rect(120, 0, 100, 30), "Score: " + score);
  148.     }
  149. }
  150.  
  151. public enum CharState
  152. {
  153.     idle,
  154.     walk,
  155.     jump,
  156.     attack
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement