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