refactor out rig code from mono

This commit is contained in:
ethan merchant 2024-11-04 04:24:56 -05:00
parent bfe939b2f7
commit e9c8e96d7f
3 changed files with 55 additions and 45 deletions

View file

@ -39,14 +39,14 @@ static class Arts
Mesh.Sphere.Draw( Mesh.Sphere.Draw(
mat_unlit, mat_unlit,
Matrix.TS( Matrix.TS(
Mono.r_con_stick.position, Rig.r_con_stick.position,
5 * U.mm 5 * U.mm
), ),
Color.White Color.White
); );
Lines.Add( Lines.Add(
Mono.r_con_stick.position + V.XYZ(0, 0, 0), Rig.r_con_stick.position + V.XYZ(0, 0, 0),
Mono.r_con_stick.position + Mono.fullstick * U.cm, Rig.r_con_stick.position + Rig.fullstick * U.cm,
Color.White, Color.White,
2 * U.mm 2 * U.mm
); );
@ -72,7 +72,7 @@ static class Arts
mat_mono, mat_mono,
Matrix.TRS( Matrix.TRS(
Mono.snake[0].ToVec3 + fall, Mono.snake[0].ToVec3 + fall,
Quat.LookDir(Mono.fullstick), Quat.LookDir(Rig.fullstick),
V.XYZ(1, 1, 0.666f + Maths.smooth_stop((float)Mono.step_t) * 0.333f) V.XYZ(1, 1, 0.666f + Maths.smooth_stop((float)Mono.step_t) * 0.333f)
) )
); );

View file

@ -22,9 +22,7 @@ static class Mono
]; ];
public static int snake_len = 4; public static int snake_len = 4;
public static int grow_buffer = 0; public static int grow_buffer = 0;
public static Vec3 fullstick = Vec3.Up; public static XYZi snake_dir = new(0, 0, 1);
public static Pose r_con_stick = Pose.Identity;
public static XYZi snake_dir = new(0, 0, 1), last_snake_dir = new(0, 0, 1);
public static DeltaBool in_box = new(true); public static DeltaBool in_box = new(true);
public static DeltaBool snake_fall = new(false); public static DeltaBool snake_fall = new(false);
public static Dictionary<XYZi, XYZi> holes = new(); public static Dictionary<XYZi, XYZi> holes = new();
@ -32,6 +30,7 @@ static class Mono
public static DeltaBool food_fall = new(false); public static DeltaBool food_fall = new(false);
public static double eat_timestamp = 0.0; public static double eat_timestamp = 0.0;
// put in SFX static class
static void PlayBox(this Sound sound, XYZi pos) static void PlayBox(this Sound sound, XYZi pos)
{ {
sound.Play(box_pose.ToMatrix(box_scale) * pos.ToVec3); sound.Play(box_pose.ToMatrix(box_scale) * pos.ToVec3);
@ -58,53 +57,19 @@ static class Mono
float ssx = Maths.smooth_start(sx); float ssx = Maths.smooth_start(sx);
box_pose.orientation *= Quat.FromAngles(0, sx * 180.0f * Time.Stepf, 0); box_pose.orientation *= Quat.FromAngles(0, sx * 180.0f * Time.Stepf, 0);
} }
if (Input.Key(Key.A).IsJustActive()) snake_dir = new(-1, 0, 0);
if (Input.Key(Key.S).IsJustActive()) snake_dir = new(+1, 0, 0);
if (Input.Key(Key.W).IsJustActive()) snake_dir = new(0, 0, -1);
if (Input.Key(Key.R).IsJustActive()) snake_dir = new(0, 0, +1);
if (Input.Key(Key.Shift).IsJustActive()) snake_dir = new(0, -1, 0);
if (Input.Key(Key.Space).IsJustActive()) snake_dir = new(0, +1, 0);
fullstick = snake_dir.ToVec3;
} }
else else
{ {
Pose head = Input.Head; box_pose.position = Rig.head.position + Rig.head.orientation * V.XYZ(0, -(SD_Y + 0.5f) * box_scale, -32 * U.cm);
box_pose.position = head.position + head.orientation * V.XYZ(0, -(SD_Y + 0.5f) * box_scale, -32 * U.cm); }
Hand r_hand = Input.Hand(Handed.Right); // filter out neck breaking from snake_dir value set
Controller r_con = Input.Controller(Handed.Right); if (snake[0] + Rig.new_dir != snake[1])
bool con_tracked = r_con.trackedPos > TrackState.Lost;
Input.HandVisible(Handed.Max, !con_tracked);
if (con_tracked)
{ {
Vec2 stick = r_con.stick; snake_dir = Rig.new_dir;
Quat stick_rot = Quat.FromAngles(stick.y * -90, 0, stick.x * +90);
float stick_sign = r_con.IsStickClicked ? -1 : +1;
r_con_stick = r_con.pose;
r_con_stick.position += r_con_stick.orientation * V.XYZ(0.0065f, -0.012f, -0.05f);
r_con_stick.orientation *= Quat.FromAngles(-50, 0, 0);
fullstick = r_con_stick.orientation * stick_rot * Vec3.Up * stick_sign;
// Vec3 fullstick = r_hand.palm.orientation * Vec3.Up;
float ax = Maths.abs(fullstick.x);
float ay = Maths.abs(fullstick.y);
float az = Maths.abs(fullstick.z);
if (ax > ay && ax > az) snake_dir = new(Maths.sign(fullstick.x), 0, 0);
if (ay > ax && ay > az) snake_dir = new(0, Maths.sign(fullstick.y), 0);
if (az > ax && az > ay) snake_dir = new(0, 0, Maths.sign(fullstick.z));
} }
} }
// catch invalid direction and revert to last_dir
if (snake[0] + snake_dir == snake[1])
{
snake_dir = last_snake_dir;
}
last_snake_dir = snake_dir;
}
public static void Step() public static void Step()
{ {
if (s_array[snake[0] + snake_dir] > -1) if (s_array[snake[0] + snake_dir] > -1)

View file

@ -5,6 +5,13 @@ namespace snake;
static class Rig static class Rig
{ {
public static Pose head = Pose.Identity;
public static Vec3 fullstick = Vec3.Up;
public static Pose r_con_stick = Pose.Identity;
public static XYZi new_dir = new(0, 0, 1);
public static void Init() public static void Init()
{ {
@ -12,6 +19,44 @@ static class Rig
public static void Frame() public static void Frame()
{ {
Pose head = Input.Head;
// flatscreen dev controls
if (Device.Name == "Simulator")
{
if (Input.Key(Key.A).IsJustActive()) new_dir = new(-1, 0, 0);
if (Input.Key(Key.S).IsJustActive()) new_dir = new(+1, 0, 0);
if (Input.Key(Key.W).IsJustActive()) new_dir = new(0, 0, -1);
if (Input.Key(Key.R).IsJustActive()) new_dir = new(0, 0, +1);
if (Input.Key(Key.Shift).IsJustActive()) new_dir = new(0, -1, 0);
if (Input.Key(Key.Space).IsJustActive()) new_dir = new(0, +1, 0);
fullstick = new_dir.ToVec3;
}
else
{
Hand r_hand = Input.Hand(Handed.Right);
Controller r_con = Input.Controller(Handed.Right);
bool con_tracked = r_con.trackedPos > TrackState.Lost;
Input.HandVisible(Handed.Max, !con_tracked);
if (con_tracked)
{
Vec2 stick = r_con.stick;
Quat stick_rot = Quat.FromAngles(stick.y * -90, 0, stick.x * +90);
float stick_sign = r_con.IsStickClicked ? -1 : +1;
r_con_stick = r_con.pose;
r_con_stick.position += r_con_stick.orientation * V.XYZ(0.0065f, -0.012f, -0.05f);
r_con_stick.orientation *= Quat.FromAngles(-50, 0, 0);
fullstick = r_con_stick.orientation * stick_rot * Vec3.Up * stick_sign;
// Vec3 fullstick = r_hand.palm.orientation * Vec3.Up;
float ax = Maths.abs(fullstick.x);
float ay = Maths.abs(fullstick.y);
float az = Maths.abs(fullstick.z);
if (ax > ay && ax > az) new_dir = new(Maths.sign(fullstick.x), 0, 0);
if (ay > ax && ay > az) new_dir = new(0, Maths.sign(fullstick.y), 0);
if (az > ax && az > ay) new_dir = new(0, 0, Maths.sign(fullstick.z));
}
}
} }
} }