-
Notifications
You must be signed in to change notification settings - Fork 0
/
gun.c
108 lines (90 loc) · 3.19 KB
/
gun.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
#include <chipmunk/chipmunk.h>
#include "gun.h"
#include "obj.h"
#include "room.h"
#include "tonic.h"
#include "extras.h"
struct guntbl fires;
struct gun instantiate_gun(struct obj *owner, const char *gunType,
const char *bulletType)
{
// fprintf(stderr, "\n\n\n%s\n\n\n", bulletType);
struct gun ret = {.bulletType = strdup(bulletType), .owner = owner, .cooldown = owner->room->ticks};
ret.fire = guntbl_find(&fires, gunType);
return ret;
}
void machinegun_fire(struct gun *self)
{
if (self->owner->room->ticks - self->cooldown > 1) {
struct obj *bullet = instantiate_object(self->bulletType,
self->owner->objectVect,
self->owner->room);
cpBodySetPosition(bullet->body,
cpBodyLocalToWorld(self->owner->body,
cpv(0, -self->owner->radius - bullet->radius - 2)));
cpBodySetMoment(bullet->body,
cpMomentForCircle(10000.0, 0.0,
bullet->radius*2, cpvzero));
cpBodySetAngle(bullet->body, cpBodyGetAngle(self->owner->body) + M_PI);
((struct bullet_extra*)bullet->extra)->speed = 20.0;
((struct bullet_extra*)bullet->extra)->owner = self->owner;
cpBodyApplyImpulseAtLocalPoint(self->owner->body,
cpv(0, 50.0),
cpv(0, -(self->owner->radius * 2)));
self->cooldown = self->owner->room->ticks;
}
}
struct gunpair {
const char *name;
const gunmethod method;
};
void register_guns(void)
{
struct gunpair pairs[] = {{"machine gun", machinegun_fire}};
for (size_t i = 0; i < ARRAY_LENGTH(pairs); i++) {
char *currentName = pairs[i].name;
gunmethod currentMethod = pairs[i].method;
if ((!currentName && currentMethod) || (currentName && !currentMethod)) {
CROAK("Unevenly sized methodpair array");
}
guntbl_insert(&fires, currentName, currentMethod);
}
}
void guntbl_insert(struct guntbl *table, const char *key, gunmethod method)
{
int offset = hash(key) % 32;
if (table->keys[offset]) {
if (strcmp(table->keys[offset], key) == 0 ) {
table->methods[offset] = method;
} else {
if (!table->next)
{
table->next = malloc(sizeof(struct guntbl));
*table->next = (struct guntbl){.next = NULL};
}
guntbl_insert(table->next, key, method);
}
} else {
table->methods[offset] = method;
int key_len = strlen(key);
// printf("length of %s is %d", key, key_len);
(table->keys[offset]) = malloc(key_len+1);
snprintf(table->keys[offset], key_len+1, "%s", key);
// printf("NEW KEY %s\n", table->keys[offset]);
}
}
gunmethod guntbl_find(struct guntbl *table, const char *key)
{
int offset = hash(key) % 32;
// fprintf(stderr, "\n\n\nkey %s\n\n\n", key);
if (!table) {
return NULL;
}
if (table->keys[offset]) {
if (strcmp(table->keys[offset], key) == 0 ) {
return table->methods[offset];
}
guntbl_find(table->next, key);
}
return NULL;
}