1 /*
2 * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3 *
4 * Solarus is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Solarus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "solarus/core/CommandsEffects.h"
18 #include <iostream>
19
20 namespace Solarus {
21
22 const std::string EnumInfoTraits<CommandsEffects::ActionKeyEffect>::pretty_name = "action key effect";
23
24 /**
25 * \brief Lua name of each value of the ActionKeyEffect enum.
26 */
27 const EnumInfo<CommandsEffects::ActionKeyEffect>::names_type EnumInfoTraits<CommandsEffects::ActionKeyEffect>::names = {
28 { CommandsEffects::ACTION_KEY_NONE, "" },
29 { CommandsEffects::ACTION_KEY_NEXT, "next" },
30 { CommandsEffects::ACTION_KEY_LOOK, "look" },
31 { CommandsEffects::ACTION_KEY_OPEN, "open" },
32 { CommandsEffects::ACTION_KEY_LIFT, "lift" },
33 { CommandsEffects::ACTION_KEY_THROW, "throw" },
34 { CommandsEffects::ACTION_KEY_GRAB, "grab" },
35 { CommandsEffects::ACTION_KEY_SPEAK, "speak" },
36 { CommandsEffects::ACTION_KEY_SWIM, "swim"}
37 };
38
39 const std::string EnumInfoTraits<CommandsEffects::AttackKeyEffect>::pretty_name = "sword key effect";
40
41 /**
42 * \brief Lua name of each value of the SwordKeyEffect enum.
43 */
44 const EnumInfo<CommandsEffects::AttackKeyEffect>::names_type EnumInfoTraits<CommandsEffects::AttackKeyEffect>::names = {
45 { CommandsEffects::ATTACK_KEY_NONE, "" },
46 { CommandsEffects::ATTACK_KEY_SWORD, "sword" }
47 };
48
49 const std::string EnumInfoTraits<CommandsEffects::PauseKeyEffect>::pretty_name = "pause key effect";
50
51 /**
52 * \brief Lua name of each value of the PauseKeyEffect enum.
53 */
54 const EnumInfo<CommandsEffects::PauseKeyEffect>::names_type EnumInfoTraits<CommandsEffects::PauseKeyEffect>::names = {
55 { CommandsEffects::PAUSE_KEY_NONE, "" },
56 { CommandsEffects::PAUSE_KEY_PAUSE, "pause" },
57 { CommandsEffects::PAUSE_KEY_RETURN, "return" }
58 };
59
60 /**
61 * \brief Constructor.
62 */
CommandsEffects()63 CommandsEffects::CommandsEffects():
64 action_key_effect(ACTION_KEY_NONE),
65 action_key_effects_saved(),
66 action_key_enabled(true),
67 sword_key_effect(ATTACK_KEY_NONE),
68 sword_key_effects_saved(),
69 sword_key_enabled(true),
70 pause_key_effect(PAUSE_KEY_PAUSE),
71 pause_key_effects_saved(),
72 pause_key_enabled(true),
73 item_keys_enabled(true) {
74
75 }
76
77 // action key
78
79 /**
80 * \brief Returns the current effect of the action key.
81 * \return the current effect of the action key
82 */
get_action_key_effect()83 CommandsEffects::ActionKeyEffect CommandsEffects::get_action_key_effect() {
84 return action_key_effect;
85 }
86 /**
87 * \brief Sets the current effect of the action key.
88 * \param action_key_effect the current effect of the action key
89 */
set_action_key_effect(CommandsEffects::ActionKeyEffect action_key_effect)90 void CommandsEffects::set_action_key_effect(CommandsEffects::ActionKeyEffect action_key_effect) {
91 this->action_key_effect = action_key_effect;
92 }
93
94 /**
95 * \brief Returns whether the action key is enabled.
96 * \return true if the action key is enabled, false otherwise
97 */
is_action_key_enabled()98 bool CommandsEffects::is_action_key_enabled() {
99 return action_key_enabled;
100 }
101
102 /**
103 * \brief Sets whether the action key is enabled.
104 * \param enable true to enable the action key, false to disable it
105 */
set_action_key_enabled(bool enable)106 void CommandsEffects::set_action_key_enabled(bool enable) {
107 this->action_key_enabled = enable;
108 }
109
110 /**
111 * \brief Saves the current effect of the action key.
112 *
113 * Call restore_action_key_effect() to restore the action key saved here.
114 */
save_action_key_effect()115 void CommandsEffects::save_action_key_effect() {
116 action_key_effects_saved.push(get_action_key_effect());
117 }
118
119 /**
120 * \brief Restores the action key effect saved by the last
121 * call to save_action_key_effect().
122 */
restore_action_key_effect()123 void CommandsEffects::restore_action_key_effect() {
124 if (!action_key_effects_saved.empty()) {
125 action_key_effect = action_key_effects_saved.top();
126 action_key_effects_saved.pop();
127 }
128 }
129
130 /**
131 * \brief Returns whether the action key is currently taking effect
132 * on the entity the hero is facing.
133 *
134 * Such action key effects are ACTION_KEY_LOOK, ACTION_KEY_OPEN,
135 * ACTION_KEY_LIFT, ACTION_KEY_SPEAK and ACTION_KEY_GRAB.
136 *
137 * \return true if the action key is acting on the facing entity
138 */
is_action_key_acting_on_facing_entity()139 bool CommandsEffects::is_action_key_acting_on_facing_entity() {
140 return action_key_effect == ACTION_KEY_LOOK
141 || action_key_effect == ACTION_KEY_OPEN
142 || action_key_effect == ACTION_KEY_LIFT
143 || action_key_effect == ACTION_KEY_SPEAK
144 || action_key_effect == ACTION_KEY_GRAB;
145 }
146
147 // sword key
148
149 /**
150 * \brief Returns the current effect of the sword key.
151 * \return the current effect of the sword key
152 */
get_sword_key_effect()153 CommandsEffects::AttackKeyEffect CommandsEffects::get_sword_key_effect() {
154 return sword_key_effect;
155 }
156
157 /**
158 * \brief Sets the current effect of the sword key.
159 * \param sword_key_effect the current effect of the sword key
160 */
set_sword_key_effect(CommandsEffects::AttackKeyEffect sword_key_effect)161 void CommandsEffects::set_sword_key_effect(CommandsEffects::AttackKeyEffect sword_key_effect) {
162 this->sword_key_effect = sword_key_effect;
163 }
164
165 /**
166 * \brief Returns whether the sword key is enabled.
167 * \return true if the sword key is enabled, false otherwise
168 */
is_sword_key_enabled()169 bool CommandsEffects::is_sword_key_enabled() {
170 return sword_key_enabled;
171 }
172
173 /**
174 * \brief Sets whether the sword key is enabled.
175 * \param enable true to enable the sword key, false to disable it
176 */
set_sword_key_enabled(bool enable)177 void CommandsEffects::set_sword_key_enabled(bool enable) {
178 this->sword_key_enabled = enable;
179 }
180
181 /**
182 * \brief Saves the current effect of the sword key.
183 *
184 * Call restore_sword_key_effect to restore the sword key saved here.
185 */
save_sword_key_effect()186 void CommandsEffects::save_sword_key_effect() {
187 sword_key_effects_saved.push(get_sword_key_effect());
188 }
189
190 /**
191 * \brief Restores the sword key effect saved by the last
192 * call to save_sword_key_effect().
193 */
restore_sword_key_effect()194 void CommandsEffects::restore_sword_key_effect() {
195 if (!sword_key_effects_saved.empty()) {
196 sword_key_effect = sword_key_effects_saved.top();
197 sword_key_effects_saved.pop();
198 }
199 }
200
201 // pause key
202
203 /**
204 * \brief Returns the current effect of the pause key.
205 * \return the current effect of the pause key
206 */
get_pause_key_effect()207 CommandsEffects::PauseKeyEffect CommandsEffects::get_pause_key_effect() {
208 return pause_key_effect;
209 }
210
211 /**
212 * \brief Sets the current effect of the pause key.
213 * \param pause_key_effect the current effect of the pause key
214 */
set_pause_key_effect(CommandsEffects::PauseKeyEffect pause_key_effect)215 void CommandsEffects::set_pause_key_effect(CommandsEffects::PauseKeyEffect pause_key_effect) {
216 this->pause_key_effect = pause_key_effect;
217 }
218
219 /**
220 * \brief Returns whether the pause key is enabled.
221 * \return true if the pause key is enabled, false otherwise
222 */
is_pause_key_enabled()223 bool CommandsEffects::is_pause_key_enabled() {
224 return pause_key_enabled;
225 }
226
227 /**
228 * \brief Sets whether the pause key is enabled.
229 * \param enable true to enable the pause key, false to disable it
230 */
set_pause_key_enabled(bool enable)231 void CommandsEffects::set_pause_key_enabled(bool enable) {
232 this->pause_key_enabled = enable;
233 }
234
235 /**
236 * \brief Saves the current effect of the pause key.
237 *
238 * Call restore_pause_key_effect to restore the pause key saved here.
239 */
save_pause_key_effect()240 void CommandsEffects::save_pause_key_effect() {
241 pause_key_effects_saved.push(get_pause_key_effect());
242 }
243
244 /**
245 * \brief Restores the pause key effect saved by the last
246 * call to save_pause_key_effect().
247 */
restore_pause_key_effect()248 void CommandsEffects::restore_pause_key_effect() {
249 if (!pause_key_effects_saved.empty()) {
250 pause_key_effect = pause_key_effects_saved.top();
251 pause_key_effects_saved.pop();
252 }
253 }
254
255 // item keys
256
257 /**
258 * \brief Returns whether the two item keys are enabled.
259 * \return true if the two item keys are enabled, false otherwise
260 */
are_item_keys_enabled()261 bool CommandsEffects::are_item_keys_enabled() {
262 return item_keys_enabled;
263 }
264
265 /**
266 * \brief Sets whether the two item keys are enabled.
267 * \param enable true to enable the two item keys, false to disable them
268 */
set_item_keys_enabled(bool enable)269 void CommandsEffects::set_item_keys_enabled(bool enable) {
270 this->item_keys_enabled = enable;
271 }
272
273 /**
274 * \brief Enables or disables the 5 keys.
275 * \param enable true to enable the 5 keys, false to disable them
276 */
set_all_keys_enabled(bool enable)277 void CommandsEffects::set_all_keys_enabled(bool enable) {
278 set_action_key_enabled(enable);
279 set_sword_key_enabled(enable);
280 set_pause_key_enabled(enable);
281 set_item_keys_enabled(enable);
282 }
283
284 }
285
286