From 5c4bf7d451135cd3af3e51976251f01cbb6e6573 Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 13 Mar 2023 09:57:34 +0100 Subject: camera, movement, collisions --- src/engine/player_controller.c | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/engine/player_controller.c (limited to 'src/engine/player_controller.c') diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c new file mode 100644 index 0000000..27e522e --- /dev/null +++ b/src/engine/player_controller.c @@ -0,0 +1 @@ +#include "engine/player_controller.h" -- cgit v1.2.3 From 30eca8d3bd03c1efe52f2432c4b6b0c842fffa2d Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Mon, 13 Mar 2023 17:10:11 +0100 Subject: demo.c clean up --- src/demo.c | 197 +---------------------------------------- src/engine/entity.c | 12 +-- src/engine/entity.h | 33 ++++++- src/engine/player_controller.c | 166 ++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 201 deletions(-) (limited to 'src/engine/player_controller.c') diff --git a/src/demo.c b/src/demo.c index 58cb461..bb0aecf 100644 --- a/src/demo.c +++ b/src/demo.c @@ -9,11 +9,10 @@ #include "engine/camera.h" #include "engine/entity.h" #include "engine/draw_screen.h" +#include "engine/player_controller.h" #include "engine/sprite_controller.h" -#define HH_DEMO_BALL_COUNT 1 -hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; hh_s_entity_player g_hh_player_1 = { .pos_x = 31000, // 0b0000 0001 0011 0110 @@ -47,207 +46,17 @@ typedef struct { hh_entity hh_g_player, hh_g_player_new; void hh_demo_setup() { -#if 0 - // load sprites - // hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); - // hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); - hh_ppu_update_sprite(1, HH_SQUARE); - // background pattern - hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t){0x4, 0x4, 0x4}); - for (unsigned i = 0; i < HH_PPU_BG_CANVAS_TILES_H * HH_PPU_BG_CANVAS_TILES_V; i++) { - hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry){ - .horizontal_flip = false, - .vertical_flip = false, - .palette_index = 0, - .tilemap_index = 1, - }); - } - - // cool colors - // hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); - // hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); - // hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); - // hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); - // hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); - - for (int i = 0; i < 8; i++) - { - hh_ppu_update_color(1,i,HH_SLIME[i]); - } - - - // balls -#else - hh_g_player = (hh_entity){ - .pos = {32,32}, - .vec = {0,0}, - .hp = 1, - .speed = 1, - .is_grounded = false - }; - - for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { - g_hh_demo_balls[i].horizontal_flip = false; - g_hh_demo_balls[i].vertical_flip = false; - g_hh_demo_balls[i].palette_index = 7; - g_hh_demo_balls[i].tilemap_index = 20; - } hh_setup_palettes(); hh_setup_screen(); -#endif + } void hh_demo_loop(unsigned long frame) { // hh_player_movement(); - // adjust map size - // g_hh_pos_x = g_hh_player_1.pos_x / 100; - // g_hh_pos_y = g_hh_player_1.pos_y / 100; - - // input testing (no hitbox stuff) - // g_hh_pos_x += (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - // g_hh_pos_y += (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U - - - - hh_g_player.vec = (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) }; - // const int8_t maa = 3; - // const int8_t mbb = -3; - // if (g_hh_controller_p1.dpad_up) - - // if (g_hh_controller_p1.dpad_down) - - // if (g_hh_controller_p1.dpad_left) { - // hh_g_player.vec.x += mbb; - // // g_hh_demo_balls[0].horizontal_flip = true; - // } - // if (g_hh_controller_p1.dpad_right) { - // hh_g_player.vec.x += maa; - // // g_hh_demo_balls[0].horizontal_flip = true; - // } - if (g_hh_controller_p1.button_primary /*&& hh_g_player.is_grounded*/) //JUMP - hh_g_player.vec.y += -6; - // // if (g_hh_controller_p1.button_secondary) - - - hh_g_player.vec.y += 1; //gravity - - - //END OF VECTOR CHANGES - // hh_g_player.vec.y = CLAMP(hh_g_player.vec.y,-32,32); - // hh_g_player.vec.x = CLAMP(hh_g_player.vec.x,-32,32); - - hh_g_player_new.pos = (vec2){ - .x = hh_g_player.pos.x + hh_g_player.vec.x, - .y = hh_g_player.pos.y + hh_g_player.vec.y, - }; - - - - - // const uint8_t empty = 0; - // hh_s_tiles tiles[9]; - // const vec2 tile_offset[9] = { - // (vec2){-16,-16},(vec2){0,-16},(vec2){+16,-16}, - // (vec2){-16,0}, (vec2){0,0}, (vec2){+16,0}, - // (vec2){-16,+16},(vec2){0,+16},(vec2){+16,+16}, - // }; - - // for (int i = 0; i < 9; i++) { - // vec2 temp_pos = vec_add(hh_g_player.pos, tile_offset[i]); - // temp_pos =(vec2){ - // .x = temp_pos.x, - // .y = temp_pos.y, - // }; - // hh_s_tiles tile = { - // .pos = temp_pos, - // .idx = hh_world_to_tile(temp_pos) - // }; - - // if(hh_colidable(tile.idx)) { - // tiles[i]=tile; - // // printf(" collidable near!"); - // } else { - // tiles[i].idx = 0; - // } - // } - /* - 012 - 345 - 678 - */ - - // for (int i = 0; i < 9; i++) - // { - // if (tiles[i].idx != 0){ - // hh_solve_collision(tiles[i].pos, &hh_g_player); - // } - // } - - hh_g_player_new.is_grounded = false; - - // solves x collision - if (hh_g_player.vec.x <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player.pos.y + 15}))) { - hh_g_player_new.pos.x = (hh_g_player_new.pos.x & ~15) + 16, - hh_g_player_new.vec.x = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player.pos.y + 15}))) { - hh_g_player_new.pos.x = hh_g_player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY - hh_g_player_new.vec.x = 0; - } - } - - //solves y collision - if (hh_g_player.vec.y <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 15}))) { - hh_g_player_new.pos.y = (hh_g_player_new.pos.y & ~15) + 16, - hh_g_player_new.vec.y = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 0, .y=hh_g_player_new.pos.y + 16})) || - hh_colidable(hh_world_to_tile((vec2){.x=hh_g_player_new.pos.x + 16, .y=hh_g_player_new.pos.y + 15}))) { - hh_g_player_new.pos.y = hh_g_player_new.pos.y & ~15, - hh_g_player_new.vec.y = 0; - hh_g_player_new.is_grounded = true; - } - } - - hh_g_player = hh_g_player_new; - - - - vec_cor cam_pos;//value in tiles - // cam_pos = (vec2){0,0}; - cam_pos = hh_update_camera(hh_g_player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) - printf("%i, %i:%i, %i\n",hh_g_player.pos.x,hh_g_player.pos.y,cam_pos.x,cam_pos.y); - hh_draw_screen(cam_pos); - // update player sprite on ppu - g_hh_demo_balls[0].position_x = (hh_g_player.pos.x-cam_pos.x); - g_hh_demo_balls[0].position_y = hh_g_player.pos.y-cam_pos.y; - hh_ppu_update_foreground(0, g_hh_demo_balls[0]); - - // for (int i = 0; i < HH_DEMO_BALL_COUNT; i++){ - // g_hh_demo_balls[i].position_x = hh_g_player.pos.x +16*i; - // g_hh_demo_balls[i].position_y = hh_g_player.pos.y; - // hh_ppu_update_foreground(i, g_hh_demo_balls[i]); - - // } - + hh_player_actions(); - // set background pattern position - // hh_ppu_update_aux((hh_s_ppu_loc_aux){ - // .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, - // .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - // .fg_fetch = 0, - // .sysreset = 0, - // }); } // void sendData(uint8_t address, uint16_t data) { diff --git a/src/engine/entity.c b/src/engine/entity.c index 152cf1d..153e7e1 100644 --- a/src/engine/entity.c +++ b/src/engine/entity.c @@ -28,19 +28,19 @@ void hh_solve_collision(vec2 pos_environment, hh_entity* entity){ return; printf("BONK!/n"); - // if (entity->vec.y > 0){ + // if (entity->vel.y > 0){ // entity->pos.y = MAX(entity->pos.y,pos_environment.y); - // entity->vec.y = 0; + // entity->vel.y = 0; // } else { // entity->pos.y = MIN(entity->pos.y,pos_environment.y); - // entity->vec.y = 0; + // entity->vel.y = 0; // } - // if (entity->vec.x <= 0){ + // if (entity->vel.x <= 0){ // entity->pos.x = MIN(entity->pos.x,pos_environment.x-16); - // entity->vec.x = 0; + // entity->vel.x = 0; // } else { // entity->pos.x = MAX(entity->pos.x,pos_environment.x+16); - // entity->vec.x = 0; + // entity->vel.x = 0; // } } diff --git a/src/engine/entity.h b/src/engine/entity.h index dee4aed..f45dae2 100644 --- a/src/engine/entity.h +++ b/src/engine/entity.h @@ -3,16 +3,47 @@ #include #include +#include "ppu/types.h" + #include "engine/maths.h" +typedef uint8_t hh_idx_t; + +typedef enum { + fire, ice, poison +}hh_e_damage_t; + +typedef struct { + hh_s_ppu_loc_fam_entry fam; //screen + hh_idx_t frame0; + hh_idx_t palette; + +}hh_s_rendering; + +typedef struct { + int8_t hp; + int8_t dmg; + hh_e_damage_t dmg_type; + int8_t speed_x, speed_y; + +} hh_s_atributes; + + typedef struct { - vec2 pos, vec; + vec2 pos, vel, vec; bool is_grounded; int8_t hp; int8_t speed; + hh_s_rendering render; //armor/block? }hh_entity; +typedef struct { + hh_entity p; + hh_s_atributes atr; +}hh_s_player; + + /// @brief detect for collision enity and eviroment /// @param pos1 position of environment tile to be checked /// @param pos2 position entity diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index 27e522e..6735620 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -1 +1,167 @@ +#include "ppu/types.h" +#include "engine/camera.h" +#include "engine/draw_screen.h" +#include "engine/sprite_controller.h" #include "engine/player_controller.h" + +#include "demo.h" +#include + +#include "input.h" + +void hh_player_actions() { + static hh_entity player={ + .hp = 4, + .speed = 6, + .is_grounded = false, + .pos = (vec2){32,32}, + .vel = (vec2){0,0}, + .vec = (vec2){0,0}, + .render = { + .frame0 = 20, + .palette = 7, + .fam = (hh_s_ppu_loc_fam_entry){ + .horizontal_flip = false, + .vertical_flip = false, + .palette_index = 7, + .tilemap_index = 2, + } + } + }, player_new = {0}; + + // hh_input_read(); + 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) }; + // const int8_t maa = 3; + // const int8_t mbb = -3; + // if (g_hh_controller_p1.dpad_up) + // + // if (g_hh_controller_p1.dpad_down) + // + // if (g_hh_controller_p1.dpad_left) { + // player.vel.x += mbb; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + // if (g_hh_controller_p1.dpad_right) { + // player.vel.x += maa; + // // g_hh_demo_balls[0].horizontal_flip = true; + // } + // if (g_hh_controller_p1.button_primary /*&& player.is_grounded*/) //JUMP + // player.vel.y += -6; + // // // if (g_hh_controller_p1.button_secondary) + + // player.render.fam.palette_index = 7; + // player.render.fam.tilemap_index = 7; + + // printf("%x ",player.render.fam.palette_index); + + // player.vel.y += 1; //gravity + + + //END OF VECTOR CHANGES + // player.vel.y = CLAMP(player.vel.y,-32,32); + // player.vel.x = CLAMP(player.vel.x,-32,32); + + player_new.pos = (vec2){ + .x = player.pos.x + player.vel.x, + .y = player.pos.y + player.vel.y, + }; + + + + // const uint8_t empty = 0; + // hh_s_tiles tiles[9]; + // const vec2 tile_offset[9] = { + // (vec2){-16,-16},(vec2){0,-16},(vec2){+16,-16}, + // (vec2){-16,0}, (vec2){0,0}, (vec2){+16,0}, + // (vec2){-16,+16},(vec2){0,+16},(vec2){+16,+16}, + // }; + // for (int i = 0; i < 9; i++) { + // vec2 temp_pos = vec_add(player.pos, tile_offset[i]); + // temp_pos =(vec2){ + // .x = temp_pos.x, + // .y = temp_pos.y, + // }; + // hh_s_tiles tile = { + // .pos = temp_pos, + // .idx = hh_world_to_tile(temp_pos) + // }; + // if(hh_colidable(tile.idx)) { + // tiles[i]=tile; + // // printf(" collidable near!"); + // } else { + // tiles[i].idx = 0; + // } + // } + /* + 012 + 345 + 678 + */ + // for (int i = 0; i < 9; i++) + // { + // if (tiles[i].idx != 0){ + // hh_solve_collision(tiles[i].pos, &player); + // } + // } + + player_new.is_grounded = false; + + // solves x collision + if (player.vel.x <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 15}))) { + player_new.pos.x = (player_new.pos.x & ~15) + 16, + player_new.vel.x = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 15}))) { + player_new.pos.x = player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY + player_new.vel.x = 0; + } + } + + //solves y collision + if (player.vel.y <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 15}))) { + player_new.pos.y = (player_new.pos.y & ~15) + 16, + player_new.vel.y = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 16})) || + hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player_new.pos.y + 15}))) { + player_new.pos.y = player_new.pos.y & ~15, + player_new.vel.y = 0; + player_new.is_grounded = true; + } + } + + player = player_new; + + vec_cor cam_pos;//value in tiles + // cam_pos = (vec2){0,0}; + cam_pos = hh_update_camera(player.pos,(vec2){0,0},(vec2){.x=20*16,.y=30*16});//TODO: remove magic number(s) + // printf("%i, %i:%i, %i\n",player.pos.x,player.pos.y,cam_pos.x,cam_pos.y); + hh_draw_screen(cam_pos); + // 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); + + + player.render.fam.tilemap_index = 2;//TODO: these two lines should be redundant + player.render.fam.palette_index = 7; + hh_ppu_update_foreground(0, player.render.fam); + + + hh_s_ppu_loc_fam_entry sprite = { + .position_x = 16, + .position_y = 16, + .palette_index = 7, + .tilemap_index = 2, + }; + hh_ppu_update_foreground(1, sprite); + +} + -- cgit v1.2.3 From 5c999fb95681ffaff41e59f8f43c82b5cf148e42 Mon Sep 17 00:00:00 2001 From: UnavailableDev Date: Tue, 14 Mar 2023 17:26:17 +0100 Subject: camera y-axis bug fixed + clean up --- src/engine/camera.c | 5 +++-- src/engine/player_controller.c | 17 ----------------- 2 files changed, 3 insertions(+), 19 deletions(-) (limited to 'src/engine/player_controller.c') diff --git a/src/engine/camera.c b/src/engine/camera.c index e756bd4..2c3e517 100644 --- a/src/engine/camera.c +++ b/src/engine/camera.c @@ -6,10 +6,10 @@ vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){ //TODO: change floating point math to fix point math - //TODO: fix buggy y-axis ?? + //TODO: remove magic number at y camera offset // new = vec_cen2cor(new,(vec2){.x=max.x/2,.y=max.y/2}); - new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH/2),.y=(new.y+(HH_PPU_SPRITE_HEIGHT/2))*2},(vec2){.x=max.x/2,.y=max.y/2}); + new = vec_cen2cor((vec2){.x=new.x+(HH_PPU_SPRITE_WIDTH),.y=new.y+(HH_PPU_SPRITE_HEIGHT*8)},(vec2){.x=max.x/2,.y=max.y/2}); // new.x = new.x << HH_MATH_FIXED_POINT; // new.y = new.y << HH_MATH_FIXED_POINT; static vec_cor old; @@ -19,6 +19,7 @@ vec_cor hh_update_camera(vec_cen new, vec2 min, vec2 max){ // int16_t some = 0; // some = some <<= HH_MATH_FIXED_POINT-1; + // Camera smoothing new.x = (int)((float)new.x*0.1f + (float)old.x*0.9f); new.y = (int)((float)new.y*0.1f + (float)old.y*0.9f); diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index 6735620..22f6eb6 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -4,9 +4,6 @@ #include "engine/sprite_controller.h" #include "engine/player_controller.h" -#include "demo.h" -#include - #include "input.h" void hh_player_actions() { @@ -50,11 +47,6 @@ void hh_player_actions() { // player.vel.y += -6; // // // if (g_hh_controller_p1.button_secondary) - // player.render.fam.palette_index = 7; - // player.render.fam.tilemap_index = 7; - - // printf("%x ",player.render.fam.palette_index); - // player.vel.y += 1; //gravity @@ -154,14 +146,5 @@ void hh_player_actions() { player.render.fam.palette_index = 7; hh_ppu_update_foreground(0, player.render.fam); - - hh_s_ppu_loc_fam_entry sprite = { - .position_x = 16, - .position_y = 16, - .palette_index = 7, - .tilemap_index = 2, - }; - hh_ppu_update_foreground(1, sprite); - } -- cgit v1.2.3 From e79c36460f400991caa82598499c0ea7c37ebb90 Mon Sep 17 00:00:00 2001 From: Flenk008 Date: Wed, 15 Mar 2023 18:58:25 +0100 Subject: Player-Enemy collision Added player with enemy collision. Also changed input system to a vector system with friction and gravity. Jump also works. Enemy is 1 stationary object. --- src/demo.c | 175 +++++++---------------------------------- src/engine/entity.c | 81 +++++++++++++++++++ src/engine/entity.h | 25 ++++++ src/engine/player_controller.c | 155 +++++++++++++++++++++++++++--------- src/ppusim/input.c | 1 + 5 files changed, 253 insertions(+), 184 deletions(-) (limited to 'src/engine/player_controller.c') diff --git a/src/demo.c b/src/demo.c index baaf73d..d4d1bf7 100644 --- a/src/demo.c +++ b/src/demo.c @@ -1,26 +1,21 @@ #include #include "demo.h" -#include "entity.h" #include "input.h" +#include "entity.h" #include "ppu/ppu.h" -#define HH_DEMO_BALL_COUNT 1 -hh_s_ppu_loc_fam_entry g_hh_demo_balls[HH_DEMO_BALL_COUNT]; +#include "engine/maths.h" +#include "engine/camera.h" +#include "engine/entity.h" +#include "engine/draw_screen.h" +#include "engine/player_controller.h" +#include "engine/sprite_controller.h" -hh_s_entity_player g_hh_player_1 = { - .pos_x = 31000, // 0b0000 0001 0011 0110 - .pos_y = 21000, - .radius = 8, - .speed = 100, - .direction_x = 1, - .rotation = 8, - .in_air = false, -}; -void hh_player_movement(); -uint16_t g_hh_pos_x; // 0b0000 0001 0011 0110 + +uint16_t g_hh_pos_x = 1000; // 0b0000 0001 0011 0110 uint16_t g_hh_pos_y; uint8_t g_hh_left = 0; uint8_t g_hh_right = 0; @@ -29,67 +24,29 @@ uint8_t g_hh_down = 0; uint8_t g_hh_pos_x_bit[2]; uint8_t g_hh_pos_y_bit[2]; uint8_t g_hh_data_send[3]; -int g_hh_tile_x; +int g_hh_tile_size = 8; int g_hh_tile_y; -void hh_demo_setup() { - // load sprites - hh_ppu_update_sprite(0, HH_DBG_SPRITE_BALL); - hh_ppu_update_sprite(1, HH_DBG_SPRITE_CHECKERBOARD); - - // background pattern - hh_ppu_update_color(0, 1, (hh_ppu_rgb_color_t){0x4, 0x4, 0x4}); - for (unsigned i = 0; i < HH_PPU_BG_CANVAS_TILES_H * HH_PPU_BG_CANVAS_TILES_V; i++) { - hh_ppu_update_background(i, (hh_s_ppu_loc_bam_entry){ - .horizontal_flip = false, - .vertical_flip = false, - .palette_index = 0, - .tilemap_index = 1, - }); - } - - // cool colors - hh_ppu_update_color(1, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0xf}); - hh_ppu_update_color(2, 1, (hh_ppu_rgb_color_t){0xf, 0xf, 0xf}); - hh_ppu_update_color(3, 1, (hh_ppu_rgb_color_t){0xf, 0x0, 0x0}); - hh_ppu_update_color(4, 1, (hh_ppu_rgb_color_t){0x0, 0xf, 0xf}); - hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t){0x0, 0x0, 0xf}); - - // balls - for (unsigned i = 0; i < HH_DEMO_BALL_COUNT; i++) { - g_hh_demo_balls[i].horizontal_flip = false; - g_hh_demo_balls[i].vertical_flip = false; - g_hh_demo_balls[i].palette_index = i + 1; - g_hh_demo_balls[i].tilemap_index = 0; - } -} - -void hh_demo_loop(unsigned long frame) { - hh_player_movement(); - // input testing (no hitbox stuff) - // g_hh_player_1.pos_x += ((-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right)) * g_hh_player_1.speed; // -1 = L || 1 == R - // g_hh_player_1.pos_y += ((-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down)) * g_hh_player_1.speed; // -1 = D || 1 == U +typedef struct { + vec2 pos; + uint8_t idx; +}hh_s_tiles; - // adjust map size - g_hh_pos_x = g_hh_player_1.pos_x / 100; - g_hh_pos_y = g_hh_player_1.pos_y / 100; +hh_entity hh_g_player, hh_g_player_new; +void hh_demo_setup() { + hh_setup_palettes(); + hh_setup_screen(); +} - // update player sprite on ppu - g_hh_demo_balls[0].position_x = g_hh_pos_x; - g_hh_demo_balls[0].position_y = g_hh_pos_y; - hh_ppu_update_foreground(0, g_hh_demo_balls[0]); +void hh_demo_loop(unsigned long frame) { - // set background pattern position - hh_ppu_update_aux((hh_s_ppu_loc_aux){ - .bg_shift_x = (frame / 2) % HH_PPU_SPRITE_WIDTH, - .bg_shift_y = (frame / 8) % HH_PPU_SPRITE_HEIGHT, - .fg_fetch = 0, - .sysreset = 0, - }); + + hh_player_actions(); + } // void sendData(uint8_t address, uint16_t data) { @@ -103,84 +60,8 @@ void hh_demo_loop(unsigned long frame) { // HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // } -void hh_player_movement() { - int8_t directionX = (-1 * g_hh_controller_p1.dpad_left) + (1 * g_hh_controller_p1.dpad_right); // -1 = L || 1 == R - int8_t directionY = (-1 * g_hh_controller_p1.dpad_up) + (1 * g_hh_controller_p1.dpad_down); // -1 = D || 1 == U - - uint8_t i, j; - uint8_t rotation = 0; // 0-7 - - // rotation calc - for (i = -1; i < 2; i++) { - for (j = -1; j < 2; j++) { - if (directionX == i) { - if (directionY == j) { - if (i != 0 && j != 0) // dont update when player idle - { - g_hh_player_1.rotation = rotation; - } - } - } - rotation++; - } - } - // direction calc - if (directionX != 0) // update direction if player is not idle - { - g_hh_player_1.direction_x = directionX; - } - // collision map x-axis - - // tile calc including radius and direction for background coliision - - uint16_t tileColX; - uint16_t tileColY = (g_hh_player_1.pos_y / 100) / 16; - ; - - // remaining space between grid and exact - uint8_t modTileX; - uint8_t modTileY; - - if (g_hh_player_1.in_air == false && directionX != 0) { - if (directionX == 1) { - tileColX = ((g_hh_player_1.pos_x / 100) + g_hh_player_1.radius) / 16; - modTileX = (g_hh_player_1.pos_x + (100 * g_hh_player_1.radius)) % 1600; - } else if (directionX == -1) { - tileColX = ((g_hh_player_1.pos_x / 100) - g_hh_player_1.radius) / 16; - modTileX = (g_hh_player_1.pos_x - (100 * g_hh_player_1.radius)) % 1600; - } - - if (HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] != 1) { - g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set - } - - else if (HH_DEMO_HITBOX_TILEMAP[tileColY][tileColX + directionX] == 1) { - if (modTileX < g_hh_player_1.speed) { - g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * modTileX); // NEW x set - } else { - g_hh_player_1.pos_x = g_hh_player_1.pos_x + (directionX * g_hh_player_1.speed); // NEW x set - } - } - - } else // if in air different all borders have to be checked - { - } - - - if(directionY != 0) - { - // g_hh_player_1.pos_y = g_hh_player_1.pos_y + (directionY * g_hh_player_1.speed * 2); // NEW x set - } - // collision map floor (y-axis) (falling) - // if falling no jump press (implement) - /* - tileColY = (( g_hh_player_1.pos_y / 100) + g_hh_player_1.radius) / 16; //bottom of player box - modTileY = 1; - if(HH_DEMO_HITBOX_TILEMAP[tileColY+1][tileColX] != 1) //rework after jumping - { - g_hh_player_1.pos_y = g_hh_player_1.pos_y + 5 ;// NEW y set //makew var gravity - //playerStat = falling; //for later use of graphics/sound - } - */ - // else if(HH_DEMO_HITBOX_TILEMAP[]) -} + + + + + diff --git a/src/engine/entity.c b/src/engine/entity.c index 153e7e1..535759d 100644 --- a/src/engine/entity.c +++ b/src/engine/entity.c @@ -43,4 +43,85 @@ void hh_solve_collision(vec2 pos_environment, hh_entity* entity){ // entity->vel.x = 0; // } } +hh_entity hh_background_collision (hh_entity temp_old_entity,hh_entity temp_new_entity){ + temp_old_entity.is_grounded = false; + +// solves x collision + if (temp_old_entity.vel.x <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_old_entity.pos.y + 15}))) { + temp_new_entity.pos.x = (temp_new_entity.pos.x & ~15) + 16, + temp_new_entity.vel.x = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 16, .y=temp_old_entity.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 16, .y=temp_old_entity.pos.y + 15}))) { + temp_new_entity.pos.x = temp_new_entity.pos.x & ~15, // <-- magic comma, NOT TOUCHY + temp_new_entity.vel.x = 0; + } + } + + //solves y collision + if (temp_old_entity.vel.y <= 0) { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + 0})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 15, .y=temp_new_entity.pos.y + 0}))) { + temp_new_entity.pos.y = (temp_new_entity.pos.y & ~15) + 16, + temp_new_entity.vel.y = 0; + } + } else { + if (hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 0, .y=temp_new_entity.pos.y + 15})) || + hh_colidable(hh_world_to_tile((vec2){.x=temp_new_entity.pos.x + 15, .y=temp_new_entity.pos.y + 15}))) { + temp_new_entity.pos.y = temp_new_entity.pos.y & ~15, + temp_new_entity.vel.y = 0; + temp_old_entity.is_grounded = true; + } + } + temp_old_entity.pos = temp_new_entity.pos; + temp_old_entity.vel = temp_new_entity.vel; + return temp_old_entity; +} + +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); + + if (collide == true && temp_player.is_hit == false) + { + temp_player.is_hit = true; + //angle = atan2( tempEntity.pos_y - tempPlayer.pos_y, tempEntity.pos_x - tempPlayer.pos_x); + if(temp_player.pos.x <= temp_enemy.pos.x) //player left of enemy -- creates flinch movement L or R + { + // printf("BONK-left!/n"); + temp_player.vel.y = -5; + temp_player.vel.x = -8; + } else { + // printf("BONK-right!/n"); + temp_player.vel.y = -5; + temp_player.vel.x = 8; + } + // ghost mode / invulnerable or other things on hit + // temp_player.hp--; + + } else { + temp_player.is_hit = false; + + } + + +return temp_player; +} + + +bool hh_distance_circles (vec2 object_1, vec2 object_2, int radius_1, int radius_2){ + int a_squared = (object_1.x - object_2.x) * (object_1.x - object_2.x); + int b_squared = (object_1.y - object_2.y) * (object_1.y - object_2.y); + int c_squared = a_squared + b_squared; + int radius = ( radius_1 + radius_2) * ( radius_1 + radius_2 ); + + if( c_squared <= radius ){ + return true; + } else { + return false; + } +} diff --git a/src/engine/entity.h b/src/engine/entity.h index f45dae2..cad6ba4 100644 --- a/src/engine/entity.h +++ b/src/engine/entity.h @@ -2,6 +2,7 @@ #include #include +#include #include "ppu/types.h" @@ -32,6 +33,8 @@ typedef struct { typedef struct { vec2 pos, vel, vec; bool is_grounded; + bool is_hit; + uint8_t radius; int8_t hp; int8_t speed; hh_s_rendering render; @@ -55,3 +58,25 @@ bool hh_collision(vec2 pos1, vec2 pos2); /// @param entity position /// @return solved new entity position void hh_solve_collision(vec2 pos_environment, hh_entity* entity); + +/// @brief solve collision of entity with background tiles +/// @param temp_old_entity old data of entity +/// @param temp_new_entity new data of entity where it wants to go to +/// @return updated new entity where it actually can go to +hh_entity hh_background_collision (hh_entity temp_old_entity, hh_entity temp_new_entity); + +/// @brief solve collision of player with enemy +/// @param temp_player data of player +/// @param temp_enemy data of enemy +/// @return updated player with new stats if hitted with enemy +hh_entity hh_enemy_collision(hh_entity temp_player, hh_entity temp_enemy); + +/// @brief calculate if circles (entity) hit each other +/// @param object_1 position of first object (entity) +/// @param object_2 position of second object (entity) +/// @param radius_1 radius of first object (entity) +/// @param radius_2 radius of second object (entity) +/// @return true if objects collids +bool hh_distance_circles (vec2 object_1, vec2 object_2, int radius_1, int radius_2); + + diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index 22f6eb6..d351bee 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -11,6 +11,8 @@ void hh_player_actions() { .hp = 4, .speed = 6, .is_grounded = false, + .is_hit = false, + .radius = 8, .pos = (vec2){32,32}, .vel = (vec2){0,0}, .vec = (vec2){0,0}, @@ -26,9 +28,113 @@ void hh_player_actions() { } }, player_new = {0}; + + static hh_entity enemy={ + .hp = 4, + .speed = 6, + .is_grounded = false, + .is_hit = false, + .radius = 8, + .pos = (vec2){128,48}, + .vel = (vec2){0,0}, + .vec = (vec2){0,0}, + .render = { + .frame0 = 20, + .palette = 7, + .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++; + } + + + 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, + }; +*/ + + player_new.vel = (vec2){ + .x = player.vel.x, + .y = player.vel.y, + }; + + player_new = hh_enemy_collision(player, enemy); + + // const int8_t maa = 3; // const int8_t mbb = -3; // if (g_hh_controller_p1.dpad_up) @@ -55,8 +161,8 @@ void hh_player_actions() { // player.vel.x = CLAMP(player.vel.x,-32,32); player_new.pos = (vec2){ - .x = player.pos.x + player.vel.x, - .y = player.pos.y + player.vel.y, + .x = player.pos.x + player_new.vel.x, + .y = player.pos.y + player_new.vel.y, }; @@ -96,41 +202,10 @@ void hh_player_actions() { // hh_solve_collision(tiles[i].pos, &player); // } // } - - player_new.is_grounded = false; - - // solves x collision - if (player.vel.x <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player.pos.y + 15}))) { - player_new.pos.x = (player_new.pos.x & ~15) + 16, - player_new.vel.x = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player.pos.y + 15}))) { - player_new.pos.x = player_new.pos.x & ~15, // <-- magic comma, NOT TOUCHY - player_new.vel.x = 0; - } - } - - //solves y collision - if (player.vel.y <= 0) { - if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 0})) || - hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 15}))) { - player_new.pos.y = (player_new.pos.y & ~15) + 16, - player_new.vel.y = 0; - } - } else { - if (hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 0, .y=player_new.pos.y + 16})) || - hh_colidable(hh_world_to_tile((vec2){.x=player_new.pos.x + 16, .y=player_new.pos.y + 15}))) { - player_new.pos.y = player_new.pos.y & ~15, - player_new.vel.y = 0; - player_new.is_grounded = true; - } - } - - player = player_new; + + player = hh_background_collision ( player, player_new); + + //player = player_new; vec_cor cam_pos;//value in tiles // cam_pos = (vec2){0,0}; @@ -141,10 +216,16 @@ void hh_player_actions() { 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); player.render.fam.tilemap_index = 2;//TODO: these two lines should be redundant player.render.fam.palette_index = 7; hh_ppu_update_foreground(0, player.render.fam); + hh_ppu_update_foreground(1, enemy.render.fam); + } + + diff --git a/src/ppusim/input.c b/src/ppusim/input.c index 08bc382..5323fb1 100644 --- a/src/ppusim/input.c +++ b/src/ppusim/input.c @@ -12,4 +12,5 @@ void hh_input_read() { g_hh_controller_p1.dpad_down = kb[SDL_SCANCODE_S]; g_hh_controller_p1.dpad_left = kb[SDL_SCANCODE_A]; g_hh_controller_p1.dpad_right = kb[SDL_SCANCODE_D]; + g_hh_controller_p1.button_primary = kb[SDL_SCANCODE_SPACE]; } -- cgit v1.2.3 From 72295b9df9c0de9e585cce3f41437965eb0390cb Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 20 Mar 2023 19:11:41 +0100 Subject: demo version --- .vscode/settings.json | 4 +- src/GameLoop/shop.c | 30 ++++++++++++++ src/GameLoop/shop.h | 16 ++++++++ src/GameLoop/startingScreen.c | 32 +++++++++++++++ src/GameLoop/startingScreen.h | 14 +++++++ src/demo.c | 54 ++++++++++++++++++++++-- src/engine/bullet.c | 44 ++++++++++++++++++++ src/engine/bullet.h | 16 ++++++++ src/engine/draw_screen.c | 26 ++++++++++++ src/engine/draw_screen.h | 5 +++ src/engine/player_controller.c | 35 ++++++++++++---- src/engine/title_screen.c | 93 ++++++++++++++++++++++++++++++++++++++++++ src/engine/title_screen.h | 3 ++ src/makefile | 6 ++- src/ppusim/input.c | 1 + 15 files changed, 367 insertions(+), 12 deletions(-) create mode 100644 src/GameLoop/shop.c create mode 100644 src/GameLoop/shop.h create mode 100644 src/GameLoop/startingScreen.c create mode 100644 src/GameLoop/startingScreen.h create mode 100644 src/engine/bullet.c create mode 100644 src/engine/bullet.h create mode 100644 src/engine/title_screen.c create mode 100644 src/engine/title_screen.h (limited to 'src/engine/player_controller.c') diff --git a/.vscode/settings.json b/.vscode/settings.json index d027762..f4d72ed 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,5 +8,7 @@ } }, "cmake.configureOnOpen": false, - "files.eol": "\n" + "files.eol": "\n", + "nrf-connect.topdir": "${nrf-connect.sdk:2.1.2}", + "nrf-connect.toolchain.path": "${nrf-connect.toolchain:2.1.2}" } diff --git a/src/GameLoop/shop.c b/src/GameLoop/shop.c new file mode 100644 index 0000000..eb6bed5 --- /dev/null +++ b/src/GameLoop/shop.c @@ -0,0 +1,30 @@ +#include "shop.h" + + +bool hh_show_Shop(){ + static hh_e_ShopStates hh_e_Shop = hh_e_STATE_SHOW; + + switch (hh_e_Shop) + { + case hh_e_STATE_SHOW: + //hh_clear_screen(); + + //hh_setup_shop(); + hh_e_Shop = hh_e_STATE_Input; + return false; + break; + case hh_e_STATE_Input: + if(g_hh_controller_p1.button_primary){ + hh_e_Shop = hh_e_STATE_END; + } + break; + case hh_e_STATE_END: + hh_e_Shop = hh_e_STATE_SHOW; + return true; + break; + default: + hh_e_Shop = hh_e_STATE_SHOW; + break; + } + return false; +} diff --git a/src/GameLoop/shop.h b/src/GameLoop/shop.h new file mode 100644 index 0000000..4014f58 --- /dev/null +++ b/src/GameLoop/shop.h @@ -0,0 +1,16 @@ +#include "input.h" +#include "engine/draw_screen.h" + + + +#include +#include + +typedef enum { + hh_e_STATE_SHOW, + hh_e_STATE_Input, + hh_e_STATE_END +} hh_e_ShopStates; + + +bool hh_show_Shop(); diff --git a/src/GameLoop/startingScreen.c b/src/GameLoop/startingScreen.c new file mode 100644 index 0000000..4fc5af9 --- /dev/null +++ b/src/GameLoop/startingScreen.c @@ -0,0 +1,32 @@ +#include "startingScreen.h" +#include "input.h" +#include "engine/title_screen.h" +#include "engine/draw_screen.h" +// #include "engine/player_controller.h" + +bool hh_show_startingScreen(){ + static hh_e_screenStates hh_e_startingScreen = hh_e_STATE_SHOW; + + switch (hh_e_startingScreen) + { + case hh_e_STATE_SHOW: + hh_clear_screen(); + hh_init_title_screen(); + hh_e_startingScreen = hh_e_STATE_Input; + return false; + break; + case hh_e_STATE_Input: + if(g_hh_controller_p1.button_primary){ + hh_e_startingScreen = hh_e_STATE_END; + } + break; + case hh_e_STATE_END: + hh_e_startingScreen = hh_e_STATE_SHOW; + return true; + break; + default: + hh_e_startingScreen = hh_e_STATE_SHOW; + break; + } + return false; +} diff --git a/src/GameLoop/startingScreen.h b/src/GameLoop/startingScreen.h new file mode 100644 index 0000000..f51cc66 --- /dev/null +++ b/src/GameLoop/startingScreen.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +typedef enum { + hh_e_STATE_SHOW, + hh_e_STATE_Input, + hh_e_STATE_END +} hh_e_screenStates; + + +bool hh_show_startingScreen(); + diff --git a/src/demo.c b/src/demo.c index d4d1bf7..22ee8b7 100644 --- a/src/demo.c +++ b/src/demo.c @@ -11,8 +11,18 @@ #include "engine/draw_screen.h" #include "engine/player_controller.h" #include "engine/sprite_controller.h" +#include "GameLoop/startingScreen.h" +typedef enum { + hh_e_STATE_startingScreen, + hh_e_STATE_Shop, + hh_e_STATE_Gameplay, + hh_e_STATE_GameOver, + hh_e_STATE_HighScore +} hh_e_GameState; +hh_e_GameState hh_gameStates; + uint16_t g_hh_pos_x = 1000; // 0b0000 0001 0011 0110 @@ -37,7 +47,7 @@ hh_entity hh_g_player, hh_g_player_new; void hh_demo_setup() { hh_setup_palettes(); - hh_setup_screen(); + // hh_setup_screen(); } @@ -45,8 +55,46 @@ void hh_demo_setup() { void hh_demo_loop(unsigned long frame) { - hh_player_actions(); - + switch (hh_gameStates) + { + case hh_e_STATE_startingScreen: + bool ret = hh_show_startingScreen(); + if(ret){ + hh_gameStates = hh_e_STATE_Shop; + } + break; + case hh_e_STATE_Shop: + // TODO: + // if(hh_show_Shop()){ + hh_clear_screen(); + hh_clear_sprite(); + hh_setup_screen(); + hh_clear_sprite(); + hh_gameStates = hh_e_STATE_Gameplay; + // } + // function: new level is chosen goto level + break; + case hh_e_STATE_Gameplay: + hh_player_actions(); + + // TODO: + // function: if level complete goto shop + // function: if player is dead goto game over + break; + case hh_e_STATE_GameOver: + // TODO: + // function: show game over screen + // function: after time goto high score + break; + case hh_e_STATE_HighScore: + // TODO: + // fucntion: show all previously scored points + // function: button pressed goto starting screen + break; + default: + hh_gameStates = hh_e_STATE_startingScreen; + break; + } } // void sendData(uint8_t address, uint16_t data) { diff --git a/src/engine/bullet.c b/src/engine/bullet.c new file mode 100644 index 0000000..5aa9e51 --- /dev/null +++ b/src/engine/bullet.c @@ -0,0 +1,44 @@ +#include "bullet.h" +#include "engine/sprite_controller.h" + + +void shootBullet(vec2 playerPos, Bullet* bullet){ + // Set bullet's x and y coordinates to player's coordinates + bullet->x = playerPos.x; + bullet->y = playerPos.y; + // Set bullet's velocity to a fixed value + bullet->velocity = 1; + // Set bullet's status to active + bullet->isActive = true; +} +void updateBullet(Bullet* bullet, int deltaTime){ + // Only update bullet if it is active + static int latestLocationBullet = 0; + if (bullet->isActive) { + // Move bullet based on velocity and deltaTime + bullet->x += bullet->velocity * deltaTime; + drawBullet(bullet); + // Check if bullet has moved 16 pixels + if (bullet->x - latestLocationBullet > 32) { + // Set bullet's status to inactive + bullet->isActive = false; + drawBullet(&(Bullet){.x = -16,.y = -16. }); + } + } + else{ + latestLocationBullet = bullet->x; + } +} +void drawBullet(Bullet* bullet){ + + + hh_ppu_update_foreground(10, (hh_s_ppu_loc_fam_entry) + { + .position_x = bullet->x, + .position_y = bullet->y, + .horizontal_flip = false, + .vertical_flip = false, + .palette_index = 7, + .tilemap_index = 84, // change tilemap to the correct foreground index; + }); +} diff --git a/src/engine/bullet.h b/src/engine/bullet.h new file mode 100644 index 0000000..ad67d84 --- /dev/null +++ b/src/engine/bullet.h @@ -0,0 +1,16 @@ +#pragma once +#include "player_controller.h" + +typedef struct { + int x; + int y; + int velocity; + int isActive; + int hit; +} Bullet; + + +//Bullet* createBullet(float x, float y, float velocity, float direction); +void shootBullet(vec2 playerPos, Bullet* bullet); +void updateBullet(Bullet* bullet, int deltaTime); +void drawBullet(Bullet* bullet); diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c index c4f3389..fed2cfa 100644 --- a/src/engine/draw_screen.c +++ b/src/engine/draw_screen.c @@ -60,3 +60,29 @@ void hh_setup_screen(){ } free(tile); } +void hh_clear_screen(){ + // (HH_PPU_SCREEN_HEIGHT*HH_PPU_SCREEN_WIDTH)/(HH_PPU_SPRITE_HEIGHT*HH_PPU_SPRITE_WIDTH) + for (int i = 0; i < HH_PPU_BG_CANVAS_TILES_H*HH_PPU_BG_CANVAS_TILES_V; i++) { + hh_s_ppu_loc_bam_entry temp = { + .vertical_flip=false,.horizontal_flip = false, + .palette_index = 3,.tilemap_index = 0 + }; + hh_ppu_update_background(i,temp); + hh_ppu_update_color(3,0,(hh_ppu_rgb_color_t){0x0,0x0,0x0}); + } + hh_ppu_update_aux((hh_s_ppu_loc_aux){ + .bg_shift_x = 0, + .bg_shift_y = 0, + .fg_fetch = 0, + .sysreset = 0, + }); +} + +void hh_clear_sprite(){ + for (int i = 0; i < HH_PPU_FG_SPRITE_COUNT; i++) { + hh_ppu_update_foreground(i,(hh_s_ppu_loc_fam_entry){ + .position_x = -16, + .position_y = -16, + }); + } +} diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h index b181108..d3abca6 100644 --- a/src/engine/draw_screen.h +++ b/src/engine/draw_screen.h @@ -19,3 +19,8 @@ uint8_t hh_world_to_tile(vec2 pos); void hh_draw_screen(vec2 viewport); /** @brief send data to BAM memory from binary level */ void hh_setup_screen(); + +/** @brief send black screen to background memory */ +void hh_clear_screen(); +/** @brief clears all sprite data */ +void hh_clear_sprite(); diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c index d351bee..647b00c 100644 --- a/src/engine/player_controller.c +++ b/src/engine/player_controller.c @@ -6,7 +6,11 @@ #include "input.h" +#include "engine/bullet.h" void hh_player_actions() { + static Bullet bullet ={ + .isActive=false, + }; static hh_entity player={ .hp = 4, .speed = 6, @@ -17,13 +21,13 @@ void hh_player_actions() { .vel = (vec2){0,0}, .vec = (vec2){0,0}, .render = { - .frame0 = 20, - .palette = 7, + .frame0 = 80, + .palette = 3, .fam = (hh_s_ppu_loc_fam_entry){ .horizontal_flip = false, .vertical_flip = false, - .palette_index = 7, - .tilemap_index = 2, + .palette_index = 2, + .tilemap_index = 60, } } }, player_new = {0}; @@ -114,6 +118,10 @@ void hh_player_actions() { player.vel.y += 1; //gravity } + if(g_hh_controller_p1.button_secondary==true){ + shootBullet(player.pos,&bullet); + } + updateBullet(&bullet,5); @@ -202,7 +210,7 @@ void hh_player_actions() { // hh_solve_collision(tiles[i].pos, &player); // } // } - + player = hh_background_collision ( player, player_new); //player = player_new; @@ -221,9 +229,22 @@ void hh_player_actions() { player.render.fam.tilemap_index = 2;//TODO: these two lines should be redundant player.render.fam.palette_index = 7; - hh_ppu_update_foreground(0, player.render.fam); + // hh_ppu_update_foreground(0, player.render.fam); + + 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.frame0 + i; + temp.palette_index = player.render.palette; + temp.horizontal_flip = !(player.vel.x>0); + hh_ppu_update_foreground(i,temp); + } + + - hh_ppu_update_foreground(1, enemy.render.fam); + hh_ppu_update_foreground(4, enemy.render.fam); } diff --git a/src/engine/title_screen.c b/src/engine/title_screen.c new file mode 100644 index 0000000..9c7ed48 --- /dev/null +++ b/src/engine/title_screen.c @@ -0,0 +1,93 @@ +#include "ppu/ppu.h" +#include "ppu/types.h" +#include "ppu/consts.h" + + +#include "engine/draw_screen.h" +#include "engine/entity.h" + +void hh_init_title_screen(){ + + // hh_clear_screen(); + + //send data + uint8_t idx = 0; + const uint8_t tilemap_offset = 59; + int tiles_h = HH_PPU_BG_CANVAS_TILES_H; + int vp_h = HH_PPU_SCREEN_WIDTH/HH_PPU_SPRITE_WIDTH; //screen_h in tiles + int vert_offset = tiles_h*3; + + const uint8_t arr[4][4] = { + {0,1,1,0}, + {2,3,3,2}, + {4,0,0,4}, + {5,6,6,5}, + }; + int val, counter =0; + hh_ppu_update_color(5, 0, (hh_ppu_rgb_color_t) {0x1, 0x1, 0x1}); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + val = arr[i][j]; + + hh_s_ppu_loc_bam_entry temp = { + .vertical_flip=false, .horizontal_flip = ((j % 4 < 2) ? false : true), + .palette_index = (counter == 9 || counter == 10? 5:3), .tilemap_index = (val > 0 ? (tilemap_offset + val) : 0) + }; + + int vert_pos = tiles_h*i; + int x_pos = j; + idx = vert_offset + vert_pos + x_pos + vp_h/2-2; + + hh_ppu_update_background(idx,temp); + counter++; + } + + } + + + const uint8_t letters_offset = 66; + const int _size_hooded = 7, _size_v = 2; + + // char* hh = "hooded"; + int hooded_lookup[7][2]={ + {0,1},{0,2},//H + {3,4},{3,4},//oo + {5,6},{13,9},//de + {5,6}//d + }; + + counter = 8; + for (int i = 0; i < _size_hooded; i++) { + for (int vert = 1; vert <= _size_v; vert++) { + //TODO: move 'H' a few pixels to the right for a more cohesive font spacing + hh_ppu_update_foreground(counter++, (hh_s_ppu_loc_fam_entry) { + .vertical_flip = false, .horizontal_flip = false, + .palette_index = 6, .tilemap_index = letters_offset + hooded_lookup[i][vert-1], + .position_x = (16*i + 64+48), .position_y = (16*(vert > 1 ? 0:1)*-1 + 64+8+16 +(i==2 || i==3 ? 6:0)) + }); + } + } + + + hh_ppu_update_color(5, 1, (hh_ppu_rgb_color_t) {0xa, 0x3, 0x3}); + hh_ppu_update_color(5, 2, (hh_ppu_rgb_color_t) {0xc, 0x5, 0x3}); + + const int _size_havoc = 6; + int lookup_havoc[6][2]={ + {0,1},{0,2},//H + {13,10},{7,8},//av + {13,11},{13,12}//oc + }; + + counter = 8 + (_size_hooded * _size_v); + for (int i = 0; i < _size_havoc; i++) { + for (int vert = 1; vert <= _size_v; vert++) { + //TODO: move 'H' a few pixels to the right for a more cohesive font spacing + hh_ppu_update_foreground(counter++, (hh_s_ppu_loc_fam_entry) { + .vertical_flip = false, .horizontal_flip = (i > 4 && vert==0 ? 1:0), + .palette_index = 5, .tilemap_index = letters_offset + lookup_havoc[i][vert-1], + .position_x = (16*i +64+32+8), .position_y = (16*(vert > 1 ? 0:1)*-1 + 64+8+48) + }); + } + } +} diff --git a/src/engine/title_screen.h b/src/engine/title_screen.h new file mode 100644 index 0000000..b5eda63 --- /dev/null +++ b/src/engine/title_screen.h @@ -0,0 +1,3 @@ +#pragma once + +void hh_init_title_screen(); diff --git a/src/makefile b/src/makefile index d7d9087..cd248e2 100644 --- a/src/makefile +++ b/src/makefile @@ -37,7 +37,11 @@ LOCAL_SRCS += main.c \ engine/draw_screen.c \ engine/camera.c \ engine/maths.c \ - engine/entity.c + engine/entity.c \ + engine/bullet.c \ + engine/title_screen.c \ + GameLoop/shop.c \ + GameLoop/startingScreen.c CFLAGS += $(SHARED_FLAGS) LFLAGS += $(SHARED_FLAGS) diff --git a/src/ppusim/input.c b/src/ppusim/input.c index 5323fb1..4bc7018 100644 --- a/src/ppusim/input.c +++ b/src/ppusim/input.c @@ -13,4 +13,5 @@ void hh_input_read() { g_hh_controller_p1.dpad_left = kb[SDL_SCANCODE_A]; g_hh_controller_p1.dpad_right = kb[SDL_SCANCODE_D]; g_hh_controller_p1.button_primary = kb[SDL_SCANCODE_SPACE]; + g_hh_controller_p1.button_secondary = kb[SDL_SCANCODE_R]; } -- cgit v1.2.3