CBattleInterface.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #include "global.h"
  3. #include "CPlayerInterface.h"
  4. #include <list>
  5. class CCreatureSet;
  6. class CGHeroInstance;
  7. class CDefHandler;
  8. class CStack;
  9. class CCallback;
  10. class AdventureMapButton;
  11. struct BattleResult;
  12. template <typename T> struct CondSh;
  13. class CBattleHero : public IShowable, public CIntObject
  14. {
  15. public:
  16. bool flip; //false if it's attacking hero, true otherwise
  17. CDefHandler * dh, *flag; //animation and flag
  18. int phase; //stage of animation
  19. int image; //frame of animation
  20. unsigned char flagAnim, flagAnimCount; //for flag animation
  21. void show(SDL_Surface * to); //prints next frame of animation to to
  22. CBattleHero(std::string defName, int phaseG, int imageG, bool filpG, unsigned char player); //c-tor
  23. ~CBattleHero(); //d-tor
  24. };
  25. class CBattleInterface;
  26. class CBattleHex : public Hoverable, public MotionInterested, public ClickableL, public ClickableR
  27. {
  28. private:
  29. bool setAlterText; //if true, this hex has set alternative text in console and will clean it
  30. public:
  31. unsigned int myNumber;
  32. bool accesible;
  33. //CStack * ourStack;
  34. bool hovered, strictHovered;
  35. CBattleInterface * myInterface; //interface that owns me
  36. static std::pair<int, int> getXYUnitAnim(int hexNum, bool attacker, CCreature * creature); //returns (x, y) of left top corner of animation
  37. //for user interactions
  38. void hover (bool on);
  39. void activate();
  40. void deactivate();
  41. void mouseMoved (SDL_MouseMotionEvent & sEvent);
  42. void clickLeft(boost::logic::tribool down);
  43. void clickRight(boost::logic::tribool down);
  44. CBattleHex();
  45. };
  46. class CBattleObstacle
  47. {
  48. std::vector<int> lockedHexes;
  49. };
  50. class CBattleConsole : public IShowable, public CIntObject
  51. {
  52. private:
  53. std::vector< std::string > texts; //a place where texts are stored
  54. int lastShown; //last shown line of text
  55. public:
  56. std::string alterTxt; //if it's not empty, this text is displayed
  57. CBattleConsole(); //c-tor
  58. ~CBattleConsole(); //d-tor
  59. void show(SDL_Surface * to = 0);
  60. bool addText(std::string text); //adds text at the last position; returns false if failed (e.g. text longer than 70 characters)
  61. void eraseText(unsigned int pos); //erases added text at position pos
  62. void changeTextAt(std::string text, unsigned int pos); //if we have more than pos texts, pos-th is changed to given one
  63. void scrollUp(unsigned int by = 1); //scrolls console up by 'by' positions
  64. void scrollDown(unsigned int by = 1); //scrolls console up by 'by' positions
  65. };
  66. class CBattleReslutWindow : public IShowable, public CIntObject, public IActivable
  67. {
  68. private:
  69. SDL_Surface * background;
  70. AdventureMapButton * exit;
  71. public:
  72. CBattleReslutWindow(const BattleResult & br, SDL_Rect & pos, const CBattleInterface * owner); //c-tor
  73. ~CBattleReslutWindow(); //d-tor
  74. void bExitf();
  75. void activate();
  76. void deactivate();
  77. void show(SDL_Surface * to = 0);
  78. };
  79. class CBattleInterface : public CMainInterface
  80. {
  81. private:
  82. SDL_Surface * background, * menu, * amountBasic, * amountNormal, * cellBorders, * backgroundWithHexes;
  83. AdventureMapButton * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
  84. * bWait, * bDefence, * bConsoleUp, * bConsoleDown;
  85. CBattleConsole * console;
  86. CBattleHero * attackingHero, * defendingHero;
  87. CCreatureSet * army1, * army2; //fighting armies
  88. CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
  89. std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order by BattleInfo's stacks' ID)
  90. std::map< int, CDefHandler * > idToProjectile; //projectiles of creaures (creatureID, defhandler)
  91. std::map< int, bool > creDir; // <creatureID, if false reverse creature's animation>
  92. unsigned char animCount;
  93. int activeStack; //number of active stack; -1 - no one
  94. std::vector<int> shadedHexes; //hexes available for active stack
  95. void showRange(SDL_Surface * to, int ID); //show helper funtion ot mark range of a unit
  96. class CAttHelper
  97. {
  98. public:
  99. int ID; //attacking stack
  100. int dest; //atacked hex
  101. int frame, maxframe; //frame of animation, number of frames of animation
  102. bool reversing;
  103. bool shooting;
  104. int shootingGroup; //if shooting is true, print this animation group
  105. } * attackingInfo;
  106. void attackingShowHelper();
  107. void printConsoleAttacked(int ID, int dmg, int killed, int IDby);
  108. struct SProjectileInfo
  109. {
  110. int x, y; //position on the screen
  111. int dx, dy; //change in position in one step
  112. int step, lastStep; //to know when finish showing this projectile
  113. int creID; //ID of creature that shot this projectile
  114. int frameNum; //frame to display form projectile animation
  115. bool spin; //if true, frameNum will be increased
  116. int animStartDelay; //how many times projectile must be attempted to be shown till it's really show (decremented after hit)
  117. };
  118. std::list<SProjectileInfo> projectiles;
  119. void projectileShowHelper(SDL_Surface * to=NULL); //prints projectiles present on the battlefield
  120. void giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional=-1);
  121. public:
  122. CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor
  123. ~CBattleInterface(); //d-tor
  124. //std::vector<TimeInterested*> timeinterested; //animation handling
  125. bool printCellBorders; //if true, cell borders will be printed
  126. CBattleHex bfield[187]; //11 lines, 17 hexes on each
  127. std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
  128. SDL_Surface * cellBorder, * cellShade;
  129. CondSh<BattleAction *> *givenCommand; //data != NULL if we have i.e. moved current unit
  130. bool myTurn; //if true, interface is active (commands can be ordered
  131. CBattleReslutWindow * resWindow; //window of end of battle
  132. //button handle funcs:
  133. void bOptionsf();
  134. void bSurrenderf();
  135. void bFleef();
  136. void bAutofightf();
  137. void bSpellf();
  138. void bWaitf();
  139. void bDefencef();
  140. void bConsoleUpf();
  141. void bConsoleDownf();
  142. //end of button handle funcs
  143. //napisz tu klase odpowiadajaca za wyswietlanie bitwy i obsluge uzytkownika, polecenia ma przekazywac callbackiem
  144. void activate();
  145. void deactivate();
  146. void show(SDL_Surface * to = NULL);
  147. bool reverseCreature(int number, int hex, bool wideTrick = false); //reverses animation of given creature playing animation of reversing
  148. //call-ins
  149. void newStack(CStack stack); //new stack appeared on battlefield
  150. void stackRemoved(CStack stack); //stack disappeared from batlefiled
  151. void stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting); //stack has been killed (but corpses remain)
  152. void stackActivated(int number); //active stack has been changed
  153. void stackMoved(int number, int destHex, bool startMoving, bool endMoving); //stack with id number moved to destHex
  154. void stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting); //called when stack id attacked by stack with id IDby
  155. void stackAttacking(int ID, int dest); //called when stack with id ID is attacking something on hex dest
  156. void newRound(int number); //caled when round is ended; number is the number of round
  157. void hexLclicked(int whichOne); //hex only call-in
  158. void stackIsShooting(int ID, int dest); //called when stack with id ID is shooting to hex dest
  159. void battleFinished(const BattleResult& br); //called when battle is finished - battleresult window should be printed
  160. friend class CBattleHex;
  161. friend class CBattleReslutWindow;
  162. };