The Battle for Wesnoth  1.15.12+dev
action_unit.hpp
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 action classes. Some important points:
18  * - This is a polymorphic hierarchy of classes, so actions are usually passed around
19  * as editor_action pointers
20  * - The pointers can, in general, be null. Always check for null before doing anything.
21  * The helper functions perform_ that take a pointer do that.
22  * - The perform() functions can throw when an error occurs. Use smart pointers if you
23  * need to ensure the pointer is deleted.
24  */
25 
26 #pragma once
27 
28 #include "editor/action/action.hpp"
29 
30 #include "units/unit.hpp"
31 
32 namespace editor
33 {
34 /**
35  * place a new unit on the map
36  */
38 {
39 public:
42  , u_(u.clone())
43  {
44  }
45 
46  std::unique_ptr<editor_action> clone() const override;
47  std::unique_ptr<editor_action> perform(map_context& mc) const override;
48  void perform_without_undo(map_context& mc) const override;
49  const std::string& get_name() const override;
50 
51 protected:
53 };
54 
55 /**
56  * Remove a unit from the map.
57  */
59 {
60 public:
63  {
64  }
65 
66  std::unique_ptr<editor_action> clone() const override;
67  std::unique_ptr<editor_action> perform(map_context& mc) const override;
68  void perform_without_undo(map_context& mc) const override;
69  const std::string& get_name() const override;
70 };
71 
73 {
74 public:
77  , new_loc_(new_loc)
78  {
79  }
80 
81  std::unique_ptr<editor_action> clone() const override;
82  std::unique_ptr<editor_action> perform(map_context& mc) const override;
83  void perform_without_undo(map_context& mc) const override;
84  const std::string& get_name() const override;
85 
86 protected:
88 };
89 
91 {
92 public:
94  map_location loc, map_location::DIRECTION new_direction, map_location::DIRECTION old_direction)
96  , new_direction_(new_direction)
97  , old_direction_(old_direction)
98  {
99  }
100 
101  std::unique_ptr<editor_action> clone() const override;
102  std::unique_ptr<editor_action> perform(map_context& mc) const override;
103  void perform_without_undo(map_context& mc) const override;
104  const std::string& get_name() const override;
105 
106 protected:
109 };
110 
111 } // end namespace editor
editor_action_unit_delete(map_location loc)
Definition: action_unit.hpp:61
This class represents a single unit of a specific type.
Definition: unit.hpp:120
void perform_without_undo(map_context &mc) const override
Perform the action without creating an undo action.
Definition: action_unit.cpp:41
const std::string & get_name() const override
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
Base class for actions which act on a specified location (and possibly on other locations that can be...
Definition: action.hpp:175
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:25
std::unique_ptr< editor_action > clone() const override
Action cloning.
editor_action_unit_replace(map_location loc, map_location new_loc)
Definition: action_unit.hpp:75
Editor action classes.
Manage the empty-palette in the editor.
Definition: action.cpp:29
Encapsulates the map of the game.
Definition: location.hpp:37
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
editor_action_unit_facing(map_location loc, map_location::DIRECTION new_direction, map_location::DIRECTION old_direction)
Definition: action_unit.hpp:93
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:39
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:59
Remove a unit from the map.
Definition: action_unit.hpp:58
map_location::DIRECTION old_direction_
map_location::DIRECTION new_direction_