/
game_vehicle.cpp
299 lines (262 loc) · 7.18 KB
/
game_vehicle.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
// Headers
#include <cassert>
#include "data.h"
#include "main_data.h"
#include "game_system.h"
#include "game_map.h"
#include "game_player.h"
#include "game_vehicle.h"
#include "output.h"
const char Game_Vehicle::TypeNames[4][8] {
"Party", // RPG_RT special case, see CommandSetVehicleLocation
"Boat",
"Ship",
"Airship"
};
static RPG::SaveVehicleLocation* getDataFromType(Game_Vehicle::Type ty) {
assert(ty >= 1 && ty <= 3 && "Invalid Vehicle index");
switch (ty) {
case Game_Vehicle::Boat:
return &Main_Data::game_data.boat_location;
case Game_Vehicle::Ship:
return &Main_Data::game_data.ship_location;
case Game_Vehicle::Airship:
return &Main_Data::game_data.airship_location;
case Game_Vehicle::None:
break;
}
return nullptr;
}
Game_Vehicle::Game_Vehicle(Type _type) :
Game_Character(Vehicle, getDataFromType(_type))
{
type = _type;
SetDirection(Left);
SetSpriteDirection(Left);
SetAnimationType(AnimType::AnimType_non_continuous);
SetLayer(RPG::EventPage::Layers_same);
LoadSystemSettings();
}
void Game_Vehicle::LoadSystemSettings() {
switch (type) {
case None:
break;
case Boat:
SetSpriteName(Data::system.boat_name);
SetSpriteIndex(Data::system.boat_index);
SetMapId(Data::treemap.start.boat_map_id);
SetX(Data::treemap.start.boat_x);
SetY(Data::treemap.start.boat_y);
break;
case Ship:
SetSpriteName(Data::system.ship_name);
SetSpriteIndex(Data::system.ship_index);
SetMapId(Data::treemap.start.ship_map_id);
SetX(Data::treemap.start.ship_x);
SetY(Data::treemap.start.ship_y);
break;
case Airship:
SetSpriteName(Data::system.airship_name);
SetSpriteIndex(Data::system.airship_index);
SetMapId(Data::treemap.start.airship_map_id);
SetX(Data::treemap.start.airship_x);
SetY(Data::treemap.start.airship_y);
break;
}
}
RPG::Music& Game_Vehicle::GetBGM() {
switch (type) {
case None:
assert(false);
break;
case Boat:
return Game_System::GetSystemBGM(Game_System::BGM_Boat);
case Ship:
return Game_System::GetSystemBGM(Game_System::BGM_Ship);
case Airship:
return Game_System::GetSystemBGM(Game_System::BGM_Airship);
}
// keeping the compiler happy
return Game_System::GetSystemBGM(Game_System::BGM_Battle);
}
void Game_Vehicle::Refresh() {
if (IsInUse()) {
SetMapId(Game_Map::GetMapId());
} else if (IsInCurrentMap()) {
MoveTo(GetX(), GetY());
}
switch (type) {
case None:
break;
case Boat:
case Ship:
SetMoveSpeed(RPG::EventPage::MoveSpeed_normal);
break;
case Airship:
SetMoveSpeed(RPG::EventPage::MoveSpeed_double);
break;
}
}
void Game_Vehicle::SetPosition(int _map_id, int _x, int _y) {
SetMapId(_map_id);
MoveTo(_x, _y);
}
bool Game_Vehicle::IsInCurrentMap() const {
return GetMapId() == Game_Map::GetMapId();
}
bool Game_Vehicle::IsInPosition(int x, int y) const {
return IsInCurrentMap() && Game_Character::IsInPosition(x, y);
}
bool Game_Vehicle::IsAscending() const {
return data()->remaining_ascent > 0;
}
bool Game_Vehicle::IsDescending() const {
return data()->remaining_descent > 0;
}
bool Game_Vehicle::IsAscendingOrDescending() const {
return IsAscending() || IsDescending();
}
bool Game_Vehicle::GetVisible() const {
return IsInCurrentMap() && Game_Character::GetVisible();
}
void Game_Vehicle::GetOn() {
if (type == Airship) {
data()->remaining_ascent = SCREEN_TILE_SIZE;
SetFlying(true);
Main_Data::game_player->SetFlying(true);
}
Game_System::BgmPlay(GetBGM());
}
void Game_Vehicle::GetOff() {
if (type == Airship) {
data()->remaining_descent = SCREEN_TILE_SIZE;
} else {
Main_Data::game_player->UnboardingFinished();
}
// Get off airship can be trigger while airship is moving. Don't break the animation
// until its finished.
if (type != Airship || (!IsMoving() && !IsJumping())) {
SetDirection(Left);
SetSpriteDirection(Left);
}
}
bool Game_Vehicle::IsInUse() const {
return Main_Data::game_player->GetVehicle() == this;
}
bool Game_Vehicle::IsAboard() const {
return IsInUse() && Main_Data::game_player->IsAboard();
}
void Game_Vehicle::SyncWithPlayer() {
SetX(Main_Data::game_player->GetX());
SetY(Main_Data::game_player->GetY());
SetRemainingStep(Main_Data::game_player->GetRemainingStep());
SetJumping(Main_Data::game_player->IsJumping());
SetBeginJumpX(Main_Data::game_player->GetBeginJumpX());
SetBeginJumpY(Main_Data::game_player->GetBeginJumpY());
if (!IsAscendingOrDescending()) {
SetDirection(Main_Data::game_player->GetDirection());
SetSpriteDirection(Main_Data::game_player->GetSpriteDirection());
} else {
if (!IsMoving() && !IsJumping()) {
SetDirection(Left);
SetSpriteDirection(Left);
}
}
}
int Game_Vehicle::GetAltitude() const {
if (!IsFlying())
return 0;
else if (IsAscending())
return (SCREEN_TILE_SIZE - data()->remaining_ascent) / (SCREEN_TILE_SIZE / TILE_SIZE);
else if (IsDescending())
return data()->remaining_descent / (SCREEN_TILE_SIZE / TILE_SIZE);
else
return SCREEN_TILE_SIZE / (SCREEN_TILE_SIZE / TILE_SIZE);
}
int Game_Vehicle::GetScreenY(bool apply_shift) const {
return Game_Character::GetScreenY(apply_shift) - GetAltitude();
}
bool Game_Vehicle::IsMovable() {
if (!IsInUse())
return false;
if (type == Airship && (IsAscending() || IsDescending()))
return false;
return !IsMoving();
}
bool Game_Vehicle::CanLand() const {
return Game_Map::CanLandAirship(GetX(), GetY());
}
void Game_Vehicle::UpdateAnimationAirship() {
if (IsAboard()) {
const auto limit = 11;
IncAnimCount();
if (GetAnimCount() > limit) {
IncAnimFrame();
}
} else {
ResetAnimation();
}
}
void Game_Vehicle::UpdateAnimationShip() {
const auto limit = 15;
IncAnimCount();
if (GetAnimCount() > limit) {
IncAnimFrame();
}
}
void Game_Vehicle::AnimateAscentDescent() {
if (!IsStopping()) {
return;
}
if (IsAscending()) {
data()->remaining_ascent = data()->remaining_ascent - 8;
} else if (IsDescending()) {
data()->remaining_descent = data()->remaining_descent - 8;
if (!IsDescending()) {
if (CanLand()) {
Main_Data::game_player->UnboardingFinished();
SetFlying(false);
Main_Data::game_player->SetFlying(false);
} else {
// Can't land here, ascend again
data()->remaining_ascent = SCREEN_TILE_SIZE;
}
}
}
}
void Game_Vehicle::Update() {
if (IsProcessed()) {
return;
}
SetProcessed(true);
if (!IsAboard()) {
Game_Character::UpdateMovement();
}
if (type == Airship) {
UpdateAnimationAirship();
} else {
UpdateAnimationShip();
}
}
bool Game_Vehicle::CheckEventTriggerTouch(int /* x */, int /* y */) {
return false;
}
int Game_Vehicle::GetVehicleType() const {
return data()->vehicle;
}