Skip to content

Instantly share code, notes, and snippets.

@jquave
Created April 6, 2014 02:06
Show Gist options
  • Select an option

  • Save jquave/10000630 to your computer and use it in GitHub Desktop.

Select an option

Save jquave/10000630 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public int metal = 200;
public int hp = 100;
QInput input;
public GameObject sentryPrefab;
float groundDist = 0.62f;
float repairDist = 0.15f;
public GUIDisplay gui;
public int enemiesKilled = 0;
public int totalMetalCollected = 0;
public string nextLevelName = "Level2";
int maxhp = 100;
int maxMetal = 400;
public AudioClip buildClip;
public AudioClip endLevelClip;
public GameObject pickupTextPrefab;
public Texture2D hpTexture;
public GameObject visTool;
bool canBuild = true;
public bool hurtEnabled = true;
void Start() {
input = FindObjectOfType(typeof(QInput)) as QInput;
FullHeal();
Physics2D.IgnoreLayerCollision(9,9); // Enemies ignore each other in phys
}
void NukeEnemies(bool givePoints) {
// Destroy all enemies
GameObject []enemyGOs = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemyGO in enemyGOs) {
Enemy enemy = enemyGO.GetComponent<Enemy>() as Enemy;
if(enemy!=null) {
if(givePoints)
enemy.Hurt(9999);
else
GameObject.Destroy(enemy.gameObject);
}
}
}
IEnumerator NextLevel() {
yield return new WaitForSeconds(5);
Application.LoadLevel(nextLevelName);
yield return null;
}
public void WinLevel(LevelPickup levelPickup) {
// Destroy all enemies
NukeEnemies(true);
audio.PlayOneShot(endLevelClip);
// Stop spawners
EnemySpawner []spawners = GameObject.FindObjectsOfType(typeof(EnemySpawner)) as EnemySpawner[];
foreach(EnemySpawner spawner in spawners) {
spawner.Deactivate();
}
// Destroy all metal..... nahhh..
StartCoroutine(NextLevel());
gui.guiState = GUIState.WIN;
}
public bool IsGrounded() {
int layerMask = ~(1 << 8);
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(0,-1), groundDist,layerMask);
if(hit!=null && hit.collider!=null) {
if(hit.collider.CompareTag("Ground")) {
// Hit ground
return true;
}
}
return false;
}
public void FullHeal() {
metal = maxMetal;
hp = maxhp;
}
bool hurtMetalEnabled = true;
public void HurtMetal(int amount) {
if(!hurtMetalEnabled) return;
metal -= amount;
hurtMetalEnabled = false;
StartCoroutine(EnableHurtMetalAfterDelay(2));
}
public void Die() {
// Destroy all enemies
NukeEnemies(false);
// Stop spawners
EnemySpawner []spawners = GameObject.FindObjectsOfType(typeof(EnemySpawner)) as EnemySpawner[];
foreach(EnemySpawner spawner in spawners) {
spawner.Deactivate();
}
// Destroy all metal..... nahhh..
//StartCoroutine(NextLevel());
StartCoroutine(ReloadLevelAfterDelay(3));
gui.guiState = GUIState.DIE;
}
IEnumerator ReloadLevelAfterDelay(float secs) {
yield return new WaitForSeconds(secs);
Application.LoadLevel(Application.loadedLevelName);
yield return null;
}
PickupText CreatePickupText(string txt) {
GameObject pickupTextInst = GameObject.Instantiate(pickupTextPrefab, transform.position, Quaternion.identity) as GameObject;
PickupText pickupText = pickupTextInst.GetComponent<PickupText>() as PickupText;
//pickupText.textMesh.color = Color.red;
pickupText.PickupWithText(txt);
return pickupText;
}
public void Hurt(int amount) {
//rigidbody2D.velocity = new Vector2(0,rigidbody2D.velocity.y);
//rigidbody2D.velocity = new Vector2(Random.Range(0,50)-25,10);
//rigidbody2D.velocity = Vector2.zero;
if(!hurtEnabled) return;
iTween.ShakeRotation(Camera.main.gameObject, Vector3.one, 0.25f);
int amountToHurt = Mathf.Min(amount, hp);
PickupText t = CreatePickupText("-"+amountToHurt+" HP");
t.textMesh.color = Color.red;
hp -= amountToHurt;
if(hp<=0) {
hp = 0;
Die();
}
hurtEnabled = false;
StartCoroutine(EnableHurtAfterDelay(2));
}
bool CanBuild(int cost) {
return ( (metal>=cost) && IsGrounded() && canBuild );
}
float nextCanShowNotEnoughMetal = 0;
void NotEnoughMetalMessage() {
if(Time.time<nextCanShowNotEnoughMetal) return;
nextCanShowNotEnoughMetal = Time.time + 0.35f;
PickupText t = CreatePickupText("Not Enough Metal");
t.textMesh.color = Color.yellow;
}
public void BuyNukeEnemies() {
int cost = 400;
if(metal>=400) {
PickupText t = CreatePickupText("-"+cost+" Metal");
t.textMesh.color = Color.yellow;
NukeEnemies(true);
metal -= cost;
}
else {
NotEnoughMetalMessage();
}
}
public void BuildSentry(SentryType sentryType) {
int cost = 50;
if(sentryType==SentryType.SLOW) cost = 100;
else if(sentryType==SentryType.EXPLOSIVE) cost = 150;
if(!CanBuild(cost)) {
//NotEnoughMetalMessage();
if( (metal<cost) ) {
NotEnoughMetalMessage();
//PickupText p = CreatePickupText("Not enough metal");
}
else if(!canBuild) {
//PickupText p = CreatePickupText("Slow down, tiger...");
}
return;
}
Vector3 sentryPos = transform.position + 0.1f*Vector3.forward;
// snap to 1.18 increments
float leftHandSnapPos = sentryPos.x - (sentryPos.x % 1.18f);
float rightHandSnapPos = leftHandSnapPos + 1.18f*2;
float dxml = Mathf.Abs( sentryPos.x - leftHandSnapPos);
float dxmr = Mathf.Abs( sentryPos.x - rightHandSnapPos);
if(dxml>dxmr) {
// Closer to right-hand side
sentryPos.x = rightHandSnapPos;
}
else {
// Closer to left-hand side
sentryPos.x = leftHandSnapPos;
}
//sentryPos.x = sentryPos.x - (sentryPos.x % 1.18f);
sentryPos.y += 0.3f;
// Check to make sure this doesn't overalp an existing sentry
RaycastHit2D []hits = Physics2D.RaycastAll(sentryPos - Vector3.forward, Vector3.forward);
foreach(RaycastHit2D hit in hits) {
if(hit.collider.CompareTag("Gun")) {
PickupText p = CreatePickupText("Can't build here.");
visTool.transform.position = sentryPos;
p.textMesh.color = Color.red;
return;
}
}
// A sentry is going to ACTUALLY BE BUILT!!!
canBuild = false;
GameObject sentryGO = GameObject.Instantiate(sentryPrefab, sentryPos, Quaternion.identity) as GameObject;
sentryGO.transform.localScale = transform.localScale;
Sentry newSentry = sentryGO.GetComponent<Sentry>() as Sentry;
newSentry.sentryType = sentryType;
metal -= cost;
audio.PlayOneShot(buildClip);
PickupText t = CreatePickupText("-"+cost+" Metal");
t.textMesh.color = Color.yellow;
newSentry.EnableSentry();
StartCoroutine(EnableBuildingAfterDelay(0.4f));
DidBuildSentry();
}
void DidBuildSentry() {
NotificationCenter.DefaultCenter.PostNotification(this, Constants.NC_DID_BUILD_SENTRTY);
}
IEnumerator EnableHurtMetalAfterDelay(float secs) {
yield return new WaitForSeconds(secs);
hurtMetalEnabled = true;
yield return null;
}
IEnumerator EnableHurtAfterDelay(float secs) {
yield return new WaitForSeconds(secs);
hurtEnabled = true;
yield return null;
}
IEnumerator EnableBuildingAfterDelay(float secs) {
yield return new WaitForSeconds(secs);
canBuild = true;
yield return null;
}
/****/
// This is just for the HP Bar
/***/
GUIStyle style = new GUIStyle();
//Texture2D tex = new Texture2D(1,1);
float yadj=0.08f;
float xadj=0.01f;
bool didInitGUI = false;
void OnGUI() {
if(!didInitGUI) {
// Init
didInitGUI = true;
style.normal.background = hpTexture;
}
Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position);
//Camera.main.WorldToScreenPoint
float fullBarWidth = Screen.width * 0.02f;
float fullBarHeight = fullBarWidth * 0.15f;
float xpos = screenPoint.x - renderer.bounds.size.x - Screen.width*xadj;
float ypos = Screen.height - screenPoint.y - Screen.height*yadj;
// Background part
GUI.Box(new Rect(xpos, ypos, fullBarWidth,fullBarHeight), "");
// Green part
GUI.Box(new Rect(xpos, ypos, fullBarWidth * hp / 100.0f,fullBarHeight), "",style);
}
void PerformRepairs() {
Collider2D []colliders = Physics2D.OverlapCircleAll(transform.position,repairDist);
foreach(Collider2D c in colliders) {
Sentry sentry = c.GetComponent<Sentry>() as Sentry;
if(sentry!=null && sentry.CanRepair()) {
int metalCost = sentry.level * 150;
if(metal>=metalCost) {
metal -= metalCost;
PickupText t = CreatePickupText("-"+metalCost+" Metal");
t.textMesh.color = Color.yellow;
sentry.Repair();
}
else {
NotEnoughMetalMessage();
}
}
}
}
public void DidKillEnemy() {
enemiesKilled++;
}
void Update() {
//if(input.buildPressed) {
//showBuildMenu = true;
//BuildSentry();
//gui.ShowBuildMenu();
//}
if(input.repairPressed) {
PerformRepairs();
}
if(input.buildPressed) {
gui.ToggleBuild();
}
}
void OnTriggerEnter2D(Collider2D c) {
Triggerable triggerableComponent = c.GetComponent<Triggerable>() as Triggerable;
if(triggerableComponent!=null) {
triggerableComponent.Trigger(this);
}
}
// Returns the amount actually picked up
public int CollectScrap(int amount) {
int amountPickedUp = Mathf.Min((maxMetal - metal),amount);
metal += amountPickedUp;
return amountPickedUp;
}
// Returns the amount actually picked up
public int CollectHealth(int amount) {
int amountPickedUp = Mathf.Min((maxhp - hp),amount);
hp += amountPickedUp;
return amountPickedUp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment