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
|
#include "party.h"
#include "consts.h"
void Party::addMember(Game& game, int spriteId) {
int index = members_.size();
PartyMember newMember;
newMember.spriteId = spriteId;
if (index > 0) {
const Sprite& sprite = game.getSprite(spriteId);
newMember.movement = std::deque<Movement>(PARTY_FRAME_DELAY * index, {.pos = sprite.loc(), .dir = sprite.getDirection()});
}
members_.push_back(std::move(newMember));
game.setSpriteState(spriteId, "still");
}
void Party::move(Game& game, const Input& keystate) {
if (members_.empty()) {
return;
}
Direction dir = Direction::left;
if (!keystate.up && !keystate.down && !keystate.left && !keystate.right) {
if (state_ == State::Running) {
dir = lastDir_;
} else {
if (state_ == State::Normal) {
for (int i = 0; i < members_.size(); i++) {
game.setSpriteState(members_[i].spriteId, "still");
}
}
return;
}
} else {
if (keystate.up)
{
dir = Direction::up;
} else if (keystate.down)
{
dir = Direction::down;
}
if (keystate.left)
{
if (dir == Direction::up) {
dir = Direction::up_left;
} else if (dir == Direction::down) {
dir = Direction::down_left;
} else {
dir = Direction::left;
}
} else if (keystate.right)
{
if (dir == Direction::up) {
dir = Direction::up_right;
} else if (dir == Direction::down) {
dir = Direction::down_right;
} else {
dir = Direction::right;
}
}
}
lastDir_ = dir;
const Sprite& p1 = game.getSprite(members_[0].spriteId);
vec2i pLoc = p1.loc();
if (state_ == State::Crouching) {
for (int i = 0; i < members_.size(); i++) {
game.setSpriteDirection(members_[i].spriteId, dir);
}
return;
} else {
game.setSpriteDirection(members_[0].spriteId, dir);
}
int speed = MOVEMENT_SPEED;
if (state_ == State::Running) {
speed *= 2;
} else {
for (int i = 0; i < members_.size(); i++) {
game.setSpriteState(members_[i].spriteId, "walk");
}
}
pLoc += (unitVecInDirection(dir) * speed);
game.moveSprite(members_[0].spriteId, pLoc);
for (int i = 1; i < members_.size(); i++) {
const Sprite& pNext = game.getSprite(members_[i].spriteId);
const Movement& posdir = members_[i].movement.front();
game.moveSprite(members_[i].spriteId, posdir.pos);
game.setSpriteDirection(members_[i].spriteId, posdir.dir);
members_[i].movement.pop_front();
members_[i].movement.push_back({.pos = pLoc, .dir = dir});
}
}
void Party::beginCrouch(Game& game) {
if (state_ == State::Running) {
state_ = State::Normal;
// Double the movement buffer for the followers.
for (int i = 1; i < members_.size(); i++) {
std::deque<Movement> newMove;
vec2i lastPos = game.getSprite(members_[i].spriteId).loc();
while (!members_[i].movement.empty()) {
Movement m1 = members_[i].movement.front();
Movement m2 = m1;
m1.pos = (m1.pos + lastPos) / 2;
lastPos = m2.pos;
newMove.push_back(m1);
newMove.push_back(m2);
members_[i].movement.pop_front();
}
members_[i].movement = std::move(newMove);
}
} else {
state_ = State::Crouching;
for (int i = 0; i < members_.size(); i++) {
game.setSpriteState(members_[i].spriteId, "crouch");
}
}
}
void Party::endCrouch(Game& game) {
if (state_ == State::Crouching) {
state_ = State::Running;
for (int i = 0; i < members_.size(); i++) {
game.setSpriteState(members_[i].spriteId, "run");
// Halve the movement buffer for the followers.
if (i > 0) {
std::deque<Movement> newMove;
while (!members_[i].movement.empty()) {
newMove.push_back(members_[i].movement.front());
members_[i].movement.pop_front();
members_[i].movement.pop_front();
}
members_[i].movement = std::move(newMove);
}
}
}
}
|