Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Character : Unit
- {
- [SerializeField]
- private float speed = 3.0F;
- [SerializeField]
- private int lives = 3;
- public int Lives
- {
- get { return lives; }
- set
- {
- if (value < 3) lives = value;
- livesBar.Refresh();
- }
- }
- private Lives livesBar;
- [SerializeField]
- private float jumpForce = 15.0F;
- [SerializeField]
- private int score = 0;
- [SerializeField]
- public AudioClip CoinSound;
- new private Rigidbody2D rigidbody;
- private Animator animator;
- private SpriteRenderer sprite;
- public bool canMove;
- public bool isGrounded = false;
- public Transform groundCheck;
- private float groundRadius = 0.2F;
- public LayerMask whatIsGround;
- private CharState State
- {
- get { return (CharState)animator.GetInteger("State"); }
- set { animator.SetInteger("State", (int)value); }
- }
- private void Awake()
- {
- livesBar = FindObjectOfType<Lives>();
- rigidbody = GetComponent<Rigidbody2D>();
- animator = GetComponent<Animator>();
- sprite = GetComponent<SpriteRenderer>();
- }
- private void FixedUpdate()
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
- if (!isGrounded)
- return;
- }
- private void Update()
- {
- if (!canMove)
- return;
- State = CharState.idle;
- if (Input.GetButton("Horizontal")) Run();
- if (isGrounded && Input.GetButtonDown("Jump"))
- Jump();
- if (Input.GetButton("Fire1"))
- Attack();
- if (lives <= 0)
- SceneManager.LoadScene("level");
- }
- private void Run()
- {
- Vector3 direction = transform.right * Input.GetAxis("Horizontal");
- transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
- sprite.flipX = direction.x < 0.0F;
- State = CharState.walk;
- }
- private void Jump()
- {
- State = CharState.jump;
- rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
- }
- private void Attack()
- {
- State = CharState.attack;
- }
- public override void ReceiveDamage()
- {
- if (State != CharState.attack)
- Lives--;
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.tag == "Finish")
- SceneManager.LoadScene("start_level");
- if (collision.transform.tag == "MovingPlatform")
- {
- transform.parent = collision.transform;
- }
- }
- void OnCollisionExit2D(Collision2D collision)
- {
- if (collision.transform.tag == "Ground")
- {
- transform.parent = collision.transform;
- }
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Coin")
- {
- score++;
- AudioSource.PlayClipAtPoint(CoinSound, transform.position);
- Destroy(collision.gameObject);
- }
- if (collision.tag == "Gem")
- {
- score += 10;
- AudioSource.PlayClipAtPoint(CoinSound, transform.position);
- Destroy(collision.gameObject);
- }
- if (collision.tag == "DiePlace")
- Lives--; }
- void OnGUI()
- {
- GUI.Box(new Rect(120, 0, 100, 30), "Score: " + score);
- }
- }
- public enum CharState
- {
- idle,
- walk,
- jump,
- attack
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement