BattleProjectileController.h 3.8 KB

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