stretch_silo/Assets/Monolith.cs
2020-09-26 06:45:08 -07:00

153 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Monolith : MonoBehaviour
{
public Rig rig = new Rig();
public Render render = new Render();
[Header("References")]
public Camera headsetCam;
void Start()
{
rig.Start(this);
render.Start(this);
for (int i = 0; i < landChunks.Length; i++)
{
landChunks[i] = Quaternion.Euler(
Random.Range(-90f, 90f),
Random.Range(-90f, 90f), 0
);
}
for (int i = 0; i < missiles.Length; i++)
{
missiles[i] = new Missile();
}
}
public float radius = 10;
public float playerOrbitHeight = 2;
public float orbitSpeed = 1;
public Vector3 globePos;
public void Orbit(ref Quaternion quat)
{
quat = Quaternion.Euler(-Vector3.right * Time.deltaTime / orbitSpeed) * quat;
}
public Quaternion[] landChunks = new Quaternion[12];
void Update()
{
rig.Update();
globePos = Vector3.forward * (radius + playerOrbitHeight);
for (int i = 0; i < landChunks.Length; i++)
{
Quaternion landChunk = landChunks[i];
Vector3 pos = globePos + landChunk * Vector3.forward * radius;
// scroll spherically
if (pos.y < 0 && pos.z > globePos.z)
{
landChunk = Quaternion.Euler(
Random.value * -60f,
Random.Range(-90f, 90f), 0
);
}
else
{
Orbit(ref landChunk);
}
landChunks[i] = landChunk;
}
for (int i = 0; i < missiles.Length; i++)
{
Missile missile = missiles[i];
if (missile.active)
{
if (rig.mainConTrigger.onPress) // before updating missiles (extra frame to live)
{
if (Vector3.Distance(rig.cursor, missile.pos) < 1)
{
missile.active = false;
}
}
missile.Update(this);
}
else
{
if (Random.value < Time.deltaTime)
{
missile.Launch();
}
}
}
render.Update();
}
public Missile[] missiles = new Missile[6];
}
public class Missile
{
public bool active;
public Quaternion from;
public Quaternion current;
Quaternion target = Quaternion.LookRotation(Vector3.back);
public Vector3 pos;
float time, t;
float speed = 40; // the higher the slower
public Missile()
{
this.active = false;
this.from = this.current = Quaternion.identity;
this.t = 0;
}
public void Launch()
{
current = from = Quaternion.LookRotation(new Vector3(
Random.Range(-1f, 1f),
Random.Range(0.2f, 1f),
-2f
)).normalized;
// Quaternion.Euler(Random.Range(-180f, -90f), 0, 0) *
// Quaternion.Euler(0, Random.Range(-30f, 30f), 0);
time = (Quaternion.Angle(from, target) / 180f) * speed;
t = 0;
active = true;
}
public void Update(Monolith mono)
{
mono.Orbit(ref from);
current = Quaternion.Lerp(from, target, t);
t += Time.deltaTime / time;
pos = mono.globePos + current * Vector3.forward * (mono.radius + (t * mono.playerOrbitHeight));
if (active)
{
if (t > 1)
{
// blow up / dmg
active = false;
}
}
}
}