-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cpp
653 lines (565 loc) · 15 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*==============================================================
This is a demo program for TheFly System
Hello !
(C)2004 Chuan-Chang Wang, All Rights Reserved
Created : 0303, 2004, C. Wang
Last Updated : 1010, 2004, C.Wang
===============================================================*/
#include "TheFlyWin32.h"
#include "KeyboardControl.h"
#include "LyubuStateMachine.h"
#include "AIControl.h"
#include "BattleRoom.h"
#include "FyFx.h"
#include "FyMedia.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
extern int errno;
int oldX, oldY, oldXM, oldYM, oldXMM, oldYMM;
WORLDid gID = FAILED_ID;
VIEWPORTid vID;
SCENEid sID;
OBJECTid nID, cID, lID;
OBJECTid tID;
ACTORid lyubu;
ACTORid donzo, robbers[100];
int robbers_count = 0;
KeyboardControl *kc;
AIControl *npc;
BattleRoom *bRoom;
LyubuStateMachine * lyubuState;
AUDIOid audioBG;
AUDIOid audioG;//guard
AUDIOid audioD;//damage
eF3DFX *fx00 = NULL;
char debug[1024] = "\0";
char loopBuff[1024] = "\0";
void debug_message(char*, char*);
void QuitGame(WORLDid, BYTE, BOOL);
void FunctionKey(WORLDid, BYTE, BOOL);
BOOL initLyubu();
BOOL initNPC();
BOOL initBattleRoom(GameControl *player, AIControl *npc);
BOOL initFX();
void CharacterInit();
void Reset(WORLDid gID, BYTE code, BOOL value);
void PlayAction(int skip);
void GameAI(int);
void Render(int);
void GetPosDetail(char *);
BOOL BlindKeys();
void KeyboardAttackCommand(WORLDid gID, BYTE code, BOOL value);
void InitPivot(WORLDid, int, int);
void PivotCam(WORLDid, int, int);
void InitMove(WORLDid, int, int);
void MoveCam(WORLDid, int, int);
void InitZoom(WORLDid, int, int);
void ZoomCam(WORLDid, int, int);
/*------------------
the main program
C.Wang 0308, 2004
-------------------*/
void main(int argc, char **argv)
{
// create a new world
gID = FyWin32CreateWorld("GameProgramming 2011 !", 0, 0, 800, 600, 32, FALSE);
FnWorld gw;
gw.Object(gID);
gw.SetObjectPath("Data\\NTU4\\Scenes"); // for terrain loading
gw.SetScenePath("Data\\NTU4\\Scenes"); // for scene loading
gw.SetTexturePath("Data\\NTU4\\Scenes\\Textures");
gw.SetShaderPath("Data\\NTU4\\Shaders");
// create a viewport
vID = gw.CreateViewport(0, 0, 800, 600);
FnViewport vp;
vp.Object(vID);
vp.SetBackgroundColor(0.3f, 0.4f, 0.5f);
// create a 3D scene & the 3D entities
sID = gw.CreateScene(10);
FnScene scene;
scene.Object(sID);
BOOL flag;
flag = scene.Load("scene2");
if (flag == FALSE){
sprintf(debug, "%s scene load fail\n", debug);
}
nID = scene.CreateObject(ROOT);
cID = scene.CreateCamera(ROOT);
lID = scene.CreateLight(ROOT);
// create terrain
tID = scene.CreateTerrain(ROOT);
if (tID == FAILED_ID){
exit(-1);
}
FnTerrain terrain;
terrain.Object(tID);
flag = terrain.Load("terrain");
if (flag == FALSE){
sprintf(debug, "%s ground load fail\n", debug);
}
flag = terrain.GenerateTerrainData();
if (flag == FALSE){
sprintf(debug, "%s ground generate fail\n", debug);
//exit(-1);
}
// translate the camera
FnCamera camera;
camera.Object(cID);
camera.Rotate(X_AXIS, 90.0f, LOCAL);
camera.Translate(0.0f, 10.0f, 100.0f, LOCAL);
CharacterInit();
// translate the light
FnLight light;
light.Object(lID);
light.SetName("MainLight");
light.Translate(-50.0f, -50.0f, 50.0f, GLOBAL);
// set Hotkeys
BlindKeys();
/* bind a timer, frame rate = 30 fps */
FyBindTimer(0, 30.0f, GameAI, TRUE);
FyBindTimer(1, 30.0f, Render, TRUE);
// invoke the system
FyInvokeTheFly(TRUE);
}
void CharacterInit(){
initLyubu();
initNPC();
initBattleRoom(kc, npc);
initFX();
}
BOOL initLyubu(){ // init Lyubu and Camera
FnWorld gw;
// create actor:lyubu
gw.Object(gID);
gw.SetTexturePath("Data\\NTU4\\Characters");
gw.SetCharacterPath("Data\\NTU4\\Characters");
FnScene scene;
scene.Object(sID);
lyubu = scene.LoadActor("Lyubu");
if (lyubu == FAILED_ID){
return FALSE;
}
FnActor actor;
actor.Object(lyubu);
float pos[3];
pos[0] = 3569.0;
pos[1] = -3210;
pos[2] = 100;
actor.SetPosition(pos);
//flag = actor.PutOnTerrain(tID,FALSE,0.0,10.0,10.0,30.0,100.0);
BOOL flag = actor.PutOnTerrain(tID,FALSE,0.0);
if (flag == FALSE){
sprintf(debug, "%s put on fail\n", debug);
return FALSE;
}
// set lyubu idle action
ACTIONid idleID = actor.GetBodyAction(NULL,"CombatIdle");
//ACTIONid idleID = actor.GetBodyAction(NULL,"WALK");
if (idleID == FAILED_ID){
sprintf(debug, "%s get action failed\n", debug);
return FALSE;
}else{
sprintf(debug, "%s get action success\n", debug);
}
if (actor.MakeCurrentAction(0,NULL,idleID) == FAILED_ID){
sprintf(debug, "%s make current fail\n", debug);
}else{
sprintf(debug, "%s make action success\n", debug);
}
if (actor.Play(0,START, 0.0, FALSE,TRUE) == FALSE){
sprintf(debug, "%s play action failed\n", debug);
}
FnCamera camera;
camera.Object(cID);
pos[0] = 3569.0;
pos[1] = -3570;
pos[2] = 115.0;
camera.SetWorldPosition(pos);
float fDir[3];
float uDir[3];
fDir[0] = 0.0;
fDir[1] = 1.0;
fDir[2] = -0.2;
uDir[0] = 0.0;
uDir[1] = 0.2;
uDir[2] = 1.0;
camera.SetWorldDirection(fDir,uDir);
sprintf(debug, "%sface:%f %f %f\n", debug,fDir[0],fDir[1],fDir[2]);
sprintf(debug, "%sup:%f %f %f\n", debug, uDir[0],uDir[1],uDir[2]);
fDir[2] = 0.0f;
uDir[1] = 0.0f;
actor.SetWorldDirection(fDir,uDir);
actor.GetWorldPosition(pos);
lyubuState = new LyubuStateMachine(lyubu, "Data\\LyubuAction.txt");
kc = new KeyboardControl(lyubuState, cID);
kc->InitCamTargetPos(pos);
sprintf(debug, "%sactorID=%d cID=%d",debug, lyubu, cID);
return TRUE;
}
BOOL initNPC(){
FnWorld gw;
gw.Object(gID);
gw.SetTexturePath("Data\\NTU4\\Characters");
gw.SetCharacterPath("Data\\NTU4\\Characters");
FnScene scene;
scene.Object(sID);
donzo = scene.LoadActor("Donzo");
if (donzo == FAILED_ID){
return FALSE;
}
FnActor actor;
actor.Object(donzo);
float pos[3];
pos[0] = -909;
pos[1] = -2300;
pos[2] = 100;
actor.SetPosition(pos);
BOOL flag = actor.PutOnTerrain(tID,FALSE,0.0);
if (flag == FALSE){
sprintf(debug, "%s put on fail\n", debug);
return FALSE;
}
// set donzo idle action
ACTIONid idleID = actor.GetBodyAction(NULL,"CombatIdle");
//actor.MakeCurrentAction(0,NULL,idleID,0.0,TRUE);
//if (actor.MakeCurrentAction(0,NULL,FAILED_ID) == FAILED_ID){
if (actor.MakeCurrentAction(0,NULL,idleID) == FAILED_ID){
sprintf(debug, "%s make current fail\n", debug);
}else{
sprintf(debug, "%s make action success\n", debug);
}
if (actor.Play(0,START, 0.0, FALSE,TRUE) == FALSE){
sprintf(debug, "%s play action failed\n", debug);
}
ifstream fin;
fin.open("Data\\npcPos.txt");
if (!fin.is_open()) {
sprintf(debug, "%s open file error\n", debug);
sprintf(debug, "%s %s\n", debug, strerror(errno));
}
float pos_robber[3];
pos_robber[2] = 100;
int i = 0;
FnActor actor_robbers[100];
while (fin >> pos_robber[0] && fin >> pos_robber[1]) {
sprintf(debug, "%s in the loading npc while loop\n", debug);
robbers[i] = scene.LoadActor("Robber02");
if (robbers[i] == FAILED_ID){
sprintf(debug, "%s Robber02 load fail\n", debug);
return FALSE;
}
else {
sprintf(debug, "%s Robber02 load success\n", debug);
}
actor_robbers[i].Object(robbers[i]);
actor_robbers[i].SetPosition(pos_robber);
flag = actor_robbers[i].PutOnTerrain(tID,FALSE,0.0);
if (flag == FALSE){
return FALSE;
}
ACTIONid idleID_robber = actor_robbers[i].GetBodyAction(NULL,"CombatIdle");
//actor.MakeCurrentAction(0,NULL,idleID,0.0,TRUE);
//if (actor.MakeCurrentAction(0,NULL,FAILED_ID) == FAILED_ID){
if (actor_robbers[i].MakeCurrentAction(0,NULL,idleID_robber) == FAILED_ID){
sprintf(debug, "%s make current fail\n", debug);
}else{
sprintf(debug, "%s make action success\n", debug);
}
if (actor_robbers[i].Play(0,START, 0.0, FALSE,TRUE) == FALSE){
sprintf(debug, "%s play action failed\n", debug);
}
i++;
}
robbers_count = i;
npc = new AIControl(lyubu);
npc->AddBossNPC(donzo,"Data\\DozonAction.txt");
for (i = 0; i < robbers_count; i++) {
npc->AddNPC(robbers[i],"Data\\Robber02Action.txt");
}
return TRUE;
}
BOOL initBattleRoom(GameControl *player, AIControl *npc){
bRoom = new BattleRoom(player->mainChar, npc->npcStateMachineList);
return TRUE;
}
BOOL initFX(){
FnWorld gw;
gw.Object(gID);
gw.SetTexturePath("Data\\FXs\\Textures");
gw.SetObjectPath("Data\\FXs\\Models");
/*
fx00 = new eF3DFX(sID);
fx00->SetWorkPath("Data\\FXs");
BOOL beOK = fx00->Load("Smoke_01");
if (beOK == FALSE){
sprintf(debug, "%s fx load failed\n", debug);
return FALSE;
}else{
sprintf(debug, "%s fx load successful\n", debug);
}
float pos[3];
pos[0] = 3569.0;
pos[1] = -3210.0;
pos[2] = 10.0;
eF3DBaseFX *fx;
int i, numFX = fx00->NumberFXs();
for (i = 0; i < numFX; i++) {
fx = fx00->GetFX(i);
fx->InitPosition(pos);
}*/
/*
FyBeginMedia("Data\\Media", 1);
HWND hwnd = FyGetWindowHandle(gw.Object());
MEDIAid mmID = FyCreateMediaPlayer(0, "battle", 0, 0, 0, 0);
FnMedia mP;
mP.Object(mmID);
mP.Play(ONCE);
*/
gw.SetAudioPath("Data\\Audio");
audioBG = gw.CreateAudio();
FnAudio audio;
audio.Object(audioBG);
BOOL beA = audio.Load("battle");
audio.Play(LOOP);
audioD = gw.CreateAudio();
audio.Object(audioD);
beA = audio.Load("damage");
if (beA == FALSE){
sprintf(debug, "%s damage load failed\n", debug);
}
audioG = gw.CreateAudio();
audio.Object(audioG);
beA = audio.Load("guard");
if (beA == FALSE){
sprintf(debug, "%s guard load failed\n", debug);
}
return TRUE;
}
void Reset(WORLDid gID, BYTE code, BOOL value){
if (code == FY_F1) {
if (value) {
FnScene scene;
scene.Object(sID);
scene.DeleteActor(lyubu);
scene.DeleteActor(donzo);
int i;
for (i = 0; i < robbers_count; i++) {
scene.DeleteActor(robbers[i]);
}
FnWorld gw;
gw.Object(gID);
gw.DeleteAudio(audioG);
gw.DeleteAudio(audioD);
gw.DeleteAudio(audioBG);
debug[0] = '\0';
ActorStateMachine * lyubuState = kc->mainChar;
delete lyubuState;
delete kc;
delete npc;
delete bRoom;
if (fx00 != NULL){
delete fx00;
}
fx00 = NULL;
CharacterInit();
}
}
}
void KeyboardAttackCommand(WORLDid gID, BYTE code, BOOL value){
if (code == FY_J && FyCheckHotKeyStatus(FY_J) == TRUE){
kc->AppendAttackCode(NORMAL_ATT);
}else if(code == FY_K && FyCheckHotKeyStatus(FY_K) == TRUE){
kc->AppendAttackCode(HEAVY_ATT);
}else if(code == FY_L && FyCheckHotKeyStatus(FY_L) == TRUE){
kc->AppendAttackCode(ULTIMATE_ATT);
}else if (code == FY_N && FyCheckHotKeyStatus(FY_N) == TRUE){
kc->ResetCamView();
}
}
void PlayAction(int skip){
kc->PlayAction(skip);
npc->PlayAction(skip);
if (fx00 != NULL) {
BOOL beOK = fx00->Play((float) skip);
if (!beOK) {
fx00->Reset(); // make it from the starting position and play it again
// if you just play the FX once, here you need to delete the FX
// delete fx00;
// fx00 = NULL;
}
}
/*
if (lyubuState->isNowAttackState()) {
kc->CamFallow();
lyubuState->resetAttackState();
}
*/
//bRoom->RefreshArena();
}
void GetPosDetail(char *buffer){
FnCamera camera;
camera.Object(cID);
float fDir[3];
float uDir[3];
float pos[3];
camera.GetWorldPosition(pos);
camera.GetWorldDirection(fDir, uDir);
sprintf(buffer, "carmer pos[0] = %f,pos[1] = %f,pos[2] = %f \n", pos[0],pos[1],pos[2]);
sprintf(buffer, "%scarmer fDir[0] = %f,fDir[1] = %f,fDir[2] = %f \n", buffer, fDir[0],fDir[1],fDir[2]);
sprintf(buffer, "%scarmer uDir[0] = %f,uDir[1] = %f,uDir[2] = %f \n", buffer, uDir[0],uDir[1],uDir[2]);
FnActor actor;
actor.Object(lyubu);
actor.GetWorldPosition(pos);
sprintf(buffer, "%slyubu pos[0] = %f,pos[1] = %f,pos[2] = %f \n", buffer, pos[0],pos[1],pos[2]);
}
BOOL BlindKeys(){
FyDefineHotKey(FY_ESCAPE, QuitGame, FALSE);
FyDefineHotKey(FY_F1, Reset, FALSE);
FyDefineHotKey(FY_F2, FunctionKey, FALSE);
FyDefineHotKey(FY_F3, FunctionKey, FALSE);
// 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);
FyDefineHotKey(FY_J, KeyboardAttackCommand, FALSE);
FyDefineHotKey(FY_K, KeyboardAttackCommand, FALSE);
FyDefineHotKey(FY_L, KeyboardAttackCommand, FALSE);
FyDefineHotKey(FY_N, KeyboardAttackCommand, FALSE);
return TRUE;
}
void FunctionKey(WORLDid gID, BYTE code, BOOL value){
if (code == FY_F2 && FyCheckHotKeyStatus(FY_F2) == TRUE){
debug[0] = '\0';
}else if (code == FY_F3 && FyCheckHotKeyStatus(FY_F3) == TRUE){
if (bRoom->hurt == FALSE){
bRoom->hurt = TRUE;
}else{
bRoom->hurt = FALSE;
}
}
}
//------------------------------------------------------------------------------------
// timer callback function which will be invoked by TheFly3D System every 1/30 second
// C.Wang 0308, 2004
//------------------------------------------------------------------------------------
void GameAI(int skip)
{
kc->Command();
bRoom->RefreshArena();
kc->CamPointToActor();
npc->moveTowardLyubu();
}
void Render(int skip){
// render the scene
FnViewport vp;
vp.Object(vID);
vp.Render(cID, TRUE, TRUE);
PlayAction(skip);
GetPosDetail(loopBuff);
debug[0] = '\0';
debug_message(debug,loopBuff);
FnWorld gw;
gw.Object(gID);
gw.SwapBuffers();
}
//-------------------
// quit the demo
// C.Wang 0304, 2004
//-------------------
void QuitGame(WORLDid gID, BYTE code, BOOL value)
{
if (code == FY_ESCAPE) {
if (value) {
FyWin32EndWorld(gID);
delete kc;
}
}
}
/*-----------------------------------
initialize the pivot of the camera
C.Wang 0329, 2004
------------------------------------*/
void InitPivot(WORLDid g, int x, int y)
{
oldX = x;
oldY = y;
}
/*------------------
pivot the camera
C.Wang 0329, 2004
-------------------*/
void PivotCam(WORLDid g, int x, int y)
{
FnObject model;
if (x != oldX) {
model.Object(cID);
model.Rotate(Z_AXIS, (float) (x - oldX), GLOBAL);
oldX = x;
}
if (y != oldY) {
model.Object(cID);
model.Rotate(X_AXIS, (float) (y - oldY), GLOBAL);
oldY = y;
}
}
/*----------------------------------
initialize the move of the camera
C.Wang 0329, 2004
-----------------------------------*/
void InitMove(WORLDid g, int x, int y)
{
oldXM = x;
oldYM = y;
}
/*------------------
move the camera
C.Wang 0329, 2004
-------------------*/
void MoveCam(WORLDid g, int x, int y)
{
if (x != oldXM) {
FnObject model;
model.Object(cID);
model.Translate((float)(x - oldXM)*0.1f, 0.0f, 0.0f, LOCAL);
oldXM = x;
}
if (y != oldYM) {
FnObject model;
model.Object(cID);
model.Translate(0.0f, (float)(oldYM - y)*0.1f, 0.0f, LOCAL);
oldYM = y;
}
}
/*----------------------------------
initialize the zoom of the camera
C.Wang 0329, 2004
-----------------------------------*/
void InitZoom(WORLDid g, int x, int y)
{
oldXMM = x;
oldYMM = y;
}
/*------------------
zoom the camera
C.Wang 0329, 2004
-------------------*/
void ZoomCam(WORLDid g, int x, int y)
{
if (x != oldXMM || y != oldYMM) {
FnObject model;
model.Object(cID);
model.Translate(0.0f, 0.0f, (float)(x - oldXMM), LOCAL);
oldXMM = x;
oldYMM = y;
}
}
void debug_message(char *string, char *string2){
FnWorld gw;
gw.Object(gID);
gw.StartMessageDisplay();
gw.MessageOnScreen(20,20,string2,255,0,0);
gw.MessageOnScreen(20,200,string,255,0,0);
gw.FinishMessageDisplay();
}