-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcannon.cpp
86 lines (74 loc) · 2.03 KB
/
cannon.cpp
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
#include "cannon.h"
#define CANNONLENGTH 5
#define ANGLEMIN 0
#define ANGLEMAX 85
#define ANGLEDELTA 1
#define FORCEMIN 20
#define FORCEMAX 120
#define FORCEDELTA 2
float rad_of_deg(uint8_t a)
{
return (PI * a) / 180;
}
Cannon::Cannon(unsigned int x, unsigned int y, uint8_t angle, float force, bool dir_right)
{
_x = x;
_y = y;
_angle = angle;
_force = force;
_dir_right = dir_right;
}
void Cannon::draw(const Gamebuino &gb, bool aiming) const
{
gb.display.setColor(BLACK);
gb.display.drawCircle(_x, LCDHEIGHT - (_y + 2), 2);
if (_dir_right)
{
gb.display.drawLine(_x - 2, LCDHEIGHT - _y, _x, LCDHEIGHT - (_y + 2));
gb.display.drawLine(_x, LCDHEIGHT - (_y + 2),
_x + CANNONLENGTH * cos(rad_of_deg(_angle)),
LCDHEIGHT - (_y + 2 + CANNONLENGTH * sin(rad_of_deg(_angle))));
}
else
{
gb.display.drawLine(_x + 2, LCDHEIGHT - _y, _x, LCDHEIGHT - (_y + 2));
gb.display.drawLine(_x, LCDHEIGHT - (_y + 2),
_x - CANNONLENGTH * cos(rad_of_deg(_angle)),
LCDHEIGHT - (_y + 2 + CANNONLENGTH * sin(rad_of_deg(_angle))));
}
if (aiming)
{
gb.display.setColor(GRAY);
gb.display.drawTriangle(_x - 1, LCDHEIGHT - (_y - 3), _x, LCDHEIGHT - (_y - 2), _x + 1, LCDHEIGHT - (_y - 3));
if (_dir_right)
{
gb.display.setColor(BLACK);
gb.display.drawFastVLine(_x - 6, LCDHEIGHT - (_y + 6), 6);
gb.display.setColor(WHITE);
gb.display.fillRect(_x - 5, LCDHEIGHT - (_y + 6), 2, 6);
gb.display.setColor(BLACK);
uint8_t forcebar = 5 * (_force - FORCEMIN) / (FORCEMAX - FORCEMIN) + 1;
gb.display.fillRect(_x - 5, LCDHEIGHT - (_y + forcebar), 2, forcebar);
}
}
}
void Cannon::move(uint8_t ypos)
{
_y = ypos;
}
void Cannon::up()
{
_angle = constrain(_angle + ANGLEDELTA, ANGLEMIN, ANGLEMAX);
}
void Cannon::down()
{
_angle = constrain(_angle - ANGLEDELTA, ANGLEMIN, ANGLEMAX);
}
void Cannon::longer()
{
_force = constrain(_force + FORCEDELTA, FORCEMIN, FORCEMAX);
}
void Cannon::shorter()
{
_force = constrain(_force - FORCEDELTA, FORCEMIN, FORCEMAX);
}