dug_deep/Assets/Monolith.cs
2020-09-25 07:51:57 -07:00

308 lines
No EOL
7 KiB
C#

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NaughtyAttributes;
using Random = UnityEngine.Random;
using ExtensionMethods;
using UnityEngine.XR;
public class Monolith : MonoBehaviour
{
public Voxel[] voxels = new Voxel[48];
public int vIndex = 0;
public Worm leftWorm, rightWorm;
public VoxelObject player;
public VoxelObject pump;
public Simulate simulate = new Simulate();
public Vhysics vhysics = new Vhysics();
public Render render;
[Header("References")]
public Camera cam;
void OnEnable()
{
vhysics.Enable(this);
render.Enable(this);
}
void OnDisable()
{
vhysics.Disable();
render.Disable();
}
[Button]
public void LightPass()
{
Lighting.CreateTexture3D();
}
[Button]
public void Reset()
{
voxels = new Voxel[voxels.Length];
voxels[0] = new Voxel(Vector3Int.zero);
leftWorm.pos = rightWorm.pos = Vector3Int.zero;
leftWorm.dirIndex = rightWorm.dirIndex = 0;
}
[Button]
public void Step()
{
simulate.Step(this);
}
InputDevice headset, offCon, mainCon;
void Start()
{
headset = InputDevices.GetDeviceAtXRNode(XRNode.Head);
offCon = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
mainCon = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
}
public Vector3Int VoxelPos(Vector3 pos)
{
return new Vector3Int(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y), Mathf.RoundToInt(pos.z));
}
public bool InVoxel(Vector3Int pos)
{
for (int i = 0; i < voxels.Length; i++)
{
if (voxels[i].pos == pos) { return true; }
}
return false;
}
[ReadOnly]
public Vector3 voxelCenter;
void Update()
{
Vector3 mousePos = Input.mousePosition;
Transform camForm = cam.transform;
// orbitcam
Quaternion headsetRot = Quaternion.identity;
headset.TryGetFeatureValue(CommonUsages.deviceRotation, out headsetRot);
if (headsetRot != null)
{
camForm.rotation = headsetRot;
}
camForm.position = voxelCenter + (camForm.rotation * Vector3.back * 10);
// cursor
Quaternion mainRot = Quaternion.identity;
mainCon.TryGetFeatureValue(CommonUsages.deviceRotation, out mainRot);
Vector2 mainStick = Vector2.zero;
mainCon.TryGetFeatureValue(CommonUsages.primary2DAxis, out mainStick);
Vector3 mainDir = new Vector3(mainStick.x, 0, mainStick.y);
if (mainDir.sqrMagnitude > 0)
{
mainRot *= Quaternion.LookRotation(mainDir);
}
Vector3 mainCursor = player.pos + (mainRot * Vector3.forward);
Vector3Int cvPos = VoxelPos(mainCursor);
// throwing
bool btnThrow = false;
mainCon.TryGetFeatureValue(CommonUsages.primaryButton, out btnThrow);
if (btnThrow)
{
pump.pos = player.pos;
pump.voxelBody.velocity = mainRot * Vector3.forward * 10;
}
// mining
bool mainTrigger = false;
mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out mainTrigger);
if (mainTrigger)
{
if (!InVoxel(cvPos))
{
voxels[vIndex].pos = cvPos;
vIndex++;
if (vIndex == voxels.Length) { vIndex = 0; }
simulate.Step(this);
}
}
Graphics.DrawMesh(render.meshPieceDebug,
mainCursor, Quaternion.identity,
render.matObject, 0
);
if (!InVoxel(cvPos))
Graphics.DrawMesh(render.meshPieceDebug,
cvPos, Quaternion.identity,
render.matObject, 0
);
// Movement
Quaternion offRot = Quaternion.identity;
offCon.TryGetFeatureValue(CommonUsages.deviceRotation, out offRot);
Vector2 offStick = Vector2.zero;
offCon.TryGetFeatureValue(CommonUsages.primary2DAxis, out offStick);
offStick += new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector3 offDir = new Vector3(offStick.x, 0, offStick.y);
if (offDir.sqrMagnitude > 0)
{
offRot *= Quaternion.LookRotation(offDir);
}
Vector3 vel = player.voxelBody.velocity;
vel += offRot * Vector3.forward * offStick.magnitude * 6 * Time.deltaTime;
if (offStick.sqrMagnitude == 0)
{
vel.x *= 1 - (60 * Time.deltaTime);
vel.z *= 1 - (60 * Time.deltaTime);
}
// jumping ?
bool jumpBtn = false;
offCon.TryGetFeatureValue(CommonUsages.triggerButton, out jumpBtn);
if (Input.GetKeyDown(KeyCode.Space) || (jumpBtn && Mathf.Abs(vel.y) < 0.1f))
{
vel.y = 8;
}
float gravStr = 1;
if ((!Input.GetKey(KeyCode.Space) && !jumpBtn) || vel.y < 0)
{
gravStr = 3;
}
vel.y += -9.81f * gravStr * Time.deltaTime;
player.voxelBody.velocity = Vector3.ClampMagnitude(vel, 60);
// the refactoring needs to be reconsidered
// I don't want an arbitrary list of VoxelObjects
// I'd like a Player
// an array of each enemy type (I want them to be deeply unique)
// an array of any/each physics object
// just deliberate what needs to physicsed in the vphysics Update method
// goal a physics object the player can throw in the direction of the cursor (auto retrieve)
vhysics.Update();
render.Update();
}
[HideInInspector]
public Vector3Int[] dirs = new Vector3Int[] {
new Vector3Int(-1, 0, 0), new Vector3Int(0, -1, 0), new Vector3Int(0, 0, -1),
new Vector3Int(1, 0, 0), new Vector3Int(0, 1, 0), new Vector3Int(0, 0, 1)
};
[HideInInspector]
public Vector3Int[] allDirs = new Vector3Int[] {
new Vector3Int(-1, 0, 0),
new Vector3Int(0, -1, 0),
new Vector3Int(0, 0, -1),
new Vector3Int(-1, -1, 0),
new Vector3Int(0, -1, -1),
new Vector3Int(-1, 0, -1),
new Vector3Int(1, 0, 0),
new Vector3Int(0, 1, 0),
new Vector3Int(0, 0, 1),
new Vector3Int(1, 1, 0),
new Vector3Int(0, 1, 1),
new Vector3Int(1, 0, 1),
new Vector3Int(-1, 1, 0),
new Vector3Int(0, -1, 1),
new Vector3Int(1, 0, -1),
new Vector3Int(1, -1, 0),
new Vector3Int(0, 1, -1),
new Vector3Int(-1, 0, 1)
};
public bool Outside(Vector3Int pos)
{
for (int v = 0; v < voxels.Length; v++)
{
if (pos == voxels[v].pos) { return false; }
}
return true;
}
}
[Serializable]
public class Voxel
{
public Vector3Int pos;
public Voxel(Vector3Int pos)
{
this.pos = pos;
}
}
[Serializable]
public class Worm
{
public Vector3Int pos;
public int dirIndex;
public Worm(Vector3Int pos)
{
this.pos = pos;
this.dirIndex = 0;
}
}
[Serializable]
public class VoxelObject
{
public Vector3 pos;
public Quaternion rot;
public float scale;
public VoxelBody voxelBody;
public Mesh mesh;
[HideInInspector] public Matrix4x4 m4;
public void Draw(Material mat)
{
m4.SetTRS(pos, rot, Vector3.one * scale);
Graphics.DrawMesh(mesh, m4, mat, 0);
}
}
[Serializable]
public class VoxelBody
{
public float boundRadius;
public Vector3 velocity;
// public float mass;
}
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int Rollover(this int index, int by, int length)
{
int rollover = index + by;
int delta = rollover - length;
if (delta >= 0)
{
rollover = delta;
}
return rollover;
}
}
}