summaryrefslogtreecommitdiff
path: root/src/player.c
blob: 9434b3cff58b449c117b51c3ec969c8eed2d7ec3 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "entity.h"
#include "entityimpl.h"
#include "game.h"
#include "input.h"
#include "rotrect.h"
#include <math.h>

IMPL_INIT(player) {
	this->width = 10;
	this->height = 10;
	this->player.scale_x = 1.0;
	this->player.scale_y = 1.0;
	this->player.dirx = 1;
}

IMPL(draw) {
	LZY_DrawSetColor(BLACK);
	const double width = this->width * this->player.scale_x + 2;
	const double height = this->height * this->player.scale_y + 2;
	rotrect(this->pos[0] - 1, this->pos[1] - 1,
	        width, height, this->player.angle);
}

IMPL(update) {
	const int on_ground = entity_collide(this, g, 0, 1);

	this->vel[0] = 2.0 * this->player.dirx;
	this->vel[1] *= AIR_RESISTANCE;
	this->vel[1] += GRAVITY;
	this->player.scale_x += 0.1 * (1.0 - this->player.scale_x);
	this->player.scale_y += 0.1 * (1.0 - this->player.scale_y);
	if (fabs(this->player.scale_x - 1.0) < 0.05) this->player.scale_x = 1.01;
	if (fabs(this->player.scale_y - 1.0) < 0.05) this->player.scale_y = 1.01;

	if (this->vel[1] >= 0.0 && on_ground) {
		this->player.rot_speed = 0.0;
		this->player.angle = 0.0;
	} else {
		this->player.angle += this->player.rot_speed;
		this->player.rot_speed *= 0.95;
	}

	if (on_ground && input_pressed(K_O)) {
		const int diry = input_down(K_UP) - input_down(K_DOWN);
		switch (diry) {
		case -1:
			this->vel[1] = -2.8;
			this->player.rot_speed = 0.3 * this->player.dirx;
			break;
		default:
		case 0:
			this->vel[1] = -3.8;
			this->player.rot_speed = 0.4 * this->player.dirx;
			break;
		case 1:
			this->vel[1] = -4.8;
			this->player.rot_speed = 0.5 * this->player.dirx;
			break;
		}
	} else if (on_ground && input_down(K_X)) {
		extern double tick;
		this->vel[0] *= 3;
		tick += 2.0;
		this->player.scale_x *= 1.05;
	}

	entity_move(this, g);
	if (this->vel[0] == 0.0 && this->vel[1] >= -0.0)
		this->player.dirx *= -1;

	if (this->bonk_ceiling ||
	    entity_place_meeting(this, g, entity_type("spike")) != NULL) {
		const unsigned int deathpart = entity_type("deathpart");
		int dy = this->pos[1] - 6;
		for (int y = 0; y < 7; y++) {
			int dx = this->pos[0] - 6;
			for (int x = 0; x < 7; x++) {
				entity_init(game_create_entity(g),
				            deathpart, dx, dy);
				dx += 2;
			}
			dy += 2;
		}
		this->type = ET_NONE;
		g->queue_restart_scene = 45;
	}

	if (entity_place_meeting(this, g, entity_type("exit")) != NULL)
		g->queue_next_scene = true;
}