forked from kn0w0n3/Super_Mario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
135 lines (93 loc) · 2.89 KB
/
player.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
/*
* Author: equati0n
* Date: December 2016
*/
#include "player.h"
#include <QPainter>
Player::Player(QGraphicsItem *parent) : QGraphicsItem(parent) ,m_direction(0),mCurrentFrame(),m_StandingDirection(), mState(Standing){
setFlag(ItemClipsToShape);
mWalkPixmap = QPixmap(":images/mario.png");
mStandingPixmap = QPixmap(":images/mariostop.png");
mJumpPixmap = QPixmap(":images/mario_jump.png");
standShootPixmap = QPixmap(":images/mariostop.png");
mPixmap = mWalkPixmap;
}
Player::~Player(){
}
void Player::stand(){
mPixmap = mStandingPixmap;
mCurrentFrame = 0;
mState = Standing;
}
void Player::jump(){
mState = Jumping;
}
void Player::standShoot(){
mPixmap = standShootPixmap;
mCurrentFrame = 0;
mState = StandShoot;
}
void Player::walk(){
if(mState == Walking) {
return;
}
mPixmap = mWalkPixmap;
mCurrentFrame = 0;
mState = Walking;
}
void Player::fall(){
mState = Falling;
}
bool Player::isFalling(){
return mState == Falling;
}
int Player::direction() const{
return m_direction;
}
int Player::standingDirection() const{
return m_StandingDirection;
}
void Player::nextFrame(){
mCurrentFrame += 57;
if (mCurrentFrame >= 1191 ) {
mCurrentFrame = 0;
}
}
QRectF Player::boundingRect() const{
return QRectF(0,0,45,73);
}
void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->drawPixmap(0,0, mPixmap, mCurrentFrame, 0,45, 73);
setTransformOriginPoint(boundingRect().center());
Q_UNUSED(widget);
Q_UNUSED(option);
}
void Player::addDirection(int direction){
if (direction == m_direction)
return;
m_direction += direction;
if (0 != m_direction){
if (-1 == m_direction)
setTransform(QTransform(-1, 0, 0, 1, boundingRect().width(), 0));
else
setTransform(QTransform());
}
}
void Player::addStandingDirection(int standingDirection){
m_StandingDirection = standingDirection;
}
bool Player::isTouchingFoot(QGraphicsItem *item){
QRectF rect(pos().x(), (pos().y() + boundingRect().height()) -5, boundingRect().width(), 5);
QRectF otherRect(item->pos().x(), item->pos().y(), item->boundingRect().width(), item->boundingRect().height());
return rect.intersects(otherRect);
}
bool Player::isTouchingHead(QGraphicsItem *item){
QRectF rect(pos().x(), pos().y(), boundingRect().width(), 5);
QRectF otherRect(item->pos().x(), item->pos().y(), item->boundingRect().width(), item->boundingRect().height());
return rect.intersects(otherRect);
}
bool Player::isTouchingPlatform(QGraphicsItem *item){
QRectF rect(pos().x(), (pos().y() + boundingRect().height()) - 5, boundingRect().width(), 10);
QRectF otherRect(item->pos().x(), item->pos().y(), item->boundingRect().width(), item->boundingRect().height());
return rect.intersects(otherRect);
}