/
main.cpp
294 lines (260 loc) · 6.87 KB
/
main.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
/*==============================================================
character movement testing using Fly2
- Load a scene
- Generate a terrain object
- Load a character
- Control a character to move
- Change poses
(C)2012-2015 Chuan-Chang Wang, All Rights Reserved
Created : 0802, 2012
Last Updated : 1004, 2015, Kevin C. Wang
===============================================================*/
#include "FlyWin32.h"
#include "DefFunction.h"
#include "template_3D.h"
#include<vector>
#include<map>
#include<algorithm>
#include<assert.h>
// some globals
int frame = 0;
int oldX, oldY, oldXM, oldYM, oldXMM, oldYMM;
#include "menumode.h"
#include "optionmode.h"
#include "selectmode.h"
#include "fightmode.h"
OBJECTid cID;
int now_mode = 1, pre_mode = 0;
//1 menu mode
//2 option mode
//3 select mode
//4 load to fight mode
//5 fight mode
//pre mode 0 -- bgm001, 1 -- bgm002
// hotkey callbacks
void QuitGame ( BYTE, BOOL4 );
void Movement ( BYTE, BOOL4 );
// timer callbacks
void GameAI ( int );
void RenderIt ( int );
void PlaySound(int skip);
// mouse callbacks
void InitPivot ( int, int );
void PivotCam ( int, int );
void InitMove ( int, int );
void MoveCam ( int, int );
void InitZoom ( int, int );
void ZoomCam ( int, int );
/*------------------
the main program
C.Wang 1010, 2014
-------------------*/
void FyMain ( int argc, char **argv )
{
// create a new world
BOOL4 beOK = FyStartFlyWin32 ( "NTU@2014 Homework #01 - Use Fly2", 0, 0, 800, 600, FALSE );
ini_actions_name();
MM.load();
OM.load();
SM.load();
FM.load();
cID = MM.cID;
// set Hotkeys
FyDefineHotKey ( FY_ESCAPE, QuitGame, FALSE ); // escape for quiting the game
FyDefineHotKey ( FY_UP, Movement, FALSE ); // Up for moving forward
FyDefineHotKey ( FY_RIGHT, Movement, FALSE ); // Right for turning right
FyDefineHotKey ( FY_LEFT, Movement, FALSE ); // Left for turning left
FyDefineHotKey ( FY_DOWN, Movement, FALSE ); // Down for moving backward
FyDefineHotKey ( FY_C, Movement, FALSE); // defence
FyDefineHotKey ( FY_X, Movement, FALSE ); // aim
FyDefineHotKey ( FY_Z, Movement, FALSE ); // normal attack
FyDefineHotKey ( FY_L, Movement, FALSE ); // something
// define some mouse functions
FyBindMouseFunction ( LEFT_MOUSE, InitPivot, PivotCam, NULL, NULL );
FyBindMouseFunction ( MIDDLE_MOUSE, InitZoom, ZoomCam, NULL, NULL );
FyBindMouseFunction ( RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL );
// bind timers, frame rate = 30 fps
FyBindTimer(0, FPS, GameAI, TRUE);
FyBindTimer(1, FPS, RenderIt, TRUE);
FyBindTimer(2, FPS, PlaySound, TRUE);
// invoke the system
FyInvokeFly ( TRUE );
}
/*-------------------------------------------------------------
Play background music && loop
--------------------------------------------------------------*/
void PlaySound(int skip){
FnAudio sd;
sd.ID(OM.BGMid);
if (now_mode == 5 && pre_mode == 0){
sd.Stop();
if (sd.Load("Bgm/bgm002") == FALSE){
exit(3);
}
//sd.SetVolume(OM.volume);
OM.Movement(FY_RIGHT, TRUE);
OM.Movement(FY_LEFT, TRUE);
pre_mode = 1;
}
else if (pre_mode == 1 && now_mode == 1){
sd.Stop();
if (sd.Load("Bgm/bgm001") == FALSE){
exit(3);
}
//sd.SetVolume(OM.volume);
OM.Movement(FY_RIGHT, TRUE);
OM.Movement(FY_LEFT, TRUE);
pre_mode = 0;
}
if (!(sd.IsPlaying())){
sd.Play(LOOP);
//sd.SetVolume(OM.volume);
OM.Movement(FY_RIGHT, TRUE);
OM.Movement(FY_LEFT, TRUE);
}
}
/*-------------------------------------------------------------
30fps timer callback in fixed frame rate for major game loop
--------------------------------------------------------------*/
void GameAI ( int skip )
{
if (now_mode == 1)MM.GameAI(skip);
if (now_mode == 2)OM.GameAI(skip);
if (now_mode == 3)SM.GameAI(skip);
if (now_mode == 4)return;
if (now_mode == 5)FM.GameAI(skip);
}
/*----------------------
perform the rendering
C.Wang 0720, 2006
-----------------------*/
void RenderIt( int skip )
{
if (now_mode == 1)MM.RenderIt(skip);
if (now_mode == 2)OM.RenderIt(skip);
if (now_mode == 3)SM.RenderIt(skip);
if (now_mode == 4 || now_mode == 5)FM.RenderIt(skip);
// swap buffer
FySwapBuffers ();
}
/*------------------
movement control
-------------------*/
void Movement ( BYTE code, BOOL4 value )
{
int next_mode = now_mode;
if (now_mode == 1)next_mode = MM.Movement(code, value);
if (now_mode == 2)next_mode = OM.Movement(code, value);
if (now_mode == 3)next_mode = SM.Movement(code, value);
if (now_mode == 4)return;
if (now_mode == 5)next_mode = FM.Movement(code, value);
if (!(1 <= next_mode && next_mode <= 5)){
fprintf(stderr, "mode %d return wrong mode %d", now_mode, next_mode);
}
assert(1 <= next_mode && next_mode <= 5);
now_mode = next_mode;
if (now_mode == 1)cID = MM.cID;
if (now_mode == 2)cID = OM.cID;
if (now_mode == 3)cID = SM.cID;
if (now_mode == 4){
FM.true_load(SM.fighter_mode, cname[SM.select_character].c_str(), cname[SM.select_enemy].c_str());
now_mode = 5;
}
if (now_mode == 5)cID = FM.cID;
}
/*------------------
quit the demo
C.Wang 0327, 2005
-------------------*/
void QuitGame ( BYTE code, BOOL4 value )
{
if ( code == FY_ESCAPE )
if ( value )
FyQuitFlyWin32 ();
}
/*-----------------------------------
initialize the pivot of the camera
C.Wang 0329, 2005
------------------------------------*/
void InitPivot ( int x, int y )
{
oldX = x;
oldY = y;
frame = 0;
}
/*------------------
pivot the camera
C.Wang 0329, 2005
-------------------*/
void PivotCam ( int x, int y )
{
FnObject model;
if ( x != oldX ) {
model.ID ( cID );
model.Rotate ( Z_AXIS, (float) (x - oldX), GLOBAL );
oldX = x;
}
if ( y != oldY ) {
model.ID ( cID );
model.Rotate ( X_AXIS, (float) (y - oldY), GLOBAL );
oldY = y;
}
}
/*----------------------------------
initialize the move of the camera
C.Wang 0329, 2005
-----------------------------------*/
void InitMove ( int x, int y )
{
oldXM = x;
oldYM = y;
frame = 0;
}
/*------------------
move the camera
C.Wang 0329, 2005
-------------------*/
void MoveCam ( int x, int y )
{
float dx = float(x - oldXM);
float dy = float(y - oldYM);
float dd = sqrt(dx*dx + dy*dy);
if (sgn(dd) == 0)return;
dx /= dd;
dy /= dd;
FnObject model;
if ( x != oldXM ) {
model.ID ( cID );
model.Translate(0.1f*dx, 0.0f, 0.0f, LOCAL);
oldXM = x;
}
if ( y != oldYM ) {
model.ID ( cID );
model.Translate(0.0f, 0.1f*dy, 0.0f, LOCAL);
oldYM = y;
}
}
/*----------------------------------
initialize the zoom of the camera
C.Wang 0329, 2005
-----------------------------------*/
void InitZoom ( int x, int y )
{
oldXMM = x;
oldYMM = y;
frame = 0;
}
/*------------------
zoom the camera
C.Wang 0329, 2005
-------------------*/
void ZoomCam ( int x, int y )
{
if ( x != oldXMM || y != oldYMM ) {
FnObject model;
model.ID ( cID );
model.Translate ( 0.0f, 0.0f, (float) (x - oldXMM)*1.0f, LOCAL );
oldXMM = x;
oldYMM = y;
}
}