The Battle for Wesnoth  1.15.7+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 
34 std::unique_ptr<editor_action> editor_action_unit::perform(map_context& mc) const
35 {
36  auto undo = std::make_unique<editor_action_unit_delete>(loc_);
37  perform_without_undo(mc);
38  return undo;
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  if(unit_it != units.end()) {
56  auto undo = std::make_unique<editor_action_unit>(loc_, *unit_it);
58  return undo;
59  }
60 
61  return nullptr;
62 }
63 
65 {
66  unit_map& units = mc.units();
67  if(!units.erase(loc_)) {
68  ERR_ED << "Could not delete unit on " << loc_ << std::endl;
69  } else {
71  }
72 }
73 
74 IMPLEMENT_ACTION(unit_replace)
75 
77 {
78  auto undo = std::make_unique<editor_action_unit_replace>(new_loc_, loc_);
80  return undo;
81 }
82 
84 {
85  unit_map& units = mc.units();
86  units.move(loc_, new_loc_);
88 
89  unit& u = *units.find(new_loc_);
90 
91  // TODO do we still need set_standing?
92  u.anim_comp().set_standing();
93 
95  mc.add_changed_location(new_loc_);
96 
97  /* @todo
98  if (mc.map().is_village(new_loc_)) {
99  (*(resources::gameboard->teams()))[u.side()].get_village(new_loc_);
100  }
101  */
102 
103  // TODO check if that is useful
104  // game_display::get_singleton()->invalidate_unit_after_move(loc_, new_loc_);
105  // display::get_singleton()->draw();
106 }
107 
108 IMPLEMENT_ACTION(unit_facing)
109 
111 {
112  auto undo = std::make_unique<editor_action_unit_facing>(loc_, old_direction_, new_direction_);
114  return undo;
115 }
116 
118 {
119  unit_map& units = mc.units();
120  unit_map::unit_iterator unit_it = units.find(loc_);
121 
122  if(unit_it != units.end()) {
123  unit_it->set_facing(new_direction_);
124  unit_it->anim_comp().set_standing();
125  }
126 }
127 
128 } // end namespace editor
void add_changed_location(const map_location &loc)
unit_iterator end()
Definition: map.hpp:429
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:83
This class represents a single unit of a specific type.
Definition: unit.hpp:129
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:41
Editor action classes.
std::unique_ptr< editor_action > perform(map_context &mc) const override
Perform the action, returning an undo action that, when performed, shall reverse any effects of this ...
Definition: action_unit.cpp:34
STL namespace.
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
map_location loc_
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:64
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:688
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_iterator find(std::size_t id)
Definition: map.cpp:311
unit_animation_component & anim_comp() const
Definition: unit.hpp:1530
place a new unit on the map
Definition: action_unit.hpp:37
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:60
virtual const unit_map & units() const override
Const units accessor.
#define ERR_ED
Remove a unit from the map.
Definition: action_unit.hpp:58
#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 override
Perform the action without creating an undo action.