Skip to content
Permalink
d3a22fb2e6
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
271 lines (231 sloc) 6.29 KB
using UnityEngine;
using System.Collections;
public class FPS_HERO: MonoBehaviour
{
#region Fields
public int Health;
public int Score;
//public int victoryScore = 15;
public GameObject Hero_Attack_03_Prefab;
public GameObject Hero_Attack_02_Prefab;
public GameObject Hero_Attack_01_Prefab;
//private Rigidbody clone;
#endregion
#region BlinkLegacy
//stuff for blinks
private float blinkRate = 0.1f;
private int numberOfTimesToBlink = 10;
private int blinkCount = 0;
#endregion
//for cooldown
private float coolDownPeriodInSeconds = 0.4f;
private float timeStamp;
private GameObject[] heroRef;
enum State
{
Playing,
Invincible,
Dead
}
private State state;
enum AttackState
{
Attack01 = 1,
Attack02 = 2,
Attack03 = 3
}
private AttackState attack = AttackState.Attack01;
// Use this for initialization
void Start ()
{
state = State.Playing;
timeStamp = Time.time + coolDownPeriodInSeconds;
//heroRef = GameObject.FindGameObjectsWithTag("Player");
}
void Update ()
{
if (state != State.Dead)
{
if(transform.position.y != 0)
transform.Translate(0, -transform.position.y, 0);
#region PlayerMovementLegacy
/*
//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 PlayerAttacksLegacy
/*
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
}
#region PlayerAttacks
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(0))
{
attackFunc();
}
#endregion
}
void attackFunc()
{
if (timeStamp <= Time.time)
{
Vector3 positionAttack = transform.position + this.gameObject.transform.forward * 3;
if(attack == AttackState.Attack01)
{
GameObject cloneMeele = Instantiate(Hero_Attack_01_Prefab , positionAttack , Quaternion.identity) as GameObject;
cloneMeele.transform.parent = transform;
//heroRef.transform;
//Hero_Attack_01_Prefab.rigidbody.AddForce(transform.forward * 400);
}
if (attack == AttackState.Attack02)
{
GameObject clone = Instantiate(Hero_Attack_02_Prefab , positionAttack , Quaternion.identity) as GameObject;
//clone.velocity = transform.TransformDirection(transform.forward * 10);
//clone.AddForce(transform.forward * 4000);
//clone.transform.Translate(transform.forward * 400 * Time.deltaTime);
clone.rigidbody.AddForce(transform.forward * 1000);
}
if(attack == AttackState.Attack03)
{
Instantiate(Hero_Attack_03_Prefab , positionAttack , Quaternion.identity);
//Hero_Attack_03_Prefab.rigidbody.AddForce(transform.forward * 400);
}
timeStamp = Time.time + coolDownPeriodInSeconds;
}
}
#region PlayerAttackFuncLegacy
/*
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);
Vector3 positionAttack01 = new Vector3(0,0, 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;
}
*/
#endregion
#region gotHitCoroutineLegacy
/*
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;
}
*/
#endregion
IEnumerator gotHitCoroutine()
{
state = State.Invincible;
yield return new WaitForSeconds(1);
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
}