summaryrefslogtreecommitdiff
path: root/player.c
blob: 07a61708ec0b74246951e503b7d5f98c3ab8212d (plain) (blame)
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
68
69
70
71
72
73
74
75
76
#include "asset.h"
#include "maths.h"
#include "plat.h"
#include "player.h"
#include "render.h"
#include "services.h"

void init_player(Player* p) {
	int ip[] = { 5 << fbits, 0, (5 << fbits) };
	vec_cpy(p->p, ip, 3);
	p->r[0] = 0;
	p->r[1] = 0;
	p->anim = 0;
}

void update_player(Player* p, struct Services* s) {
	App* a = s->a;
	int fb, lr;
	int dir[4];
	int t = (p->r[1] / 10);
	int t2 = (t + 64) & sin_table_mask;
	t &= sin_table_mask;
	fb = !!(a->key_states[key_W] & key_state_pressed);
	fb -= 2 * !!(a->key_states[key_S] & key_state_pressed);
	lr = !!(a->key_states[key_A] & key_state_pressed);
	lr -= 2 * !!(a->key_states[key_D] & key_state_pressed);
	p->r[1] -= a->dmx;
	dir[0] = -sin_table[t];
	dir[1] =  cos_table[t];
	dir[2] = -sin_table[t2];
	dir[3] =  cos_table[t2];
	p->p[0] += (dir[0] * fb + dir[2] * lr) / 10;
	p->p[2] += (dir[1] * fb + dir[3] * lr) / 10;
	p->f[0] = dir[0];
	p->f[1] = dir[1];
	p->l[0] = dir[2];
	p->l[1] = dir[3];
	if (a->mbtn_states[mbtn_left] & key_state_just_pressed && p->anim < 16)
		p->anim = 0xff;
	if (p->anim > 16)
		p->anim -= 16;
}

void push_player_cam(const Player* p) {
	int v[3];
	v[0] = -p->p[0];
	v[1] = -p->p[1];
	v[2] = -p->p[2];
	mtx_push_rot_y(p->r[1] / 10);
	mtx_push_rot_x(p->r[0] / 10);
	mtx_push_trans(v);
}

void pop_player_cam(void) {
	mtx_popn(3);
}

void draw_gun(Renderer* r, const Player* p) {
	const Mesh* m = get_mesh(asset_id_gun_mesh);
	const Texture* tex = get_texture(asset_id_gun_texture);
	int pos[3];
	pos[0] = 0;
	pos[1] = -f1;
	pos[2] = cos_table[p->anim];
	mtx_push_trans(pos);
	mtx_push_scale(f1/4);
	mtx_push_rot_y(-64);
	ren_mesh(r, m, tex);
	mtx_popn(3);
}


void draw_player_world(Renderer* r, const Player* p) {
	ren_cleard(r, f1 * 300);
	draw_gun(r, p);
}