-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstalker.cpp
More file actions
141 lines (125 loc) · 4.04 KB
/
Copy pathcstalker.cpp
File metadata and controls
141 lines (125 loc) · 4.04 KB
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
#include "cstalker.h"
#include "cscene.h"
CStalker::CStalker(QObject *parent) :
QObject(parent)
{
m_actualState = WALKING;
m_imgStalker = new QGraphicsPixmapItem;
m_imgStalker->setPixmap(QPixmap(":/images/online"));
m_imgStalker->setPos(0, 310);
m_imgStalker->setZValue(1);
m_iWalkSpeed = 0;
m_iJumpSpeed = 0;
extern CScene *scene;
scene->addItem(m_imgStalker);
connect(scene->GetTimer(), SIGNAL(timeout()), this, SLOT(HandleWalk()));
connect(scene->GetTimer(), SIGNAL(timeout()), this, SLOT(HandleJump()));
}
CStalker::~CStalker()
{
delete m_imgStalker;
}
QGraphicsPixmapItem *CStalker::GetImage()
{
// this method return actual loaded image
return m_imgStalker;
}
void CStalker::SetWalk(Direction side)
{
switch(side)
{
case RIGHT:
m_iWalkSpeed = WALK_SPEED; break;
case LEFT:
m_iWalkSpeed = -WALK_SPEED; break;
}
if(m_actualState != JUMPING && m_actualState != FALLING)
m_actualState = WALKING;
}
void CStalker::StopWalk(Direction side)
{
m_iWalkSpeed = 0;
if(m_actualState != JUMPING && m_actualState != FALLING)
m_actualState = STANDING;
}
void CStalker::SetJump()
{
if(m_actualState == JUMPING || m_actualState == FALLING)
return;
m_iJumpSpeed = JUMP_SPEED;
m_actualState = JUMPING;
m_iMaxJump = m_imgStalker->pos().y() - 60;
}
void CStalker::HandleWalk()
{
extern CScene *scene;
// if statements to not go behind the scene
if((m_imgStalker->pos().x() + m_imgStalker->pixmap().width()) >= scene->width())
{
m_imgStalker->setPos(scene->width() - m_imgStalker->pixmap().width(), m_imgStalker->pos().y());
if(m_iWalkSpeed > 0)
return;
}
else if(m_imgStalker->pos().x() <= 0)
{
m_imgStalker->setPos(0, m_imgStalker->pos().y());
if(m_iWalkSpeed < 0)
return;
}
// ready to walk?
if(m_iWalkSpeed != 0)
{
QPointF prevPos = m_imgStalker->pos();
m_imgStalker->moveBy(m_iWalkSpeed, 0);
// collide item on new location? back to old one
if(!scene->collidingItems(m_imgStalker).isEmpty())
m_imgStalker->setPos(prevPos);
}
// "fake" gravity when walk in progress
if(m_actualState != JUMPING && m_actualState != FALLING)
{
int iPrevJumpSpeed = m_iJumpSpeed;
State prevState = m_actualState;
m_actualState = FALLING;
m_iJumpSpeed = JUMP_SPEED;
HandleJump();
m_actualState = prevState;
m_iJumpSpeed = iPrevJumpSpeed;
}
}
void CStalker::HandleJump()
{
// ready to jump?
if(m_iJumpSpeed != 0)
{
QPointF prevPos = m_imgStalker->pos();
extern CScene *scene;
if(m_actualState == JUMPING) // jumping handler
{
if(m_imgStalker->pos().y() <= m_iMaxJump) // hero reached max height
{
m_actualState = FALLING;
return;
}
m_imgStalker->moveBy(0, -m_iJumpSpeed);
if(!scene->collidingItems(m_imgStalker).isEmpty())
{
QList<QGraphicsItem *> colidList = scene->collidingItems(m_imgStalker);
m_imgStalker->setPos(m_imgStalker->pos().x(), colidList[0]->pos().y() + colidList[0]->boundingRect().height() + 1);
m_actualState = FALLING;
}
}
else if(m_actualState == FALLING || m_actualState == WALKING) // falling handler
{
m_imgStalker->moveBy(0, m_iJumpSpeed * 1.4);
if(!scene->collidingItems(m_imgStalker).isEmpty())
{
QList<QGraphicsItem *> colidList = scene->collidingItems(m_imgStalker);
m_imgStalker->setPos(m_imgStalker->pos().x(), colidList[0]->pos().y() - m_imgStalker->pixmap().height() - 1);
m_actualState = STANDING;
m_iJumpSpeed = 0;
m_iMaxJump = 0;
}
}
}
}