Mercurial > MadButterfly
annotate examples/tank/tank_main.c @ 154:6ce68c1f7405
Tank can fire bullet.
1. Add the redraw subject on redraw_man_t.
2. mb_c_source.m4 & mb_c_header.m4 are changed to free & remove shapes.
3. Add rdman_coord_subtree_free() to remove a subtree of coords.
4. Fix bug of rdman_remove_shape().
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 30 Sep 2008 02:44:06 +0800 |
parents | 9870b049b7f6 |
children | 6749f6639924 |
rev | line source |
---|---|
120 | 1 #include <sys/time.h> |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
2 #include <string.h> |
115 | 3 #include <mb/mb.h> |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
4 #include <mb/tools.h> |
115 | 5 #include "svgs.h" |
6 | |
7 enum { MUD, ROC, BRI, BSH }; | |
8 | |
9 static char map[12][16] = { | |
10 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
11 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
12 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, | |
13 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, | |
14 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, | |
15 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD}, | |
16 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
17 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
18 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI, | |
19 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD}, | |
20 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD, | |
21 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
22 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
23 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD}, | |
24 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
25 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD}, | |
26 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD, | |
27 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI}, | |
28 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
29 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD}, | |
30 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, | |
31 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, | |
32 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
33 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} | |
34 }; | |
35 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
36 #define MAP_W 16 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
37 #define MAP_H 12 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
38 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
39 /*! \defgroup tank_elf Tank Elf |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
40 * \brief Tank elf module provides control functions of tanks in game. |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
41 * @{ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
42 */ |
154 | 43 struct _tank_bullet { |
44 redraw_man_t *rdman; | |
45 coord_t *coord_pos; | |
46 coord_t *coord_rot; | |
47 bullet_t *bullet_obj; | |
48 int start_map_x, start_map_y; | |
49 int direction; | |
50 mb_progm_t *progm; | |
51 mb_timeval_t start_time; | |
52 observer_t *ob_redraw; | |
53 }; | |
54 typedef struct _tank_bullet tank_bullet_t; | |
55 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT }; | |
56 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
57 struct _tank { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
58 coord_t *coord_pos; /*!< \brief coordinate for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
59 coord_t *coord_rot; /*!< \brief coordinate for rotation */ |
154 | 60 coord_t *coord_center; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
61 int map_x, map_y; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
62 int direction; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
63 mb_progm_t *progm; |
154 | 64 tank_bullet_t *bullet; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
65 }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
66 typedef struct _tank tank_t; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
67 enum { TD_UP = 0, TD_RIGHT, TD_DOWN, TD_LEFT }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
68 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
69 /* @} */ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
70 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
71 typedef struct _tank_rt tank_rt_t; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
72 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
73 struct _tank_rt { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
74 tank_t *tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
75 tank1_t *tank1_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
76 tank_t *tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
77 tank2_t *tank2_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
78 int n_enemy; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
79 tank_t *tank_enemies[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
80 tank_en_t *tank_enemies_o[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
81 tank_t *tanks[12]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
82 int n_tanks; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
83 void *map[12][16]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
84 X_MB_runtime_t *mb_rt; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
85 observer_t *kb_observer; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
86 }; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
87 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
88 /*! \ingroup tank_elf |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
89 * @{ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
90 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
91 static tank_t *tank_new(coord_t *coord_pos, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
92 coord_t *coord_rot, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
93 int map_x, int map_y, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
94 X_MB_runtime_t *mb_rt) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
95 tank_t *tank; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
96 redraw_man_t *rdman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
97 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
98 tank = O_ALLOC(tank_t); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
99 if(tank == NULL) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
100 return NULL; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
101 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
102 rdman = X_MB_rdman(mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
103 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
104 tank->coord_pos = coord_pos; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
105 tank->coord_rot = coord_rot; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
106 tank->map_x = map_x; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
107 tank->map_y = map_y; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
108 tank->direction = TD_UP; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
109 tank->progm = NULL; |
154 | 110 tank->bullet = NULL; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
111 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
112 memset(coord_pos->matrix, 0, sizeof(co_aix[6])); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
113 coord_pos->matrix[0] = 1; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
114 coord_pos->matrix[2] = map_x * 50; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
115 coord_pos->matrix[4] = 1; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
116 coord_pos->matrix[5] = map_y * 50; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
117 rdman_coord_changed(rdman, coord_pos); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
118 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
119 return tank; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
120 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
121 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
122 static void tank_free(tank_t *tank, X_MB_runtime_t *xmb_rt) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
123 mb_tman_t *tman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
124 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
125 if(tank->progm) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
126 tman = X_MB_tman(xmb_rt); |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
127 mb_progm_abort(tank->progm); |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
128 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
129 free(tank); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
130 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
131 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
132 /*! \brief Clean program for a tank. |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
133 * |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
134 * It is called when the program is completed. |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
135 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
136 static void clean_tank_progm_handler(event_t *event, void *arg) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
137 tank_t *tank = (tank_t *)arg; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
138 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
139 mb_progm_free(tank->progm); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
140 tank->progm = NULL; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
141 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
142 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
143 #define PI 3.1415926 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
144 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
145 static void tank_move(tank_t *tank, int direction, |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
146 tank_rt_t *tank_rt) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
147 X_MB_runtime_t *xmb_rt = tank_rt->mb_rt; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
148 redraw_man_t *rdman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
149 mb_tman_t *tman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
150 ob_factory_t *factory; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
151 /* for the program */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
152 mb_progm_t *progm; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
153 subject_t *comp_sub; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
154 mb_word_t *word; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
155 mb_timeval_t start, playing; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
156 mb_timeval_t now; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
157 /* for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
158 co_aix sh_x, sh_y; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
159 /* for direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
160 float ang1, ang2; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
161 float rot_diff; |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
162 int i; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
163 static co_aix shift_xy[][2] = {{0, -50}, {50, 0}, {0, 50}, {-50, 0}}; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
164 static int map_shift[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
165 static float angles[4] = {0, PI / 2, PI , PI * 3 / 2}; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
166 static float rotations[7] = {PI / 2, PI , -PI / 2, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
167 0, PI / 2, PI , -PI / 2}; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
168 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
169 if(tank->progm != NULL) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
170 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
171 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
172 /* |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
173 * Keep inside the map. |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
174 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
175 if(direction == tank->direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
176 switch(direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
177 case TD_UP: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
178 if(tank->map_y == 0) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
179 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
180 if(map[tank->map_y - 1][tank->map_x] != MUD) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
181 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
182 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
183 case TD_RIGHT: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
184 if(tank->map_x >= (MAP_W - 1)) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
185 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
186 if(map[tank->map_y][tank->map_x + 1] != MUD) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
187 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
188 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
189 case TD_DOWN: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
190 if(tank->map_y >= (MAP_H - 1)) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
191 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
192 if(map[tank->map_y + 1][tank->map_x] != MUD) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
193 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
194 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
195 case TD_LEFT: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
196 if(tank->map_x == 0) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
197 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
198 if(map[tank->map_y][tank->map_x - 1] != MUD) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
199 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
200 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
201 } |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
202 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
203 tank->map_x += map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
204 tank->map_y += map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
205 for(i = 0; i < tank_rt->n_tanks; i++) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
206 if(tank != tank_rt->tanks[i] && |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
207 tank->map_x == tank_rt->tanks[i]->map_x && |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
208 tank->map_y == tank_rt->tanks[i]->map_y) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
209 tank->map_x -= map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
210 tank->map_y -= map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
211 return; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
212 } |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
213 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
214 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
215 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
216 rdman = X_MB_rdman(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
217 tman = X_MB_tman(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
218 factory = X_MB_ob_factory(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
219 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
220 progm = mb_progm_new(1, rdman); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
221 tank->progm = progm; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
222 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
223 MB_TIMEVAL_SET(&start, 0, 0); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
224 MB_TIMEVAL_SET(&playing, 0, 500000); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
225 word = mb_progm_next_word(progm, &start, &playing); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
226 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
227 if(direction == tank->direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
228 /* Shift/move */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
229 sh_x = shift_xy[direction][0]; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
230 sh_y = shift_xy[direction][1]; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
231 mb_shift_new(sh_x, sh_y, tank->coord_pos, word); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
232 } else { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
233 /* Change direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
234 rot_diff = rotations[3 - tank->direction + direction]; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
235 ang1 = angles[tank->direction]; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
236 ang2 = ang1 + rot_diff; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
237 mb_rotate_new(ang1, ang2, tank->coord_rot, word); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
238 tank->direction = direction; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
239 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
240 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
241 /* Clean program when it is completed. */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
242 comp_sub = mb_progm_get_complete(progm); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
243 subject_add_observer(factory, comp_sub, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
244 clean_tank_progm_handler, tank); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
245 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
246 get_now(&now); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
247 mb_progm_start(progm, tman, &now); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
248 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
249 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
250 /* @} */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
251 |
154 | 252 /*! \brief Make coord objects for bullet elfs. */ |
253 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, | |
254 coord_t **coord_rot, | |
255 coord_t **coord_center) { | |
256 coord_t *coord_back; | |
257 | |
258 *coord_pos = rdman_coord_new(rdman, rdman->root_coord); | |
259 | |
260 coord_back = rdman_coord_new(rdman, *coord_pos); | |
261 coord_back->matrix[2] = 25; | |
262 coord_back->matrix[5] = 25; | |
263 rdman_coord_changed(rdman, coord_back); | |
264 | |
265 *coord_rot = rdman_coord_new(rdman, coord_back); | |
266 | |
267 *coord_center = rdman_coord_new(rdman, *coord_rot); | |
268 (*coord_center)->matrix[2] = -5; | |
269 (*coord_center)->matrix[5] = +15; | |
270 rdman_coord_changed(rdman, *coord_center); | |
271 } | |
272 | |
273 static tank_bullet_t *tank_bullet_new(redraw_man_t *rdman, | |
274 int map_x, int map_y, | |
275 int direction) { | |
276 tank_bullet_t *bullet; | |
277 coord_t *coord_center; | |
278 co_aix *matrix; | |
279 static float _sins[] = { 0, 1, 0, -1}; | |
280 static float _coss[] = { 1, 0, -1, 0}; | |
281 float _sin, _cos; | |
282 | |
283 bullet = O_ALLOC(tank_bullet_t); | |
284 bullet->rdman = rdman; | |
285 | |
286 make_bullet_elf_coords(rdman, &bullet->coord_pos, | |
287 &bullet->coord_rot, | |
288 &coord_center); | |
289 bullet->bullet_obj = bullet_new(rdman, coord_center); | |
290 | |
291 bullet->start_map_x = map_x; | |
292 bullet->start_map_y = map_y; | |
293 bullet->direction = direction; | |
294 bullet->progm = NULL; | |
295 | |
296 matrix = bullet->coord_pos->matrix; | |
297 matrix[2] = map_x * 50; | |
298 matrix[5] = map_y * 50; | |
299 rdman_coord_changed(rdman, bullet->coord_pos); | |
300 | |
301 _sin = _sins[direction]; | |
302 _cos = _coss[direction]; | |
303 matrix = bullet->coord_rot->matrix; | |
304 matrix[0] = _cos; | |
305 matrix[1] = -_sin; | |
306 matrix[3] = _sin; | |
307 matrix[4] = _cos; | |
308 | |
309 return bullet; | |
310 } | |
311 | |
312 static void tank_bullet_free(tank_bullet_t *bullet) { | |
313 bullet_free(bullet->bullet_obj); | |
314 rdman_coord_subtree_free(bullet->rdman, bullet->coord_pos); | |
315 } | |
316 | |
317 static void bullet_go_out_map_and_redraw(event_t *event, void *arg) { | |
318 tank_t *tank = (tank_t *)arg; | |
319 tank_bullet_t *bullet; | |
320 ob_factory_t *factory; | |
321 subject_t *redraw; | |
322 | |
323 bullet = tank->bullet; | |
324 mb_progm_free(bullet->progm); | |
325 rdman_force_clean(bullet->rdman); | |
326 factory = rdman_get_ob_factory(bullet->rdman); | |
327 redraw = rdman_get_redraw_subject(bullet->rdman); | |
328 subject_remove_observer(factory, redraw, bullet->ob_redraw); | |
329 tank_bullet_free(tank->bullet); | |
330 tank->bullet = NULL; | |
331 } | |
332 | |
333 static void bullet_go_out_map(event_t *event, void *arg) { | |
334 tank_t *tank = (tank_t *)arg; | |
335 tank_bullet_t *bullet; | |
336 redraw_man_t *rdman; | |
337 subject_t *redraw; | |
338 ob_factory_t *factory; | |
339 | |
340 /*! \todo Simplify the procdure of using observer pattern. */ | |
341 bullet = tank->bullet; | |
342 rdman = bullet->rdman; | |
343 factory = rdman_get_ob_factory(rdman); | |
344 redraw = rdman_get_redraw_subject(rdman); | |
345 bullet->ob_redraw = | |
346 subject_add_observer(factory, redraw, | |
347 bullet_go_out_map_and_redraw, tank); | |
348 } | |
349 | |
350 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) { | |
351 X_MB_runtime_t *xmb_rt; | |
352 redraw_man_t *rdman; | |
353 int map_x, map_y; | |
354 int shift_x, shift_y; | |
355 int shift_len; | |
356 int dir; | |
357 tank_bullet_t *bullet; | |
358 mb_progm_t *progm; | |
359 mb_word_t *word; | |
360 mb_action_t *act; | |
361 mb_timeval_t start, playing; | |
362 mb_timeval_t now; | |
363 ob_factory_t *factory; | |
364 mb_tman_t *tman; | |
365 subject_t *subject; | |
366 static int map_xy_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; | |
367 | |
368 if(tank->bullet != NULL) | |
369 return; | |
370 | |
371 xmb_rt = tank_rt->mb_rt; | |
372 rdman = X_MB_rdman(xmb_rt); | |
373 tman = X_MB_tman(xmb_rt); | |
374 | |
375 dir = tank->direction; | |
376 map_x = tank->map_x + map_xy_adj[dir][0]; | |
377 map_y = tank->map_y + map_xy_adj[dir][1]; | |
378 tank->bullet = tank_bullet_new(rdman, map_x, map_y, dir); | |
379 bullet = tank->bullet; | |
380 | |
381 switch(dir) { | |
382 case TD_UP: | |
383 shift_len = map_y + 1; | |
384 shift_x = 0; | |
385 shift_y = -shift_len * 50; | |
386 break; | |
387 case TD_RIGHT: | |
388 shift_len = 16 - map_x; | |
389 shift_x = shift_len * 50; | |
390 shift_y = 0; | |
391 break; | |
392 case TD_DOWN: | |
393 shift_len = 12 - map_y; | |
394 shift_x = 0; | |
395 shift_y = shift_len * 50; | |
396 break; | |
397 case TD_LEFT: | |
398 shift_len = map_x + 1; | |
399 shift_x = -shift_len * 50; | |
400 shift_y = 0; | |
401 break; | |
402 } | |
403 | |
404 progm = mb_progm_new(2, rdman); | |
405 MB_TIMEVAL_SET(&start, 0, 0); | |
406 MB_TIMEVAL_SET(&playing, shift_len / 4, (shift_len % 4) * 250000); | |
407 word = mb_progm_next_word(progm, &start, &playing); | |
408 act = mb_shift_new(shift_x, shift_y, bullet->coord_pos, word); | |
409 bullet->progm = progm; | |
410 | |
411 subject = mb_progm_get_complete(progm); | |
412 factory = rdman_get_ob_factory(rdman); | |
413 subject_add_observer(factory, subject, bullet_go_out_map, tank); | |
414 | |
415 get_now(&now); | |
416 MB_TIMEVAL_CP(&bullet->start_time, &now); | |
417 mb_progm_start(progm, tman, &now); | |
418 } | |
419 | |
115 | 420 #define CHANGE_POS(g, x, y) do { \ |
421 (g)->root_coord->matrix[0] = 1.0; \ | |
422 (g)->root_coord->matrix[2] = x; \ | |
423 (g)->root_coord->matrix[4] = 1.0; \ | |
424 (g)->root_coord->matrix[5] = y; \ | |
425 rdman_coord_changed(rdman, (g)->root_coord); \ | |
426 } while(0) | |
427 | |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
428 static void keyboard_handler(event_t *event, void *arg) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
429 X_kb_event_t *xkey = (X_kb_event_t *)event; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
430 tank_rt_t *tank_rt = (tank_rt_t *)arg; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
431 int direction; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
432 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
433 if(xkey->event.type != EVT_KB_PRESS) |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
434 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
435 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
436 switch(xkey->sym) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
437 case 0xff51: /* left */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
438 direction = TD_LEFT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
439 tank_move(tank_rt->tank1, direction, tank_rt); |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
440 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
441 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
442 case 0xff52: /* up */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
443 direction = TD_UP; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
444 tank_move(tank_rt->tank1, direction, tank_rt); |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
445 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
446 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
447 case 0xff53: /* right */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
448 direction = TD_RIGHT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
449 tank_move(tank_rt->tank1, direction, tank_rt); |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
450 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
451 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
452 case 0xff54: /* down */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
453 direction = TD_DOWN; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
454 tank_move(tank_rt->tank1, direction, tank_rt); |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
455 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
456 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
457 case 0x20: /* space */ |
154 | 458 tank_fire_bullet(tank_rt, tank_rt->tank1); |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
459 break; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
460 case 0xff0d: /* enter */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
461 default: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
462 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
463 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
464 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
465 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
466 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
467 static void init_keyboard(tank_rt_t *tank_rt) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
468 X_MB_runtime_t *mb_rt; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
469 subject_t *kbevents; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
470 redraw_man_t *rdman; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
471 ob_factory_t *factory; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
472 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
473 mb_rt = tank_rt->mb_rt; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
474 kbevents = X_MB_kbevents(mb_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
475 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
476 rdman = X_MB_rdman(mb_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
477 factory = rdman_get_ob_factory(rdman); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
478 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
479 tank_rt->kb_observer = |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
480 subject_add_observer(factory, kbevents, keyboard_handler, tank_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
481 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
482 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
483 /*! \brief Make coord objects for elfs (tanks). */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
484 static void make_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
485 coord_t **coord_rot, coord_t **coord_center) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
486 coord_t *coord_back; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
487 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
488 *coord_pos = rdman_coord_new(rdman, rdman->root_coord); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
489 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
490 coord_back = rdman_coord_new(rdman, *coord_pos); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
491 coord_back->matrix[2] = 25; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
492 coord_back->matrix[5] = 25; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
493 rdman_coord_changed(rdman, coord_back); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
494 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
495 *coord_rot = rdman_coord_new(rdman, coord_back); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
496 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
497 *coord_center = rdman_coord_new(rdman, *coord_rot); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
498 (*coord_center)->matrix[2] = -25; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
499 (*coord_center)->matrix[5] = -25; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
500 rdman_coord_changed(rdman, *coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
501 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
502 |
115 | 503 void |
504 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) { | |
505 redraw_man_t *rdman; | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
506 /* for map areas */ |
115 | 507 mud_t *mud; |
508 brick_t *brick; | |
509 rock_t *rock; | |
510 bush_t *bush; | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
511 /* for tanks */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
512 coord_t *coord_center, *coord_pos, *coord_rot; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
513 tank1_t *tank1_o; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
514 tank2_t *tank2_o; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
515 tank_en_t *tank_en_o; |
115 | 516 int i, j; |
517 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
518 rdman = X_MB_rdman(mb_rt); |
115 | 519 |
520 tank_rt->mb_rt = mb_rt; | |
521 for(i = 0; i < 12; i++) { | |
522 for(j = 0; j < 16; j++) { | |
523 switch(map[i][j]) { | |
524 case MUD: | |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
129
diff
changeset
|
525 mud = mud_new(rdman, rdman->root_coord); |
115 | 526 CHANGE_POS(mud, j * 50, i * 50); |
527 tank_rt->map[i][j] = (void *)mud; | |
528 break; | |
529 case BRI: | |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
129
diff
changeset
|
530 brick = brick_new(rdman, rdman->root_coord); |
115 | 531 CHANGE_POS(brick, j * 50, i * 50); |
532 tank_rt->map[i][j] = (void *)brick; | |
533 break; | |
534 case ROC: | |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
129
diff
changeset
|
535 rock = rock_new(rdman, rdman->root_coord); |
115 | 536 CHANGE_POS(rock, j * 50, i * 50); |
537 tank_rt->map[i][j] = (void *)rock; | |
538 break; | |
539 case BSH: | |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
129
diff
changeset
|
540 bush = bush_new(rdman, rdman->root_coord); |
115 | 541 CHANGE_POS(bush, j * 50, i * 50); |
542 tank_rt->map[i][j] = (void *)bush; | |
543 break; | |
544 } | |
545 } | |
546 } | |
547 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
548 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
549 tank1_o = tank1_new(rdman, coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
550 tank_rt->tank1 = tank_new(coord_pos, coord_rot, 5, 11, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
551 tank_rt->tank1_o = tank1_o; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
552 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
553 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
554 tank2_o = tank2_new(rdman, coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
555 tank_rt->tank2 = tank_new(coord_pos, coord_rot, 10, 11, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
556 tank_rt->tank2_o = tank2_o; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
557 |
117 | 558 for(i = 0; i < 3; i++) { |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
559 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
560 tank_en_o = tank_en_new(rdman, coord_center); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
561 tank_rt->tank_enemies[i] = tank_new(coord_pos, coord_rot, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
562 i * 3 + 3, 0, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
563 tank_rt->tank_enemies_o[i] = tank_en_o; |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
564 tank_rt->tanks[i] = tank_rt->tank_enemies[i]; |
117 | 565 } |
566 tank_rt->n_enemy = i; | |
120 | 567 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
568 tank_rt->tanks[i++] =tank_rt->tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
569 tank_rt->tanks[i++] =tank_rt->tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
570 tank_rt->n_tanks = i; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
571 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
572 init_keyboard(tank_rt); |
115 | 573 } |
574 | |
575 int | |
576 main(int argc, char *const argv[]) { | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
577 X_MB_runtime_t *rt; |
115 | 578 tank_rt_t tank_rt; |
579 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
580 rt = X_MB_new(":0.0", 800, 600); |
115 | 581 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
582 initial_tank(&tank_rt, rt); |
115 | 583 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
584 X_MB_handle_connection(rt); |
115 | 585 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
586 X_MB_free(rt); |
115 | 587 } |