The Battle for Wesnoth  1.19.0-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(unit_ptr u);
54  void proceed_to(unit_ptr u, std::size_t path_index, bool update=false, bool wait=true);
55  void wait_for_anims();
57 
58 private: // functions
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. INT_MIN for no wait; INT_MAX to wait for end. */
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, 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_attack_ptr attack=nullptr, const_attack_ptr secondary_attack=nullptr,const map_location& defender_loc = map_location::null_location(), 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, unit_ptr u=unit_ptr(), const_attack_ptr attack=nullptr, const_attack_ptr secondary_attack=nullptr,const map_location& defender_loc = map_location::null_location(), 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_attack_ptr attack=nullptr, const_attack_ptr secondary_attack=nullptr,
107  const map_location& winner_loc=map_location::null_location(),
108  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  * @retval true if the defending unit is dead, should be
118  * removed from the playing field.
119  */
120 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
121  const map_location& a, const map_location& b, int damage,
122  const attack_type& attack, const_attack_ptr secondary_attack,
123  int swing, const std::string& hit_text, int drain_amount, const std::string& att_text, const std::vector<std::string>* extra_hit_sounds=nullptr,
124  bool attacking=true);
125 
126 
127 void unit_recruited(const map_location& loc,
128  const map_location& leader_loc=map_location::null_location());
129 
130 /**
131  * This will use a poisoning anim if healing<0.
132  */
133 void unit_healing(unit &healed, const std::vector<unit *> &healers, int healing,
134  const std::string & extra_text="");
135 
136 }
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:81
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:46
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
int wait_until_
The animation potential to wait until.
Definition: udisplay.hpp:69
void wait_for_anims()
Waits for the final animation of the most recent proceed_to() to finish.
Definition: udisplay.cpp:394
void start(unit_ptr u)
Initiates the display of movement for the supplied unit.
Definition: udisplay.cpp:258
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
void finish(unit_ptr u, map_location::DIRECTION dir=map_location::NDIRECTIONS)
Finishes the display of movement for the supplied unit.
Definition: udisplay.cpp:434
void replace_temporary(unit_ptr u)
Makes the temporary unit used by this match the supplied unit.
Definition: udisplay.cpp:217
game_display *const disp_
Definition: udisplay.hpp:63
void update_shown_unit()
Switches the display back to *shown_unit_ after animating.
Definition: udisplay.cpp:243
void proceed_to(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:315
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:83
Contains a number of free functions which display units.
Definition: udisplay.cpp:38
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:848
void unit_recruited(const map_location &loc, const map_location &leader_loc)
Definition: udisplay.cpp:800
void unit_draw_weapon(const map_location &loc, unit &attacker, const_attack_ptr attack, const_attack_ptr secondary_attack, const map_location &defender_loc, unit_ptr defender)
Play a pre-fight animation First unit is the attacker, second unit the defender.
Definition: udisplay.cpp:520
void move_unit(const std::vector< map_location > &path, unit_ptr u, bool animate, map_location::DIRECTION dir, bool force_scroll)
Display a unit moving along a given path.
Definition: udisplay.cpp:506
void unit_die(const map_location &loc, unit &loser, const_attack_ptr attack, const_attack_ptr secondary_attack, const map_location &winner_loc, unit_ptr winner)
Show a unit fading out.
Definition: udisplay.cpp:570
void unit_sheath_weapon(const map_location &primary_loc, unit_ptr primary_unit, const_attack_ptr primary_attack, const_attack_ptr secondary_attack, const map_location &secondary_loc, 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:540
void unit_attack(display *disp, game_board &board, const map_location &a, const map_location &b, int damage, const attack_type &attack, 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:596
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:38
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
static const map_location & null_location()
Definition: location.hpp:81
#define a
#define b