comparison examples/tank/tank_main.c @ 132:c65b30e2eda9

detect collison between tanks
author Thinker K.F. Li <thinker@branda.to>
date Wed, 17 Sep 2008 13:40:32 +0800
parents 6a8588df68af
children 9870b049b7f6
comparison
equal deleted inserted replaced
131:6a8588df68af 132:c65b30e2eda9
48 mb_progm_t *progm; 48 mb_progm_t *progm;
49 }; 49 };
50 typedef struct _tank tank_t; 50 typedef struct _tank tank_t;
51 enum { TD_UP = 0, TD_RIGHT, TD_DOWN, TD_LEFT }; 51 enum { TD_UP = 0, TD_RIGHT, TD_DOWN, TD_LEFT };
52 52
53
54 /* @} */
55
56 typedef struct _tank_rt tank_rt_t;
57
58 struct _tank_rt {
59 tank_t *tank1;
60 tank1_t *tank1_o;
61 tank_t *tank2;
62 tank2_t *tank2_o;
63 int n_enemy;
64 tank_t *tank_enemies[10];
65 tank_en_t *tank_enemies_o[10];
66 tank_t *tanks[12];
67 int n_tanks;
68 void *map[12][16];
69 X_MB_runtime_t *mb_rt;
70 observer_t *kb_observer;
71 };
72
73 /*! \ingroup tank_elf
74 * @{
75 */
53 static tank_t *tank_new(coord_t *coord_pos, 76 static tank_t *tank_new(coord_t *coord_pos,
54 coord_t *coord_rot, 77 coord_t *coord_rot,
55 int map_x, int map_y, 78 int map_x, int map_y,
56 X_MB_runtime_t *mb_rt) { 79 X_MB_runtime_t *mb_rt) {
57 tank_t *tank; 80 tank_t *tank;
102 } 125 }
103 126
104 #define PI 3.1415926 127 #define PI 3.1415926
105 128
106 static void tank_move(tank_t *tank, int direction, 129 static void tank_move(tank_t *tank, int direction,
107 X_MB_runtime_t *xmb_rt) { 130 tank_rt_t *tank_rt) {
131 X_MB_runtime_t *xmb_rt = tank_rt->mb_rt;
108 redraw_man_t *rdman; 132 redraw_man_t *rdman;
109 mb_tman_t *tman; 133 mb_tman_t *tman;
110 ob_factory_t *factory; 134 ob_factory_t *factory;
111 /* for the program */ 135 /* for the program */
112 mb_progm_t *progm; 136 mb_progm_t *progm;
117 /* for position */ 141 /* for position */
118 co_aix sh_x, sh_y; 142 co_aix sh_x, sh_y;
119 /* for direction */ 143 /* for direction */
120 float ang1, ang2; 144 float ang1, ang2;
121 float rot_diff; 145 float rot_diff;
146 int i;
122 static co_aix shift_xy[][2] = {{0, -50}, {50, 0}, {0, 50}, {-50, 0}}; 147 static co_aix shift_xy[][2] = {{0, -50}, {50, 0}, {0, 50}, {-50, 0}};
123 static int map_shift[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; 148 static int map_shift[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
124 static float angles[4] = {0, PI / 2, PI , PI * 3 / 2}; 149 static float angles[4] = {0, PI / 2, PI , PI * 3 / 2};
125 static float rotations[7] = {PI / 2, PI , -PI / 2, 150 static float rotations[7] = {PI / 2, PI , -PI / 2,
126 0, PI / 2, PI , -PI / 2}; 151 0, PI / 2, PI , -PI / 2};
156 return; 181 return;
157 if(map[tank->map_y][tank->map_x - 1] != MUD) 182 if(map[tank->map_y][tank->map_x - 1] != MUD)
158 return; 183 return;
159 break; 184 break;
160 } 185 }
186
187 tank->map_x += map_shift[direction][0];
188 tank->map_y += map_shift[direction][1];
189 for(i = 0; i < tank_rt->n_tanks; i++) {
190 if(tank != tank_rt->tanks[i] &&
191 tank->map_x == tank_rt->tanks[i]->map_x &&
192 tank->map_y == tank_rt->tanks[i]->map_y) {
193 tank->map_x -= map_shift[direction][0];
194 tank->map_y -= map_shift[direction][1];
195 return;
196 }
197 }
161 } 198 }
162 199
163 rdman = X_MB_rdman(xmb_rt); 200 rdman = X_MB_rdman(xmb_rt);
164 tman = X_MB_tman(xmb_rt); 201 tman = X_MB_tman(xmb_rt);
165 factory = X_MB_ob_factory(xmb_rt); 202 factory = X_MB_ob_factory(xmb_rt);
174 if(direction == tank->direction) { 211 if(direction == tank->direction) {
175 /* Shift/move */ 212 /* Shift/move */
176 sh_x = shift_xy[direction][0]; 213 sh_x = shift_xy[direction][0];
177 sh_y = shift_xy[direction][1]; 214 sh_y = shift_xy[direction][1];
178 mb_shift_new(sh_x, sh_y, tank->coord_pos, word); 215 mb_shift_new(sh_x, sh_y, tank->coord_pos, word);
179 tank->map_x += map_shift[direction][0];
180 tank->map_y += map_shift[direction][1];
181 } else { 216 } else {
182 /* Change direction */ 217 /* Change direction */
183 rot_diff = rotations[3 - tank->direction + direction]; 218 rot_diff = rotations[3 - tank->direction + direction];
184 ang1 = angles[tank->direction]; 219 ang1 = angles[tank->direction];
185 ang2 = ang1 + rot_diff; 220 ang2 = ang1 + rot_diff;
196 mb_progm_start(progm, tman, &now); 231 mb_progm_start(progm, tman, &now);
197 } 232 }
198 233
199 /* @} */ 234 /* @} */
200 235
201 typedef struct _tank_rt tank_rt_t;
202
203 struct _tank_rt {
204 tank_t *tank1;
205 tank1_t *tank1_o;
206 tank_t *tank2;
207 tank2_t *tank2_o;
208 int n_enemy;
209 tank_t *tank_enemies[10];
210 tank_en_t *tank_enemies_o[10];
211 void *map[12][16];
212 X_MB_runtime_t *mb_rt;
213 observer_t *kb_observer;
214 };
215
216 #define CHANGE_POS(g, x, y) do { \ 236 #define CHANGE_POS(g, x, y) do { \
217 (g)->root_coord->matrix[0] = 1.0; \ 237 (g)->root_coord->matrix[0] = 1.0; \
218 (g)->root_coord->matrix[2] = x; \ 238 (g)->root_coord->matrix[2] = x; \
219 (g)->root_coord->matrix[4] = 1.0; \ 239 (g)->root_coord->matrix[4] = 1.0; \
220 (g)->root_coord->matrix[5] = y; \ 240 (g)->root_coord->matrix[5] = y; \
250 case 0xff0d: /* enter */ 270 case 0xff0d: /* enter */
251 default: 271 default:
252 return; 272 return;
253 } 273 }
254 274
255 tank_move(tank_rt->tank1, direction, tank_rt->mb_rt); 275 tank_move(tank_rt->tank1, direction, tank_rt);
256 } 276 }
257 277
258 static void init_keyboard(tank_rt_t *tank_rt) { 278 static void init_keyboard(tank_rt_t *tank_rt) {
259 X_MB_runtime_t *mb_rt; 279 X_MB_runtime_t *mb_rt;
260 subject_t *kbevents; 280 subject_t *kbevents;
350 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center); 370 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center);
351 tank_en_o = tank_en_new(rdman, coord_center); 371 tank_en_o = tank_en_new(rdman, coord_center);
352 tank_rt->tank_enemies[i] = tank_new(coord_pos, coord_rot, 372 tank_rt->tank_enemies[i] = tank_new(coord_pos, coord_rot,
353 i * 3 + 3, 0, mb_rt); 373 i * 3 + 3, 0, mb_rt);
354 tank_rt->tank_enemies_o[i] = tank_en_o; 374 tank_rt->tank_enemies_o[i] = tank_en_o;
375 tank_rt->tanks[i] = tank_rt->tank_enemies[i];
355 } 376 }
356 tank_rt->n_enemy = i; 377 tank_rt->n_enemy = i;
378
379 tank_rt->tanks[i++] =tank_rt->tank1;
380 tank_rt->tanks[i++] =tank_rt->tank2;
381 tank_rt->n_tanks = i;
357 382
358 init_keyboard(tank_rt); 383 init_keyboard(tank_rt);
359 } 384 }
360 385
361 int 386 int