-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tank.cc
143 lines (121 loc) · 3.98 KB
/
Tank.cc
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Created by zr on 23-2-8.
//
#include "Tank.h"
#include "view/component/GameArea.h"
#include "Shell.h"
#include "util/Math.h"
#include "util/Id.h"
namespace TankTrouble
{
Tank::Tank(int id, const util::Vec& p, double angle, const Color& c):
Object(p, angle, c, id),
remainBullets(5)
{
recalculate();
}
void Tank::recalculate()
{
auto corners = util::getCornerVec(posInfo.pos, posInfo.angle,
Tank::TANK_WIDTH, Tank::TANK_HEIGHT);
topLeft = corners[0]; topRight = corners[1]; bottomLeft = corners[2]; bottomRight = corners[3];
}
void Tank::stop()
{
movingStatus = 0;
movingStatus |= MOVING_STATIONARY;
}
void Tank::forward(bool enable)
{
if(enable)
{
movingStatus &= ~MOVING_BACKWARD;
movingStatus |= MOVING_FORWARD;
}
else movingStatus &= ~MOVING_FORWARD;
}
void Tank::backward(bool enable)
{
if(enable)
{
movingStatus &= ~MOVING_FORWARD;
movingStatus |= MOVING_BACKWARD;
}
else movingStatus &= ~MOVING_BACKWARD;
}
void Tank::rotateCW(bool enable)
{
if(enable)
{
movingStatus &= ~ROTATING_CCW;
movingStatus |= ROTATING_CW;
}
else movingStatus &= ~ROTATING_CW;
}
void Tank::rotateCCW(bool enable)
{
if(enable)
{
movingStatus &= ~ROTATING_CW;
movingStatus |= ROTATING_CCW;
}
else movingStatus &= ~ROTATING_CCW;
}
bool Tank::isForwarding() {return movingStatus & MOVING_FORWARD;}
bool Tank::isBackwarding() {return movingStatus & MOVING_BACKWARD;}
bool Tank::isRotatingCW() {return movingStatus & ROTATING_CW;}
bool Tank::isRotatingCCW() {return movingStatus & ROTATING_CCW;}
void Tank::draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
cr->save();
GameArea::drawRect(cr, color, topLeft, topRight, bottomLeft, bottomRight);
cr->close_path();
cr->fill();
cr->set_source_rgb(color[0] - 0.3, color[1] - 0.3, color[2] - 0.3);
cr->arc(posInfo.pos.x(), posInfo.pos.y(), 9, 0.0, 2 * M_PI);
cr->fill();
cr->set_line_width(9.0);
cr->move_to(posInfo.pos.x(), posInfo.pos.y());
util::Vec to = util::polar2Cart(posInfo.angle, 17, posInfo.pos);
cr->line_to(to.x(), to.y());
cr->stroke();
cr->restore();
}
Object::PosInfo Tank::getNextPosition(int movingStep, int rotationStep)
{
Object::PosInfo next = getNextPosition(posInfo, movingStatus, movingStep, rotationStep);
nextPos = next;
return next;
}
Object::PosInfo Tank::getNextPosition(const Object::PosInfo& cur, int movingStatus, int movingStep, int rotationStep)
{
if(movingStep == 0)
movingStep = TANK_MOVING_STEP;
if(rotationStep == 0)
rotationStep = Tank::ROTATING_STEP;
Object::PosInfo next = cur;
if(movingStatus & ROTATING_CW)
next.angle = static_cast<int>(360 + cur.angle - rotationStep) % 360;
if(movingStatus & ROTATING_CCW)
next.angle = static_cast<int>(cur.angle + rotationStep) % 360;
if(movingStatus & MOVING_FORWARD)
next.pos = util::polar2Cart(next.angle, movingStep, cur.pos);
if(movingStatus & MOVING_BACKWARD)
next.pos = util::polar2Cart(next.angle + 180, movingStep, cur.pos);
return next;
}
void Tank::moveToNextPosition()
{
posInfo = nextPos;
recalculate();
}
ObjType Tank::type() {return OBJ_TANK;}
int Tank::remainShells() const {return remainBullets;}
Shell* Tank::makeShell()
{
remainBullets--;
util::Vec shellPos = util::polar2Cart(posInfo.angle, 15, posInfo.pos);
return new Shell(util::Id::getShellId(), shellPos, posInfo.angle, _id);
}
void Tank::getRemainShell() {remainBullets++;}
}