-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.cpp
331 lines (304 loc) · 8.23 KB
/
unit.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "unit.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
void Unit::equipArmor(Armor* a)
{
if(armor != NULL){
stats.HP -= armor->BHP;
stats.MP -= armor->BMP;
}
armor = a;
stats.HP += a->BHP;
stats.MP += a->BMP;
stats.reset();
}
void Unit::equipWeapon(Weapon * w)
{
weapon = w;
stats.atkRange = w->range;
}
std::string Unit::getCharName()
{
return charName;
}
void Unit::update(Ogre::Real time,Player *p1,Player *p2){}
Unit::Unit(Ogre::SceneManager *s,Player *owner,std::string name, std::string mesh, Ogre::Vector3 v,Ogre::Vector3 init,Stats stats):stats(stats)
{
this->owner = owner;
//Maybe add a list of names and assign it the name randomly?
if(stats.characterClass == Stats::WARRIOR)charName = name+"warrior ";
if(stats.characterClass == Stats::ARCHER)charName = name+"archer ";
if(stats.characterClass == Stats::MAGE)charName = name+"mage ";
sn = s->getRootSceneNode()->createChildSceneNode( name+"node",v);
armor = NULL;
weapon = NULL;
entity = s->createEntity(name,mesh);
sn->attachObject(entity);
animState=IDLE;
currAnim = NULL;
//sn->setDirection(0,0,-1);
x = 0;
y = 0;
initial_direction = init;
yoffset = 0;
if(mesh == "tux.mesh")
{
yoffset = 50;
sn->scale(Ogre::Vector3(100,100,100));
Ogre::Vector3 newpos = sn->getPosition();
newpos.y=yoffset;
sn->setPosition(newpos);
Ogre::Entity *knife = s->createEntity(name+"knife","Cylinder.mesh");
entity->attachObjectToBone("knife_bone", knife);
Ogre::Material *oldmat = (entity->getSubEntity(0)->getMaterial()).getPointer();
std::string matname = name+"classcolor";
Ogre::Material *mat = (oldmat->clone(matname)).getPointer();
Ogre::ColourValue classColor = mat->getTechnique(0)->getPass(0)->getDiffuse();
if(stats.characterClass == Stats::WARRIOR)classColor+=Ogre::ColourValue(1,0,0);
if(stats.characterClass == Stats::ARCHER)classColor+=Ogre::ColourValue(0,1,0);
if(stats.characterClass == Stats::MAGE)classColor+=Ogre::ColourValue(0,0,1);
mat->getTechnique(0)->getPass(0)->setDiffuse(classColor);
entity->getSubEntity(0)->setMaterialName(matname);
}
if(mesh == "fishing_hole.mesh")
{
yoffset = -49;
sn->scale(Ogre::Vector3(50,50,50));
sn->pitch(Ogre::Radian(3.14));
Ogre::Vector3 newpos = sn->getPosition();
newpos.y=yoffset;
sn->setPosition(newpos);
}
if(mesh == "bear.mesh")
{
yoffset = 0;
sn->scale(Ogre::Vector3(15,15,15));
Ogre::Vector3 newpos = sn->getPosition();
newpos.y=yoffset;
sn->setPosition(newpos);
//sn->pitch(Ogre::Radian(3.14/2));
for(int i=0; i < entity->getNumSubEntities(); i++)
{
Ogre::Material *oldmat = (entity->getSubEntity(i)->getMaterial()).getPointer();
std::string matname = name+"classcolor";
matname+=i;
Ogre::Material *mat = (oldmat->clone(matname)).getPointer();
Ogre::ColourValue classColor = mat->getTechnique(0)->getPass(0)->getDiffuse();
if(stats.characterClass == Stats::WARRIOR)classColor+=Ogre::ColourValue(1,0,0);
if(stats.characterClass == Stats::ARCHER)classColor+=Ogre::ColourValue(0,1,0);
if(stats.characterClass == Stats::MAGE)classColor+=Ogre::ColourValue(0,0,1);
mat->getTechnique(0)->getPass(0)->setDiffuse(classColor);
entity->getSubEntity(i)->setMaterialName(matname);
}
}
std::string filename = mesh+".anims";
std::ifstream in(filename.c_str(), std::ifstream::in);
in >> animations[IDLE];
in >> animations[WALK];
in >> animations[ATTACK];
in >> animations[FLINCH];
in >> animations[DEATH];
in.close();
if(animations[IDLE] != "None")currAnim = entity->getAnimationState(animations[IDLE]);
}
Ogre::Entity * Unit::getEntity() {return entity;}
Ogre::SceneNode * Unit::getSceneNode(){return sn;}
void Unit::move(int x,int y) //onMap
{
sn->setPosition(x,yoffset,y);
}
Player * Unit::getOwner()
{
return owner;
}
void Unit::reset()
{
stats.reset();
animState = NONE;
setAnimState(IDLE);
sn->setVisible(true);
}
Ogre::Vector3 Unit::getInitialDirection()
{
return initial_direction;
}
void Unit::setInitialDirection(Ogre::Vector3 direction)
{
this->initial_direction = direction;
}
void Unit::setDirection(Ogre::Vector3 direction)
{
Ogre::Vector3 orig = sn->_getDerivedOrientation() * initial_direction;
orig.y = 0;
orig.normalise();
direction.y = 0;
direction.normalise();
Ogre::Quaternion src = sn->_getDerivedOrientation();
Ogre::Quaternion dest = orig.getRotationTo(direction);
dest.normalise();
if ((1.0f + orig.dotProduct(direction)) < 0.0001f) // Work around 180 degree quaternion rotation quirk
{
sn->yaw(Ogre::Degree(180));
}
else
{
sn->rotate(dest);
}
}
int Unit::getX(){return x;}
int Unit::getY(){return y;}
void Unit::setOrientation(Ogre::Quaternion q)
{
orientation = q;
}
Ogre::Quaternion Unit::getOrientation()
{
return orientation;
}
Ogre::Vector3 Unit::getDirection()
{
return sn->_getDerivedOrientation() * initial_direction;
}
void Unit::update(Ogre::Real time)
{
//std::cerr<<"Unit update begin"<<std::endl;
if(currAnim)
{
switch(animState)
{
case IDLE:
currAnim = entity->getAnimationState(animations[IDLE]);
currAnim->setEnabled(true);
currAnim->setLoop(true);
currAnim->addTime(time);
break;
case WALK:
currAnim = entity->getAnimationState(animations[WALK]);
currAnim->setEnabled(true);
currAnim->setLoop(true);
currAnim->addTime(time);
break;
case NONE:
currAnim = NULL;
break;
case ATTACK:
currAnim = entity->getAnimationState(animations[ATTACK]);
currAnim->setEnabled(true);
currAnim->setLoop(false);
currAnim->addTime(time);
if(currAnim->hasEnded())
{
currAnim->setTimePosition(0);
setAnimState(Unit::IDLE);
}
break;
case FLINCH:
currAnim = entity->getAnimationState(animations[FLINCH]);
currAnim->setEnabled(true);
currAnim->setLoop(false);
currAnim->addTime(time);
if(currAnim->hasEnded())
{
currAnim->setTimePosition(0);
setAnimState(Unit::IDLE);
}
break;
case DEATH:
currAnim = entity->getAnimationState(animations[DEATH]);
currAnim->setEnabled(true);
currAnim->setLoop(false);
currAnim->addTime(time);
if(currAnim->hasEnded())
{
currAnim->setTimePosition(0);
sn->setVisible(false);
}
break;
}
}
//std::cerr<<"Unit update end"<<std::endl;
}
Unit::Anim_State Unit::getAnimState()
{
return animState;
}
bool Unit::isAlive()
{
return stats.currentHP > 0;
}
bool Unit::inRange(int x, int y)
{
return (abs(this->x-x)+abs(this->y-y))<= stats.atkRange;
}
void Unit::setAnimState(Anim_State state)
{
if(animState == DEATH || (animState == ATTACK && state == ATTACK))return;
if(currAnim)
{
currAnim->setEnabled(false);
}
animState = state;
}
void Unit::setX(int x)
{
this->x = x;
}
void Unit::setY(int y)
{
this->y = y;
}
Unit::~Unit()
{
delete armor;
delete weapon;
}
std::string Unit::attacked(Unit *u,int h1, int h2)
{
if(!isAlive() || u->animState == ATTACK)return "";
//stats.Attack(u);
std::stringstream ss;
int damage = 0;
int dcmod = 0;
int dc;
if(h1< h2)
dcmod = 1;
else if (h2 > h1)
dcmod = -2;
if(!u->weapon){
damage = u->stats.PA * u->stats.PA * .7;
std::cout<<"Attacked with fists for "<<damage<<std::endl;
}
else{
switch(u->weapon->weaponType){
case Weapon::Physical:
damage = u->stats.PA * u->weapon->WP;
dc = 4 + dcmod;
if((rand() % 20) < dc) return "Attack missed";
break;
case Weapon::Speed:
damage = ((u->stats.PA + u->stats.Sp)/2)*u->weapon->WP;
dc = 3 + dcmod;
if((rand() % 20) < dc) return "Attack missed";
break;
case Weapon::Magic:
dc = 5 + dcmod;
damage = u->stats.MA * u->weapon->WP;
if((rand() % 20) < dc) return "Attack missed";
break;
}
}
stats.currentHP -= damage;
std::cout<<"Attacked with "<<u->weapon->name<<" for "<<damage<<std::endl;
std::cout<<"HP remaining "<<stats.currentHP<<"/"<<stats.HP<<std::endl;
if(stats.currentHP <= 0)
{
setAnimState(Unit::DEATH);
}else
{
setAnimState(Unit::FLINCH);
}
ss<<u->charName<<"attacked "<<charName<<" with "<<u->weapon->name<<" for "<<damage<<" damage ";
return ss.str();
}