BattleProjectileController.h 4.0 KB

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