refactor for player throw

This commit is contained in:
spatialfree 2020-09-25 07:51:57 -07:00
parent 5d67732bb8
commit 4f886f0011
4 changed files with 123 additions and 104 deletions

View file

@ -14,7 +14,8 @@ public class Monolith : MonoBehaviour
public Worm leftWorm, rightWorm; public Worm leftWorm, rightWorm;
public VoxelObject[] voxelObjects; public VoxelObject player;
public VoxelObject pump;
public Simulate simulate = new Simulate(); public Simulate simulate = new Simulate();
public Vhysics vhysics = new Vhysics(); public Vhysics vhysics = new Vhysics();
@ -79,16 +80,6 @@ public class Monolith : MonoBehaviour
return false; return false;
} }
bool jumpBtn = false;
Vector3 onTouchPos, oldTouchPos;
Vector2 camAngle = new Vector2(30, 45);
Vector2 camAngleVel, oldCamAngleVel;
float delay;
[ReadOnly]
public bool movePiece;
[ReadOnly] [ReadOnly]
public Vector3 voxelCenter; public Vector3 voxelCenter;
void Update() void Update()
@ -105,9 +96,6 @@ public class Monolith : MonoBehaviour
} }
camForm.position = voxelCenter + (camForm.rotation * Vector3.back * 10); camForm.position = voxelCenter + (camForm.rotation * Vector3.back * 10);
// player
VoxelObject player = voxelObjects[0];
// cursor // cursor
Quaternion mainRot = Quaternion.identity; Quaternion mainRot = Quaternion.identity;
mainCon.TryGetFeatureValue(CommonUsages.deviceRotation, out mainRot); mainCon.TryGetFeatureValue(CommonUsages.deviceRotation, out mainRot);
@ -123,6 +111,15 @@ public class Monolith : MonoBehaviour
Vector3 mainCursor = player.pos + (mainRot * Vector3.forward); Vector3 mainCursor = player.pos + (mainRot * Vector3.forward);
Vector3Int cvPos = VoxelPos(mainCursor); 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 // mining
bool mainTrigger = false; bool mainTrigger = false;
mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out mainTrigger); mainCon.TryGetFeatureValue(CommonUsages.triggerButton, out mainTrigger);
@ -172,7 +169,8 @@ public class Monolith : MonoBehaviour
} }
// jumping ? // jumping ?
mainCon.TryGetFeatureValue(CommonUsages.primaryButton, out jumpBtn); bool jumpBtn = false;
offCon.TryGetFeatureValue(CommonUsages.triggerButton, out jumpBtn);
if (Input.GetKeyDown(KeyCode.Space) || (jumpBtn && Mathf.Abs(vel.y) < 0.1f)) if (Input.GetKeyDown(KeyCode.Space) || (jumpBtn && Mathf.Abs(vel.y) < 0.1f))
{ {
vel.y = 8; vel.y = 8;
@ -189,9 +187,13 @@ public class Monolith : MonoBehaviour
// the refactoring needs to be reconsidered // the refactoring needs to be reconsidered
// I don't want an arbitrary list of VoxelObjects // I don't want an arbitrary list of VoxelObjects
// I'd like a Player // I'd like a Player
// an array of each enemy type // an array of each enemy type (I want them to be deeply unique)
// an array of any/each physics object // 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(); vhysics.Update();
render.Update(); render.Update();
} }
@ -270,6 +272,13 @@ public class VoxelObject
public VoxelBody voxelBody; public VoxelBody voxelBody;
public Mesh mesh; 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] [Serializable]

View file

@ -27,30 +27,16 @@ public class Render
{ {
Voxels(); Voxels();
for (int i = 0; i < mono.voxelObjects.Length; i++) mono.player.Draw(matObject);
{ mono.pump.Draw(matObject);
VoxelObject voxelObject = mono.voxelObjects[i];
// render voxelbody bounds
if (voxelObject.voxelBody != null)
{
tempM4.SetTRS(
voxelObject.pos,
Quaternion.identity,
Vector3.one * voxelObject.voxelBody.boundRadius * 2
);
Graphics.DrawMesh(meshCube, tempM4, matBounds, 0);
}
// render voxelobject mesh // Render vhysics bounds
if (voxelObject.mesh != null) // tempM4.SetTRS(
{ // voxelObject.pos,
voxelObject.rot.Normalize(); // Quaternion.identity,
Graphics.DrawMesh(voxelObject.mesh, // Vector3.one * voxelObject.voxelBody.boundRadius * 2
voxelObject.pos, voxelObject.rot, // );
matObject, 0 // Graphics.DrawMesh(meshCube, tempM4, matBounds, 0);
);
}
}
// Draw Enemy // Draw Enemy
// Graphics.DrawMesh(meshPieceDebug, // Graphics.DrawMesh(meshPieceDebug,

View file

@ -220,14 +220,56 @@ MonoBehaviour:
rightWorm: rightWorm:
pos: {x: 1, y: 2, z: 5} pos: {x: 1, y: 2, z: 5}
dirIndex: 4 dirIndex: 4
voxelObjects: player:
- pos: {x: 0, y: 0, z: 0} pos: {x: 0, y: 0, z: 0}
rot: {x: 0, y: 0, z: 0, w: 1} rot: {x: 0, y: 0, z: 0, w: 1}
scale: 1 scale: 1
voxelBody: voxelBody:
boundRadius: 0.32 boundRadius: 0.3
velocity: {x: 0, y: 0, z: 0} velocity: {x: 0, y: 0, z: 0}
mesh: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4, type: 3} mesh: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4, type: 3}
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
pump:
pos: {x: 0, y: 0, z: 0}
rot: {x: 0, y: 0, z: 0, w: 1}
scale: 0.5
voxelBody:
boundRadius: 0.1
velocity: {x: 0, y: 0, z: 0}
mesh: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4, type: 3}
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
render: render:
meshVoxelDebug: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0} meshVoxelDebug: {fileID: 10210, guid: 0000000000000000e000000000000000, type: 0}
meshPieceDebug: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4, meshPieceDebug: {fileID: 8102167970221282723, guid: f17ff0e1c561a0a4bba0f4c3bb139cc4,
@ -239,7 +281,6 @@ MonoBehaviour:
matEnemy: {fileID: 2100000, guid: d9312e72de6721d41975fc093421fc2f, type: 2} matEnemy: {fileID: 2100000, guid: d9312e72de6721d41975fc093421fc2f, type: 2}
matPath: {fileID: 2100000, guid: 525b9c2ebdaac4440b94ea0fa8ca42dd, type: 2} matPath: {fileID: 2100000, guid: 525b9c2ebdaac4440b94ea0fa8ca42dd, type: 2}
cam: {fileID: 963194227} cam: {fileID: 963194227}
movePiece: 0
voxelCenter: {x: 0, y: 0, z: 0} voxelCenter: {x: 0, y: 0, z: 0}
dirs: dirs:
- {x: -1, y: 0, z: 0} - {x: -1, y: 0, z: 0}

View file

@ -18,73 +18,56 @@ public class Vhysics
public void Update() public void Update()
{ {
// bounds // bounds
for (int i = 0; i < mono.voxelObjects.Length; i++) VoxelCollision(mono.player);
{ VoxelCollision(mono.pump);
VoxelObject vobj = mono.voxelObjects[i];
if (vobj.voxelBody != null)
{
Vector3 toPos = vobj.pos + vobj.voxelBody.velocity * Time.deltaTime;
int w = 0;
while (w < 3)
{
Vector3 clampPos = new Vector3(
Mathf.Clamp(toPos.x, Bound(vobj, 0, -1), Bound(vobj, 0, 1)),
Mathf.Clamp(toPos.y, Bound(vobj, 1, -1), Bound(vobj, 1, 1)),
Mathf.Clamp(toPos.z, Bound(vobj, 2, -1), Bound(vobj, 2, 1))
);
float largest = 0;
int largeIndex = -1;
for (int j = 0; j < 3; j++)
{
float dist = Mathf.Abs(toPos[j] - clampPos[j]);
if (dist > largest)
{
largeIndex = j;
largest = dist;
}
}
if (largeIndex > -1)
{
toPos[largeIndex] = clampPos[largeIndex];
vobj.voxelBody.velocity[largeIndex] *= -0.25f; // Bounce
}
else
{
break;
}
w++;
}
vobj.pos = toPos;
// hit checks
for (int j = 0; j < mono.voxelObjects.Length; j++)
{
// space voxelBodys
VoxelObject otherObject = mono.voxelObjects[j];
if (i != j && otherObject.voxelBody != null)
{
Vector3 delta = otherObject.pos - vobj.pos;
// if (delta.magnitude < )
// {
// otherObject.voxelBody.velocity += ;
// }
}
}
}
}
// FAT // FAT
// hit callbacks
// rectangular bounds // rectangular bounds
// larger than one voxel // larger than one voxel
} }
public void VoxelCollision(VoxelObject vobj)
{
Vector3 toPos = vobj.pos + vobj.voxelBody.velocity * Time.deltaTime;
int w = 0;
while (w < 3)
{
Vector3 clampPos = new Vector3(
Mathf.Clamp(toPos.x, Bound(vobj, 0, -1), Bound(vobj, 0, 1)),
Mathf.Clamp(toPos.y, Bound(vobj, 1, -1), Bound(vobj, 1, 1)),
Mathf.Clamp(toPos.z, Bound(vobj, 2, -1), Bound(vobj, 2, 1))
);
float largest = 0;
int largeIndex = -1;
for (int j = 0; j < 3; j++)
{
float dist = Mathf.Abs(toPos[j] - clampPos[j]);
if (dist > largest)
{
largeIndex = j;
largest = dist;
}
}
if (largeIndex > -1)
{
toPos[largeIndex] = clampPos[largeIndex];
vobj.voxelBody.velocity[largeIndex] *= -0.25f; // Bounce
}
else
{
break;
}
w++;
}
vobj.pos = toPos;
}
public float Bound(VoxelObject vobj, int axis, int dir) public float Bound(VoxelObject vobj, int axis, int dir)
{ {
Vector3Int step = Vector3Int.zero; Vector3Int step = Vector3Int.zero;