aboutsummaryrefslogtreecommitdiff
path: root/src/engine/bullet.c
blob: eb9a8bb354224776edd2ce723be59ccdfdcc1a0e (plain)
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
#include "bullet.h"
int bullets_size=0;
static int current_bullet=0;

hh_entity* all_bullets=NULL;
hh_entity* hh_init_bullets(int size) {
    if (all_bullets != NULL) {
        free(all_bullets);
    }
    bullets_size = size;
    all_bullets = malloc(size * sizeof(hh_entity));
    hh_entity bullet = {
        .speed = 1,
        .is_grounded = true,
        .is_hit = false,
        .radius = 4,
        .pos = (vec2){-16,-16},
        .vel = (vec2){0,0},
        .size = (vec2) { 13,16},
        .render = {
            .frame0 = 84,
            .palette = 3,
            .fam = (hh_s_ppu_loc_fam_entry){
                .horizontal_flip = false,
                .vertical_flip = false,
                .palette_index = 7,
                .tilemap_index = 84,
            }
        }
    };
    for (int i = 0; i < size; i++) {
        all_bullets[i] = bullet;
    }
    return all_bullets;
}
bool rising_edge(bool signal, bool* prev) {
    bool edge = false;
    if (signal && !(*prev)) {
        edge = true;
    }
    *prev = signal;
    return edge;
}
bool prev_signal = false;
void hh_handle_bullet_direction(hh_entity* bullet){
	bullet->vel = (vec2){0,0};
	if (g_hh_controller_p1.dpad_left) {
		if (g_hh_controller_p1.dpad_up) {
			bullet->vel.x = -1;
			bullet->vel.y = -1;
		} else if (g_hh_controller_p1.dpad_down) {
			bullet->vel.x = -1;
			bullet->vel.y = 1;
		} else {
			bullet->vel.x = -1;
		}
	}else if (g_hh_controller_p1.dpad_right) {
		if (g_hh_controller_p1.dpad_up) {
			bullet->vel.x = 1;
			bullet->vel.y = -1;
		} else if (g_hh_controller_p1.dpad_down) {
			bullet->vel.x = 1;
			bullet->vel.y = 1;
		} else {
			bullet->vel.x = 1;
		}
	} else if (g_hh_controller_p1.dpad_up){
		bullet->vel.y = -1;
	} else if (g_hh_controller_p1.dpad_down){
		bullet->vel.y = 1;
	}
	else{
		bullet->vel.x = 1;
	}
	bullet->render.fam.horizontal_flip = (bullet->vel.x < 0);
	bullet->render.fam.vertical_flip = (bullet->vel.y != 0);
	
}
void hh_shoot_bullet(vec2 player, hh_entity* bullet){
	vec2 temp;
	if(rising_edge(g_hh_controller_p1.button_secondary,&prev_signal) && bullet->is_grounded){
			bullet->is_grounded=false;
			bullet->pos = player;
			current_bullet = (current_bullet + 1) % bullets_size;
			hh_handle_bullet_direction(bullet);
	}

}

void hh_update_bullet(hh_entity* bullet){
	if(hh_background_collision_bulllet(*bullet)){
		hh_bullet_death(bullet);
	}
	else{
		bullet->pos.x = bullet->pos.x + bullet->vel.x * bullet->speed;	
		bullet->pos.y = bullet->pos.y  + bullet->vel.y * bullet->speed;	
	}

}

void hh_multiple_bullets(vec2 player, hh_entity* bullets){

	hh_shoot_bullet(player, &bullets[current_bullet]);
	for(int i=0; i < bullets_size;i++){
		if(!bullets[i].is_grounded){
			hh_update_bullet(&bullets[i]);
		}
	}
}

void hh_bullet_death(hh_entity* bullet){
	bullet->is_grounded=true;
	bullet->pos.x= -16;
	bullet->pos.y= -16;
}