aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/bullet.c88
-rw-r--r--src/engine/bullet.h13
-rw-r--r--src/engine/draw_screen.c4
-rw-r--r--src/engine/draw_screen.h14
-rw-r--r--src/engine/enemy.c31
-rw-r--r--src/engine/enemy.h18
-rw-r--r--src/engine/entity.c107
-rw-r--r--src/engine/entity.h15
-rw-r--r--src/engine/level_const.c2
-rw-r--r--src/engine/level_const.h1
-rw-r--r--src/engine/player_controller.c187
-rw-r--r--src/engine/player_controller.h2
12 files changed, 272 insertions, 210 deletions
diff --git a/src/engine/bullet.c b/src/engine/bullet.c
index e6ca6df..55b20cc 100644
--- a/src/engine/bullet.c
+++ b/src/engine/bullet.c
@@ -1,35 +1,81 @@
#include "bullet.h"
+int bullets_size=0;
+static int current_bullet=0;
-
-
-// TODO: use hh_entity as bullet struct
-void hh_shoot_bullet(vec2 player, vec_cor cam_pos, hh_entity* bullet){
+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_shoot_bullet(vec2 player, 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;
+ }
+
+}
+
+void hh_update_bullet(hh_entity* bullet){
+ if(hh_background_collision_bulllet(*bullet)){
+ hh_bullet_death(bullet);
+
+ // printf("x %d y %d\n",(bullet->pos.x-cam_pos.x),(bullet->pos.y-cam_pos.y));
}
else{
- if(!bullet->is_grounded){
- hh_update_bullet(bullet , cam_pos);
- hh_draw_bullet(*bullet);
- }
+ bullet->pos.x += bullet->speed;
}
-
}
-void hh_update_bullet(hh_entity* bullet, vec_cor cam_pos){
- bullet->pos.x += 1;
-
- // 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);
+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_draw_bullet(hh_entity bullet){
- hh_s_ppu_loc_fam_entry temp = bullet.render.fam;
- hh_ppu_update_foreground(10,temp);
+
+void hh_bullet_death(hh_entity* bullet){
+ bullet->is_grounded=true;
+ bullet->pos.x= -16;
+ bullet->pos.y= -16;
}
diff --git a/src/engine/bullet.h b/src/engine/bullet.h
index 5f07b2e..4133032 100644
--- a/src/engine/bullet.h
+++ b/src/engine/bullet.h
@@ -1,11 +1,18 @@
#pragma once
#include "player_controller.h"
#include "engine/sprite_controller.h"
+#include "types.h"
#include "input.h"
+hh_entity* hh_init_bullets(int size);
-void hh_shoot_bullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
+/** @brief checks if player hit button and sets the activate bullet*/
+void hh_shoot_bullet(vec2 playerPos, hh_entity*);
-void hh_update_bullet(hh_entity* , vec_cor );
+/** @brief updates single bullet position*/
+void hh_update_bullet(hh_entity* );
-void hh_draw_bullet(hh_entity);
+/** @brief calculates all the bullets positions and which to shoot */
+void hh_multiple_bullets(vec2 player, hh_entity* bullets);
+
+void hh_bullet_death(hh_entity* bullet);
diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c
index 823c284..74b4ff2 100644
--- a/src/engine/draw_screen.c
+++ b/src/engine/draw_screen.c
@@ -10,7 +10,7 @@ uint8_t hh_world_to_tile(vec2 pos){
return tile;
}
-void hh_update_screen(vec2 view, vec2 player){
+void hh_update_screen(vec2 view){
int current_tile_y = view.y / HH_PPU_SPRITE_HEIGHT;
int current_tile_x = view.x / HH_PPU_SPRITE_WIDTH;
int offset_py = view.y - (offsetY / 40 * HH_PPU_SPRITE_HEIGHT);
@@ -86,7 +86,7 @@ vec_cor hh_draw_screen(vec2 player) {
viewport.x = CLAMP(viewport.x, 0, level.size.x * HH_PPU_SPRITE_WIDTH - HH_PPU_SCREEN_WIDTH);
viewport.y = CLAMP(viewport.y, 0, level.size.y * HH_PPU_SPRITE_HEIGHT - HH_PPU_SCREEN_HEIGHT);
- hh_update_screen((vec2){.y=viewport.y,.x=viewport.x},player);
+ hh_update_screen((vec2){.y=viewport.y,.x=viewport.x});
if (viewport.x == previousViewport.x && viewport.y == previousViewport.y){}
else{
hh_ppu_update_aux((hh_s_ppu_loc_aux){
diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h
index 9130842..4829bc6 100644
--- a/src/engine/draw_screen.h
+++ b/src/engine/draw_screen.h
@@ -11,24 +11,22 @@
#include <stdlib.h>
#include "engine/camera.h"
-#define hh_max_x_size 40
-#define hh_max_y_size 30
-
-/** @brief return a single tile from world binary */
+/** @brief return a single tile from world */
uint8_t hh_world_to_tile(vec2 pos);
-/** @brief shift to level if viewport changed position */
+/** @brief main function for the screen. shift through the map and update the camera based on the player */
vec_cor hh_draw_screen(vec2 player);
-/** @brief send data to BAM memory from binary level */
+/** @brief sets the start of a level */
void hh_setup_screen(hh_level_entity currentlevel);
-/** @brief updates screen based on view and maybe player position if it needs to turn back*/
-void hh_update_screen(vec2 view, vec2 );
+/** @brief updates screen if view is at the beginning or end */
+void hh_update_screen(vec2 view);
/** @brief send black screen to background memory */
void hh_clear_screen();
/** @brief clears all sprite data */
void hh_clear_sprite();
+
/** @brief send data to BAM memory from binary from shop */
void hh_setup_Shop();
diff --git a/src/engine/enemy.c b/src/engine/enemy.c
new file mode 100644
index 0000000..186cfa0
--- /dev/null
+++ b/src/engine/enemy.c
@@ -0,0 +1,31 @@
+#include "engine/enemy.h"
+
+void hh_update_enemy(hh_entity* enemy, vec_cor cam_pos){
+ //Bjorn functions
+
+
+}
+
+void hh_multiple_enemies( vec_cor cam_pos, hh_entity* enemies, int total_enemies){
+ for(int i=0; i < total_enemies;i++){
+ hh_update_enemy(&enemies[i] , cam_pos);
+ }
+
+}
+
+void hh_enemy_death_check(hh_entity* enemy){
+ if(enemy->hp == 0){
+ enemy->is_hit=false;
+ enemy->pos = (vec2){-16, -16};
+ }
+ else{
+ enemy->hp--;
+ }
+}
+void hh_solve_hitted_enemies(hh_entity* enemies, int total_enemies){
+ for(int i = 0; i < total_enemies; i++){
+ if(enemies[i].is_hit){
+ hh_enemy_death_check(&enemies[i]);
+ }
+ }
+}
diff --git a/src/engine/enemy.h b/src/engine/enemy.h
new file mode 100644
index 0000000..80498f5
--- /dev/null
+++ b/src/engine/enemy.h
@@ -0,0 +1,18 @@
+#pragma once
+#include "player_controller.h"
+#include "engine/sprite_controller.h"
+#include "types.h"
+#include "input.h"
+
+/** @brief updates a single enemy locations TODO: Bjorn sets functions in here*/
+void hh_update_enemy(hh_entity* , vec_cor );
+
+/** @brief calculates all the given enemies positions*/
+void hh_multiple_enemies( vec_cor cam_pos, hh_entity* enemies, int total_enemies);
+
+/** @brief checks if the enemy has zero hp else it takes hp from the enemy */
+void hh_enemy_death_check(hh_entity* enemy);
+
+/** @brieg all the given enemies get controlled if there hit and than calculates the dmg */
+void hh_solve_hitted_enemies(hh_entity* enemies, int total_enemies);
+
diff --git a/src/engine/entity.c b/src/engine/entity.c
index 58b62c9..1cb88be 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);
@@ -127,3 +162,75 @@ bool hh_distance_circles (vec2 object_1, vec2 object_2, int radius_1, int radius
return false;
}
}
+
+void hh_jump_entity(hh_entity* object_1){
+ if (object_1->is_grounded == true) {//JUMP
+ object_1->vel.y = -10;
+ object_1->is_grounded = false;
+ }
+}
+void hh_gravity_entity(hh_entity* object_1){
+ if (object_1->is_grounded == false && object_1->vel.y < 6) {
+ object_1->vel.y += 1; //gravity
+ }
+}
+void hh_hit_entity(hh_entity* object_1, int8_t* hit_timer, int8_t* direction){
+ if(object_1->is_hit == true){
+ hit_timer = 9;
+ object_1->is_hit = false;
+ }
+ if(hit_timer > -10){
+ hit_timer--;
+ }
+
+ if(hit_timer <= 0){
+ if(direction != 0){
+ if(object_1->vel.x > -1 * object_1->speed && object_1->vel.x < object_1->speed) {
+ object_1->vel.x = object_1->vel.x + direction;
+ } else {
+ if (object_1->vel.x > 0) {
+ object_1->vel.x--;
+ } else if(object_1->vel.x < 0) {
+ object_1->vel.x++;
+ }
+ }
+ } else {
+ if (object_1->vel.x > 0) {
+ object_1->vel.x--;
+ } else if(object_1->vel.x < 0) {
+ object_1->vel.x++;
+ }
+ }
+
+ } else {
+ if (object_1->vel.x > 0) {
+ object_1->vel.x--;
+ } else if(object_1->vel.x < 0) {
+ object_1->vel.x++;
+ }
+ object_1->vel.y++;
+ }
+
+}
+
+void hh_check_all_collisions(hh_entity* player, hh_entity* enemies, int total_enemies, hh_entity* bullets, int total_bullets, vec_cor cam_pos){
+ for(int enemy = 0; enemy < total_enemies; enemy++){
+ *player = hh_enemy_collision(*player, enemies[enemy]);
+ enemies[enemy].is_hit=false;
+ }
+
+ for(int i = 0; i < total_bullets; i++){
+ if(!bullets[i].is_grounded){
+ for (int enemy = 0; enemy < total_enemies; enemy++){
+
+ if(hh_distance_circles(bullets[i].pos,enemies[enemy].pos,bullets[i].radius,enemies[enemy].radius)){
+ enemies[enemy].is_hit=true;
+ hh_bullet_death(&bullets[i]);
+ }
+ }
+ }
+ }
+
+}
+
+
diff --git a/src/engine/entity.h b/src/engine/entity.h
index 68b450d..fde9341 100644
--- a/src/engine/entity.h
+++ b/src/engine/entity.h
@@ -7,6 +7,9 @@
#include "ppu/types.h"
#include "engine/maths.h"
#include "engine/types.h"
+#include "engine/animator.h"
+#include "engine/sprite_controller.h"
+
// #include "engine/animator.h"
// TODO: make a sprite update function (and required data structs?)
@@ -43,4 +46,14 @@ hh_entity hh_enemy_collision(hh_entity temp_player, hh_entity temp_enemy);
/// @return true if objects collids
bool hh_distance_circles(vec2 object_1, vec2 object_2, int radius_1, int radius_2);
-
+/** @brief jumps a entity. TODO: input jumping how high and no magic number*/
+void hh_jump_entity(hh_entity* );
+/** @brief looks if the player is grounded and if not it sets the position down */
+void hh_gravity_entity(hh_entity* );
+/** @brief if object is hit bounches in specific direction and moves the object */
+void hh_hit_entity(hh_entity* object_1, int8_t* hit_timer, int8_t* direction);
+
+/** @brief specific bullet background collisions detection */
+bool hh_background_collision_bulllet (hh_entity temp_old_entity);
+/** @brief checks any entity has a hit on each othe*/
+void hh_check_all_collisions(hh_entity* player, hh_entity* enemies, int total_enemies, hh_entity* bullets, int total_bullets, vec_cor cam_pos);
diff --git a/src/engine/level_const.c b/src/engine/level_const.c
index 58b2514..642a474 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/level_const.h b/src/engine/level_const.h
index b86ae7b..6275b72 100644
--- a/src/engine/level_const.h
+++ b/src/engine/level_const.h
@@ -28,4 +28,5 @@ typedef struct {
}hh_g_all_levels;
+/** @brief init all the levels*/
hh_g_all_levels hh_init_game_levels();
diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c
index 807037a..6400b02 100644
--- a/src/engine/player_controller.c
+++ b/src/engine/player_controller.c
@@ -9,195 +9,34 @@
#include "engine/animator.h"
#include "engine/bullet.h"
-void hh_player_actions() {
- static hh_entity player={
- .hp = 4,
- .speed = 6,
- .is_grounded = false,
- .is_hit = false,
- .radius = 16,
- .pos = (vec2){128+16,32},
- .vel = (vec2){0,0},
- .size = (vec2){16,32},
- .render = {
- .frame0 = HH_TM_GOZER_OFFSET,
- .palette = 3,
- .fam = (hh_s_ppu_loc_fam_entry){
- .horizontal_flip = false,
- .vertical_flip = false,
- .palette_index = 3,
- .tilemap_index = HH_TM_GOZER_OFFSET,
- }
- }
- }, player_new = {0};
+void hh_player_actions(hh_entity* player){
- 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 = HH_TM_BULLET_OFFSET,
- .palette = 3,
- .fam = (hh_s_ppu_loc_fam_entry){
- .horizontal_flip = false,
- .vertical_flip = false,
- .palette_index = 3,
- .tilemap_index = HH_TM_BULLET_OFFSET,
- }
- }
- };
+ static hh_entity player_new = {0};
+ player_new = *player;
- static hh_entity enemy={
- .hp = 4,
- .speed = 6,
- .is_grounded = false,
- .is_hit = false,
- .radius = 8,
- .pos = (vec2){128,48},
- .vel = (vec2){0,0},
- .size = (vec2){16,16},
- .render = {
- .frame0 = 20,
- .palette = 7,
- .ppu_foreground_index = 16,
- .fam = (hh_s_ppu_loc_fam_entry){
- .horizontal_flip = false,
- .vertical_flip = false,
- .palette_index = 7,
- .tilemap_index = 1,
- }
- }
- };
- player_new = player;
- // hh_input_read();
- static uint8_t hit = 0;
int8_t hit_timer = 0;
int8_t direction_x = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right);
int8_t direction_y = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down);
- if(player.is_hit == true){
- hit_timer = 9;
- player.is_hit = false;
- }
- if(hit_timer > -10){
- hit_timer--;
- }
-
- if(hit_timer <= 0){
- if(direction_x != 0){
- if(player.vel.x > -1 * player.speed && player.vel.x < player.speed) {
- player.vel.x = player.vel.x + direction_x;
- } else {
- if (player.vel.x > 0) {
- player.vel.x--;
- } else if(player.vel.x < 0) {
- player.vel.x++;
- }
- }
- } else {
- if (player.vel.x > 0) {
- player.vel.x--;
- } else if(player.vel.x < 0) {
- player.vel.x++;
- }
- }
-
- /* // movement Y (w-s) disable gravity to use this
- if(direction_y != 0){
- if(player.vel.y > -4 && player.vel.y < 4 ) {
- player.vel.y = player.vel.y + direction_y;
- }
- } else {
- if (player.vel.y > 0) {
- player.vel.y--;
- } else if(player.vel.y < 0) {
- player.vel.y++;
- }
- }
-
- */
- } else {
- if (player.vel.x > 0) {
- player.vel.x--;
- } else if(player.vel.x < 0) {
- player.vel.x++;
- }
- player.vel.y++;
+ hh_hit_entity(player,hit_timer,direction_x);
+ hh_gravity_entity(player);
+ if(g_hh_controller_p1.button_primary){
+ hh_jump_entity(player);
}
-
- if (g_hh_controller_p1.button_primary && player.is_grounded == true) {//JUMP
- player.vel.y = -10;
- player.is_grounded = false;
- } else if (player.vel.y < 6){
- player.vel.y += 1; //gravity
- }
-
-/*
- player.vel = (vec2){.x = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right),
- .y = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down) };
player_new.vel = (vec2){
- .x = player.vel.x,
- .y = player.vel.y,
+ .x = player->vel.x,
+ .y = player->vel.y,
};
-*/
-
- player_new.vel = (vec2){
- .x = player.vel.x,
- .y = player.vel.y,
- };
-
- player_new = hh_enemy_collision(player, enemy);
-
+
player_new.pos = (vec2){
- .x = player.pos.x + player_new.vel.x,
- .y = player.pos.y + player_new.vel.y,
+ .x = player->pos.x + player_new.vel.x,
+ .y = player->pos.y + player_new.vel.y,
};
- player = hh_background_collision( player, player_new);
-
- vec_cor cam_pos;//value in tiles
- cam_pos = hh_draw_screen(player.pos);
- hh_shoot_bullet(player.pos, cam_pos ,&bullet);
- uint16_t idx = 16;
- // hh_clear_sprite();
- hh_update_sprite(&idx, &enemy, cam_pos);
- hh_update_sprite(&idx, &player, cam_pos);
-
- idx =16;
-
- // TODO: make this a function call
- // update player sprite on ppu
- // player.render.fam.position_x = (player.pos.x-cam_pos.x);
- // player.render.fam.position_y = (player.pos.y-cam_pos.y);
-
- // enemy.render.fam.position_x = (enemy.pos.x-cam_pos.x);
- // enemy.render.fam.position_y = (enemy.pos.y-cam_pos.y);
-
-
- // TODO: make this loop a function call
- // for (int i = 0; i < 4; i++)
- // {
- // hh_s_ppu_loc_fam_entry temp = player.render.fam;
- // temp.position_x = player.render.fam.position_x+(!(player.vel.x>0)?-1:1)*(i%2?8:-8);
- // temp.position_y = player.render.fam.position_y+(i>1?0:-16);
- // temp.tilemap_index = player.render.fam.tilemap_index + i;
- // temp.horizontal_flip = !(player.vel.x>0);
- // hh_ppu_update_foreground(i,temp);
-
- // // hh_s_ppu_loc_fam_entry temp = {
- // // .position_x = player.render.fam.position_x+(!(player.vel.x>0)?-1:1)*(i%2?8:-8)
- // // };
-
- // }
-
- // hh_ppu_update_foreground(4, enemy.render.fam);
+ *player = hh_background_collision ( *player, player_new);
}
diff --git a/src/engine/player_controller.h b/src/engine/player_controller.h
index 400c18e..23749e5 100644
--- a/src/engine/player_controller.h
+++ b/src/engine/player_controller.h
@@ -4,4 +4,4 @@
#include "engine/entity.h"
// inputs
-void hh_player_actions();
+void hh_player_actions(hh_entity* player);