The Battle for Wesnoth  1.19.7+dev
udisplay.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Display units performing various actions: moving, attacking, and dying.
19  */
20 
21 #pragma once
22 
23 #include "fake_unit_ptr.hpp"
24 #include "map/location.hpp"
25 #include "units/animation.hpp"
26 
27 class attack_type;
28 class game_board;
29 class game_display;
30 class unit;
31 
32 /**
33  * Contains a number of free functions which display units
34  *
35  * performing various on-screen actions - moving, attacking, and dying.
36  */
37 namespace unit_display
38 {
39 
40 /**
41  * A class to encapsulate the steps of drawing a unit's move.
42  * If control over how far the unit moves is not needed, move_unit() may
43  * be a more convenient interface.
44  */
45 class unit_mover {
46 public:
47  unit_mover(const unit_mover&) = delete;
48  unit_mover& operator=(const unit_mover&) = delete;
49 
50  explicit unit_mover(const std::vector<map_location>& path, bool animate=true, bool force_scroll=false);
51  ~unit_mover();
52 
53  void start(const unit_ptr& u);
54  void proceed_to(const unit_ptr& u, std::size_t path_index, bool update=false, bool wait=true);
55  void wait_for_anims();
57 
58 private: // functions
59  void replace_temporary(const unit_ptr& u);
60  void update_shown_unit();
61 
62 private: // data
64  const bool can_draw_;
65  const bool animate_;
66  const bool force_scroll_;
68  /** The animation potential to wait until. milliseconds::min for no wait; milliseconds::max to wait for end. */
69  std::chrono::milliseconds wait_until_;
70  /** The unit to be (re-)shown after an animation finishes. */
72  const std::vector<map_location>& path_;
73  std::size_t current_;
76  bool is_enemy_;
77 };
78 
79 
80 /**
81  * Display a unit moving along a given path.
82  */
83 void move_unit(const std::vector<map_location>& path, const unit_ptr& u,
84  bool animate=true,
86  bool force_scroll=false);
87 
88 /**
89  * Play a pre-fight animation
90  * First unit is the attacker, second unit the defender
91  */
92 void unit_draw_weapon( const map_location& loc, unit& u, const const_attack_ptr& attack=nullptr, const const_attack_ptr& secondary_attack=nullptr,const map_location& defender_loc = map_location::null_location(), const unit_ptr& defender=unit_ptr());
93 
94 /**
95  * Play a post-fight animation
96  * Both unit can be set to null, only valid units will play their animation
97  */
98 void unit_sheath_weapon( const map_location& loc, const unit_ptr& u=unit_ptr(), const const_attack_ptr& attack=nullptr, const const_attack_ptr& secondary_attack=nullptr,const map_location& defender_loc = map_location::null_location(), const unit_ptr& defender=unit_ptr());
99 
100 /**
101  * Show a unit fading out.
102  *
103  * Note: this only shows the effect, it doesn't actually kill the unit.
104  */
105  void unit_die( const map_location& loc, unit& u,
106  const const_attack_ptr& attack=nullptr, const const_attack_ptr& secondary_attack=nullptr,
107  const map_location& winner_loc=map_location::null_location(),
108  const unit_ptr& winner = unit_ptr());
109 
110 
111 /**
112  * Make the unit on tile 'a' attack the unit on tile 'b'.
113  *
114  * The 'damage' will be subtracted from the unit's hitpoints,
115  * and a die effect will be displayed if the unit dies.
116  */
117 void unit_attack(display * disp, game_board & board, //TODO: Would be nice if this could be purely a display function and defer damage dealing to its caller
118  const map_location& a, const map_location& b, int damage,
119  const attack_type& attack, const const_attack_ptr& secondary_attack,
120  int swing, const std::string& hit_text, int drain_amount, const std::string& att_text, const std::vector<std::string>* extra_hit_sounds=nullptr,
121  bool attacking=true);
122 
123 
124 void unit_recruited(const map_location& loc,
125  const map_location& leader_loc=map_location::null_location());
126 
127 /**
128  * This will use a poisoning anim if healing<0.
129  */
130 void unit_healing(unit &healed, const std::vector<unit *> &healers, int healing,
131  const std::string & extra_text="");
132 
133 }
map_location loc
Definition: move.cpp:172
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:97
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
Game board class.
Definition: game_board.hpp:47
A class to encapsulate the steps of drawing a unit's move.
Definition: udisplay.hpp:45
unit_mover & operator=(const unit_mover &)=delete
const bool force_scroll_
Definition: udisplay.hpp:66
void finish(const unit_ptr &u, map_location::direction dir=map_location::direction::indeterminate)
Finishes the display of movement for the supplied unit.
Definition: udisplay.cpp:433
void replace_temporary(const unit_ptr &u)
Makes the temporary unit used by this match the supplied unit.
Definition: udisplay.cpp:216
void wait_for_anims()
Waits for the final animation of the most recent proceed_to() to finish.
Definition: udisplay.cpp:393
unit_ptr shown_unit_
The unit to be (re-)shown after an animation finishes.
Definition: udisplay.hpp:71
unit_animator animator_
Definition: udisplay.hpp:67
game_display *const disp_
Definition: udisplay.hpp:63
void update_shown_unit()
Switches the display back to *shown_unit_ after animating.
Definition: udisplay.cpp:242
std::chrono::milliseconds wait_until_
The animation potential to wait until.
Definition: udisplay.hpp:69
void proceed_to(const unit_ptr &u, std::size_t path_index, bool update=false, bool wait=true)
Visually moves a unit from the last hex we drew to the one specified by path_index.
Definition: udisplay.cpp:314
void start(const unit_ptr &u)
Initiates the display of movement for the supplied unit.
Definition: udisplay.cpp:257
const std::vector< map_location > & path_
Definition: udisplay.hpp:72
unit_mover(const unit_mover &)=delete
fake_unit_ptr temp_unit_ptr_
Definition: udisplay.hpp:74
This class represents a single unit of a specific type.
Definition: unit.hpp:133
static void update()
std::string path
Definition: filesystem.cpp:91
Contains a number of free functions which display units.
Definition: udisplay.cpp:38
void unit_die(const map_location &loc, unit &loser, const const_attack_ptr &attack, const const_attack_ptr &secondary_attack, const map_location &winner_loc, const unit_ptr &winner)
Show a unit fading out.
Definition: udisplay.cpp:569
void unit_draw_weapon(const map_location &loc, unit &attacker, const const_attack_ptr &attack, const const_attack_ptr &secondary_attack, const map_location &defender_loc, const unit_ptr &defender)
Play a pre-fight animation First unit is the attacker, second unit the defender.
Definition: udisplay.cpp:519
void unit_attack(display *disp, game_board &board, const map_location &a, const map_location &b, int damage, const attack_type &attack, const const_attack_ptr &secondary_attack, int swing, const std::string &hit_text, int drain_amount, const std::string &att_text, const std::vector< std::string > *extra_hit_sounds, bool attacking)
Make the unit on tile 'a' attack the unit on tile 'b'.
Definition: udisplay.cpp:595
void unit_healing(unit &healed, const std::vector< unit * > &healers, int healing, const std::string &extra_text)
This will use a poisoning anim if healing<0.
Definition: udisplay.cpp:847
void unit_recruited(const map_location &loc, const map_location &leader_loc)
Definition: udisplay.cpp:799
void move_unit(const std::vector< map_location > &path, const unit_ptr &u, bool animate, map_location::direction dir, bool force_scroll)
Display a unit moving along a given path.
Definition: udisplay.cpp:505
void unit_sheath_weapon(const map_location &primary_loc, const unit_ptr &primary_unit, const const_attack_ptr &primary_attack, const const_attack_ptr &secondary_attack, const map_location &secondary_loc, const unit_ptr &secondary_unit)
Play a post-fight animation Both unit can be set to null, only valid units will play their animation.
Definition: udisplay.cpp:539
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:34
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Encapsulates the map of the game.
Definition: location.hpp:45
direction
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:47
static const map_location & null_location()
Definition: location.hpp:102
#define b