2024-03-13 17:46:25 -05:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class Hero : MonoBehaviour
|
|
|
|
{
|
|
|
|
static public Hero S;
|
|
|
|
|
|
|
|
[Header("Set in Inspector")]
|
|
|
|
// These fields control the movement of the ship
|
|
|
|
public float speed = 30;
|
|
|
|
public float rollMult = -45;
|
|
|
|
public float pitchMult = 30;
|
2024-03-13 20:01:39 -05:00
|
|
|
public float gameRestartDelay = 2f;
|
|
|
|
public GameObject projectilePrefab;
|
|
|
|
public float projectileSpeed = 40;
|
2024-03-14 19:23:52 -05:00
|
|
|
public float firingDelay = 0.02f;
|
2024-03-13 17:46:25 -05:00
|
|
|
|
|
|
|
[Header("Set Dynamically")]
|
2024-03-13 20:01:39 -05:00
|
|
|
public float _shieldLevel = 1;
|
|
|
|
// This variables holds a reference to the last triggering GameObject
|
|
|
|
private GameObject lastTriggerGo = null;
|
2024-03-14 19:23:52 -05:00
|
|
|
private bool isFiring = false;
|
|
|
|
private float lastFiringTime = 0;
|
2024-03-13 17:46:25 -05:00
|
|
|
|
|
|
|
void Awake() {
|
|
|
|
if (S == null) {
|
|
|
|
S = this;
|
|
|
|
} else {
|
|
|
|
Debug.LogError("Hero.Awake() - Attempted to assign second Hero.S!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
// Pull in information from the Input class
|
|
|
|
float xAxis = Input.GetAxis("Horizontal");
|
|
|
|
float yAxis = Input.GetAxis("Vertical");
|
|
|
|
|
|
|
|
// Change transform.position based on the axes
|
|
|
|
Vector3 pos = transform.position;
|
|
|
|
pos.x += xAxis * speed * Time.deltaTime;
|
|
|
|
pos.y += yAxis * speed * Time.deltaTime;
|
|
|
|
transform.position = pos;
|
|
|
|
|
|
|
|
// Rotate the ship to make it feel more dynamic
|
|
|
|
transform.rotation = Quaternion.Euler(yAxis*pitchMult,xAxis*rollMult,0);
|
2024-03-13 20:01:39 -05:00
|
|
|
// Allow the ship to fire
|
|
|
|
if (Input.GetKeyDown(KeyCode.Space)) {
|
2024-03-14 19:23:52 -05:00
|
|
|
isFiring = true;
|
|
|
|
}
|
|
|
|
lastFiringTime += Time.deltaTime;
|
|
|
|
if (isFiring && (lastFiringTime > firingDelay)) {
|
2024-03-13 20:01:39 -05:00
|
|
|
TempFire();
|
2024-03-14 19:23:52 -05:00
|
|
|
lastFiringTime = 0;
|
|
|
|
}
|
|
|
|
if (Input.GetKeyUp(KeyCode.Space)) {
|
|
|
|
isFiring = false;
|
2024-03-13 20:01:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TempFire() {
|
|
|
|
GameObject projGO = Instantiate<GameObject>(projectilePrefab);
|
|
|
|
projGO.transform.position = transform.position;
|
|
|
|
Rigidbody rigidB = projGO.GetComponent<Rigidbody>();
|
|
|
|
rigidB.velocity = Vector3.up * projectileSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTriggerEnter(Collider other) {
|
|
|
|
Transform rootT = other.gameObject.transform.root;
|
|
|
|
GameObject go = rootT.gameObject;
|
|
|
|
//print("Triggered: " + go.name);
|
|
|
|
// Make sure it's not the same triggering go as last time
|
|
|
|
if (go == lastTriggerGo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastTriggerGo = go;
|
|
|
|
if (go.tag == "Enemy") {
|
|
|
|
shieldLevel--;
|
|
|
|
Destroy(go);
|
|
|
|
} else {
|
|
|
|
print("Triggered by non-Enemy: " + go.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public float shieldLevel {
|
|
|
|
get {
|
|
|
|
return(_shieldLevel);
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
_shieldLevel = Mathf.Min(value, 4);
|
|
|
|
// If the shield is going to be set to less than zero
|
|
|
|
if (value < 0) {
|
|
|
|
Destroy(this.gameObject);
|
|
|
|
// Tell Main.S to restart the game after a delay
|
|
|
|
Main.S.DelayedRestart(gameRestartDelay);
|
|
|
|
}
|
|
|
|
}
|
2024-03-13 17:46:25 -05:00
|
|
|
}
|
|
|
|
}
|