Mercurial > MadButterfly
annotate examples/tank/tank_main.c @ 787:0899dcac441c
Set bounding box attributes for JS
Parse inkscape:bbox-x/y/width/height to
- bbox_x
- bbox_y
- bbox_width
- bbox_height
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 31 Aug 2010 01:26:22 +0800 |
parents | 5235aded379c |
children | c693cd8c6e23 |
rev | line source |
---|---|
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
1 #include <math.h> |
120 | 2 #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
|
3 #include <string.h> |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
4 #include <mb.h> |
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
160
diff
changeset
|
5 #include <mb_tools.h> |
115 | 6 #include "svgs.h" |
7 | |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
8 /*! \defgroup tank Example Tank |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
9 * @{ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
10 */ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
11 /*! \brief Tile types in a map. */ |
115 | 12 enum { MUD, ROC, BRI, BSH }; |
13 | |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
14 /*! \brief Map of the game. */ |
115 | 15 static char map[12][16] = { |
16 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
17 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
18 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, | |
19 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, | |
20 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, | |
21 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD}, | |
22 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
23 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
24 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI, | |
25 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD}, | |
26 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD, | |
27 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
28 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
29 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD}, | |
30 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
31 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD}, | |
32 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD, | |
33 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI}, | |
34 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
35 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD}, | |
36 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, | |
37 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, | |
38 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
39 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} | |
40 }; | |
41 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
42 #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
|
43 #define MAP_H 12 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
44 /* @} */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
45 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
46 /*! \defgroup bullet_elf Bullet Elf |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
47 * \ingroup tank |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
48 * @{ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
49 */ |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
50 /*! \brief Information about bullet elf |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
51 */ |
154 | 52 struct _tank_bullet { |
53 redraw_man_t *rdman; | |
54 coord_t *coord_pos; | |
55 coord_t *coord_rot; | |
56 bullet_t *bullet_obj; | |
57 int start_map_x, start_map_y; | |
58 int direction; | |
59 mb_progm_t *progm; | |
60 mb_timeval_t start_time; | |
61 observer_t *ob_redraw; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
62 mb_timer_t *hit_tmr; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
63 mb_tman_t *tman; |
154 | 64 }; |
65 typedef struct _tank_bullet tank_bullet_t; | |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
66 /*! \brief The direction a bullet is going. |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
67 */ |
154 | 68 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT }; |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
69 /* @} */ |
154 | 70 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
71 /*! \defgroup tank_elf Tank Elf |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
72 * \brief Tank elf module provides control functions of tanks in game. |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
73 * \ingroup tank |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
74 * @{ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
75 */ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
76 /*! \brief Information about a tank elf. */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
77 struct _tank { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
78 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
|
79 coord_t *coord_rot; /*!< \brief coordinate for rotation */ |
154 | 80 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
|
81 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
|
82 int direction; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
83 mb_progm_t *progm; |
154 | 84 tank_bullet_t *bullet; |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
85 struct _tank_rt *tank_rt; /*!< \brief for bullet to check |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
86 * hitting on tanks. |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
87 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
88 }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
89 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
|
90 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
|
91 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
92 /* @} */ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
93 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
94 typedef struct _tank_rt tank_rt_t; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
95 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
96 /*! \brief Runtime information for tank, this game/example. |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
97 */ |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
98 struct _tank_rt { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
99 tank_t *tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
100 tank1_t *tank1_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
101 tank_t *tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
102 tank2_t *tank2_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
103 int n_enemy; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
104 tank_t *tank_enemies[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
105 tank_en_t *tank_enemies_o[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
106 tank_t *tanks[12]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
107 int n_tanks; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
108 void *map[12][16]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
109 X_MB_runtime_t *mb_rt; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
110 observer_t *kb_observer; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
111 }; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
112 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
113 /*! \ingroup tank_elf |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
114 * @{ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
115 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
116 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
|
117 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
|
118 int map_x, int map_y, |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
119 tank_rt_t *tank_rt) { |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
120 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
|
121 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
|
122 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
123 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
|
124 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
|
125 return NULL; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
126 |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
127 rdman = X_MB_rdman(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
|
128 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 tank->progm = NULL; |
154 | 135 tank->bullet = NULL; |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
136 tank->tank_rt = tank_rt; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
137 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
145 return tank; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
146 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
147 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
148 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
|
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 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
151 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
|
152 tman = X_MB_tman(xmb_rt); |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
153 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
|
154 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
155 free(tank); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
156 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
157 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
158 /*! \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
|
159 * |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
160 * 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
|
161 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
162 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
|
163 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
|
164 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
165 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
|
166 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
|
167 } |
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 #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
|
170 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
171 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
|
172 tank_rt_t *tank_rt) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 /* 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 /* for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
184 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
|
185 /* for direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
186 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
|
187 float rot_diff; |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
195 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
|
196 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
197 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
198 /* |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
199 * 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
|
200 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
201 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
|
202 switch(direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
203 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
|
204 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
|
205 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
206 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
|
207 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
208 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
209 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
|
210 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
|
211 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
212 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
|
213 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
214 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
215 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
|
216 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
|
217 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
218 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
|
219 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
220 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
221 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
|
222 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
|
223 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
224 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
|
225 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
226 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
227 } |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
228 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
229 tank->map_x += map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
230 tank->map_y += map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
231 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
|
232 if(tank != tank_rt->tanks[i] && |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
233 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
|
234 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
|
235 tank->map_x -= map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
236 tank->map_y -= map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
237 return; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
238 } |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
239 } |
131
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 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
242 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
|
243 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
|
244 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
|
245 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
246 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
|
247 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
|
248 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
253 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
|
254 /* Shift/move */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 } else { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
259 /* Change direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 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
|
264 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
|
265 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
266 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
267 /* 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
|
268 comp_sub = mb_progm_get_complete(progm); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
190
diff
changeset
|
269 subject_add_observer(comp_sub, clean_tank_progm_handler, tank); |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
270 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
271 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
|
272 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
|
273 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
274 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
275 /* @} */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
276 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
277 /*! \ingroup bullet_elf |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
278 * @{ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
279 */ |
154 | 280 /*! \brief Make coord objects for bullet elfs. */ |
281 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, | |
282 coord_t **coord_rot, | |
283 coord_t **coord_center) { | |
284 coord_t *coord_back; | |
285 | |
286 *coord_pos = rdman_coord_new(rdman, rdman->root_coord); | |
287 | |
288 coord_back = rdman_coord_new(rdman, *coord_pos); | |
289 coord_back->matrix[2] = 25; | |
290 coord_back->matrix[5] = 25; | |
291 rdman_coord_changed(rdman, coord_back); | |
292 | |
293 *coord_rot = rdman_coord_new(rdman, coord_back); | |
294 | |
295 *coord_center = rdman_coord_new(rdman, *coord_rot); | |
296 (*coord_center)->matrix[2] = -5; | |
297 (*coord_center)->matrix[5] = +15; | |
298 rdman_coord_changed(rdman, *coord_center); | |
299 } | |
300 | |
301 static tank_bullet_t *tank_bullet_new(redraw_man_t *rdman, | |
302 int map_x, int map_y, | |
303 int direction) { | |
304 tank_bullet_t *bullet; | |
305 coord_t *coord_center; | |
306 co_aix *matrix; | |
307 static float _sins[] = { 0, 1, 0, -1}; | |
308 static float _coss[] = { 1, 0, -1, 0}; | |
309 float _sin, _cos; | |
310 | |
311 bullet = O_ALLOC(tank_bullet_t); | |
312 bullet->rdman = rdman; | |
313 | |
314 make_bullet_elf_coords(rdman, &bullet->coord_pos, | |
315 &bullet->coord_rot, | |
316 &coord_center); | |
317 bullet->bullet_obj = bullet_new(rdman, coord_center); | |
318 | |
319 bullet->start_map_x = map_x; | |
320 bullet->start_map_y = map_y; | |
321 bullet->direction = direction; | |
322 bullet->progm = NULL; | |
323 | |
324 matrix = bullet->coord_pos->matrix; | |
325 matrix[2] = map_x * 50; | |
326 matrix[5] = map_y * 50; | |
327 rdman_coord_changed(rdman, bullet->coord_pos); | |
328 | |
329 _sin = _sins[direction]; | |
330 _cos = _coss[direction]; | |
331 matrix = bullet->coord_rot->matrix; | |
332 matrix[0] = _cos; | |
333 matrix[1] = -_sin; | |
334 matrix[3] = _sin; | |
335 matrix[4] = _cos; | |
336 | |
337 return bullet; | |
338 } | |
339 | |
340 static void tank_bullet_free(tank_bullet_t *bullet) { | |
341 bullet_free(bullet->bullet_obj); | |
342 rdman_coord_subtree_free(bullet->rdman, bullet->coord_pos); | |
343 } | |
344 | |
345 static void bullet_go_out_map(event_t *event, void *arg) { | |
346 tank_t *tank = (tank_t *)arg; | |
347 tank_bullet_t *bullet; | |
348 redraw_man_t *rdman; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
349 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
350 bullet = tank->bullet; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
351 rdman = bullet->rdman; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
352 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
353 if(bullet->hit_tmr != NULL) |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
354 mb_tman_remove(bullet->tman, bullet->hit_tmr); |
156
2aad042b30a4
Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents:
155
diff
changeset
|
355 |
2aad042b30a4
Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents:
155
diff
changeset
|
356 coord_hide(bullet->coord_pos); |
2aad042b30a4
Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents:
155
diff
changeset
|
357 rdman_coord_changed(rdman, bullet->coord_pos); |
154 | 358 |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
359 bullet = tank->bullet; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
360 mb_progm_free(bullet->progm); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
361 tank_bullet_free(tank->bullet); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
362 tank->bullet = NULL; |
154 | 363 } |
364 | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
365 static void bullet_bang(tank_bullet_t *bullet, int map_x, int map_y) { |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
366 redraw_man_t *rdman; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
367 mb_tman_t *tman; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
368 mb_progm_t *progm; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
369 mb_word_t *word; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
370 mb_timeval_t start, playing; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
371 mb_timeval_t now; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
372 bang_t *bang; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
373 co_aix *matrix; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
374 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
375 rdman = bullet->rdman; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
376 tman = bullet->tman; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
377 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
378 bang = bang_new(rdman, rdman->root_coord); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
379 matrix = bang->root_coord->matrix; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
380 matrix[2] = map_x * 50; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
381 matrix[5] = map_y * 50; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
382 rdman_coord_changed(rdman, bang->root_coord); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
383 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
384 progm = mb_progm_new(3, rdman); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
385 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
386 MB_TIMEVAL_SET(&start, 1, 0); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
387 MB_TIMEVAL_SET(&playing, 0, 0); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
388 word = mb_progm_next_word(progm, &start, &playing); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
389 mb_visibility_new(VIS_HIDDEN, bang->root_coord, word); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
390 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
391 MB_TIMEVAL_SET(&start, 1, 500000); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
392 word = mb_progm_next_word(progm, &start, &playing); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
393 mb_visibility_new(VIS_VISIBLE, bang->root_coord, word); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
394 |
193
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
395 MB_TIMEVAL_SET(&start, 2, 500000); |
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
396 word = mb_progm_next_word(progm, &start, &playing); |
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
397 mb_visibility_new(VIS_HIDDEN, bang->root_coord, word); |
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
398 |
194
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
193
diff
changeset
|
399 mb_subtree_free_new(bang->root_coord, word); |
45d9a1e2764d
Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents:
193
diff
changeset
|
400 |
193
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
401 mb_progm_free_completed(progm); |
923d91dfb6af
Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents:
192
diff
changeset
|
402 |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
403 get_now(&now); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
404 mb_progm_start(progm, tman, &now); |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
405 } |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
406 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
407 /*! \brief To check if a bullet hits walls or tanks. */ |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
408 static void bullet_hit_chk(const mb_timeval_t *tmo, |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
409 const mb_timeval_t *now, |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
410 void *arg) { |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
411 tank_t *tank = (tank_t *)arg; |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
412 tank_rt_t *tank_rt = tank->tank_rt; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
413 tank_t *tank_hitted; |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
414 tank_bullet_t *bullet; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
415 mb_timeval_t diff, next; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
416 mb_timeval_t unit_tm; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
417 float move_units_f; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
418 int move_units; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
419 int x, y; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
420 int dir; |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
421 int i; |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
422 static int move_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
423 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
424 bullet = tank->bullet; |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
425 bullet->hit_tmr = NULL; /* clear hit timer that bullet_go_out_map() |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
426 * can not remove it & currupt memory. |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
427 */ |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
428 |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
429 MB_TIMEVAL_CP(&diff, now); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
430 MB_TIMEVAL_DIFF(&diff, &bullet->start_time); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
431 MB_TIMEVAL_SET(&unit_tm, 0, 250000); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
432 move_units_f = MB_TIMEVAL_DIV(&diff, &unit_tm); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
433 move_units = floorl(move_units_f); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
434 dir = bullet->direction; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
435 x = bullet->start_map_x + move_adj[dir][0] * move_units; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
436 y = bullet->start_map_y + move_adj[dir][1] * move_units; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
437 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
438 if(map[y][x] != MUD) { |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
439 mb_progm_abort(bullet->progm); |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
440 bullet_go_out_map(NULL, tank); |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
441 bullet_bang(bullet, x, y); |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
442 return; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
443 } |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
444 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
445 /*! \todo Move tanks into a list. */ |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
446 for(i = 0; i < tank_rt->n_enemy; i++) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
447 tank_hitted = tank_rt->tank_enemies[i]; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
448 if(tank_hitted == tank) |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
449 continue; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
450 if(tank_hitted->map_x == x && |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
451 tank_hitted->map_y == y) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
452 mb_progm_abort(bullet->progm); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
453 bullet_go_out_map(NULL, tank); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
454 bullet_bang(bullet, x, y); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
455 return; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
456 } |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
457 } |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
458 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
459 if(tank_rt->tank1 != tank) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
460 tank_hitted = tank_rt->tank1; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
461 if(tank_hitted->map_x == x && |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
462 tank_hitted->map_y == y) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
463 mb_progm_abort(bullet->progm); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
464 bullet_go_out_map(NULL, tank); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
465 bullet_bang(bullet, x, y); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
466 return; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
467 } |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
468 } |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
469 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
470 if(tank_rt->tank2 != tank) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
471 tank_hitted = tank_rt->tank2; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
472 if(tank_hitted->map_x == x && |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
473 tank_hitted->map_y == y) { |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
474 mb_progm_abort(bullet->progm); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
475 bullet_go_out_map(NULL, tank); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
476 bullet_bang(bullet, x, y); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
477 return; |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
478 } |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
479 } |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
480 |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
481 MB_TIMEVAL_SET(&next, 0, 100000); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
482 MB_TIMEVAL_ADD(&next, now); |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
483 bullet->hit_tmr = mb_tman_timeout(bullet->tman, &next, |
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
484 bullet_hit_chk, arg); |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
485 } |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
486 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
487 /*! \brief To fire a bullet for a tank. */ |
154 | 488 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) { |
489 X_MB_runtime_t *xmb_rt; | |
490 redraw_man_t *rdman; | |
491 int map_x, map_y; | |
492 int shift_x, shift_y; | |
493 int shift_len; | |
494 int dir; | |
495 tank_bullet_t *bullet; | |
496 mb_progm_t *progm; | |
497 mb_word_t *word; | |
498 mb_action_t *act; | |
499 mb_timeval_t start, playing; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
500 mb_timeval_t now, next; |
154 | 501 mb_tman_t *tman; |
502 subject_t *subject; | |
503 static int map_xy_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; | |
504 | |
505 if(tank->bullet != NULL) | |
506 return; | |
507 | |
508 xmb_rt = tank_rt->mb_rt; | |
509 rdman = X_MB_rdman(xmb_rt); | |
510 tman = X_MB_tman(xmb_rt); | |
511 | |
512 dir = tank->direction; | |
513 map_x = tank->map_x + map_xy_adj[dir][0]; | |
514 map_y = tank->map_y + map_xy_adj[dir][1]; | |
515 switch(dir) { | |
516 case TD_UP: | |
517 shift_len = map_y + 1; | |
518 shift_x = 0; | |
519 shift_y = -shift_len * 50; | |
520 break; | |
521 case TD_RIGHT: | |
522 shift_len = 16 - map_x; | |
523 shift_x = shift_len * 50; | |
524 shift_y = 0; | |
525 break; | |
526 case TD_DOWN: | |
527 shift_len = 12 - map_y; | |
528 shift_x = 0; | |
529 shift_y = shift_len * 50; | |
530 break; | |
531 case TD_LEFT: | |
532 shift_len = map_x + 1; | |
533 shift_x = -shift_len * 50; | |
534 shift_y = 0; | |
535 break; | |
536 } | |
537 | |
157
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
538 if(shift_len <= 0) |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
539 return; |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
540 |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
541 tank->bullet = tank_bullet_new(rdman, map_x, map_y, dir); |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
542 bullet = tank->bullet; |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
543 bullet->tman = tman; |
5cd12609a5c7
Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents:
156
diff
changeset
|
544 |
154 | 545 progm = mb_progm_new(2, rdman); |
546 MB_TIMEVAL_SET(&start, 0, 0); | |
547 MB_TIMEVAL_SET(&playing, shift_len / 4, (shift_len % 4) * 250000); | |
548 word = mb_progm_next_word(progm, &start, &playing); | |
549 act = mb_shift_new(shift_x, shift_y, bullet->coord_pos, word); | |
550 bullet->progm = progm; | |
551 | |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
552 /*! \todo Simplify the procdure of using observer pattern. */ |
154 | 553 subject = mb_progm_get_complete(progm); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
190
diff
changeset
|
554 subject_add_observer(subject, bullet_go_out_map, tank); |
154 | 555 |
556 get_now(&now); | |
557 MB_TIMEVAL_CP(&bullet->start_time, &now); | |
558 mb_progm_start(progm, tman, &now); | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
559 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
560 MB_TIMEVAL_SET(&next, 0, 100000); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
561 MB_TIMEVAL_ADD(&next, &now); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
562 bullet->hit_tmr = mb_tman_timeout(tman, &next, bullet_hit_chk, tank); |
154 | 563 } |
564 | |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
565 /* @} */ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
566 |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
567 /*! \ingroup tank |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
568 * @{ |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
569 */ |
115 | 570 #define CHANGE_POS(g, x, y) do { \ |
571 (g)->root_coord->matrix[0] = 1.0; \ | |
572 (g)->root_coord->matrix[2] = x; \ | |
573 (g)->root_coord->matrix[4] = 1.0; \ | |
574 (g)->root_coord->matrix[5] = y; \ | |
575 rdman_coord_changed(rdman, (g)->root_coord); \ | |
576 } while(0) | |
577 | |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
578 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
|
579 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
|
580 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
|
581 int direction; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
582 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
583 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
|
584 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
585 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
586 switch(xkey->sym) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
587 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
|
588 direction = TD_LEFT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
589 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
|
590 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
591 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
592 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
|
593 direction = TD_UP; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
594 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
|
595 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
596 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
597 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
|
598 direction = TD_RIGHT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
599 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
|
600 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
601 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
602 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
|
603 direction = TD_DOWN; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
604 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
|
605 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
606 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
607 case 0x20: /* space */ |
154 | 608 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
|
609 break; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
610 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
|
611 default: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
612 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
613 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
614 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
615 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
616 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
617 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
|
618 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
|
619 subject_t *kbevents; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
620 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
|
621 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
622 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
|
623 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
|
624 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
625 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
|
626 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
627 tank_rt->kb_observer = |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
190
diff
changeset
|
628 subject_add_observer(kbevents, keyboard_handler, tank_rt); |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
629 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
630 |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
631 /*! \brief Make coord objects to decorate elfs (tanks). |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
632 * |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
633 * These coords are used to shift (move) and rotate decorated graphic. |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
634 * The coords can easy work of programmer to manipulate geometry of |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
635 * decorated graphic. |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
636 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
637 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
|
638 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
|
639 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
|
640 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
641 *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
|
642 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
643 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
|
644 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
|
645 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
|
646 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
|
647 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
648 *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
|
649 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
650 *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
|
651 (*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
|
652 (*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
|
653 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
|
654 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
655 |
115 | 656 void |
657 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) { | |
658 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
|
659 /* for map areas */ |
115 | 660 mud_t *mud; |
661 brick_t *brick; | |
662 rock_t *rock; | |
663 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
|
664 /* for tanks */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
665 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
|
666 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
|
667 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
|
668 tank_en_t *tank_en_o; |
115 | 669 int i, j; |
670 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
671 rdman = X_MB_rdman(mb_rt); |
115 | 672 |
673 tank_rt->mb_rt = mb_rt; | |
674 for(i = 0; i < 12; i++) { | |
675 for(j = 0; j < 16; j++) { | |
676 switch(map[i][j]) { | |
677 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
|
678 mud = mud_new(rdman, rdman->root_coord); |
115 | 679 CHANGE_POS(mud, j * 50, i * 50); |
680 tank_rt->map[i][j] = (void *)mud; | |
681 break; | |
682 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
|
683 brick = brick_new(rdman, rdman->root_coord); |
115 | 684 CHANGE_POS(brick, j * 50, i * 50); |
685 tank_rt->map[i][j] = (void *)brick; | |
686 break; | |
687 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
|
688 rock = rock_new(rdman, rdman->root_coord); |
115 | 689 CHANGE_POS(rock, j * 50, i * 50); |
690 tank_rt->map[i][j] = (void *)rock; | |
691 break; | |
692 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
|
693 bush = bush_new(rdman, rdman->root_coord); |
115 | 694 CHANGE_POS(bush, j * 50, i * 50); |
695 tank_rt->map[i][j] = (void *)bush; | |
696 break; | |
697 } | |
698 } | |
699 } | |
700 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
701 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
|
702 tank1_o = tank1_new(rdman, coord_center); |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
703 tank_rt->tank1 = tank_new(coord_pos, coord_rot, 5, 11, tank_rt); |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
704 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
|
705 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
706 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
|
707 tank2_o = tank2_new(rdman, coord_center); |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
708 tank_rt->tank2 = tank_new(coord_pos, coord_rot, 10, 11, tank_rt); |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
709 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
|
710 |
117 | 711 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
|
712 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
|
713 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
|
714 tank_rt->tank_enemies[i] = tank_new(coord_pos, coord_rot, |
189
257af0ed5852
When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
715 i * 3 + 3, 0, tank_rt); |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
716 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
|
717 tank_rt->tanks[i] = tank_rt->tank_enemies[i]; |
117 | 718 } |
719 tank_rt->n_enemy = i; | |
120 | 720 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
721 tank_rt->tanks[i++] =tank_rt->tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
722 tank_rt->tanks[i++] =tank_rt->tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
723 tank_rt->n_tanks = i; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
724 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
725 init_keyboard(tank_rt); |
115 | 726 } |
727 | |
728 int | |
729 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
|
730 X_MB_runtime_t *rt; |
115 | 731 tank_rt_t tank_rt; |
732 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
733 rt = X_MB_new(":0.0", 800, 600); |
115 | 734 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
735 initial_tank(&tank_rt, rt); |
115 | 736 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
737 X_MB_handle_connection(rt); |
115 | 738 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
739 X_MB_free(rt); |
115 | 740 } |
190
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
741 |
0a924eb9ccab
Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents:
189
diff
changeset
|
742 /* @} */ |