forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.cpp
73 lines (62 loc) · 2.2 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
/** Copyright 2014 EtlamGit */
#include <QPainter>
#include "./entity.h"
#include "./entityidentifier.h"
#include "./nbt.h"
QSharedPointer<OverlayItem> Entity::TryParse(const Tag* tag) {
EntityIdentifier& ei = EntityIdentifier::Instance();
QSharedPointer<OverlayItem> ret;
auto pos = tag->at("Pos");
if (pos && pos != &NBT::Null) {
Entity* entity = new Entity();
entity->pos.x = pos->at(0)->toDouble();
entity->pos.y = pos->at(1)->toDouble();
entity->pos.z = pos->at(2)->toDouble();
auto id = tag->at("id");
if (id && id != &NBT::Null) {
QString type = id->toString().toLower().remove("minecraft:");
EntityInfo const & info = ei.getEntityInfo(type);
QMap<QString, QVariant> props = tag->getData().toMap();
// get something more descriptive if its an item
if (type == "item") {
auto itemId = tag->at("Item")->at("id");
QString itemtype = itemId->toString();
entity->setDisplay(itemtype.mid(itemtype.indexOf(':') + 1));
} else { // or just use the Entity's name
if (info.name == "Name unknown")
entity->setDisplay(type); // use Minecraft internal name if not found
else
entity->setDisplay(info.name); // use name as defined in JSON
}
entity->setType("Entity." + info.category);
entity->setColor(info.brushColor);
entity->setExtraColor(info.penColor);
entity->setProperties(props);
ret.reset(entity);
}
}
return ret;
}
bool Entity::intersects(const Point& min, const Point& max) const {
return min.x <= pos.x && max.x >= pos.x &&
min.y <= pos.y && max.y >= pos.y &&
min.z <= pos.z && max.z >= pos.z;
}
void Entity::draw(double offsetX, double offsetZ, double scale,
QPainter *canvas) const {
QPoint center((pos.x - offsetX) * scale,
(pos.z - offsetZ) * scale);
QColor penColor = extraColor;
penColor.setAlpha(192);
QPen pen = canvas->pen();
pen.setColor(penColor);
pen.setWidth(2);
canvas->setPen(pen);
QColor brushColor = color();
brushColor.setAlpha(128);
canvas->setBrush(brushColor);
canvas->drawEllipse(center, RADIUS, RADIUS);
}
Entity::Point Entity::midpoint() const {
return pos;
}