-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviolentmoblistmodel.cpp
More file actions
190 lines (164 loc) · 4.77 KB
/
violentmoblistmodel.cpp
File metadata and controls
190 lines (164 loc) · 4.77 KB
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
#include "violentmoblistmodel.h"
#include <QDebug>
#include <sstream>
#include <QStringList>
// Initialize the counter
long ViolentMob::id_counter = 0;
ViolentMob::ViolentMob(const QString &type,
const float &posX,
const float &posY,
const float &posZ,
const float &rotation,
const float &health,
const float &subtype,
const bool &leader,
QObject * parent)
: ListItem(parent),
m_id(id_counter),
m_type(type),
m_posX(posX),
m_posY(posY),
m_posZ(posZ),
m_rotation(rotation),
m_health(health),
m_subtype(subtype),
m_leader(leader)
{
id_counter++;
}
ViolentMob * ViolentMob::build(QStringList & unitData)
{
if (unitData.size() == 9)
{
return (new ViolentMob( unitData[0],
unitData[1].toFloat(),
unitData[2].toFloat(),
unitData[3].toFloat(),
unitData[4].toFloat(),
unitData[5].toFloat(),
unitData[6].toFloat(),
unitData[7].compare("True") ? false : true));
}
return Q_NULLPTR;
}
void ViolentMob::setType(QString type)
{
m_type = type;
}
QHash<int, QByteArray> ViolentMob::roleNames() const
{
QHash<int, QByteArray> names;
names[IdRole] = "id";
names[FilterStringRole] = "filterString";
names[TypeRole] = "type";
names[PosXRole] = "posX";
names[PosYRole] = "posY";
names[PosZRole] = "posZ";
names[RotationRole] = "rotation";
names[HealthRole] = "health";
names[SubtypeRole] = "subtype";
return names;
}
QVariant ViolentMob::data(int role) const
{
switch(role) {
case IdRole:
return (unsigned int)id();
case FilterStringRole:
return filterString();
case TypeRole:
return type();
case PosXRole:
return posX();
case PosYRole:
return posY();
case PosZRole:
return posZ();
case RotationRole:
return rotation();
case HealthRole:
return health();
case SubtypeRole:
return subtype();
default:
return QVariant();
}
}
QString ViolentMob::filterString() const
{
std::stringstream completeString;
completeString << type().toStdString();
return (QString(completeString.str().c_str()));
}
void ViolentMob::print()
{
qDebug() << type()
<< "x" << posX()
<< "y" << posY()
<< "z" << posZ()
<< "rotation" << rotation()
<< "health" << health();
}
ViolentMobListModel::ViolentMobListModel(ListItem * prototype, QObject * parent )
: ListModel(prototype, parent)
{
}
void ViolentMobListModel::setData(const long id, const QVariant &value, int role)
{
switch (role)
{
case ViolentMob::TypeRole:
{
ViolentMob * item = (ViolentMob *)find( id );
item->setType(value.toString());
break;
}
default:
qWarning() << "ViolentMobListModel::setData does not understand what role" << role << "is.";
break;
}
}
void ViolentMobListModel::remove(const long id)
{
// Find the id of the item you want to delete,
// get the index of that item, and remove it.
removeRow(indexFromItem(find( id )).row());
}
// This function only exists because we can't parse the map file.
// TODO: change how units are placed.
float ViolentMobListModel::getFirstPosition(const char label)
{
if (getList().size() != 0) {
if (label==0) {
return ((ViolentMob*)getList().first())->posX();
}
else if (label==1) {
return ((ViolentMob*)getList().first())->posY();
}
else if (label==2) {
return ((ViolentMob*)getList().first())->posZ();
}
}
return 0.0;
}
void ViolentMobListModel::add(const QString type, int subtype, float x, float y, float z)
{
// Randomly choose leader or not (about 5 percent
// of the bad guys are leaders)
int high = 100;
int low = 0;
bool isALeader = ((qrand() % ((high + 1) - low) + low) > 95) ? true : false;
// Randomly choose a rotation, so they aren't all
// facing the same way. (hopefully better move dynamics?)
high = 180;
low = 0;
float rotation = qrand() % ((high + 1) - low) + low;
appendRow(new ViolentMob(
type,
x, y, z, // position
rotation, // rotation
100, // health
subtype, // type of the type of mob
isALeader)); // leader
qDebug() << "Added a" << type << "mob of type" << subtype;
}