The Battle for Wesnoth  1.15.0-dev
action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
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  * Editor unit action class
18  */
19 
20 // TODO is a textdomain needed?
21 #define GETTEXT_DOMAIN "wesnoth-editor"
22 
24 
26 
28 #include "units/unit.hpp"
29 
30 namespace editor
31 {
33 
35 {
37  perform_without_undo(mc);
38  return undo.release();
39 }
40 
42 {
43  mc.units().add(loc_, *u_);
44  mc.units().find(loc_)->set_location(loc_);
46 }
47 
48 IMPLEMENT_ACTION(unit_delete)
49 
51 {
52  unit_map& units = mc.units();
53  unit_map::const_unit_iterator unit_it = units.find(loc_);
54 
55  editor_action_ptr undo;
56  if(unit_it != units.end()) {
57  undo.reset(new editor_action_unit(loc_, *unit_it));
59  return undo.release();
60  }
61 
62  return nullptr;
63 }
64 
66 {
67  unit_map& units = mc.units();
68  if(!units.erase(loc_)) {
69  ERR_ED << "Could not delete unit on " << loc_ << std::endl;
70  } else {
72  }
73 }
74 
75 IMPLEMENT_ACTION(unit_replace)
76 
78 {
80 
82  return undo.release();
83 }
84 
86 {
87  unit_map& units = mc.units();
88  units.move(loc_, new_loc_);
90 
91  unit& u = *units.find(new_loc_);
92 
93  // TODO do we still need set_standing?
94  u.anim_comp().set_standing();
95 
97  mc.add_changed_location(new_loc_);
98 
99  /* @todo
100  if (mc.map().is_village(new_loc_)) {
101  (*(resources::gameboard->teams()))[u.side()].get_village(new_loc_);
102  }
103  */
104 
105  // TODO check if that is useful
106  // game_display::get_singleton()->invalidate_unit_after_move(loc_, new_loc_);
107  // display::get_singleton()->draw();
108 }
109 
110 IMPLEMENT_ACTION(unit_facing)
111 
113 {
114  editor_action_ptr undo(new editor_action_unit_facing(loc_, old_direction_, new_direction_));
116  return undo.release();
117 }
118 
120 {
121  unit_map& units = mc.units();
122  unit_map::unit_iterator unit_it = units.find(loc_);
123 
124  if(unit_it != units.end()) {
125  unit_it->set_facing(new_direction_);
126  unit_it->anim_comp().set_standing();
127  }
128 }
129 
130 } // end namespace editor
void add_changed_location(const map_location &loc)
unit_iterator end()
Definition: map.hpp:415
This class represents a single unit of a specific type.
Definition: unit.hpp:99
std::unique_ptr< editor_action > editor_action_ptr
Action pointer typedef.
Editor action classes.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:85
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:65
map_location loc_
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:685
umap_retval_pair_t add(const map_location &l, const unit &u)
Adds a copy of unit u at location l of the map.
Definition: map.cpp:78
editor_action * perform(map_context &mc) const
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:34
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
umap_retval_pair_t move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:94
Manage the empty-palette in the editor.
Definition: action.cpp:29
unit_iterator find(std::size_t id)
Definition: map.cpp:311
unit_animation_component & anim_comp() const
Definition: unit.hpp:1361
place a new unit on the map
Definition: action_unit.hpp:37
editor_action_unit(map_location loc, const unit &u)
Definition: action_unit.hpp:40
Base class for all editor actions.
Definition: action_base.hpp:40
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:55
virtual const unit_map & units() const override
Const units accessor.
#define ERR_ED
Remove a unit from the map.
Definition: action_unit.hpp:63
#define IMPLEMENT_ACTION(id)
Helper macro to implement common action methods.
Container associating units to locations.
Definition: map.hpp:99
void set_standing(bool with_bars=true)
Sets the animation state to standing.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:41