-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
watch_bot.cpp
205 lines (163 loc) · 5.46 KB
/
watch_bot.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright (C) 2019, Nikolai Wuttke. All rights reserved.
*
* 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; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "watch_bot.hpp"
#include "base/match.hpp"
#include "data/sound_ids.hpp"
#include "engine/collision_checker.hpp"
#include "engine/entity_tools.hpp"
#include "engine/movement.hpp"
#include "engine/random_number_generator.hpp"
#include "engine/sprite_tools.hpp"
#include "engine/physical_components.hpp"
#include "game_logic/player.hpp"
#include "game_service_provider.hpp"
namespace rigel { namespace game_logic { namespace behaviors {
namespace {
const int LAND_ON_GROUND_ANIM[] = { 1, 2, 1 };
const int LOOK_LEFT_RIGHT_ANIM[] = {
1, 1, 1, 3, 3, 1, 6, 6, 7, 8, 7, 6, 6, 6, 7, 8, 7, 6, 6, 6,
1, 1, 3, 3, 3, 1, 1, 1, 6, 6, 1, 1
};
const int LOOK_RIGHT_LEFT_ANIM[] = {
1, 1, 6, 6, 7, 8, 7, 6, 6, 1, 1, 3, 3, 1, 6, 6, 1, 1, 1, 3,
4, 5, 4, 3, 3, 3, 4, 5, 4, 3, 1, 1
};
void advanceRandomNumberGenerator(GlobalDependencies& d) {
// The result isn't used, this is just done in order to exactly mimick
// how the original game uses the random number generator (since each
// invocation influences subsequent calls).
d.mpRandomGenerator->gen();
}
}
void WatchBot::update(
GlobalDependencies& d,
GlobalState& s,
const bool isOnScreen,
entityx::Entity entity
) {
using namespace engine;
using namespace engine::components;
using namespace watch_bot;
const auto& position = *entity.component<WorldPosition>();
const auto& bbox = *entity.component<BoundingBox>();
const auto& playerPos = s.mpPlayer->orientedPosition();
auto& animationFrame = entity.component<Sprite>()->mFramesToRender[0];
auto& movingBody = *entity.component<MovingBody>();
auto jump = [&]() {
animationFrame = 0;
const auto newOrientation = position.x > playerPos.x
? Orientation::Left
: Orientation::Right;
mState = Jumping{newOrientation};
};
base::match(mState,
[&, this](Jumping& state) {
moveHorizontally(
*d.mpCollisionChecker,
entity,
orientation::toMovement(state.mOrientation));
const auto speed = state.mFramesElapsed < 2 ? 2 : 1;
const auto moveResult = moveVertically(
*d.mpCollisionChecker,
entity,
-speed);
++state.mFramesElapsed;
const auto collidedWithCeiling = moveResult != MovementResult::Completed;
if (collidedWithCeiling || state.mFramesElapsed >= 5) {
movingBody.mGravityAffected = true;
movingBody.mVelocity.y = -0.5f;
mState = Falling{state.mOrientation};
return;
}
},
[&, this](const Falling& state) {
moveHorizontally(
*d.mpCollisionChecker,
entity,
orientation::toMovement(state.mOrientation));
if (d.mpCollisionChecker->isOnSolidGround(position, bbox)) {
land(entity, d);
}
},
[&, this](OnGround& state) {
const auto shouldLookAround = (d.mpRandomGenerator->gen() & 0x21) != 0;
++state.mFramesElapsed;
if (shouldLookAround && state.mFramesElapsed == 1) {
// Stop landing animation
removeSafely<AnimationSequence>(entity);
const auto orientation = d.mpRandomGenerator->gen() % 2 == 0
? Orientation::Left
: Orientation::Right;
mState = LookingAround{orientation};
return;
}
if (state.mFramesElapsed == 3) {
jump();
}
},
[&, this](LookingAround& state) {
// TODO: Is there a way to use AnimationSequence here, but still only
// update on odd frames?
if (state.mFramesElapsed < 32) {
const auto sequence = state.mOrientation == Orientation::Left
? LOOK_LEFT_RIGHT_ANIM
: LOOK_RIGHT_LEFT_ANIM;
animationFrame = sequence[state.mFramesElapsed];
}
if (s.mpPerFrameState->mIsOddFrame) {
++state.mFramesElapsed;
}
if (state.mFramesElapsed == 33) {
animationFrame = 1;
advanceRandomNumberGenerator(d);
} else if (state.mFramesElapsed == 34) {
jump();
}
}
);
engine::synchronizeBoundingBoxToSprite(entity);
}
void WatchBot::onCollision(
GlobalDependencies& d,
GlobalState& s,
const engine::events::CollidedWithWorld&,
entityx::Entity entity
) {
using namespace watch_bot;
base::match(mState,
[&, this](const Falling& state) {
land(entity, d);
},
[](const auto&) {}
);
}
void WatchBot::land(
entityx::Entity entity,
GlobalDependencies& d
) {
using namespace engine::components;
using namespace watch_bot;
const auto isOnScreen = entity.component<Active>()->mIsOnScreen;
if (isOnScreen) {
d.mpServiceProvider->playSound(data::SoundId::DukeJumping);
}
engine::startAnimationSequence(entity, LAND_ON_GROUND_ANIM);
entity.component<MovingBody>()->mGravityAffected = false;
mState = OnGround{};
advanceRandomNumberGenerator(d);
engine::synchronizeBoundingBoxToSprite(entity);
}
}}}