diff options
author | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:11:21 +0200 |
---|---|---|
committer | Lassi Pulkkinen <lassi@pulk.fi> | 2024-10-31 03:51:35 +0200 |
commit | ae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch) | |
tree | 5f462459ae4b47d22114eed717d1382d08cf4dfe /player.ha |
Diffstat (limited to 'player.ha')
-rw-r--r-- | player.ha | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/player.ha b/player.ha new file mode 100644 index 0000000..e2ab10d --- /dev/null +++ b/player.ha @@ -0,0 +1,67 @@ +use glm; +use mcproto; + +def PLAYER_MAX_HEALTH = 20.0f32; +def PLAYER_MAX_FOOD = 20i32; +def PLAYER_MAX_SATURATION = 5.0f32; + +let PLAYER_POS: glm::v3 = [0.0...]; +let PLAYER_YAW = 0.0f32; +let PLAYER_PITCH = 0.0f32; +let PLAYER_HEALTH = PLAYER_MAX_HEALTH; +let PLAYER_FOOD = PLAYER_MAX_FOOD; +let PLAYER_SATURATION = PLAYER_MAX_SATURATION; + +let PLAYER_CONTROL_WALK = 0.0f32; +let PLAYER_CONTROL_STRAFE = 0.0f32; +let PLAYER_CONTROL_JUMP = false; +let PLAYER_CONTROL_SNEAK = false; + +fn player_despawn() void = { + PLAYER_POS = [0.0...]; + PLAYER_YAW = 0.0; + PLAYER_PITCH = 0.0; + PLAYER_HEALTH = PLAYER_MAX_HEALTH; + PLAYER_FOOD = PLAYER_MAX_FOOD; + PLAYER_SATURATION = PLAYER_MAX_SATURATION; + + PLAYER_CONTROL_WALK = 0.0; + PLAYER_CONTROL_STRAFE = 0.0; + PLAYER_CONTROL_JUMP = false; + PLAYER_CONTROL_SNEAK = false; +}; + +fn player_control(control: *Control) void = { + PLAYER_YAW += control.yaw; + PLAYER_PITCH += control.pitch; + PLAYER_CONTROL_WALK = control.walk; + PLAYER_CONTROL_STRAFE = control.strafe; + PLAYER_CONTROL_JUMP = control.jump; + PLAYER_CONTROL_SNEAK = control.sneak; +}; + +fn player_tick() void = { + // as noted in game.ha, we need to negate yaw to obtain + // a conventionally oriented angle. + const yaw_trans = glm::rotation_make(-glm::rad(PLAYER_YAW), + &glm::v3_new(0.0, 1.0, 0.0)); + const motion: glm::v3 = [ + PLAYER_CONTROL_STRAFE, + (if (PLAYER_CONTROL_JUMP) 1.0f32 else 0.0f32) + - (if (PLAYER_CONTROL_SNEAK) 1.0f32 else 0.0f32), + PLAYER_CONTROL_WALK, + ]; + glm::v3_scale(&motion, 1.0); + motion = glm::affine_mul_v3(&yaw_trans, &motion); + PLAYER_POS = glm::v3_add(&PLAYER_POS, &motion); + + let out: []u8 = []; + defer free(out); + mcproto::encode_double(&out, PLAYER_POS[0]); + mcproto::encode_double(&out, PLAYER_POS[1]); + mcproto::encode_double(&out, PLAYER_POS[2]); + mcproto::encode_float(&out, PLAYER_YAW); + mcproto::encode_float(&out, PLAYER_PITCH); + mcproto::encode_bool(&out, true); + network_send(0x14, out); +}; |