Mercurial > hg > old > magoroku_racing
view car.c @ 53:a233027e895c
*** empty log message ***
author | gongo |
---|---|
date | Wed, 08 Nov 2006 07:19:26 +0000 |
parents | a1908b83f8fa |
children | ce9ef7dcf2f0 |
line wrap: on
line source
/* * $Id$ */ #include <stdio.h> #include <stdlib.h> #include "libps2.h" #include "ps2util.h" #include "field.h" #include "car.h" #include "mytype.h" #include "quotanion.h" #include "game.h" #define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT) #define BUFSIZE 256 /* $B=i4|0LCV$H=i4|J}8~(B */ static FVECTOR location = {0, 0, 0, 1}; static FVECTOR direction = {0, 0, 1, 1}; extern FILE* main_fp; /* --- field.c --- */ extern int field_rap_increment(int); extern void field_set_actual(FieldPtr); extern FieldPtr field_get_actual(); static CarPtr car_create(int car_id, char *filename, char *texname, float speed_accel, float speed_max, float rot, float brake) { CarPtr car; OBJECT *body; TEXTURE* tex; void *free_addr; body = ps2util_obj_Create_fromXML(filename); ps2util_obj_Set_effect(body, DEFAULT_SET); if (malloc_align16(&free_addr, &car, sizeof(Car)) == -1) { fprintf(main_fp, "car.c: malloc_align16 error\n"); exit(EXIT_FAILURE); } car->body = body; car->next = NULL; car->speed = 0.0; car->speed_accel = speed_accel; car->speed_max = speed_max; car->brake = brake; car->rotation_angle = rot; car->y_angle = 0.0; car->free_addr = free_addr; ps2_vu0_copy_vector(car->direction, direction); ps2_vu0_copy_vector(car->location, location); INIT_VECTOR(car->vertical, 0, -1, 0, 1); INIT_VECTOR(car->body->xyz, 0, 0, 0, 1); INIT_VECTOR(car->body->angle, 0, 0, 0, 1); ps2util_obj_Renew_transMatrix(car->body); ps2util_obj_Set_effect(car->body, DEFAULT_SET); if (*texname != 'n') { tex = read_png_file(texname); ps2util_tex_Set(tex); ps2util_obj_Set_texture(car->body, tex); } #ifdef DEBUG fprintf(main_fp, "malloc car addr = %x\n", (int)free_addr); #endif return car; } static CarPtr car_new_readCSV(FILE *fp, int id) { CarPtr new; int car_id; float speed_accel, speed_max, brake, rot; char buff[BUFSIZE], *bufp; char carImg[BUFSIZE], texImg[BUFSIZE]; Bool flag; flag = FALSE; while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) { bufp++; switch (buff[0]) { case 'n': if (flag == FALSE) { sscanf(bufp, " %d\n", &car_id); if (id == car_id) { flag = TRUE; } } break; case 't': if (flag == TRUE) { sscanf(bufp, " %s %s %f %f %f %f\n", carImg, texImg, &speed_accel, &speed_max, &rot, &brake); new = car_create(id, carImg, texImg, speed_accel, speed_max, rot, brake); return new; } break; default: break; } } /* $B$3$3$^$GC)$jCe$$$?$iFI$_9~$_<:GT(B */ fprintf(main_fp, "error - car_new_readCSV\n"); exit(EXIT_FAILURE); } CarPtr car_init(int id) { CarPtr new; FILE *fp; char *filename = "car/car.dat"; if (!(fp = fopen(filename, "r"))) { fprintf(main_fp, "error read file %s\n", filename); exit(EXIT_FAILURE); } new = car_new_readCSV(fp, id); fclose(fp); return new; } /*--------------------------- car$B$r(BY$B<4$G2sE>$5$;$k(B flg: $B2sE>J}8~(B 1:$B1&(B, -1:$B:8(B ---------------------------*/ void car_swerve(CarPtr car, int flg) { FMATRIX rot; FVECTOR v; car->y_angle += (float)flg*car->rotation_angle; car->y_angle += (car->y_angle < 0) ? 360.0 : 0; car->y_angle += (car->y_angle > 360.0) ? -360.0 : 0; ps2_vu0_unit_matrix(rot); ps2_vu0_rot_matrix_y(rot, rot, degree2radian((float)flg*car->rotation_angle)); ps2_vu0_copy_vector(v, car->direction); ps2_vu0_apply_matrix(car->direction, rot, v); } /*---------------------------- car$B$r2CB.!&8:B.$9$k(B (flg == 1) ? $B2CB.(B : $B8:B.(B ---------------------------*/ void car_accelerate(CarPtr car, int flg) { car->speed += (flg == 1) ? car->speed_accel : -car->brake; car->speed = (car->speed < 0) ? 0 : car->speed; car->speed = (car->speed > car->speed_max) ? car->speed_max : car->speed; } /*--------------------------------- $B?J9T8e$N(Bcar$B$KBP$7$F%3!<%9LL$NFb30H=Dj$r9T$$!"(B : $B8=:_$N%3!<%9(B : $B8=:_$N%3!<%9$N<!$N%3!<%9(B : $B8=:_$N%3!<%9$NA0$N%3!<%9(B $B$KB8:_$9$l$P(BTRUE$B$rJV$9(B $B$I$N%3!<%9$K$b5o$J$$(B($BJI$K>WFM$7$?(B)$B>l9g$O(B FALSE$B$rJV$9!#(B ---------------------------------*/ static Bool car_field_check(CarPtr car) { FieldPtr p; FieldPtr f = field_get_actual(); // $B8=:_$N%3!<%9$G>WFM!&Fb30H=Dj(B p = f; if (col_detect(&p->colface, car->vertical, car->location) == TRUE) { goto FIELD_CHECK_OK; } // $B8=:_$N%3!<%9$N<!$KNY@\$9$k%3!<%9$G>WFM!&Fb30H=Dj(B p = f->next; if (col_detect(&p->colface, car->vertical, car->location) == TRUE) { if (field_rap_increment(1)) { game.rap++; } goto FIELD_CHECK_OK; } // $B8=:_$N%3!<%9$NA0$KNY@\$9$k%3!<%9$G>WFM!&Fb30H=Dj(B p = f->prev; if (col_detect(&p->colface, car->vertical, car->location) == TRUE) { field_rap_increment(-1); goto FIELD_CHECK_OK; } // $B$I$N%3!<%9>e$K$b5o$J$$(B($BJI$K>WFM(B) return FALSE; FIELD_CHECK_OK: field_set_actual(p); return TRUE; } /*------------------------------ $BB.EY(Bspeed$B$H8~$-(Bdirection$B$+$i(B $B?J9T8e$N0LCV(Blocation$B$r5a$a$k!#(B $B$^$?!"?J9T8e$N>WFMH=Dj$b9T$&!#(B ------------------------------*/ static void car_move(CarPtr car) { FVECTOR mov, prev_location; ps2_vu0_scale_vector(mov, car->direction, car->speed); ps2_vu0_copy_vector(prev_location, car->location); ps2_vu0_add_vector(car->location, car->location, mov); if (car_field_check(car) == FALSE) { ps2_vu0_copy_vector(car->location, prev_location); car->speed = car->speed*0.5; } } static void car_axis_rotation(CarPtr car) { FMATRIX pose, yrot, yrotinv; ps2_vu0_unit_matrix(yrot); ps2_vu0_rot_matrix_y(yrot, yrot, -degree2radian(car->y_angle)); ps2_vu0_unit_matrix(yrotinv); ps2_vu0_rot_matrix_y(yrotinv, yrotinv, degree2radian(car->y_angle)); { FVECTOR yd = {0, -1, 0, 1}; FVECTOR rotaxis; FVECTOR q; ps2_vu0_outer_product(rotaxis, car->vertical, yd); { float scale=1/NORM(rotaxis); rotaxis[0] *= scale; rotaxis[1] *= scale; rotaxis[2] *= scale; } quotanion(q, rotaxis, acos(ps2_vu0_inner_product(yd, car->vertical)/(NORM(yd)*NORM(car->vertical)))); quotanion_rotmatrix(pose, q); } ps2_vu0_mul_matrix(pose, yrot, pose); ps2_vu0_mul_matrix(pose, pose, yrotinv); ps2_vu0_copy_matrix(car->body->transfer, pose); ps2_vu0_rot_matrix_y(car->body->transfer, car->body->transfer, degree2radian(car->y_angle)); } void car_update(CarPtr car) { // $B0\F0(B car_move(car); // $BK`;$$K$h$kB.EY8:>/(B car->speed -= 0.005; car->speed = (car->speed < 0) ? 0 : car->speed; // $B79$-(B car_axis_rotation(car); } void car_destroy(CarPtr p) { TEXTURE *t; if ((t = p->body->surfaces->texture)) { ps2util_tex_Exclude(t); free_texture(t); } ps2util_obj_Free(p->body); free(p->free_addr); #ifdef DEBUG fprintf(main_fp, "free car addr = %x\n", (int)p->free_addr); #endif }