-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.c
153 lines (151 loc) · 3.87 KB
/
bubble.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "bubbletrouble.h"
#include <stdlib.h>
#include <chipmunk.h>
/**
* Initialize a bubble that will bounce around.
* \param b The bubble to initialize.
* \param x The x location of the bubble.
* \param y The y location of the bubble.
* \param v_x The x velocity of the bubble.
* \param v_y The y velocity of the bubble.
* \param r The radius of the bubble.
* \param l The bubble's life.
*/
struct bubble *bubble_init(struct bubble *b, double x, double y, double v_x,
double v_y, double r, uint8_t l) {
cpBody *body = cpBodyInit(&b->body, r,
cpMomentForCircle(r, 0, r, cpv(0, 0)));
if (!body) {
ERR_ERRNO();
return 0;
}
cpShape *shape = cpCircleShapeInit(&b->shape, body, r, cpv(0,0));
if (!shape) {
cpBodyDestroy(body);
ERR_ERRNO();
return 0;
}
cpBodySetVel(body, cpv(v_x, v_y));
cpBodySetPos(body, cpv(x, y));
shape->e = 1.0;
shape->data = b;
shape->collision_type = BUBBLE;
b->l = l;
return b;
}
/**
* Create a bubble that will bounce around the screen.
* \param g The game in which to create the bubble.
* \param x The x location of the bubble.
* \param y The y location of the bubble.
* \param v_x The x velocity of the bubble.
* \param v_y The y velocity of the bubble.
* \param r The radius of the bubble.
* \param l The bubble's life.
*/
struct bubble *bubble_create(double x, double y, double v_x, double v_y,
double r, uint8_t l) {
struct bubble *b = calloc(1, sizeof(struct bubble));
if (!b) {
ERR_ERRNO();
return 0;
}
return bubble_init(b, x, y, v_x, v_y, r, l);
}
/**
* Add the bubble into the game.
*/
int bubble_add(struct bubble *b, struct game *g) {
if (linkedlist_add_last(g->b, b)) {
ERR_TRACE();
return -1;
}
b->node = g->b->last;
cpSpaceAddBody(g->cp, &b->body);
cpSpaceAddShape(g->cp, &b->shape);
return 0;
}
/**
* Frees any internal memory of the bubble.
*/
void bubble_destroy(struct bubble *b) {
cpShapeDestroy(&b->shape);
cpBodyDestroy(&b->body);
}
/**
* Frees any memory used by the bubble.
* Frees the internal and the struct memory.
*/
void bubble_free(struct bubble *b) {
bubble_destroy(b);
free(b);
}
/**
* Remove the bubble from the game.
*/
void bubble_remove(struct bubble *b, struct game *g) {
cpSpaceRemoveShape(g->cp, &b->shape);
cpSpaceRemoveBody(g->cp, &b->body);
linkedlist_remove_node(g->b, b->node);
}
/**
* Pop the bubble splitting it into two bubbles.
* No children are created if the bubble was too small.
*/
int bubble_pop(struct bubble *b, struct game *g) {
if (b->l <= 1) {
bubble_destroy(b);
return 0;
}
double radius = cpCircleShapeGetRadius(&b->shape);
cpVect pos = cpBodyGetPos(&b->body);
cpVect vel = cpBodyGetVel(&b->body);
bubble_remove(b, g);
bubble_destroy(b);
b = bubble_init(b, pos.x, pos.y, vel.x + 5, vel.y + 5,
radius / 2, b->l - 1);
if (!b) {
ERR_TRACE();
return -1;
}
//b->events = b->events;
if (bubble_add(b, g)) {
ERR_TRACE();
return -1;
}
struct bubble *c = bubble_create(pos.x, pos.y, vel.x - 5, vel.y + 5,
radius / 2, b->l - 1);
if (!c) {
ERR_TRACE();
return -1;
}
//c->events = b->events;
if (bubble_add(c, g)) {
ERR_TRACE();
return -1;
}
return 0;
}
/**
* Called when two bubbles collide.
* They may or may not bounce off each other depending on a flag.
*/
int bubble_bubble_collision(cpArbiter *arb, struct cpSpace *space, void *data) {
struct game *g = data;
if (g->data & 1) {
return 1;
} else {
return 0;
}
}
//FIXME move this into bullet, as different bullets may do different damage
/**
* Called when a bubble and popper (bullet) collide.
* The bubble will be split in two.
*/
int bubble_popper_collision(cpArbiter *arb, struct cpSpace *space, void *data) {
CP_ARBITER_GET_SHAPES(arb, shape_a, shape_b);
struct bubble *p = shape_b->data;
cpSpaceAddPostStepCallback(space, bubble_collision_pop, p, data);
return 0;
}