-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.cpp
164 lines (143 loc) · 4.03 KB
/
unit.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "unit.h"
#include<QDebug>
Unit::Unit(QPixmap pix, QObject *parent) : QObject(parent), QGraphicsPixmapItem(pix)
{
width=boundingRect().width();
height=boundingRect().height();//todo -4?
}
int Unit::getHorizontalSpeed() const{
return horizontalSpeed;
}
void Unit::setHorizontalMove(int dir){
horizontalMoveStatus = dir;
}
int Unit::getHorizontalMoveStatus(){
return horizontalMoveStatus;
}
//QRectF Unit::boundingRect() const{
// qreal adjust = 1;
// return QRectF(-MARIO_WIDTH/2-adjust,-MARIO_HEIGHT/2-adjust,MARIO_WIDTH +2 * adjust,MARIO_HEIGHT+ 2 * adjust);
//}
double Unit::getVerticalSpeed() const{
return verticalSpeed;
}
void Unit::setVerticalSpeed(double value){
verticalSpeed = value;
}
int Unit::getVerticalMoveStatus() const
{
return verticalMoveStatus;
}
void Unit::setVerticalMoveStatus(int dir)
{
verticalMoveStatus = dir;
}
void Unit::move(){
if(!isOnGround){//drop
verticalMoveStatus = DOWN;
if(verticalSpeed < MAXSPEED) verticalSpeed += G;//up is -, down is +
moveBy(0, verticalSpeed);
}
if(!isLeftBlocked&&horizontalMoveStatus==STOP){
horizontalMoveStatus = LEFT;
}
else if(!isRightBlocked&&horizontalMoveStatus==STOP){
horizontalMoveStatus = RIGHT;
}
if(horizontalMoveStatus != STOP){// move
moveBy(horizontalMoveStatus * horizontalSpeed, 0);
}
// left out = right in
if(x()<0){
setX(VIEW_WIDTH);
}
else if(x()>VIEW_WIDTH){
setX(1);
}
if(y()<0){
setY(0);
}
else if(y()>VIEW_HEIGHT){
hide();
}
}
void Unit::checkCollideDirection(){
collideItemsList = collidingItems(Qt::IntersectsItemBoundingRect);
if(!collideItemsList.isEmpty()){
bool bottomFlag = 0;// check is not collided
bool floorFlag = 0;
bool leftFlag = 0;
bool rightFlag = 0;
for(auto i:collideItemsList){// find Items on the buttom of hero
if(i->y() > y()&&y() + height<i->y() + EPS&&i->x()<x()+width&&i->x()>x()-i->boundingRect().width()){// ensure up and down collide
if(y() + height>= i->y()&&y()+height<i->y()+EPS){// avoid digging in ground
setY(i->y() - height + 1);
}
setVerticalSpeed(0);// reset vv
setVerticalMoveStatus(STOP);
isOnGround = true;
bottomFlag = true;
}
else if(i->x() > x()&&i->y()<y() + height - 1&&i->y() + i->boundingRect().height()>y()){
isRightBlocked = true;
rightFlag = true;
}
else if(i->x() < x()&&i->y()<y() + height - 1&&i->y() + i->boundingRect().height()>y()){
isLeftBlocked = true;
leftFlag = true;
}
else if(i->y() < y()&&(y() - i->y()<=height/2 + i->boundingRect().height()/2+EPS&&y() - i->y()>=height/2 + i->boundingRect().height()/2-EPS)){
isFloorBlocked = true;
floorFlag = true;
}
}
if(bottomFlag == false){
isOnGround = false;
}
if(floorFlag == false){
isFloorBlocked = false;
}
if(rightFlag == false){
isRightBlocked = false;
}
if(leftFlag == false){
isLeftBlocked = false;
}
}
else{
isOnGround = false;
isFloorBlocked = false;
isRightBlocked = false;
isLeftBlocked = false;
}
}
void Unit::setIsOnGround(bool value)
{
isOnGround = value;
}
void Unit::monsterAttackHero(Unit * hero){
if(hero->collideItemsList.contains(this)&&attackInterval <= 0&&isVisible()){
hero->beAttacked();
attackInterval = 1000 / TIMER_INTERVAL;
}
attackInterval--;
}
void Unit::jump(){
setVerticalSpeed(JUMPSPEED);
moveBy(0, -1);
isOnGround = false;
}
void Unit::beAttacked(){
health --;
if(health < 1){
hide();
}
}
void Unit::setHealth(int value)
{
health = value;
}
int Unit::getHealth() const
{
return health;
}