forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature_action.cpp
62 lines (49 loc) · 1.24 KB
/
creature_action.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
#include "stdafx.h"
#include "creature_action.h"
CreatureAction::CreatureAction(const Creature* c, ActionFun f) : action(f), performer(c) {
}
CreatureAction::CreatureAction(const string& msg)
: action(nullptr), failedMessage(msg) {
}
#ifndef RELEASE
static bool usageCheck = false;
CreatureAction::~CreatureAction() {
CHECK(wasUsed || !usageCheck);
}
void CreatureAction::checkUsage(bool b) {
usageCheck = b;
}
CreatureAction::CreatureAction(const CreatureAction& a)
: action(a.action), failedMessage(a.failedMessage), performer(a.performer), wasUsed(true) {
a.wasUsed = true;
}
#endif
void CreatureAction::perform(Creature* c) {
CHECK(c == performer);
CHECK(action);
action(c);
#ifndef RELEASE
wasUsed = true;
#endif
}
CreatureAction CreatureAction::prepend(ActionFun f) {
CHECK(action);
ActionFun tmp = action;
action = [=] (Creature* c) { f(c); tmp(c); };
return *this;
}
CreatureAction CreatureAction::append(ActionFun f) {
CHECK(action);
ActionFun tmp = action;
action = [=] (Creature* c) { tmp(c); f(c); };
return *this;
}
string CreatureAction::getFailedReason() const {
return failedMessage;
}
CreatureAction::operator bool() const {
#ifndef RELEASE
wasUsed = true;
#endif
return action != nullptr;
}