Revision 226
implemented double-linked list. implemented bullet collision with walls from gunner collision. implemented bullet position updating with certain speed
proj/include/ent.h | ||
---|---|---|
32 | 32 |
double (bullet_get_y) (const bullet_t *p); |
33 | 33 |
int16_t (bullet_get_x_screen)(const bullet_t *p); |
34 | 34 |
int16_t (bullet_get_y_screen)(const bullet_t *p); |
35 |
void (bullet_update_movement)(bullet_t *p); |
|
35 | 36 |
void (bullet_draw)(bullet_t *p); |
36 | 37 |
|
37 | 38 |
struct map; |
... | ... | |
39 | 40 |
map_t* (map_ctor)(const char **background, const char **collide); |
40 | 41 |
void (map_dtor)(map_t *p); |
41 | 42 |
int (map_collides_point)(const map_t *p, double x, double y); |
42 |
int (map_collides_gunner)(const map_t *p, gunner_t *gunner); |
|
43 |
int (map_collides_bullet)(const map_t *p, bullet_t *bullet); |
|
43 |
int (map_collides_gunner)(const map_t *p, const gunner_t *gunner);
|
|
44 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bullet);
|
|
44 | 45 |
void (map_draw)(map_t *p); |
45 | 46 |
|
46 | 47 |
#endif //ENT_H_INCLUDED |
proj/include/list.h | ||
---|---|---|
1 |
#ifndef LIST_H_INCLUDED |
|
2 |
#define LIST_H_INCLUDED |
|
3 |
|
|
4 |
struct list_node; |
|
5 |
typedef struct list_node list_node_t; |
|
6 |
|
|
7 |
list_node_t* (list_node_ctor)(list_node_t *p, list_node_t *n, void *val); |
|
8 |
void (list_node_dtor)(list_node_t *p); |
|
9 |
list_node_t* (list_node_next)(const list_node_t *p); |
|
10 |
list_node_t* (list_node_prev)(const list_node_t *p); |
|
11 |
void** (list_node_val )(list_node_t *p); |
|
12 |
|
|
13 |
struct list; |
|
14 |
typedef struct list list_t; |
|
15 |
|
|
16 |
list_t* (list_ctor)(void); |
|
17 |
int (list_dtor)(list_t *l); |
|
18 |
list_node_t* (list_begin )(list_t *l); |
|
19 |
list_node_t* (list_end )(list_t *l); |
|
20 |
size_t (list_size )(const list_t *l); |
|
21 |
list_node_t* (list_insert)(list_t *l, list_node_t *position, void *val); |
|
22 |
void* (list_erase )(list_t *l, list_node_t *position); |
|
23 |
|
|
24 |
#endif //LIST_H_INCLUDED |
|
0 | 25 |
proj/src/ent.c | ||
---|---|---|
4 | 4 |
|
5 | 5 |
#include "graph.h" |
6 | 6 |
#include "sprite.h" |
7 |
|
|
7 |
#include "utils.h" |
|
8 | 8 |
#include <math.h> |
9 | 9 |
|
10 | 10 |
static double scale = 1.0; |
... | ... | |
62 | 62 |
|
63 | 63 |
struct bullet{ |
64 | 64 |
double x, y; //real position |
65 |
double vx, vy; |
|
65 | 66 |
sprite_t *b; |
66 | 67 |
}; |
67 | 68 |
bullet_t* (bullet_ctor)(basic_sprite_t *b){ |
68 | 69 |
bullet_t *ret = malloc(sizeof(bullet_t)); |
69 | 70 |
if(ret == NULL) return NULL; |
70 |
ret->x = 0.0; |
|
71 |
ret->y = 0.0; |
|
71 |
ret-> x = 0.0; |
|
72 |
ret-> y = 0.0; |
|
73 |
ret->vx = 0.0; |
|
74 |
ret->vy = 0.0; |
|
72 | 75 |
ret->b = sprite_ctor(b); |
73 | 76 |
if(ret->b == NULL){ |
74 | 77 |
bullet_dtor(ret); |
... | ... | |
86 | 89 |
double (bullet_get_y) (const bullet_t *p){ return p->y; } |
87 | 90 |
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; } |
88 | 91 |
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; } |
92 |
void (bullet_update_movement)(bullet_t *p){ |
|
93 |
p->x += p->vx; |
|
94 |
p->y += p->vy; |
|
95 |
} |
|
89 | 96 |
void (bullet_draw)(bullet_t *p){ |
90 | 97 |
const int16_t x_screen = bullet_get_x_screen(p); |
91 | 98 |
const int16_t y_screen = bullet_get_y_screen(p); |
... | ... | |
143 | 150 |
return p->collide[pos]; |
144 | 151 |
} |
145 | 152 |
|
146 |
int (map_collides_gunner)(const map_t *p, gunner_t *shooter) { |
|
153 |
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
|
|
147 | 154 |
double radius = sprite_get_w(shooter->dude)/2.0; |
148 | 155 |
double shooter_x = gunner_get_x(shooter); |
149 | 156 |
double shooter_y = gunner_get_y(shooter); |
... | ... | |
155 | 162 |
return 0; |
156 | 163 |
} |
157 | 164 |
|
158 |
int (map_collides_bullet)(const map_t *p, bullet_t *bullet) { |
|
165 |
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){ |
|
166 |
double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0; |
|
167 |
double bullet_x = bullet_get_x(bull); |
|
168 |
double bullet_y = bullet_get_y(bull); |
|
169 |
for (double x = -radius; x < radius; x += 1){ |
|
170 |
double y1 = sqrt(radius*radius - x*x); |
|
171 |
double y2 = -y1; |
|
172 |
if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1; |
|
173 |
} |
|
159 | 174 |
return 0; |
160 | 175 |
} |
161 | 176 |
|
proj/src/list.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "list.h" |
|
4 |
|
|
5 |
struct list_node{ |
|
6 |
list_node_t *p, *n; |
|
7 |
void *val; |
|
8 |
}; |
|
9 |
list_node_t* (list_node_ctor)(list_node_t *p, list_node_t *n, void *val){ |
|
10 |
list_node_t *ret = malloc(sizeof(list_node_t)); |
|
11 |
if(ret == NULL) return NULL; |
|
12 |
ret->p = p; |
|
13 |
ret->n = n; |
|
14 |
ret->val = val; |
|
15 |
return ret; |
|
16 |
} |
|
17 |
void (list_node_dtor)(list_node_t *p){ |
|
18 |
free(p); |
|
19 |
} |
|
20 |
list_node_t* (list_node_next)(const list_node_t *p){ return p->n; } |
|
21 |
list_node_t* (list_node_prev)(const list_node_t *p){ return p->p; } |
|
22 |
void** (list_node_val )(list_node_t *p){ return &p->val; } |
|
23 |
|
|
24 |
struct list{ |
|
25 |
list_node_t *begin_; |
|
26 |
list_node_t *end_; |
|
27 |
size_t sz; |
|
28 |
}; |
|
29 |
list_t* (list_ctor)(void){ |
|
30 |
list_t *ret = malloc(sizeof(list_t)); |
|
31 |
if(ret == NULL) return NULL; |
|
32 |
ret->sz = 0; |
|
33 |
ret->begin_ = NULL; ret->end_ = NULL; |
|
34 |
ret->begin_ = list_node_ctor(NULL, NULL, NULL); |
|
35 |
ret->end_ = list_node_ctor(NULL, NULL, NULL); |
|
36 |
if(ret->begin_ == NULL || ret->end_ == NULL){ |
|
37 |
list_dtor(ret); |
|
38 |
return NULL; |
|
39 |
} |
|
40 |
ret->begin_->n = ret->end_ ; |
|
41 |
ret->end_ ->p = ret->begin_; |
|
42 |
return ret; |
|
43 |
} |
|
44 |
int (list_dtor)(list_t *l){ |
|
45 |
if(l == NULL) return 0; |
|
46 |
if(list_size(l) > 0) return 1; |
|
47 |
list_node_dtor(l->begin_); |
|
48 |
list_node_dtor(l->end_); |
|
49 |
free(l); |
|
50 |
return 0; |
|
51 |
} |
|
52 |
list_node_t* (list_begin )( list_t *l){ return l->begin_->n; } |
|
53 |
list_node_t* (list_end )( list_t *l){ return l->end_; } |
|
54 |
size_t (list_size )(const list_t *l){ return l->sz; } |
|
55 |
list_node_t* (list_insert)(list_t *l, list_node_t *position, void *val){ |
|
56 |
list_node_t *node = list_node_ctor(position->p, position, val); |
|
57 |
position->p->n = node; |
|
58 |
position->p = node; |
|
59 |
++l->sz; |
|
60 |
return node; |
|
61 |
} |
|
62 |
void* (list_erase )(list_t *l, list_node_t *position){ |
|
63 |
position->p->n = position->n; |
|
64 |
position->n->p = position->p; |
|
65 |
void *ret = position->val; |
|
66 |
list_node_dtor(position); |
|
67 |
--l->sz; |
|
68 |
return ret; |
|
69 |
} |
|
0 | 70 |
proj/src/proj.c | ||
---|---|---|
26 | 26 |
#include "bullet.h" |
27 | 27 |
#include "map1.h" |
28 | 28 |
|
29 |
#include "list.h" |
|
30 |
|
|
29 | 31 |
int main(int argc, char* argv[]) { |
30 | 32 |
|
31 | 33 |
lcf_set_language("EN-US"); |
... | ... | |
106 | 108 |
graph_draw(); |
107 | 109 |
rectangle_dtor(rect); |
108 | 110 |
|
111 |
list_t *l = list_ctor(); |
|
112 |
int *p = NULL; |
|
113 |
for(int i = 10; i < 20; ++i){ |
|
114 |
p = malloc(sizeof(int)); |
|
115 |
*p = i; |
|
116 |
printf("INSERTING %d\n", i); |
|
117 |
list_insert(l, list_end(l), p); |
|
118 |
printf("INSERTED, SIZE=%d\n", list_size(l)); |
|
119 |
} |
|
120 |
list_node_t *it = list_begin(l); |
|
121 |
while(it != list_end(l)){ |
|
122 |
printf("%d\n", **(int**)list_node_val(it)); |
|
123 |
it = list_node_next(it); |
|
124 |
} |
|
125 |
while(list_size(l) > 0){ |
|
126 |
printf("ERASING\n"); |
|
127 |
void *p = list_erase(l, list_begin(l)); |
|
128 |
printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l)); |
|
129 |
free(p); |
|
130 |
} |
|
131 |
printf("DONE\n"); |
|
132 |
if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n"); |
|
133 |
|
|
134 |
|
|
109 | 135 |
tickdelay(micros_to_ticks(1000000)); |
110 | 136 |
|
111 | 137 |
#endif |
proj/DR.mk | ||
---|---|---|
2 | 2 |
|
3 | 3 |
.PATH: ${.CURDIR}/src |
4 | 4 |
|
5 |
SRCS= proj.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c |
|
5 |
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
|
|
6 | 6 |
|
7 | 7 |
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_ |
8 | 8 |
|
proj/Makefile | ||
---|---|---|
2 | 2 |
|
3 | 3 |
.PATH: ${.CURDIR}/src |
4 | 4 |
|
5 |
SRCS= proj.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c |
|
5 |
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
|
|
6 | 6 |
|
7 | 7 |
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_ |
8 | 8 |
|
proj/TB.mk | ||
---|---|---|
2 | 2 |
|
3 | 3 |
.PATH: ${.CURDIR}/src |
4 | 4 |
|
5 |
SRCS= proj.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c |
|
5 |
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
|
|
6 | 6 |
|
7 | 7 |
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_ |
8 | 8 |
|
Also available in: Unified diff