-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
action_unit.cpp
130 lines (101 loc) · 3.19 KB
/
action_unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright (C) 2008 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Editor unit action class
*/
// TODO is a textdomain needed?
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "editor/action/action_unit.hpp"
#include "editor/map/map_context.hpp"
#include "units/animation_component.hpp"
#include "units/unit.hpp"
namespace editor
{
IMPLEMENT_ACTION(unit)
editor_action* editor_action_unit::perform(map_context& mc) const
{
editor_action_ptr undo(new editor_action_unit_delete(loc_));
perform_without_undo(mc);
return undo.release();
}
void editor_action_unit::perform_without_undo(map_context& mc) const
{
mc.units().add(loc_, u_);
mc.units().find(loc_)->set_location(loc_);
mc.add_changed_location(loc_);
}
IMPLEMENT_ACTION(unit_delete)
editor_action* editor_action_unit_delete::perform(map_context& mc) const
{
unit_map& units = mc.units();
unit_map::const_unit_iterator unit_it = units.find(loc_);
editor_action_ptr undo;
if(unit_it != units.end()) {
undo.reset(new editor_action_unit(loc_, *unit_it));
perform_without_undo(mc);
return undo.release();
}
return nullptr;
}
void editor_action_unit_delete::perform_without_undo(map_context& mc) const
{
unit_map& units = mc.units();
if(!units.erase(loc_)) {
ERR_ED << "Could not delete unit on " << loc_ << std::endl;
} else {
mc.add_changed_location(loc_);
}
}
IMPLEMENT_ACTION(unit_replace)
editor_action* editor_action_unit_replace::perform(map_context& mc) const
{
editor_action_ptr undo(new editor_action_unit_replace(new_loc_, loc_));
perform_without_undo(mc);
return undo.release();
}
void editor_action_unit_replace::perform_without_undo(map_context& mc) const
{
unit_map& units = mc.units();
units.move(loc_, new_loc_);
unit::clear_status_caches();
unit& u = *units.find(new_loc_);
// TODO do we still need set_standing?
u.anim_comp().set_standing();
mc.add_changed_location(loc_);
mc.add_changed_location(new_loc_);
/* @todo
if (mc.map().is_village(new_loc_)) {
(*(resources::gameboard->teams()))[u.side()].get_village(new_loc_);
}
*/
// TODO check if that is useful
// game_display::get_singleton()->invalidate_unit_after_move(loc_, new_loc_);
// game_display::get_singleton()->draw();
}
IMPLEMENT_ACTION(unit_facing)
editor_action* editor_action_unit_facing::perform(map_context& mc) const
{
editor_action_ptr undo(new editor_action_unit_facing(loc_, old_direction_, new_direction_));
perform_without_undo(mc);
return undo.release();
}
void editor_action_unit_facing::perform_without_undo(map_context& mc) const
{
unit_map& units = mc.units();
unit_map::unit_iterator unit_it = units.find(loc_);
if(unit_it != units.end()) {
unit_it->set_facing(new_direction_);
unit_it->anim_comp().set_standing();
}
}
} // end namespace editor