BattleProjectileController.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "../../lib/Point.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CStack;
  15. class CSpell;
  16. VCMI_LIB_NAMESPACE_END
  17. class CAnimation;
  18. class Canvas;
  19. class BattleInterface;
  20. /// Base class for projectiles
  21. struct ProjectileBase
  22. {
  23. virtual ~ProjectileBase() = default;
  24. virtual void show(Canvas & canvas) = 0;
  25. virtual void tick(uint32_t msPassed) = 0;
  26. Point from; // initial position on the screen
  27. Point dest; // target position on the screen
  28. float progress; // current position of projectile on from->dest line
  29. float speed; // how much progress is gained per second
  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. void tick(uint32_t msPassed) override;
  38. std::shared_ptr<CAnimation> animation;
  39. int frameNum; // frame to display from projectile animation
  40. bool reverse; // if true, projectile will be flipped by vertical axis
  41. };
  42. /// Projectile for spell - render animation moving in straight line from origin to destination
  43. struct ProjectileAnimatedMissile : ProjectileMissile
  44. {
  45. void tick(uint32_t msPassed) override;
  46. float frameProgress;
  47. };
  48. /// Projectile for catapult - render spinning projectile moving at parabolic trajectory to its destination
  49. struct ProjectileCatapult : ProjectileBase
  50. {
  51. void show(Canvas & canvas) override;
  52. void tick(uint32_t msPassed) override;
  53. std::shared_ptr<CAnimation> animation;
  54. float frameProgress;
  55. };
  56. /// Projectile for mages/evil eye - render ray expanding from origin position to destination
  57. struct ProjectileRay : ProjectileBase
  58. {
  59. void show(Canvas & canvas) override;
  60. void tick(uint32_t msPassed) override;
  61. std::vector<CCreature::CreatureAnimation::RayColor> rayConfig;
  62. };
  63. /// Class that manages all ongoing projectiles in the game
  64. /// ... even though in H3 only 1 projectile can be on screen at any point of time
  65. class BattleProjectileController
  66. {
  67. BattleInterface & owner;
  68. /// all projectiles loaded during current battle
  69. std::map<std::string, std::shared_ptr<CAnimation>> projectilesCache;
  70. /// projectiles currently flying on battlefield
  71. std::vector<std::shared_ptr<ProjectileBase>> projectiles;
  72. std::shared_ptr<CAnimation> getProjectileImage(const CStack * stack);
  73. std::shared_ptr<CAnimation> createProjectileImage(const std::string & path );
  74. void initStackProjectile(const CStack * stack);
  75. bool stackUsesRayProjectile(const CStack * stack) const;
  76. bool stackUsesMissileProjectile(const CStack * stack) const;
  77. void showProjectile(Canvas & canvas, std::shared_ptr<ProjectileBase> projectile);
  78. const CCreature & getShooter(const CStack * stack) const;
  79. int computeProjectileFrameID( Point from, Point dest, const CStack * stack);
  80. float computeProjectileFlightTime( Point from, Point dest, double speed);
  81. public:
  82. BattleProjectileController(BattleInterface & owner);
  83. /// renders all currently active projectiles
  84. void render(Canvas & canvas);
  85. /// updates positioning / animations of all projectiles
  86. void tick(uint32_t msPassed);
  87. /// returns true if stack has projectile that is yet to hit target
  88. bool hasActiveProjectile(const CStack * stack, bool emittedOnly) const;
  89. /// starts rendering previously created projectile
  90. void emitStackProjectile(const CStack * stack);
  91. /// creates (but not emits!) projectile and initializes it based on parameters
  92. void createProjectile(const CStack * shooter, Point from, Point dest);
  93. void createSpellProjectile(const CStack * shooter, Point from, Point dest, const CSpell * spell);
  94. void createCatapultProjectile(const CStack * shooter, Point from, Point dest);
  95. };