BattleProjectileController.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * BattleSiegeController.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/CCreatureHandler.h"
  12. #include "../gui/Geometries.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. class CSpell;
  16. VCMI_LIB_NAMESPACE_END
  17. struct Point;
  18. class CAnimation;
  19. class Canvas;
  20. class BattleInterface;
  21. /// Base class for projectiles
  22. struct ProjectileBase
  23. {
  24. virtual ~ProjectileBase() = default;
  25. virtual void show(Canvas & canvas) = 0;
  26. Point from; // initial position on the screen
  27. Point dest; // target position on the screen
  28. int step; // current step counter
  29. int steps; // total number of steps/frames to show
  30. int shooterID; // ID of shooter stack
  31. bool playing; // if set to true, projectile animation is playing, e.g. flying to target
  32. };
  33. /// Projectile for most shooters - render pre-selected frame moving in straight line from origin to destination
  34. struct ProjectileMissile : ProjectileBase
  35. {
  36. void show(Canvas & canvas) override;
  37. std::shared_ptr<CAnimation> animation;
  38. int frameNum; // frame to display from projectile animation
  39. bool reverse; // if true, projectile will be flipped by vertical axis
  40. };
  41. /// Projectile for spell - render animation moving in straight line from origin to destination
  42. struct ProjectileAnimatedMissile : ProjectileMissile
  43. {
  44. void show(Canvas & canvas) override;
  45. float frameProgress;
  46. };
  47. /// Projectile for catapult - render spinning projectile moving at parabolic trajectory to its destination
  48. struct ProjectileCatapult : ProjectileBase
  49. {
  50. void show(Canvas & canvas) override;
  51. std::shared_ptr<CAnimation> animation;
  52. int frameNum; // frame to display from projectile animation
  53. };
  54. /// Projectile for mages/evil eye - render ray expanding from origin position to destination
  55. struct ProjectileRay : ProjectileBase
  56. {
  57. void show(Canvas & canvas) override;
  58. std::vector<CCreature::CreatureAnimation::RayColor> rayConfig;
  59. };
  60. /// Class that manages all ongoing projectiles in the game
  61. /// ... even though in H3 only 1 projectile can be on screen at any point of time
  62. class BattleProjectileController
  63. {
  64. BattleInterface & owner;
  65. /// all projectiles loaded during current battle
  66. std::map<std::string, std::shared_ptr<CAnimation>> projectilesCache;
  67. /// projectiles currently flying on battlefield
  68. std::vector<std::shared_ptr<ProjectileBase>> projectiles;
  69. std::shared_ptr<CAnimation> getProjectileImage(const CStack * stack);
  70. std::shared_ptr<CAnimation> createProjectileImage(const std::string & path );
  71. void initStackProjectile(const CStack * stack);
  72. bool stackUsesRayProjectile(const CStack * stack) const;
  73. bool stackUsesMissileProjectile(const CStack * stack) const;
  74. void showProjectile(Canvas & canvas, std::shared_ptr<ProjectileBase> projectile);
  75. const CCreature & getShooter(const CStack * stack) const;
  76. int computeProjectileFrameID( Point from, Point dest, const CStack * stack);
  77. int computeProjectileFlightTime( Point from, Point dest, double speed);
  78. public:
  79. BattleProjectileController(BattleInterface & owner);
  80. /// renders all currently active projectiles
  81. void showProjectiles(Canvas & canvas);
  82. /// returns true if stack has projectile that is yet to hit target
  83. bool hasActiveProjectile(const CStack * stack) const;
  84. /// starts rendering previously created projectile
  85. void emitStackProjectile(const CStack * stack);
  86. /// creates (but not emits!) projectile and initializes it based on parameters
  87. void createProjectile(const CStack * shooter, Point from, Point dest);
  88. void createSpellProjectile(const CStack * shooter, Point from, Point dest, const CSpell * spell);
  89. void createCatapultProjectile(const CStack * shooter, Point from, Point dest);
  90. };