summaryrefslogtreecommitdiff
path: root/player.ha
blob: e2ab10d1fe37f36a2dc56d435234c9a2580e1e7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
};