-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotagonist.cpp
127 lines (114 loc) · 4.75 KB
/
protagonist.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
#include "protagonist.h"
#include <QDebug>
#include <QGraphicsScene>
#include <QKeyEvent>
#include <QPainter>
#include <QtMath>
Protagonist::Protagonist() {
pic_normal.load(":/entity/protagonist/normal");
pic_walk1.load(":/entity/protagonist/walk1");
pic_walk2.load(":/entity/protagonist/walk2");
setFlag(QGraphicsObject::ItemIsFocusable);
setFocus();
sword.setParentItem(this);
sword.setPos(111, 0);
sword.setZValue(1);
sword.setTransformOriginPoint(19, 137);
sword.setVisible(false);
}
QRectF Protagonist::boundingRect() const { return QRectF(0, 0, 111, 210); }
void Protagonist::paint(QPainter* painter, const QStyleOptionGraphicsItem*,
QWidget*) {
painter->save();
if (hurt) painter->setOpacity(0.4);
if (state.empty())
painter->drawPixmap(boundingRect().topLeft(), pic_normal);
else {
if (walk_phase)
painter->drawPixmap(boundingRect().topLeft(), pic_walk1);
else
painter->drawPixmap(boundingRect().topLeft(), pic_walk2);
walk_phase = !walk_phase;
}
if (showShape) {
painter->save();
painter->setPen(Qt::red);
painter->drawPath(shape());
painter->restore();
}
painter->restore();
}
QPainterPath Protagonist::shape() const {
QPainterPath path;
path.addEllipse(QPointF(111 / 2, 210 / 2), 20, 20);
return path;
}
void Protagonist::advance(int phase) {
if (!phase || state.empty()) return;
QPointF topUp = pos();
QPointF buttonRight(topUp.x() + boundingRect().width(),
topUp.y() + boundingRect().height());
QRectF sceneGeometry = scene()->sceneRect();
// I am crazy
const qreal MOVEY = 4.5, MOVEX = 6;
if (state.contains(Qt::Key_Up) && !state.contains(Qt::Key_Down) &&
!state.contains(Qt::Key_Left) && !state.contains(Qt::Key_Right)) {
if (topUp.y() >= MOVEY) moveBy(0, -MOVEY);
} else if (!state.contains(Qt::Key_Up) && state.contains(Qt::Key_Down) &&
!state.contains(Qt::Key_Left) &&
!state.contains(Qt::Key_Right)) {
if (buttonRight.y() + MOVEY <= sceneGeometry.height()) moveBy(0, MOVEY);
} else if (!state.contains(Qt::Key_Up) && !state.contains(Qt::Key_Down) &&
state.contains(Qt::Key_Left) && !state.contains(Qt::Key_Right)) {
if (topUp.x() >= MOVEX) moveBy(-MOVEX, 0);
} else if (!state.contains(Qt::Key_Up) && !state.contains(Qt::Key_Down) &&
!state.contains(Qt::Key_Left) && state.contains(Qt::Key_Right)) {
if (buttonRight.x() + MOVEX <= sceneGeometry.width()) moveBy(MOVEX, 0);
} else {
const qreal DMOVEX = (MOVEX * sqrt(2) / 2.0),
DMOVEY = (MOVEY * sqrt(2) / 2.0);
if (state.contains(Qt::Key_Up) && !state.contains(Qt::Key_Down) &&
state.contains(Qt::Key_Left) && !state.contains(Qt::Key_Right)) {
if (topUp.y() >= DMOVEY && topUp.x() >= DMOVEX)
moveBy(-DMOVEX, -DMOVEY);
} else if (state.contains(Qt::Key_Up) &&
!state.contains(Qt::Key_Down) &&
!state.contains(Qt::Key_Left) &&
state.contains(Qt::Key_Right)) {
if (topUp.y() >= DMOVEY &&
buttonRight.x() + DMOVEX <= sceneGeometry.width())
moveBy(DMOVEX, -DMOVEY);
} else if (!state.contains(Qt::Key_Up) &&
state.contains(Qt::Key_Down) &&
state.contains(Qt::Key_Left) &&
!state.contains(Qt::Key_Right)) {
if (buttonRight.y() + DMOVEY <= sceneGeometry.height() &&
topUp.x() >= DMOVEX)
moveBy(-DMOVEX, DMOVEY);
} else if (!state.contains(Qt::Key_Up) &&
state.contains(Qt::Key_Down) &&
!state.contains(Qt::Key_Left) &&
state.contains(Qt::Key_Right)) {
if (buttonRight.y() + DMOVEY <= sceneGeometry.height() &&
buttonRight.x() + DMOVEX <= sceneGeometry.width())
moveBy(DMOVEX, DMOVEY);
}
}
// emit currentPos(topUp);
}
// set the move smoothly
void Protagonist::keyPressEvent(QKeyEvent* event) {
if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down ||
event->key() == Qt::Key_Left || event->key() == Qt::Key_Right)
state.insert(event->key());
if (event->key() == Qt::Key_Z) {
if (event->isAutoRepeat() || sword.isVisible()) return;
emit attack();
}
}
void Protagonist::keyReleaseEvent(QKeyEvent* event) {
if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down ||
event->key() == Qt::Key_Left || event->key() == Qt::Key_Right)
state.remove(event->key());
}
void Protagonist::setShowShape(bool show) { showShape = show; }