-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity.cpp
157 lines (138 loc) · 4.05 KB
/
entity.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
#include "entity.h"
#include "game.h"
#include "tile.h"
#include <QDebug>
#include <math.h>
#include "coin.h"
Entity::Entity(Game *game, int x, int y, int width, int height)
{
this->game = game;
this->x = x;
this->y = y;
this->width = width;
this->height = height;
this->speed = 1;
this->health = 20;
this->setZValue(1);
}
void Entity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
painter->translate(QPointF(-game->camera->pos.rx(), -game->camera->pos.ry()));
painter->translate(QPointF(x, y));
this->draw(painter);
painter->translate(QPointF(-x, -y));
if(game->DEBUG) painter->drawRect(collisionRect());
painter->restore();
}
void Entity::setDX(int dx)
{
this->dx = dx;
if(dx != 0) this->adx = dx;
if(dx != 0){this->ldx = dx; this->ldy = 0;}
}
void Entity::setDY(int dy)
{
this->dy = dy;
if(dy != 0) this->ady = dy;
if(dy != 0){this->ldy = dy; this->ldx = 0;}
}
void Entity::damaged(int damage)
{
health-=damage;
if(health <=0){
for(int i = 0; i < game->world->entities->count(); i++){
Entity *entity = game->world->entities->at(i);
if(entity == this){
game->world->entities->removeAt(i);
game->world->scene()->removeItem(entity);
game->world->addCoin(x, y);
}
}
}
}
QRectF Entity::boundingRect() const
{
return QRectF(0, 0, width, height);
}
QRectF Entity::collisionRect() const
{
return QRectF(x, y, width, height);
}
QPainterPath Entity::shape() const
{
QPainterPath path;
path.addRect(collisionRect());
return path;
}
void Entity::updateMove()
{
int rdx = dx;
int rdy = dy;
if(adx == 0) adx = dx;
if(ady == 0) ady = dy;
int GRIDSIZE = Tile::SIZE / 2;
if(dx != 0 && adx != 0){
QRectF xBoxX;
if(dx == -1){
xBoxX = QRectF(collisionRect().x() - 2, collisionRect().y(), 2, collisionRect().height());
}else if(dx == 1){
xBoxX = QRectF(collisionRect().x() + collisionRect().width(), collisionRect().y(), 2, collisionRect().height());
}
AABB aabb = game->world->collision(xBoxX);
if(!aabb.collide) atx = x / GRIDSIZE + (adx == 1 ? 1 : 0);
else adx = 0;
}
if(dy != 0 && ady != 0){
QRectF xXxBoxXxX;
if(dy == -1){
xXxBoxXxX = QRectF(collisionRect().x(), collisionRect().y() - 2, collisionRect().width(), 2);
}else if(dy == 1){
xXxBoxXxX = QRectF(collisionRect().x(), collisionRect().y() + collisionRect().height(), collisionRect().width(), 2);
}
AABB aabb = game->world->collision(xXxBoxXxX);
if(!aabb.collide) aty = y / GRIDSIZE + (ady == 1 ? 1 : 0);
else ady = 0;
}
if(adx != 0){
if(atx > x / GRIDSIZE && adx == -1) adx = 0;
if(atx < x / GRIDSIZE && adx == 1) adx = 0;
if(adx == 0) x = atx * GRIDSIZE;
else rdx = adx;
}
if(dy == 0 && ady != 0){
if(aty > y / GRIDSIZE && ady == -1) ady = 0;
if(aty < y / GRIDSIZE && ady == 1) ady = 0;
if(ady == 0) y = aty * GRIDSIZE;
else rdy = ady;
}
float moveX = adx * speed;
float moveY = ady * speed;
x += moveX;
y += moveY;
}
bool Entity::playerWithinRange(float range)
{
return (hypotf(abs(game->player->x - this->x),abs(game->player->y - this->y)) <= range);
}
void Entity::chasePlayer(float distance)
{
if(!this->playerWithinRange(distance)){
if(game->player->x - this->x == 0){
setDX(0);
}
else{
setDX((game->player->x - this->x)/abs(game->player->x - this->x));
}
if(game->player->y - this->y == 0){
setDY(0);
}
else{
setDY((game->player->y - this->y)/abs(game->player->y - this->y));
}
}
else{
setDX(0);
setDY(0);
}
}