Skip to content
Permalink
594d3a66de
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
221 lines (185 sloc) 4.92 KB
using UnityEngine;
using System.Collections;
public class Hero: MonoBehaviour
{
#region Fields
public float HeroMoveSpeed;
public int Health;
public int Score;
public GameObject Hero_Attack_03_Prefab;
public GameObject Hero_Attack_02_Prefab;
public GameObject Hero_Attack_01_Prefab;
public Vector3 Face;
private float lastX;
private float lastZ;
//stuff for blinks
private float blinkRate = 0.1f;
private int numberOfTimesToBlink = 10;
private int blinkCount = 0;
//for cooldown
private float coolDownPeriodInSeconds = 0.4f;
private float timeStamp;
//C controller test
public float speed = 6.0F;
private Vector3 moveDirection = Vector3.zero;
enum State
{
Playing,
Invincible,
Dead
}
private State state;
enum AttackState
{
Attack01 = 1,
Attack02 = 2,
Attack03 = 3
}
private AttackState attack = AttackState.Attack01;
#endregion
// Use this for initialization
void Start () {
state = State.Playing;
timeStamp = Time.time + coolDownPeriodInSeconds;
Screen.lockCursor = true;
Screen.showCursor = false;
}
// Update is called once per frame\
/*
void FixedUpdate()
{
rigidbody.MovePosition(rigidbody.position + Vector3.forward * Time.deltaTime);
}
*/
void Update ()
{
// if(Input.GetKey(KeyCode.Escape))
// Screen.lockCursor = false;
// else
// Screen.lockCursor = true;
if (state != State.Dead)
{
#region PlayerMovement
//moving control
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
controller.Move(moveDirection * Time.deltaTime);
if(transform.position.y != 0)
transform.Translate(0, -transform.position.y, 0);
Face = new Vector3(lastX,0,lastZ);
//Face the hero to the direction he's moving
// transform.LookAt(transform.position + Face);//not using this atm
//transform.LookAt(Forward);
#endregion
#region PlayerAttacks
if (Input.GetAxisRaw("Horizontal") != 0 && lastX != Input.GetAxisRaw("Horizontal"))
{
lastZ = 0;
lastX = Input.GetAxisRaw("Horizontal");
}
if (Input.GetAxisRaw("Vertical") != 0 && lastZ != Input.GetAxisRaw("Vertical"))
{
lastX = 0;
lastZ = Input.GetAxisRaw("Vertical");
}
if(Input.GetKeyDown("1"))
{
attack = AttackState.Attack01;
}
if(Input.GetKeyDown("2"))
{
attack = AttackState.Attack02;
}
if(Input.GetKeyDown("3"))
{
//Vector3 position = new Vector3(transform.position.x ,0 , transform.position.z);
attack = AttackState.Attack03;
}
if(Input.GetKeyDown("4"))
//if(Input.GetMouseButtonDown)
{
attackFunc();
}
}
#endregion
}
void attackFunc()
{
if (timeStamp <= Time.time)
{
if(attack == AttackState.Attack01)
{
Vector3 positionAttack01 = new Vector3(transform.position.x + lastX * 2 ,0, transform.position.z + lastZ * 2);
Instantiate(Hero_Attack_01_Prefab , positionAttack01 , Quaternion.identity);
}
if (attack == AttackState.Attack02)
{
Vector3 positionAttack02 = new Vector3(transform.position.x + lastX * 2 ,0, transform.position.z + lastZ * 2);
Instantiate(Hero_Attack_02_Prefab , positionAttack02 , Quaternion.identity);
}
if(attack == AttackState.Attack03)
{
Vector3 position = new Vector3(transform.position.x ,0 , transform.position.z);
Instantiate(Hero_Attack_03_Prefab , position , Quaternion.identity);
}
timeStamp = Time.time + coolDownPeriodInSeconds;
}
//else
// Debug.Log("something is wrong");
//return 0;
}
/*void OnTriggerEnter(Collider otherObject)
{
//Debug.Log ("got hit");
if (otherObject.tag == "Enemy" && state == State.Playing)
{
StartCoroutine(gotHitCoroutine());
}
}
*/
//when gothit, become invincible and blink for a short while
IEnumerator gotHitCoroutine()
{
state = State.Invincible;
gameObject.renderer.enabled = false;
while (blinkCount < numberOfTimesToBlink)
{
gameObject.renderer.enabled = !gameObject.renderer.enabled;
if(gameObject.renderer.enabled == true)
blinkCount++;
yield return new WaitForSeconds(blinkRate);
}
blinkCount = 0;
state = State.Playing;
}
//called by enemy collide or enemy ammo unit to lose health and start invincible-blink coroutin
void heroGotHit(int dmg)
{
if(state == State.Playing)
{
this.Health -= dmg;
if(this.Health > 0)
StartCoroutine(gotHitCoroutine());
else state = State.Dead;
}
}
//add score, called by joystone
void heroGotCoin(int Coin)
{
Score += Coin;
}
#region simpleUI
void OnGUI()
{
BuildUI();
}
void BuildUI()
{
GUI.Label(new Rect(10, 10, 120, 20), "Score: " + Score);
GUI.Label(new Rect(10, 30, 60, 20), "Lives: " + Health);
GUI.Label(new Rect(10, 50, 180, 30), "Form: Attack" + attack);
}
#endregion
}