aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-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
6 files changed, 119 insertions, 2 deletions
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)