From 37e038131342de9936667c2a27d8d44c3304977f Mon Sep 17 00:00:00 2001 From: spatialfree Date: Mon, 28 Sep 2020 14:21:03 -0700 Subject: [PATCH] enemies that you can pump up and pop --- Assets/Monolith.cs | 124 ++++++++++++++++++++++++--------------- Assets/Render.cs | 28 ++++----- Assets/Rig.cs | 113 +++++++++++++++++++++++++++++++++++ Assets/Rig.cs.meta | 11 ++++ Assets/Scenes/Main.unity | 45 +++++++++++++- Assets/Vhysics.cs | 15 +++-- 6 files changed, 265 insertions(+), 71 deletions(-) create mode 100644 Assets/Rig.cs create mode 100644 Assets/Rig.cs.meta diff --git a/Assets/Monolith.cs b/Assets/Monolith.cs index 4ee4cd3..f817fef 100644 --- a/Assets/Monolith.cs +++ b/Assets/Monolith.cs @@ -16,23 +16,25 @@ public class Monolith : MonoBehaviour public VoxelObject player; public VoxelObject pump; + VoxelObject pumpObject = null; + public VoxelObject[] bats = new VoxelObject[3]; + + public Rig rig = new Rig(); public Simulate simulate = new Simulate(); public Vhysics vhysics = new Vhysics(); public Render render; [Header("References")] - public Camera cam; + public Camera headsetCam; void OnEnable() { - vhysics.Enable(this); render.Enable(this); } void OnDisable() { - vhysics.Disable(); render.Disable(); } @@ -58,12 +60,10 @@ public class Monolith : MonoBehaviour simulate.Step(this); } - InputDevice headset, offCon, mainCon; void Start() { - headset = InputDevices.GetDeviceAtXRNode(XRNode.Head); - offCon = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); - mainCon = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); + rig.Start(this); + vhysics.Start(this); } public Vector3Int VoxelPos(Vector3 pos) @@ -82,76 +82,76 @@ public class Monolith : MonoBehaviour [ReadOnly] public Vector3 voxelCenter; + float stepTime = 0; void Update() { - Vector3 mousePos = Input.mousePosition; - Transform camForm = cam.transform; - - // orbitcam - Quaternion headsetRot = Quaternion.identity; - headset.TryGetFeatureValue(CommonUsages.deviceRotation, out headsetRot); - if (headsetRot != null) + rig.Update(); + + if (stepTime < Time.time) { - camForm.rotation = headsetRot; + simulate.Step(this); + + stepTime = Time.time + 3; } - 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) + if (pumpObject == null) { - pump.pos = player.pos; - pump.voxelBody.velocity = mainRot * Vector3.forward * 10; + if (rig.mainConOne.onPress) + { + pump.pos = player.pos; + pump.voxelBody.velocity = rig.mainConRot * Vector3.forward * 10; + } + } + else + { + pump.voxelBody.velocity = pumpObject.voxelBody.velocity = Vector3.zero; + + if (rig.mainConOne.onPress) + { + pumpObject.scale *= 1.333f; + + if (pumpObject.scale > 3) + { + pumpObject.scale = 1; + pumpObject.active = false; + pumpObject = null; + } + } } // mining bool mainTrigger = false; - mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out mainTrigger); + rig.mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out mainTrigger); if (mainTrigger) { - if (!InVoxel(cvPos)) + if (!InVoxel(rig.cvPos)) { - voxels[vIndex].pos = cvPos; + voxels[vIndex].pos = rig.cvPos; vIndex++; if (vIndex == voxels.Length) { vIndex = 0; } - simulate.Step(this); + // simulate.Step(this); } } Graphics.DrawMesh(render.meshPieceDebug, - mainCursor, Quaternion.identity, + rig.mainCursor, Quaternion.identity, render.matObject, 0 ); - if (!InVoxel(cvPos)) + if (!InVoxel(rig.cvPos)) Graphics.DrawMesh(render.meshPieceDebug, - cvPos, Quaternion.identity, + rig.cvPos, Quaternion.identity, render.matObject, 0 ); // Movement Quaternion offRot = Quaternion.identity; - offCon.TryGetFeatureValue(CommonUsages.deviceRotation, out offRot); + rig.offCon.TryGetFeatureValue(CommonUsages.deviceRotation, out offRot); Vector2 offStick = Vector2.zero; - offCon.TryGetFeatureValue(CommonUsages.primary2DAxis, out offStick); + rig.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) @@ -170,7 +170,7 @@ public class Monolith : MonoBehaviour // jumping ? bool jumpBtn = false; - offCon.TryGetFeatureValue(CommonUsages.triggerButton, out jumpBtn); + rig.offCon.TryGetFeatureValue(CommonUsages.triggerButton, out jumpBtn); if (Input.GetKeyDown(KeyCode.Space) || (jumpBtn && Mathf.Abs(vel.y) < 0.1f)) { vel.y = 8; @@ -194,6 +194,36 @@ public class Monolith : MonoBehaviour // goal a physics object the player can throw in the direction of the cursor (auto retrieve) + // Code motionless Enemy core, where the pump sticks to it, and then the player can pump + for (int i = 0; i < bats.Length; i++) + { + VoxelObject bat = bats[i]; + + if (!bat.active) + { + bat.pos = voxels[Random.Range(0, voxels.Length)].pos; + bat.rot = Quaternion.identity; + bat.mesh = render.meshPieceDebug; + bat.scale = 1; + + bat.voxelBody.boundRadius = 0.4f; + + bat.active = true; + } + else + { + if (Random.value < Time.deltaTime) + { + bat.voxelBody.velocity += Random.rotation * Vector3.forward; + } + + if (pumpObject == null && Vector3.Distance(pump.pos, bat.pos) < bat.voxelBody.boundRadius) + { + pumpObject = bat; + } + } + } + vhysics.Update(); render.Update(); } @@ -265,6 +295,8 @@ public class Worm [Serializable] public class VoxelObject { + public bool active; + public Vector3 pos; public Quaternion rot; public float scale; diff --git a/Assets/Render.cs b/Assets/Render.cs index 2ae169f..a09818c 100644 --- a/Assets/Render.cs +++ b/Assets/Render.cs @@ -21,7 +21,6 @@ public class Render public Mesh meshVoxelDebug, meshPieceDebug, meshCube; public Material matVoxelDebug, matObject, matBounds, matEnemy, matPath; - Matrix4x4 tempM4 = new Matrix4x4(); public void Update() { @@ -30,6 +29,16 @@ public class Render mono.player.Draw(matObject); mono.pump.Draw(matObject); + // Draw Enemy + for (int i = 0; i < mono.bats.Length; i++) + { + VoxelObject bat = mono.bats[i]; + if (bat.active) + { + bat.Draw(matEnemy); + } + } + // Render vhysics bounds // tempM4.SetTRS( // voxelObject.pos, @@ -37,23 +46,6 @@ public class Render // Vector3.one * voxelObject.voxelBody.boundRadius * 2 // ); // Graphics.DrawMesh(meshCube, tempM4, matBounds, 0); - - // Draw Enemy - // Graphics.DrawMesh(meshPieceDebug, - // mono.enemy.pos, - // Quaternion.identity, - // matEnemy, 0 - // ); - - // Draw Piece - - // Draw Path...(s) - // lets start constraining the movement to the generated level - // convert to instanced matrices and check for duplicates - // for (int i = 0; i < mono.dirs.Length; i++) - // { - // Paths(i, mono.piece.pos); - // } } List voxelM4 = new List(); diff --git a/Assets/Rig.cs b/Assets/Rig.cs new file mode 100644 index 0000000..b8f0c7a --- /dev/null +++ b/Assets/Rig.cs @@ -0,0 +1,113 @@ +using UnityEngine; +using UnityEngine.XR; +using System; + +[Serializable] +public class Rig +{ + Monolith mono; + + public InputDevice headset, offCon, mainCon; + + // Tracking + public Vector3 offset = Vector3.down; + public Vector3 headsetPos, offConPos, mainConPos; + [HideInInspector] + public Quaternion headsetRot, offConRot, mainConRot; + + // Input + public Btn mainConTrigger = new Btn(); + public Btn mainConOne = new Btn(); + public Vector2 mainConJoystick; + + // Player + public Vector3 mainCursor; + public Vector3Int cvPos; + + // Stretch Cursor + public Vector3 cursor; + public float str = 6; + + public void Start(Monolith mono) + { + this.mono = mono; + + headset = InputDevices.GetDeviceAtXRNode(XRNode.Head); + offCon = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); + mainCon = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); + } + + public void Update() + { + // Tracking + headset.TryGetFeatureValue(CommonUsages.devicePosition, out headsetPos); + headset.TryGetFeatureValue(CommonUsages.deviceRotation, out headsetRot); + + offCon.TryGetFeatureValue(CommonUsages.devicePosition, out offConPos); + offCon.TryGetFeatureValue(CommonUsages.deviceRotation, out offConRot); + + mainCon.TryGetFeatureValue(CommonUsages.devicePosition, out mainConPos); + mainCon.TryGetFeatureValue(CommonUsages.deviceRotation, out mainConRot); + + headsetRot.Normalize(); + offConRot.Normalize(); + mainConRot.Normalize(); + + headsetPos += offset; + offConPos += offset; + mainConPos += offset; + + mono.headsetCam.transform.position = headsetPos; + mono.headsetCam.transform.rotation = headsetRot; + + // Input + bool state; + mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out state); + mainConTrigger.On(state); + + mainCon.TryGetFeatureValue(CommonUsages.primaryButton, out state); + mainConOne.On(state); + + mainCon.TryGetFeatureValue(CommonUsages.primary2DAxis, out mainConJoystick); + + // cursor + Quaternion rot = mainConRot; + Vector3 mainDir = new Vector3(mainConJoystick.x, 0, mainConJoystick.y); + if (mainDir.sqrMagnitude > 0) + { + rot *= Quaternion.LookRotation(mainDir); + } + + mainCursor = mono.player.pos + (rot * Vector3.forward); + cvPos = mono.VoxelPos(mainCursor); + + // orbitcam + Transform camForm = mono.headsetCam.transform; + camForm.rotation = headsetRot; + camForm.position = mono.voxelCenter + (headsetRot * Vector3.back * 10); + + // Stretch Cursor + float stretch = Vector3.Distance(mainConPos, offConPos); + cursor = mainConPos + mainConRot * Vector3.forward * stretch * str; + } +} + +public class Btn +{ + public bool onPress, held, onUp; + + public void On(bool state) + { + onPress = onUp = false; + if (state) + { + if (!held) { onPress = true; } + held = true; + } + else + { + if (held) { onUp = true; } + held = false; + } + } +} \ No newline at end of file diff --git a/Assets/Rig.cs.meta b/Assets/Rig.cs.meta new file mode 100644 index 0000000..baa7fdf --- /dev/null +++ b/Assets/Rig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b18c01f84bb23a442bdf337f592395c1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 6c33ab5..b570fb0 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -221,6 +221,7 @@ MonoBehaviour: pos: {x: 1, y: 2, z: 5} dirIndex: 4 player: + active: 1 pos: {x: 0, y: 0, z: 0} rot: {x: 0, y: 0, z: 0, w: 1} scale: 1 @@ -246,6 +247,7 @@ MonoBehaviour: e32: 0 e33: 0 pump: + active: 0 pos: {x: 0, y: 0, z: 0} rot: {x: 0, y: 0, z: 0, w: 1} scale: 0.5 @@ -270,6 +272,45 @@ MonoBehaviour: e31: 0 e32: 0 e33: 0 + bats: + - active: 0 + pos: {x: 0, y: 0, z: 0} + rot: {x: 0, y: 0, z: 0, w: 0} + scale: 0 + voxelBody: + boundRadius: 0 + velocity: {x: 0, y: 0, z: 0} + mesh: {fileID: 0} + m4: + e00: 0 + e01: 0 + e02: 0 + e03: 0 + e10: 0 + e11: 0 + e12: 0 + e13: 0 + e20: 0 + e21: 0 + e22: 0 + e23: 0 + e30: 0 + e31: 0 + e32: 0 + e33: 0 + rig: + offset: {x: 0, y: -1, z: 0} + headsetPos: {x: 0, y: 0, z: 0} + offConPos: {x: 0, y: 0, z: 0} + mainConPos: {x: 0, y: 0, z: 0} + headsetRot: {x: 0, y: 0, z: 0, w: 0} + offConRot: {x: 0, y: 0, z: 0, w: 0} + mainConRot: {x: 0, y: 0, z: 0, w: 0} + mainConJoystick: {x: 0, y: 0} + mainCursor: {x: 0, y: 0, z: 0} + cvPos: {x: 0, y: 0, z: 0} + cursor: {x: 0, y: 0, z: 0} + str: 6 render: meshVoxelDebug: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} meshPieceDebug: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4, @@ -280,7 +321,7 @@ MonoBehaviour: matBounds: {fileID: 2100000, guid: 39d0301d3b1720349bf2ce1b81029634, type: 2} matEnemy: {fileID: 2100000, guid: d9312e72de6721d41975fc093421fc2f, type: 2} matPath: {fileID: 2100000, guid: 525b9c2ebdaac4440b94ea0fa8ca42dd, type: 2} - cam: {fileID: 963194227} + headsetCam: {fileID: 963194227} voxelCenter: {x: 0, y: 0, z: 0} dirs: - {x: -1, y: 0, z: 0} @@ -412,7 +453,7 @@ GameObject: - component: {fileID: 963194227} - component: {fileID: 963194226} m_Layer: 0 - m_Name: Main Camera + m_Name: Headset Camera m_TagString: MainCamera m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Assets/Vhysics.cs b/Assets/Vhysics.cs index b93b9c2..de0367c 100644 --- a/Assets/Vhysics.cs +++ b/Assets/Vhysics.cs @@ -6,21 +6,26 @@ public class Vhysics { Monolith mono; - public void Enable(Monolith mono) + public void Start(Monolith mono) { this.mono = mono; } - public void Disable() - { - } - public void Update() { // bounds VoxelCollision(mono.player); VoxelCollision(mono.pump); + for (int i = 0; i < mono.bats.Length; i++) + { + VoxelObject bat = mono.bats[i]; + if (bat.active) + { + VoxelCollision(bat); + } + } + // FAT // hit callbacks // rectangular bounds