aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-03-15 15:37:21 +0100
committerNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-03-15 15:37:21 +0100
commit3891486b368f4cdd1c5e00019a2a66ca05e656e6 (patch)
treecd27ef74ef917bed16c90be9552dd007b4a7f2b5
parent67bbe8dc3e7e0eb5eeb00b84f9766ef3d3b6b4b4 (diff)
game loop and bullet
TODO game loop: -(startingscreen) read correct indexes -(shop) read correct indexes -gameplay still needs alot with different levels etc.. rest not yet done bullet needs testing and correct PAL and tilemap reading
-rw-r--r--src/GameLoop/shop.c28
-rw-r--r--src/GameLoop/shop.h15
-rw-r--r--src/GameLoop/startingScreen.c30
-rw-r--r--src/GameLoop/startingScreen.h14
-rw-r--r--src/demo.c62
-rw-r--r--src/engine/bullet.c39
-rw-r--r--src/engine/bullet.h14
-rw-r--r--src/engine/draw_screen.c49
-rw-r--r--src/engine/draw_screen.h5
-rw-r--r--src/engine/engine.c1
-rw-r--r--src/engine/player_controller.c13
-rw-r--r--src/makefile6
12 files changed, 268 insertions, 8 deletions
diff --git a/src/GameLoop/shop.c b/src/GameLoop/shop.c
new file mode 100644
index 0000000..31ff5ce
--- /dev/null
+++ b/src/GameLoop/shop.c
@@ -0,0 +1,28 @@
+#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_setup_startingScreen();
+ hh_e_Shop = hh_e_STATE_Input;
+ return false;
+ break;
+ case hh_e_STATE_Input:
+ if(g_hh_controller_p1.dpad_right){
+ 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..35bb780
--- /dev/null
+++ b/src/GameLoop/shop.h
@@ -0,0 +1,15 @@
+#include "input.h"
+#include "engine/draw_screen.h"
+
+
+#include <stdint.h>
+#include <stdbool.h>
+
+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..a0cd283
--- /dev/null
+++ b/src/GameLoop/startingScreen.c
@@ -0,0 +1,30 @@
+#include "startingScreen.h"
+#include "input.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_setup_startingScreen();
+ hh_e_startingScreen = hh_e_STATE_Input;
+ return false;
+ break;
+ case hh_e_STATE_Input:
+ if(g_hh_controller_p1.dpad_left){
+ 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 <stdint.h>
+#include <stdbool.h>
+
+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 95347cb..f5b5c29 100644
--- a/src/demo.c
+++ b/src/demo.c
@@ -13,6 +13,17 @@
#include "engine/sprite_controller.h"
+#include "GameLoop/startingScreen.h"
+#include "GameLoop/shop.h"
+// states
+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;
hh_s_entity_player g_hh_player_1 = {
.pos_x = 31000, // 0b0000 0001 0011 0110
@@ -45,18 +56,59 @@ typedef struct {
hh_entity hh_g_player, hh_g_player_new;
-void hh_demo_setup() {
-
+void hh_demo_setup() {
hh_setup_palettes();
- hh_setup_screen();
+ //hh_setup_screen();
}
-
void hh_demo_loop(unsigned long frame) {
// hh_player_movement();
+ switch (hh_gameStates)
+ {
+ case hh_e_STATE_startingScreen:
- hh_player_actions();
+ if(hh_show_startingScreen()){
+ hh_gameStates = hh_e_STATE_Shop;
+ }
+ break;
+ case hh_e_STATE_Shop:
+ // TODO:
+
+ if(hh_show_Shop()){
+ hh_gameStates = hh_e_STATE_Gameplay;
+ }
+ // function: new level is chosen goto level
+ break;
+ case hh_e_STATE_Gameplay:
+ static int run_test=0;
+ if(!run_test){
+ hh_setup_screen();
+ run_test=1;
+
+ }
+ else{
+ 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;
+ }
+
}
diff --git a/src/engine/bullet.c b/src/engine/bullet.c
new file mode 100644
index 0000000..eafd4e7
--- /dev/null
+++ b/src/engine/bullet.c
@@ -0,0 +1,39 @@
+#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 > 16) {
+ // Set bullet's status to inactive
+ bullet->isActive = false;
+ }
+ }
+ else{
+ latestLocationBullet = bullet->x;
+ }
+}
+void drawBullet(Bullet* bullet){
+
+ hh_ppu_update_foreground(1, (hh_s_ppu_loc_fam_entry)
+ {
+ .horizontal_flip = false,
+ .vertical_flip = false,
+ .palette_index = 7,
+ .tilemap_index = 2, // 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..f119829
--- /dev/null
+++ b/src/engine/bullet.h
@@ -0,0 +1,14 @@
+#pragma once
+#include "player_controller.h"
+
+typedef struct {
+ int x;
+ int y;
+ int velocity;
+ int isActive;
+} 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..205d4b6 100644
--- a/src/engine/draw_screen.c
+++ b/src/engine/draw_screen.c
@@ -19,7 +19,6 @@ uint8_t hh_world_to_tile(vec2 pos){
return tile;
}
-
// remeber old value to know which part to update.
vec2 previousViewport = { .x = 0, .y = 0 };
void hh_draw_screen(vec_cor viewport){
@@ -60,3 +59,51 @@ void hh_setup_screen(){
}
free(tile);
}
+
+void hh_setup_startingScreen(){
+ int size = 300; // 40 x as tiles 30 y as tiles
+ FILE* level = fopen("../test/bin/startingScreen.bin", "rb"); /* open binary file */
+ if (!level) { /* check if file opened successfully */
+ fprintf(stderr, "Error: Failed to open file.\n");
+ return;
+ }
+ fseek(level, (0* sizeof(int)) + sizeof(int), SEEK_SET);
+ int* tile = (int*)malloc(size*sizeof(int));
+ fread(tile, sizeof(int), size, level); // read 1 tile from binary
+
+ fclose(level);
+
+ for(int BAM_index = 0; BAM_index < size; BAM_index++){
+ hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){
+ .horizontal_flip = false,
+ .vertical_flip = false,
+ .palette_index = hh_get_palette(tile[BAM_index]),
+ .tilemap_index = tile[BAM_index],
+ });
+ }
+ free(tile);
+}
+
+void hh_setup_shopScreen(){
+ int size = 300; // 40 x as tiles 30 y as tiles
+ FILE* level = fopen("../test/bin/shopScreen.bin", "rb"); /* open binary file */
+ if (!level) { /* check if file opened successfully */
+ fprintf(stderr, "Error: Failed to open file.\n");
+ return;
+ }
+ fseek(level, (0* sizeof(int)) + sizeof(int), SEEK_SET);
+ int* tile = (int*)malloc(size*sizeof(int));
+ fread(tile, sizeof(int), size, level); // read 1 tile from binary
+
+ fclose(level);
+
+ for(int BAM_index = 0; BAM_index < size; BAM_index++){
+ hh_ppu_update_background(BAM_index, (hh_s_ppu_loc_bam_entry){
+ .horizontal_flip = false,
+ .vertical_flip = false,
+ .palette_index = hh_get_palette(tile[BAM_index]),
+ .tilemap_index = tile[BAM_index],
+ });
+ }
+ free(tile);
+}
diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h
index b181108..bfe8bf8 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 starting screen data to BAM memory from binary level */
+void hh_setup_startingScreen();
+
+/** @brief send shop screen data to BAM memory from binary level */
+void hh_setup_shopScreen();
diff --git a/src/engine/engine.c b/src/engine/engine.c
index 799ee7c..3280892 100644
--- a/src/engine/engine.c
+++ b/src/engine/engine.c
@@ -1,4 +1,5 @@
#include "engine/draw_screen.h"
+#include "engine/bullet.h""
#include "engine/level.h"
#include "engine/maths.h"
#include "engine/sprite_controller.h"
diff --git a/src/engine/player_controller.c b/src/engine/player_controller.c
index 22f6eb6..f938844 100644
--- a/src/engine/player_controller.c
+++ b/src/engine/player_controller.c
@@ -3,7 +3,7 @@
#include "engine/draw_screen.h"
#include "engine/sprite_controller.h"
#include "engine/player_controller.h"
-
+#include "engine/bullet.h"
#include "input.h"
void hh_player_actions() {
@@ -29,6 +29,17 @@ void hh_player_actions() {
// 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) };
+
+ // shooting code
+ static Bullet bullet;
+ if(g_hh_controller_p1.button_primary){
+ bullet.isActive = false;
+ shootBullet(player.vel, &bullet);
+ }
+ else{
+ updateBullet(&bullet, 1);
+
+ }
// const int8_t maa = 3;
// const int8_t mbb = -3;
// if (g_hh_controller_p1.dpad_up)
diff --git a/src/makefile b/src/makefile
index d7d9087..94406ca 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/bullet.c \
+ engine/entity.c \
+ GameLoop/shop.c \
+ GameLoop/startingScreen.c
+
CFLAGS += $(SHARED_FLAGS)
LFLAGS += $(SHARED_FLAGS)