summaryrefslogtreecommitdiffstats
path: root/static/games/stallman-shooter/player.js
blob: 83144e05494b93e240ab51941a4ebae6dd3b193f (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
77
78
79
80
81
82
83
84
85
86
87
88
import * as constants from '/static/games/stallman-shooter/constants.js';

export class Player {
	constructor() {
		this.x = 400;
		this.y = 500;
		this.x_d = 0;
		this.y_d = 0;
		this.down_p = false;
		this.left_p = false;
		this.right_p = false;
		this.up_p = false;
		this.fire_rate = 15;
		this.shift_p = false;
	}

	act(events) {
		for (const event of events) {
			const [kind, key] = event;
			if (kind === 'keydown' && key === 'ArrowDown') {
				this.down_p = true;
				this.y_d = -1;
			} else if (kind === 'keyup' && key === 'ArrowDown') {
				this.down_p = false;
			} else if (kind === 'keydown' && key === 'ArrowUp') {
				this.up_p = true;
				this.y_d = 1;
			} else if (kind === 'keyup' && key === 'ArrowUp') {
				this.up_p = false;
			} else if (kind === 'keydown' && key === 'ArrowLeft') {
				this.left_p = true;
				this.x_d = -1;
			} else if (kind === 'keyup' && key === 'ArrowLeft') {
				this.left_p = false;
			} else if (kind === 'keydown' && key === 'ArrowRight') {
				this.right_p = true;
				this.x_d = 1;
			} else if (kind === 'keyup' && key === 'ArrowRight') {
				this.right_p = false;
			} else if (kind === 'keydown' && key === 'Shift') {
				this.shift_p = true;
			} else if (kind === 'keyup' && key === 'Shift') {
				this.shift_p = false;
			}
		}
		if (this.left_p) {
			if (!this.right_p) {
				this.x_d = -1;
			}
		} else if (this.right_p) {
			this.x_d = 1;
		} else {
			this.x_d = 0;
		}
		if (this.up_p) {
			if (!this.down_p) {
				this.y_d = -1;
			}
		} else if (this.down_p) {
			this.y_d = 1;
		} else {
			this.y_d = 0;
		}
		/* Don't go off the side */
		if (this.x_d < 0 && this.x <= 0 || this.x >= constants.x_dim && this.x_d > 0) {
			this.x_d = 0;
		}
		if (this.y <= 0 && this.y_d < 0 || this.y >= constants.y_dim && this.y_d > 0) {
			this.y_d = 0;
		}

		if (this.shift_p) {
			this.x += this.x_d;
			this.y += this.y_d;
		} else {
			this.x += this.x_d * 3;
			this.y += this.y_d * 3;
		}
	}

	radius() {
		return 3;
	}

	center() {
		return [33, 21];
	}
}