-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
217 lines (200 loc) · 5.73 KB
/
Player.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
#include "Player.h"
//int型は32bitなので記憶できるキーは32個まで
const static int g_keys[]= {GLUT_KEY_UP,GLUT_KEY_DOWN,
GLUT_KEY_LEFT,GLUT_KEY_RIGHT,
' ','h','j','k','l','u','i','z','x'
};
Player::Player():quad1(6.0,6.0),quad2(6.0,6.0){
quad2.zPos = -50.0;
model_pos.x = 0.0; model_pos.y = 1.0; model_pos.z = 0.0;
dash_vec.x = 0.0; dash_vec.y = 0.0; dash_vec.z = 0.0;
ay = 0.0; dash_charge = 0.0;
str_charge[0] = '\0'; str_score[0] = '\0'; str_total[0] = '\0';
model_state = WAIT; keyFlag=0;
cam_angle[0]=0.0; cam_angle[1]=0.0; cam_zoom=8.0;
target_pos.x = 0.0; target_pos.y = 0.0; target_pos.z = 0.0;
#ifdef WIN32
mqoModel = mqoCreateModel("GLMetaseq/Models/tachikoma.mqo",0.02);
model_pos.y= -0.05;
#else
model.Load("XMesh/Models/dosei.x");
#endif
}
Player::~Player(){
#ifdef WIN32
mqoDeleteModel(mqoModel);
#endif
}
void Player::Render3D(){
LookpointSet();
quad1.Render();
quad2.Render();
Move();
glPushMatrix();
glTranslated(model_pos.x, model_pos.y, model_pos.z); //移動
glRotated(ay,0.0,1.0,0.0);
// モデルの描画
#ifdef WIN32
mqoCallModel(mqoModel);
#else
float scale = 2.0f/model.sphere.radius; // 拡大係数
model.Render(scale); // 描画
#endif
glPopMatrix();
}
void Player::Render2D(){
void *font = GLUT_BITMAP_HELVETICA_18;
glColor4d(1.0, 1.0, 1.0, 0.8);
glRasterPos2d(15.0, 15.0);
glutBitmapString(font,reinterpret_cast<const unsigned char*>(str_charge));
glRasterPos2d(15.0, 35.0);
glutBitmapString(font,reinterpret_cast<const unsigned char*>(str_score));
glRasterPos2d(15.0, 55.0);
glutBitmapString(font,reinterpret_cast<const unsigned char*>(str_total));
}
void Player::KeyStateSet(int key,bool state){
for(unsigned int i=0;i<sizeof(keyFlag)*8;i++){
if(key == g_keys[i]){
if(state) keyFlag |= 1<<i;
else keyFlag &= ~(1<<i);
}
}
}
bool Player::KeyStateGet(int key){
for(unsigned int i=0;i<sizeof(keyFlag)*8;i++){
if(key == g_keys[i] && keyFlag & 1<<i) return true;
}
return false;
}
void Player::Move(){
float distance;
switch (model_state){
case WAIT: // 待機中
Vector3 model_vec;
if(!(KeyStateGet(' ')) && dash_charge>0.0) model_state = DASH;
model_vec.x = 0.1; model_vec.z = 0.1;
if( (KeyStateGet(GLUT_KEY_LEFT) && KeyStateGet(GLUT_KEY_UP)) ||
(KeyStateGet(GLUT_KEY_LEFT) && KeyStateGet(GLUT_KEY_DOWN)) ||
(KeyStateGet(GLUT_KEY_RIGHT) && KeyStateGet(GLUT_KEY_UP)) ||
(KeyStateGet(GLUT_KEY_RIGHT) && KeyStateGet(GLUT_KEY_DOWN)) ){
model_vec.x *= 1/sqrt(2.0);
model_vec.z *= 1/sqrt(2.0);
}
if(KeyStateGet(GLUT_KEY_LEFT)){
model_pos.x -= model_vec.x;
if(model_pos.x < -5.0) model_pos.x = -5.0;
}
if(KeyStateGet(GLUT_KEY_RIGHT)){
model_pos.x += model_vec.x;
if(model_pos.x > 5.0) model_pos.x = 5.0;
}
if(KeyStateGet(GLUT_KEY_UP)){
model_pos.z -= model_vec.z;
if(model_pos.z < -5.0) model_pos.z = -5.0;
}
if(KeyStateGet(GLUT_KEY_DOWN)){
model_pos.z += model_vec.z;
if(model_pos.z > 5.0) model_pos.z = 5.0;
}
if(KeyStateGet('x')){
ay -= 1.0;
}
if(KeyStateGet('z')){
ay += 1.0;
}
if(KeyStateGet(' ')){
dash_charge +=0.02;
sprintf(str_charge,"%d",(int)(dash_charge*100));
}
break;
case DASH: // ダッシュ中
target_pos.x = model_pos.x;
target_pos.y = model_pos.y;
target_pos.z = model_pos.z;
dash_vec.x = -sin(ay/180 * M_PI);
dash_vec.z = -cos(ay/180 * M_PI);
model_pos.x += dash_vec.x * dash_charge;
model_pos.z += dash_vec.z * dash_charge;
if((dash_charge-=0.01)<0.0) dash_charge = 0.0;
distance = sqrt(pow(model_pos.x - quad2.xPos, 2) + pow(model_pos.z - quad2.zPos, 2));
sprintf(str_score, "Score: %d", (int)(9000 - distance * 1000));
if(dash_time1++ > 100 || dash_charge == 0.0){
dash_time1 = 0;
ay = 0.0;
dash_vec.x = 0.0;
dash_vec.y = 0.0;
dash_vec.z = - 1.0;
str_charge[0] = '\0';
str_score[0] = '\0';
if (distance < 9.0){
total += (int)(9000 - distance * 1000);
sprintf(str_total, "Total: %d", total);
model_state = RESET;
}else{
total = 0;
str_total[0] = '\0';
dash_vec.x = 0.0;
dash_vec.y = 1.0;
dash_vec.z = 0.0;
model_state = FLY;
}
}
target_pos.x = 0.0; target_pos.y = 0.0; target_pos.z = 0.0;
break;
case RESET: // モデルを初期位置へ
model_pos.x = 0.0;
model_pos.y = 1.0;
model_pos.z = 0.0;
str_charge[0] = '\0';
model_state = CHANGE;
break;
case FLY:
model_pos.x += dash_vec.x * dash_charge;
model_pos.y += dash_vec.y * dash_charge;
model_pos.z += dash_vec.z * dash_charge;
dash_charge += 0.1;
if (dash_time1++ > 100){
dash_time1 = 0;
ay = 0.0;
dash_charge = 0.0;
model_pos.x = 0.0; model_pos.y = 1.0; model_pos.z = 0.0;
dash_vec.x = 0.0; dash_vec.y = 0.0; dash_vec.z = 0.0;
model_state = CHANGE;
}
break;
case CHANGE:
quad2.xPos = (double)(rand() % 400 - 200) / 10.0;
model_state = WAIT;
break;
}
}
void Player::LookpointSet(){
//視点移動
if(KeyStateGet('h')){
cam_angle[0] -= M_PI/180.0;
if(cam_angle[0] < 0) cam_angle[0] += 2*M_PI;
}if(KeyStateGet('j')){
cam_angle[1] -= M_PI/180.0;
if(cam_angle[1] < 0) cam_angle[1] += 2*M_PI;
}if(KeyStateGet('k')){
cam_angle[1] += M_PI/180.0;
if(cam_angle[1] > 2*M_PI) cam_angle[1] -= 2*M_PI;
}if(KeyStateGet('l')){
cam_angle[0] += M_PI/180.0;
if(cam_angle[0] > 2*M_PI) cam_angle[0] -= 2*M_PI;
}if(KeyStateGet('u')){
cam_zoom -= 0.05;
}if(KeyStateGet('i')){
cam_zoom += 0.05;
}
cam_pos.x = model_pos.x + sin(cam_angle[0]) * cos(cam_angle[1]) * cam_zoom;
cam_pos.y = model_pos.y + sin(cam_angle[1]) * cam_zoom;
cam_pos.z = model_pos.z + cos(cam_angle[0]) * cos(cam_angle[1]) * cam_zoom;
target_pos.x=model_pos.x;
target_pos.y=model_pos.y;
target_pos.z=model_pos.z;
// 視点の設定
gluLookAt(cam_pos.x, cam_pos.y, cam_pos.z,
target_pos.x, target_pos.y, target_pos.z,
0.0,1.0,0.0);
}