The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action_unit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Fabian Mueller <fabianmueller5@gmx.de>
3  Part of the Battle for Wesnoth Project http://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.get_units().add(loc_, u_);
44  mc.get_units().find(loc_)->set_location(loc_);
46 }
47 
48 IMPLEMENT_ACTION(unit_delete)
49 
51 {
52  unit_map& units = mc.get_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));
58  perform_without_undo(mc);
59  return undo.release();
60  }
61 
62  return nullptr;
63 }
64 
66 {
67  unit_map& units = mc.get_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 
81  perform_without_undo(mc);
82  return undo.release();
83 }
84 
86 {
87  unit_map& units = mc.get_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 
98 
99  /* @todo
100  if (mc.get_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  // resources::screen->invalidate_unit_after_move(loc_, new_loc_);
107  // resources::screen->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_));
115  perform_without_undo(mc);
116  return undo.release();
117 }
118 
120 {
121  unit_map& units = mc.get_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:101
std::unique_ptr< editor_action > editor_action_ptr
Action pointer typedef.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:41
Editor action classes.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:85
map_location loc_
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:617
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
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_animation_component & anim_comp() const
Definition: unit.hpp:1329
place a new unit on the map
Definition: action_unit.hpp:37
unit_map & get_units()
Get the unit map from the current map context object.
Base class for all editor actions.
Definition: action_base.hpp:40
void perform_without_undo(map_context &mc) const
Perform the action without creating an undo action.
Definition: action_unit.cpp:65
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:55
size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
#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
map_location::DIRECTION new_direction_
void set_standing(bool with_bars=true)
Sets the animation state to standing.
unit_iterator find(size_t id)
Definition: map.cpp:311
unit_map * units
Definition: resources.cpp:34