This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.hpp
208 lines (182 loc) · 5.31 KB
/
Player.hpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* @file Player.hpp
* @author H14
* @brief the player class
* @version 0.1
* @date 2021-11-30
*
* @copyright Copyright (c) 2021
*
*/
#pragma once
#include "Texture/IHasModel.hpp"
#include <QGraphicsItem>
#include <QObject>
#include <vector>
#include "Block/Block.hpp"
#include "Texture/IHasModel.hpp"
struct Channel;
// Inventory holds the blocks that the player can place
struct Inventory
{
std::vector<std::string> items;
Inventory();
// select items from inventory
std::string select(size_t) const;
};
/* The player */
class Player : public QObject, public QGraphicsItem, virtual public IHasModel
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
public:
static constexpr QRect rect = QRect(0, 0, 32, 64); // takes up 1x2 blocks
static constexpr int32_t range = 4; // how far the player can reach
static constexpr int32_t maxHP = 100; // max hp of player
static constexpr size_t hotbarSize = 9; // length of the hotbar
private:
const QImage model; // model of the player
const QImage GGimage_0, GGimage_1;
Channel *channel;
QRectF Head_L_i, Head_R_i, Head_F_i, Head;
QRectF Body_F_i, Body_L_i, Body_R_i, Body_F, Body_L;
QRectF Arm_i;
QRectF Leg_L_i, Leg_R_i, Leg_L_s, Leg_R_s, Leg_L, Leg_R;
double offset; // adapt to different screen
int32_t hp = maxHP; // hp of the player
QPointF position; // player position
size_t holding; // item the player is holding
// the inventiry, we have infinite items
Inventory inventory = Inventory(); // inventory of the player
// motion related
double vx;
double vy, ay;
double fallTime;
int distanceToGround;
int distanceToCeiling;
// state related
int attack_progress = 0;
bool attacking = false;
int place_progress = 0;
bool placing = false;
int move_progress = 0;
bool moving = false;
int jump_progress = 0;
bool jumping = false;
int dying = 0;
bool dead = false;
QPointF punchPos;
HarvestLevel weapon;
/**
* @brief Get the Block By Pos object
*
* @param dp block position
* @return const Block::Ptr&
*/
const Block::Ptr &getBlockByPos(QPointF dp) const;
public:
// default constructor with channel init
Player(Channel *channel);
~Player() = default;
// update player status: position, hp, speed, item holding, pick item
// evoked by World
void update();
// render the player
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
QRectF boundingRect() const override;
virtual const QImage *acquireModel(Texture::Ptr texture) const override;
// player actions
void attack(const bool &attacking = true);
void place(const bool &placing = true);
void move(const bool &moving = true);
void jump(const bool &jumping = true); // jump
// store data to save
std::string store() const;
// load data from save
void load(int32_t _hp, QPointF _position, double _dx, double _dy, double _ay);
/**
* @brief break block/attack, return true on success
*
* @param pos position
*/
void punchAt(QPointF pos);
/**
* @brief player place a block, evoke updateBlock in Map
*
* @param pos position
*/
void place(QPointF pos);
/**
* @brief respawn the player when dead
*
*/
void respawn();
/**
* @brief can reach block
*
* @param pos position
* @return true can reach
* @return false cannot
*/
bool canReach(QPointF pos) const;
/**
* @brief Get the Position object
*
* @return QPointF
*/
QPointF getPosition() const { return position; }
/**
* @brief if player is in the world
*
* @return true in
* @return false not in
*/
bool isInbound() const;
/**
* @brief if player is dead
*
* @return true dead
* @return false live
*/
bool isDead() const;
/**
* @brief draw image when player is dead
*
* @param painter
*/
void die(QPainter *painter);
/**
* @brief if the player is falling
*
* @return true is falling
* @return false not falling
*/
bool isFalling() const { return distanceToGround > 0; }
// set channel
void setChannel(Channel *c);
// set dx,dy
void setDX(int32_t vx) { this->vx = vx; }
void setDY(int32_t vy) { this->vy = vy; }
// set hp
void setHP(int32_t h) { hp = h; }
// set item holding, via inventory index. 0 <= i <= 8
void setHolding(int i) { holding = i; }
// get item holding
int getHolding() const { return holding; }
// set pos
void setPosition(QPointF pos) { position = pos; }
// set punch position
void setPunch(QPointF p) { punchPos = p; }
// set weapon
void setWeapon(HarvestLevel h) { weapon = h; }
// getter functions
int32_t getDX() const { return vx; }
int32_t x() const { return position.x(); }
int32_t mx() const { return position.x() / 32; }
int32_t getDY() const { return vy; }
int32_t y() const { return position.y(); }
int32_t my() const { return position.y() / 32; }
HarvestLevel getWeapon() const { return weapon; }
int32_t getHP() const { return hp; }
const Inventory &getInventory() const { return inventory; }
};