using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
using UnityEngine.SceneManagement;
using System;
public class Character : Unit
{
[SerializeField]
private float speed = 3.0F;
[SerializeField]
private int lives = 5;
[SerializeField]
private int fallController = -10;
private int coins = 0;
public int Lives
{
get { return lives; }
set
{
if (value < 5) lives = value;
livesBar.Refresh ();
}
}
public int Coins
{
get { return coins; }
}
private LivesBar livesBar;
[SerializeField]
private float jumpForce = 15.0F;
private bool isGrounded = false;
private Bullet bullet;
private CharState State
{
get { return (CharState)animator.GetInteger ("State"); }
set { animator.SetInteger("State", (int) value); }
}
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
livesBar = FindObjectOfType<LivesBar> ();
rigidbody = GetComponent<Rigidbody2D> ();
animator = GetComponent<Animator> ();
sprite = GetComponentInChildren<SpriteRenderer> ();
bullet = Resources.Load<Bullet> ("Bullet");
}
private void FixedUpdate()
{
CheckGround ();
}
private void Update()
{
if (isGrounded) State = CharState.Idle;
if (Input.GetButtonDown ("Fire1")) Shoot ();
if (Input.GetButton ("Horizontal")) Run ();
if (isGrounded && Input.GetButtonDown ("Jump")) Jump ();
if (gameObject.transform.position.y < fallController)
{
Debug.Log("Player Dead");
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}
}
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;
if (isGrounded) State = CharState.Run;
}
private void Jump()
{
rigidbody.AddForce (transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot()
{
Vector3 position = transform.position; position.y += 0.8F;
Bullet newBullet = Instantiate (bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * (sprite.flipX ? -1.0F : 1.0F);
}
public override void ReceiveDamage()
{
Lives--;
if (lives == 0)
{
ReloadScene();
}
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 12.0F, ForceMode2D.Impulse);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll (transform.position, 0.3F);
isGrounded = colliders.Length > 1;
if (!isGrounded) State = CharState.Jump;
}
private void OnTriggerEnter2D(Collider2D collider)
{
Bullet bullet = collider.gameObject.GetComponent<Bullet> ();
if (bullet && bullet.Parent != gameObject)
{
ReceiveDamage ();
}
}
private void ReloadScene()
{
Thread.Sleep (500);
SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
}
}
public enum CharState
{
Idle,
Run,
Jump
}