aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-04-03 21:30:43 +0200
committerNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-04-03 21:30:43 +0200
commitf6c1eb582ac44b92c86816352bd56da5a6f4f1b5 (patch)
tree8925f2c90b30dfe7abaa8b35a5cdf750520e1f8f
parentf47d082d2fb2d5e5b5a4d3e685d8d093699938d7 (diff)
multiple bullets and bg collisions.
-rw-r--r--src/engine/bullet.c85
-rw-r--r--src/engine/bullet.h11
-rw-r--r--src/engine/entity.c35
-rw-r--r--src/engine/entity.h3
-rw-r--r--src/engine/level_const.c2
-rw-r--r--src/engine/player_controller.c21
-rw-r--r--src/game_loop/gameplay.c6
-rw-r--r--src/game_loop/gameplay.h1
8 files changed, 121 insertions, 43 deletions
diff --git a/src/engine/bullet.c b/src/engine/bullet.c
index e6ca6df..40f33d9 100644
--- a/src/engine/bullet.c
+++ b/src/engine/bullet.c
@@ -1,35 +1,84 @@
#include "bullet.h"
+int bullets_size=0;
+static int current_bullet=0;
-
-// TODO: use hh_entity as bullet struct
+hh_entity* hh_init_bullets(int size) {
+ bullets_size = size;
+ hh_entity* 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_shoot_bullet(vec2 player, vec_cor cam_pos, hh_entity* bullet){
vec2 temp;
- if(g_hh_controller_p1.button_secondary){
- if(bullet->is_grounded){
+ 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;
}
- else{
- if(!bullet->is_grounded){
- hh_update_bullet(bullet , cam_pos);
- hh_draw_bullet(*bullet);
- }
- }
-
}
void hh_update_bullet(hh_entity* bullet, vec_cor cam_pos){
- bullet->pos.x += 1;
+ if(hh_background_collision_bulllet(*bullet)){
+ bullet->is_grounded=true;
+ bullet->pos.x=-16;
+ bullet->pos.y=-16;
+ // printf("x %d y %d\n",(bullet->pos.x-cam_pos.x),(bullet->pos.y-cam_pos.y));
+ }
+ else{
+ bullet->pos.x += bullet->speed;
+ }
+
// update bullet sprite on ppu
- bullet->render.fam.position_x = (bullet->pos.x-cam_pos.x);
- bullet->render.fam.position_y = (bullet->pos.y-cam_pos.y);
+ bullet->render.fam.position_x = (bullet->pos.x - cam_pos.x);
+ bullet->render.fam.position_y = (bullet->pos.y - cam_pos.y);
}
-void hh_draw_bullet(hh_entity bullet){
- hh_s_ppu_loc_fam_entry temp = bullet.render.fam;
- hh_ppu_update_foreground(10,temp);
+void hh_draw_bullet(hh_entity bullet,int number){
+ // hh_s_ppu_loc_fam_entry temp = bullet.render.fam;
+ // hh_ppu_update_foreground(hh_ppu_bullet_fg_offset_idx + number,temp);
+
+}
+void hh_multiple_bullets(vec2 player, vec_cor cam_pos, hh_entity* bullets){
+
+ hh_shoot_bullet(player,cam_pos, &bullets[current_bullet]);
+ for(int i=0; i < bullets_size;i++){
+ if(!bullets[i].is_grounded){
+ hh_update_bullet(&bullets[i] , cam_pos);
+ hh_update_sprite(hh_ppu_bullet_fg_offset_idx + i, bullets[i], cam_pos);
+ }
+
+ }
}
diff --git a/src/engine/bullet.h b/src/engine/bullet.h
index 5f07b2e..4084327 100644
--- a/src/engine/bullet.h
+++ b/src/engine/bullet.h
@@ -1,11 +1,16 @@
#pragma once
#include "player_controller.h"
#include "engine/sprite_controller.h"
+#include "types.h"
#include "input.h"
+#define hh_ppu_bullet_fg_offset_idx 10
+hh_entity* hh_init_bullets(int size);
-void hh_shoot_bullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
+static void hh_shoot_bullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
-void hh_update_bullet(hh_entity* , vec_cor );
+static void hh_update_bullet(hh_entity* , vec_cor );
-void hh_draw_bullet(hh_entity);
+static void hh_draw_bullet(hh_entity, int);
+
+void hh_multiple_bullets(vec2 player, vec_cor cam_pos, hh_entity* bullets);
diff --git a/src/engine/entity.c b/src/engine/entity.c
index 7b780cf..43a1b73 100644
--- a/src/engine/entity.c
+++ b/src/engine/entity.c
@@ -84,6 +84,41 @@ hh_entity hh_background_collision (hh_entity temp_old_entity,hh_entity temp_new_
return temp_old_entity;
}
+bool hh_background_collision_bulllet (hh_entity temp_old_entity){
+
+ // temp_new_entity.is_grounded = false;
+
+// solves x collision
+ if (temp_old_entity.vel.x <= 0) {
+ if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) ||
+ hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
+ return true;
+ }
+ } else {
+ if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + 0})) ||
+ hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + temp_old_entity.size.x, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
+ return true;
+ }
+ }
+
+ //solves y collision
+ if (temp_old_entity.vel.y <= 0) {
+ if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) ||
+ hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + (temp_old_entity.size.x-1), .y=temp_old_entity.pos.y + 0}))) {
+ return true;
+ }
+ } else {
+ if (hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + 0, .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)})) ||
+ hh_colidable(hh_world_to_tile((vec2){.x=temp_old_entity.pos.x + (temp_old_entity.size.x-1), .y=temp_old_entity.pos.y + (temp_old_entity.size.y-1)}))) {
+ return true;
+ }
+ }
+ // temp_old_entity = temp_new_entity;
+ // temp_old_entity.is_grounded = temp_new_entity.is_grounded;
+ // temp_old_entity.pos = temp_new_entity.pos;
+ // temp_old_entity.vel = temp_new_entity.vel;
+ return false;
+}
hh_entity hh_enemy_collision(hh_entity temp_player, hh_entity temp_enemy){
bool collide = hh_distance_circles( temp_player.pos, temp_enemy.pos, temp_player.radius, temp_enemy.radius);
diff --git a/src/engine/entity.h b/src/engine/entity.h
index b681bd8..9a1d51e 100644
--- a/src/engine/entity.h
+++ b/src/engine/entity.h
@@ -46,5 +46,8 @@ bool hh_distance_circles(vec2 object_1, vec2 object_2, int radius_1, int radius_
// TODO: comments on functions below
void hh_jump_entity(hh_entity* );
void hh_gravity_entity(hh_entity* );
+
void hh_hit_entity(hh_entity* object_1, int8_t* hit_timer, int8_t* direction);
+
+bool hh_background_collision_bulllet (hh_entity temp_old_entity);
diff --git a/src/engine/level_const.c b/src/engine/level_const.c
index 3847ad8..896d0e0 100644
--- a/src/engine/level_const.c
+++ b/src/engine/level_const.c
@@ -41,6 +41,8 @@ hh_g_all_levels hh_init_game_levels(){
levels.level[0].place = hh_game_level1;
levels.level[1].place = hh_game_level2;
+ // free(hh_game_level1);
+ // free(hh_game_level2);
return levels;
}
diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c
index 096243b..ee0dfc5 100644
--- a/src/engine/player_controller.c
+++ b/src/engine/player_controller.c
@@ -8,25 +8,7 @@
#include "engine/animator.h"
#include "engine/bullet.h"
void hh_player_actions(hh_entity* player, vec_cor cam_pos){
- static hh_entity bullet={
-// .hp = 4,
- .speed = 6,
- .is_grounded = true,
- .is_hit = false,
- .radius = 8,
- .pos = (vec2){-16,-16},
- .vel = (vec2){0,0},
- .render = {
- .frame0 = 84,
- .palette = 3,
- .fam = (hh_s_ppu_loc_fam_entry){
- .horizontal_flip = false,
- .vertical_flip = false,
- .palette_index = 7,
- .tilemap_index = 84,
- }
- }
- };
+
static hh_entity player_new = {0};
player_new = *player;
@@ -54,7 +36,6 @@ void hh_player_actions(hh_entity* player, vec_cor cam_pos){
*player = hh_background_collision ( *player, player_new);
- hh_shoot_bullet(player->pos, cam_pos ,&bullet);
uint16_t idx = 16;
idx = hh_update_sprite(idx, player, cam_pos);
// idx = hh_update_sprite(idx, &enemy, cam_pos);
diff --git a/src/game_loop/gameplay.c b/src/game_loop/gameplay.c
index bc6c98d..521ab85 100644
--- a/src/game_loop/gameplay.c
+++ b/src/game_loop/gameplay.c
@@ -5,6 +5,7 @@
void hh_gameplay(hh_g_all_levels game, hh_e_game_state* hh_game_state){
static hh_e_gameplay gameplay = hh_e_setup_screen;
+ static hh_entity* bullets;
static hh_entity player1={
.hp = 4,
.speed = 6,
@@ -50,6 +51,7 @@ void hh_gameplay(hh_g_all_levels game, hh_e_game_state* hh_game_state){
switch (gameplay)
{
case hh_e_setup_screen:
+ bullets = hh_init_bullets(10);
hh_setup_screen(game.level[game.current_level]);
gameplay = hh_e_play_level;
break;
@@ -57,8 +59,8 @@ void hh_gameplay(hh_g_all_levels game, hh_e_game_state* hh_game_state){
// TODO: here come all the different functions for the gameplay
vec_cor cam_pos;//value in tiles
cam_pos = hh_draw_screen(player1.pos);
- hh_player_actions(&player1,cam_pos);
-
+ hh_player_actions(&player1 ,cam_pos);
+ hh_multiple_bullets(player1.pos, cam_pos,bullets);
// enemy's
diff --git a/src/game_loop/gameplay.h b/src/game_loop/gameplay.h
index d309e78..fb5637c 100644
--- a/src/game_loop/gameplay.h
+++ b/src/game_loop/gameplay.h
@@ -1,5 +1,6 @@
#pragma once
#include "engine/draw_screen.h"
+#include "engine/bullet.h"
#include "engine/player_controller.h"
#include "engine/sprite_controller.h"
#include "game_loop/starting_screen.h"