Created
March 7, 2016 11:43
-
-
Save IronMonk-UK/a3cb8977daf1af3b36f4 to your computer and use it in GitHub Desktop.
The scripts relating to the modular spaceship project created.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class barScale : MonoBehaviour { | |
public stats shipStats; | |
public int maxStat; | |
public string type; | |
void Start () { | |
} | |
void Update () { | |
float xScale = 0; | |
// Resize bar based on stat percentage | |
switch (type) { | |
case "health": | |
xScale = ((float)shipStats.health / (float)maxStat); | |
break; | |
case "speed": | |
xScale = ((float)shipStats.speed / (float)maxStat); | |
break; | |
case "cap": | |
xScale = ((float)shipStats.cap / (float)maxStat); | |
break; | |
case "man": | |
xScale = ((float)shipStats.man / (float)maxStat); | |
break; | |
case "fireRate": | |
xScale = ((float)shipStats.fireRate / (float)maxStat); | |
break; | |
case "regen": | |
xScale = ((float)shipStats.regen / (float)maxStat); | |
break; | |
case "damage": | |
xScale = ((float)shipStats.damage / (float)maxStat); | |
break; | |
} | |
transform.localScale = new Vector3 (xScale ,1,1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class bulletScript : MonoBehaviour { | |
public float speed; | |
public float timer; | |
private Rigidbody bulletBody; | |
void Start () | |
{ | |
bulletBody = GetComponent<Rigidbody> (); | |
} | |
void Update () | |
{ | |
// Keep the bullet moving | |
bulletBody.AddForce (transform.up * speed); | |
timer += Time.deltaTime; | |
// Destroy bullet after 3 seconds | |
if (timer >= 3.0f) | |
{ | |
Destroy (gameObject); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class cockpitScript : MonoBehaviour { | |
public int maneuver; | |
public int fireRate; | |
public int health; | |
void Start () { | |
} | |
void Update () { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class gunScript : MonoBehaviour { | |
public GameObject bullet; | |
public Transform bulletSpawn; | |
public int fireRate; | |
public int damage; | |
private int fireWait; | |
private bool canFire = true; | |
void Update () | |
{ | |
// Keep counting fire rate delay | |
if (!canFire) | |
{ | |
fireWait++; | |
} | |
if (fireWait >= fireRate) | |
{ | |
canFire = true; | |
} | |
} | |
public void Fire () | |
{ | |
// Fire if the fire rate delay permits and mouse button is clicked to call this function | |
if (canFire) { | |
Instantiate (bullet, bulletSpawn.position, transform.rotation); | |
fireWait = 0; | |
canFire = false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class hullScript : MonoBehaviour { | |
public int health; | |
public int speed; | |
public int shieldCap; | |
void Start () | |
{ | |
} | |
void Update () | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class shieldScript : MonoBehaviour { | |
public int shieldCap; | |
public int shieldRegen; | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class shipControl : MonoBehaviour { | |
public GameObject hull; | |
public GameObject wings; | |
public GameObject engine; | |
public GameObject cockpit; | |
public GameObject leftGun; | |
public GameObject rightGun; | |
public GameObject shield; | |
private hullScript hullPart; | |
private wingsScript wingsPart; | |
private engineScript enginePart; | |
private cockpitScript cockpitPart; | |
private gunScript leftGunPart; | |
private gunScript rightGunPart; | |
private shieldScript shieldPart; | |
private Rigidbody rigid; | |
public float thrustAmount; | |
public float torque; | |
public float turn; | |
public float minTurn;// = -0.5f; | |
public float maxTurn;// = 0.5f; | |
public float maxRotate;// = 50; | |
public float minRotate;// = -50; | |
public float maxThrust;// = 25; | |
public float rotate; | |
private int health; | |
private int speed; | |
private int man; | |
private int cap; | |
private int regen; | |
private int maxHealth; | |
public GameObject healthbar; | |
public stats _stats; | |
private KeyCode fire = KeyCode.Mouse0; | |
public void Astart () | |
{ | |
engine = gameObject.GetComponent<menu> ().engine; | |
hull = gameObject.GetComponent<menu> ().hull; | |
wings = gameObject.GetComponent<menu> ().wings; | |
leftGun = gameObject.GetComponent<menu> ().gunLeft; | |
rightGun = gameObject.GetComponent<menu> ().gunRight; | |
cockpit = gameObject.GetComponent<menu> ().cockpit; | |
shield = gameObject.GetComponent<menu> ().shield; | |
enginePart = engine.GetComponent<engineScript>(); | |
hullPart = hull.GetComponent<hullScript>(); | |
wingsPart = wings.GetComponent<wingsScript>(); | |
leftGunPart = leftGun.GetComponent<gunScript>(); | |
rightGunPart = rightGun.GetComponent<gunScript>(); | |
cockpitPart = cockpit.GetComponent<cockpitScript>(); | |
shieldPart = shield.GetComponent<shieldScript>(); | |
health = _stats.health; | |
speed = _stats.speed; | |
man = _stats.man; | |
cap = _stats.cap; | |
regen = _stats.regen; | |
minTurn = man * -12; | |
maxTurn = man * 12; | |
minRotate = man * -12; | |
maxRotate = man * 12; | |
gameObject.GetComponent<stats> ().enabled = false; | |
maxHealth = health; | |
} | |
void Awake () | |
{ | |
rigid = GetComponent<Rigidbody>(); | |
} | |
void FixedUpdate() | |
{ | |
rigid.AddTorque(transform.up * torque * turn); | |
rigid.AddForce(transform.forward * thrustAmount); | |
if (transform.rotation.x <= maxRotate && transform.rotation.x >= minRotate) { | |
rigid.AddTorque(transform.right * torque * rotate); | |
} | |
} | |
void Update () | |
{ | |
Screen.lockCursor = true; | |
// Use physics engine to turn the ship | |
if ( Input.GetAxis("Vertical") >= 1 ) | |
{ | |
thrustAmount = Mathf.Min(thrustAmount + 2.5f, maxThrust); | |
} else { | |
thrustAmount = Mathf.Max(0, thrustAmount - 5.0f); | |
} | |
turn = Input.GetAxis("Horizontal"); | |
if (turn > maxTurn) | |
turn = maxTurn; | |
if (turn < minTurn) | |
turn = minTurn; | |
rotate = Input.GetAxis ("Rotate"); | |
// Fire both guns when mouse is down | |
if (Input.GetKey(fire)) | |
{ | |
leftGunPart.Fire (); | |
rightGunPart.Fire (); | |
} | |
// Game over when no health left | |
if(health == 0) | |
{ | |
// Destroy ship so it doesn't carry over | |
Destroy(gameObject); | |
// Load game over screen | |
Application.LoadLevel("Scene_02_GameOver"); | |
} | |
} | |
void OnCollisionEnter(Collision collision) | |
{ | |
// Reduce health on impact | |
health--; | |
// Update health bar | |
float hpPercent = ((float)health/(float)maxHealth)*1.64f; | |
Vector3 newScale = new Vector3(hpPercent, 0.48f, 1f); | |
healthbar.transform.localScale = newScale; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class stats : MonoBehaviour { | |
public GameObject hull; | |
public GameObject wings; | |
public GameObject engine; | |
public GameObject cockpit; | |
public GameObject leftGun; | |
public GameObject rightGun; | |
public GameObject shield; | |
private hullScript hullPart; | |
private wingsScript wingsPart; | |
private engineScript enginePart; | |
private cockpitScript cockpitPart; | |
private gunScript leftGunPart; | |
private gunScript rightGunPart; | |
private shieldScript shieldPart; | |
public int health; | |
public int speed; | |
public int man; | |
public int cap; | |
public int regen; | |
public int fireRate; | |
public int damage; | |
public Text healthText; | |
public Text speedText; | |
public Text manText; | |
public Text capText; | |
public Text regenText; | |
public Text fireText; | |
public Text damText; | |
void Start () { | |
} | |
void Update () | |
{ | |
engine = gameObject.GetComponent<menu> ().engine; | |
hull = gameObject.GetComponent<menu> ().hull; | |
wings = gameObject.GetComponent<menu> ().wings; | |
leftGun = gameObject.GetComponent<menu> ().gunLeft; | |
rightGun = gameObject.GetComponent<menu> ().gunRight; | |
cockpit = gameObject.GetComponent<menu> ().cockpit; | |
shield = gameObject.GetComponent<menu> ().shield; | |
enginePart = engine.GetComponent<engineScript>(); | |
hullPart = hull.GetComponent<hullScript>(); | |
wingsPart = wings.GetComponent<wingsScript>(); | |
leftGunPart = leftGun.GetComponent<gunScript>(); | |
rightGunPart = rightGun.GetComponent<gunScript>(); | |
cockpitPart = cockpit.GetComponent<cockpitScript>(); | |
shieldPart = shield.GetComponent<shieldScript>(); | |
// Calculate player stats | |
health = hullPart.health + enginePart.health + cockpitPart.health; | |
speed = hullPart.speed + wingsPart.speed + enginePart.speed; | |
man = wingsPart.maneuver + cockpitPart.maneuver; | |
cap = hullPart.shieldCap + shieldPart.shieldCap; | |
regen = enginePart.shieldRegen + shieldPart.shieldRegen; | |
fireRate = leftGunPart.fireRate + rightGunPart.fireRate; | |
damage = leftGunPart.damage + rightGunPart.damage; | |
// Update text display | |
healthText.text = health.ToString(); | |
speedText.text = speed.ToString (); | |
manText.text = man.ToString (); | |
capText.text = cap.ToString (); | |
regenText.text = regen.ToString (); | |
fireText.text = fireRate.ToString (); | |
damText.text = damage.ToString (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class turretDetection : MonoBehaviour { | |
public turretScript _turret; | |
void Start () { | |
} | |
void Update () { | |
} | |
void OnTriggerEnter(Collider target) | |
{ | |
if (target.tag == "Player") | |
{ | |
_turret.seen = true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class turretScript : MonoBehaviour { | |
public GameObject _base; | |
public GameObject _swivel; | |
public GameObject _gun; | |
private GameObject _ship; | |
public GameObject _detection; | |
public bool seen; | |
private int health = 10; | |
void Start () | |
{ | |
_ship = GameObject.FindGameObjectWithTag ("Player"); | |
} | |
void Update () | |
{ | |
if (health == 0) | |
{ | |
Destroy(gameObject); | |
} | |
} | |
void OnCollisionEnter(Collision target) | |
{ | |
if (target.gameObject.tag == "Bullet") | |
{ | |
health--; | |
Debug.Log (health); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class wingsScript : MonoBehaviour { | |
public int maneuver; | |
public int speed; | |
void Start () { | |
} | |
void Update () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment