]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/GameLayer/FightGL.cpp
cd938d3bfb8a65e8a4ef91b081d77a69f4aafac1
[irc/gameservirc.git] / gameserv-2.0 / libgameservgldl / src / GameLayer / FightGL.cpp
1 #include <GameServ/GameLayer/FightGL.h>
2 using GameServ::GameLayer::FightGL;
3
4 #include <GameServ/DataLayer/DataAccess.h>
5 using GameServ::DataLayer::DataAccess;
6 #include <GameServ/DataLayer/IDAOFactory.h>
7 using GameServ::DataLayer::IDAOFactory;
8
9 #include <GameServ/Types.h>
10 using GameServ::Types::Range;
11 using GameServ::Types::Modifiers;
12
13 FightGL::FightGL()
14 {
15 Initialize();
16 }
17
18 FightGL::~FightGL()
19 {
20 }
21
22 void FightGL::Initialize()
23 {
24 // TODO: Load from config
25 shared_ptr<IDAOFactory> factory = DataAccess::GetDataAccessFactory("File");
26 mspPlayerDAO = factory->GetPlayerDAO();
27 }
28
29 unsigned int FightGL::PlayerAttackMonster(shared_ptr<PlayerGO> spPlayer, shared_ptr<MonsterGO> spMonster)
30 {
31 unsigned int attack = CalculateAttack(spPlayer->Strength(), spMonster->Defense());
32 if (attack >= spMonster->Health())
33 {
34 spMonster->Health(0);
35 spMonster->Alive(false);
36 }
37 else
38 {
39 spMonster->Health(spMonster->Health() - attack);
40 }
41 return attack;
42 }
43
44 unsigned int FightGL::MonsterAttackPlayer(shared_ptr<MonsterGO> spMonster, shared_ptr<PlayerGO> spPlayer)
45 {
46 unsigned int attack = CalculateAttack(spMonster->Strength(), spPlayer->Defense());
47 if (attack >= spPlayer->Health())
48 {
49 spPlayer->Health(0);
50 spPlayer->Alive(false);
51 }
52 else
53 {
54 spPlayer->Health(spPlayer->Health() - attack);
55 }
56 mspPlayerDAO->Update(spPlayer);
57 return attack;
58 }
59
60 unsigned int FightGL::PlayerAttackPlayer(shared_ptr<PlayerGO> spPlayerAttacker, shared_ptr<PlayerGO> spPlayerDefender)
61 {
62 unsigned int attack = CalculateAttack(spPlayerAttacker->Strength(), spPlayerDefender->Defense());
63 if (attack >= spPlayerDefender->Health())
64 {
65 spPlayerDefender->Health(0);
66 spPlayerDefender->Alive(false);
67 }
68 else
69 {
70 spPlayerDefender->Health(spPlayerDefender->Health() - attack);
71 }
72 mspPlayerDAO->Update(spPlayerDefender);
73 return attack;
74 }
75
76 unsigned int FightGL::CalculateAttack(unsigned int strength, unsigned int defense)
77 {
78 if (defense >= strength)
79 {
80 return 0;
81 }
82 Range<unsigned int> r(strength, strength / 2);
83 if (r.Random() <= defense)
84 {
85 return 0;
86 }
87 else
88 {
89 return r.LastRandom() - defense;
90 }
91 }