0
|
1 /*
|
|
2 * $Id$
|
|
3 */
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include "libps2.h"
|
|
7 #include "ps2util.h"
|
|
8 #include "field.h"
|
|
9 #include "car.h"
|
|
10 #include "mytype.h"
|
|
11 #include "quotanion.h"
|
|
12 #include "game.h"
|
|
13
|
|
14 #define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT)
|
|
15 #define BUFSIZE 256
|
|
16
|
88
|
17 /* 初期位置と初期方向 */
|
0
|
18 static FVECTOR location = {0, 0, 0, 1};
|
|
19 static FVECTOR direction = {0, 0, 1, 1};
|
|
20
|
|
21 extern FILE* main_fp;
|
|
22
|
|
23 /* --- field.c --- */
|
|
24 extern int field_rap_increment(int);
|
|
25 extern void field_set_actual(FieldPtr);
|
|
26 extern FieldPtr field_get_actual();
|
|
27
|
|
28
|
|
29 static CarPtr
|
|
30 car_create(int car_id, char *filename, char *texname, float speed_accel,
|
|
31 float speed_max, float rot, float brake)
|
|
32 {
|
|
33 CarPtr car;
|
|
34 OBJECT *body;
|
|
35 TEXTURE* tex;
|
|
36 void *free_addr;
|
|
37
|
|
38 body = ps2util_obj_Create_fromXML(filename);
|
|
39 ps2util_obj_Set_effect(body, DEFAULT_SET);
|
|
40
|
|
41 if (malloc_align16(&free_addr, &car, sizeof(Car)) == -1) {
|
|
42 fprintf(main_fp, "car.c: malloc_align16 error\n");
|
|
43 exit(EXIT_FAILURE);
|
|
44 }
|
|
45 car->body = body;
|
|
46 car->next = NULL;
|
|
47 car->speed = 0.0;
|
|
48 car->speed_accel = speed_accel;
|
|
49 car->speed_max = speed_max;
|
|
50 car->brake = brake;
|
|
51 car->rotation_angle = rot;
|
|
52 car->y_angle = 0.0;
|
|
53 car->free_addr = free_addr;
|
|
54
|
|
55 ps2_vu0_copy_vector(car->direction, direction);
|
|
56 ps2_vu0_copy_vector(car->location, location);
|
|
57 INIT_VECTOR(car->vertical, 0, -1, 0, 1);
|
|
58 INIT_VECTOR(car->body->xyz, 0, 0, 0, 1);
|
|
59 INIT_VECTOR(car->body->angle, 0, 0, 0, 1);
|
|
60
|
|
61 ps2util_obj_Renew_transMatrix(car->body);
|
|
62 ps2util_obj_Set_effect(car->body, DEFAULT_SET);
|
|
63
|
|
64 if (*texname != 'n') {
|
|
65 tex = read_png_file(texname);
|
|
66 ps2util_tex_Set(tex);
|
|
67 ps2util_obj_Set_texture(car->body, tex);
|
|
68 }
|
|
69
|
|
70 #ifdef DEBUG
|
|
71 fprintf(main_fp, "malloc car addr = %x\n", (int)free_addr);
|
|
72 #endif
|
|
73
|
|
74 return car;
|
|
75 }
|
|
76
|
|
77 static CarPtr
|
|
78 car_new_readCSV(FILE *fp, int id)
|
|
79 {
|
|
80 CarPtr new;
|
|
81 int car_id;
|
|
82 float speed_accel, speed_max, brake, rot;
|
|
83 char buff[BUFSIZE], *bufp;
|
|
84 char carImg[BUFSIZE], texImg[BUFSIZE];
|
|
85 Bool flag;
|
|
86
|
|
87 flag = FALSE;
|
|
88
|
|
89 while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) {
|
|
90 bufp++;
|
|
91
|
|
92 switch (buff[0]) {
|
|
93 case 'n':
|
|
94 if (flag == FALSE) {
|
|
95 sscanf(bufp, " %d\n", &car_id);
|
|
96 if (id == car_id) {
|
|
97 flag = TRUE;
|
|
98 }
|
|
99 }
|
|
100 break;
|
|
101 case 't':
|
|
102 if (flag == TRUE) {
|
|
103 sscanf(bufp, " %s %s %f %f %f %f\n",
|
|
104 carImg, texImg, &speed_accel, &speed_max, &rot, &brake);
|
|
105 new = car_create(id, carImg, texImg,
|
|
106 speed_accel, speed_max, rot, brake);
|
|
107 return new;
|
|
108 }
|
|
109 break;
|
|
110 default:
|
|
111 break;
|
|
112 }
|
|
113 }
|
|
114
|
88
|
115 /* ここまで辿り着いたら読み込み失敗 */
|
0
|
116 fprintf(main_fp, "error - car_new_readCSV\n");
|
|
117 exit(EXIT_FAILURE);
|
|
118 }
|
|
119
|
|
120 CarPtr
|
|
121 car_init(int id)
|
|
122 {
|
|
123 CarPtr new;
|
|
124 FILE *fp;
|
|
125 char *filename = "car/car.dat";
|
|
126
|
|
127 if (!(fp = fopen(filename, "r"))) {
|
|
128 fprintf(main_fp, "error read file %s\n", filename);
|
|
129 exit(EXIT_FAILURE);
|
|
130 }
|
|
131 new = car_new_readCSV(fp, id);
|
|
132 fclose(fp);
|
|
133
|
|
134 return new;
|
|
135 }
|
|
136
|
|
137 /*---------------------------
|
88
|
138 carをY軸で回転させる
|
|
139 flg: 回転方向 1:右, -1:左
|
0
|
140 ---------------------------*/
|
|
141 void
|
|
142 car_swerve(CarPtr car, int flg)
|
|
143 {
|
|
144 FMATRIX rot;
|
|
145 FVECTOR v;
|
|
146
|
|
147 car->y_angle += (float)flg*car->rotation_angle;
|
|
148 car->y_angle += (car->y_angle < 0) ? 360.0 : 0;
|
|
149 car->y_angle += (car->y_angle > 360.0) ? -360.0 : 0;
|
|
150
|
|
151 ps2_vu0_unit_matrix(rot);
|
|
152 ps2_vu0_rot_matrix_y(rot, rot, degree2radian((float)flg*car->rotation_angle));
|
|
153 ps2_vu0_copy_vector(v, car->direction);
|
|
154 ps2_vu0_apply_matrix(car->direction, rot, v);
|
|
155 }
|
|
156
|
|
157 /*----------------------------
|
88
|
158 carを加速・減速する
|
|
159 (flg == 1) ? 加速 : 減速
|
0
|
160 ---------------------------*/
|
|
161 void
|
|
162 car_accelerate(CarPtr car, int flg)
|
|
163 {
|
|
164 car->speed += (flg == 1) ? car->speed_accel : -car->brake;
|
|
165 car->speed = (car->speed < 0) ? 0 : car->speed;
|
|
166 car->speed = (car->speed > car->speed_max) ? car->speed_max : car->speed;
|
|
167 }
|
|
168
|
|
169
|
|
170 /*---------------------------------
|
88
|
171 進行後のcarに対してコース面の内外判定を行い、
|
|
172 : 現在のコース
|
|
173 : 現在のコースの次のコース
|
|
174 : 現在のコースの前のコース
|
|
175 に存在すればTRUEを返す
|
|
176 どのコースにも居ない(壁に衝突した)場合は
|
|
177 FALSEを返す。
|
0
|
178 ---------------------------------*/
|
|
179 static Bool
|
|
180 car_field_check(CarPtr car)
|
|
181 {
|
|
182 FieldPtr p;
|
|
183 FieldPtr f = field_get_actual();
|
|
184
|
88
|
185 // 現在のコースで衝突・内外判定
|
0
|
186 p = f;
|
|
187 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
188 goto FIELD_CHECK_OK;
|
|
189 }
|
|
190
|
88
|
191 // 現在のコースの次に隣接するコースで衝突・内外判定
|
0
|
192 p = f->next;
|
|
193 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
194 if (field_rap_increment(1)) {
|
|
195 game.rap++;
|
|
196 }
|
|
197 goto FIELD_CHECK_OK;
|
|
198 }
|
|
199
|
88
|
200 // 現在のコースの前に隣接するコースで衝突・内外判定
|
0
|
201 p = f->prev;
|
|
202 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
203 field_rap_increment(-1);
|
|
204 goto FIELD_CHECK_OK;
|
|
205 }
|
|
206
|
88
|
207 // どのコース上にも居ない(壁に衝突)
|
0
|
208 return FALSE;
|
|
209
|
|
210 FIELD_CHECK_OK:
|
|
211 field_set_actual(p);
|
|
212 return TRUE;
|
|
213 }
|
|
214
|
|
215
|
|
216 /*------------------------------
|
88
|
217 速度speedと向きdirectionから
|
|
218 進行後の位置locationを求める。
|
|
219 また、進行後の衝突判定も行う。
|
0
|
220 ------------------------------*/
|
|
221 static void
|
|
222 car_move(CarPtr car)
|
|
223 {
|
|
224 FVECTOR mov, prev_location;
|
|
225
|
|
226 ps2_vu0_scale_vector(mov, car->direction, car->speed);
|
|
227 ps2_vu0_copy_vector(prev_location, car->location);
|
|
228 ps2_vu0_add_vector(car->location, car->location, mov);
|
|
229
|
|
230 if (car_field_check(car) == FALSE) {
|
|
231 ps2_vu0_copy_vector(car->location, prev_location);
|
|
232 car->speed = car->speed*0.5;
|
|
233 }
|
|
234 }
|
|
235
|
|
236
|
|
237 static void
|
|
238 car_axis_rotation(CarPtr car)
|
|
239 {
|
|
240 FMATRIX pose, yrot, yrotinv;
|
|
241
|
|
242 ps2_vu0_unit_matrix(yrot);
|
|
243 ps2_vu0_rot_matrix_y(yrot, yrot, -degree2radian(car->y_angle));
|
|
244 ps2_vu0_unit_matrix(yrotinv);
|
|
245 ps2_vu0_rot_matrix_y(yrotinv, yrotinv, degree2radian(car->y_angle));
|
|
246
|
|
247 {
|
|
248 FVECTOR yd = {0, -1, 0, 1};
|
|
249 FVECTOR rotaxis;
|
|
250 FVECTOR q;
|
|
251
|
|
252 ps2_vu0_outer_product(rotaxis, car->vertical, yd);
|
|
253 {
|
|
254 float scale=1/NORM(rotaxis);
|
|
255 rotaxis[0] *= scale;
|
|
256 rotaxis[1] *= scale;
|
|
257 rotaxis[2] *= scale;
|
|
258 }
|
|
259
|
|
260 quotanion(q, rotaxis, acos(ps2_vu0_inner_product(yd, car->vertical)/(NORM(yd)*NORM(car->vertical))));
|
|
261 quotanion_rotmatrix(pose, q);
|
|
262 }
|
|
263
|
|
264 ps2_vu0_mul_matrix(pose, yrot, pose);
|
|
265 ps2_vu0_mul_matrix(pose, pose, yrotinv);
|
|
266 ps2_vu0_copy_matrix(car->body->transfer, pose);
|
|
267 ps2_vu0_rot_matrix_y(car->body->transfer, car->body->transfer,
|
|
268 degree2radian(car->y_angle));
|
|
269 }
|
|
270
|
|
271 void
|
|
272 car_update(CarPtr car)
|
|
273 {
|
88
|
274 // 移動
|
0
|
275 car_move(car);
|
|
276
|
88
|
277 // 摩擦による速度減少
|
0
|
278 car->speed -= 0.005;
|
|
279 car->speed = (car->speed < 0) ? 0 : car->speed;
|
|
280
|
88
|
281 // 傾き
|
0
|
282 car_axis_rotation(car);
|
|
283 }
|
|
284
|
|
285 void
|
|
286 car_destroy(CarPtr p)
|
|
287 {
|
|
288 TEXTURE *t;
|
|
289
|
|
290 if ((t = p->body->surfaces->texture)) {
|
|
291 ps2util_tex_Exclude(t);
|
|
292 free_texture(t);
|
|
293 }
|
|
294 ps2util_obj_Free(p->body);
|
|
295 free(p->free_addr);
|
|
296
|
|
297 #ifdef DEBUG
|
|
298 fprintf(main_fp, "free car addr = %x\n", (int)p->free_addr);
|
|
299 #endif
|
|
300 }
|