aboutsummaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-03-30 13:02:36 +0200
committerNielsCoding <48092678+heavydemon21@users.noreply.github.com>2023-03-30 13:02:36 +0200
commit552dcc17db2b98287ba1b480c2e70759cd94d81f (patch)
tree7f7bbd7a8b3964bbebc2878d33be0ed8a9382e3f /src/engine
parent7f51cd925883bbf958baa289d4d19231667c9eba (diff)
gameloop/bullet/levels
gameloop moet nog wat gefixt worden. shop moet nog wat art en functie krijgen zefde voor gameover/highscore bullet moet meerdere entityten kunnen doen nu nog alleen gemaakt voor 1. levels moet alleen nog backwords kunnen
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/bullet.c65
-rw-r--r--src/engine/bullet.h17
-rw-r--r--src/engine/draw_screen.c103
-rw-r--r--src/engine/draw_screen.h22
-rw-r--r--src/engine/level_const.c22
-rw-r--r--src/engine/level_const.h31
6 files changed, 159 insertions, 101 deletions
diff --git a/src/engine/bullet.c b/src/engine/bullet.c
index 5aa9e51..2016a4d 100644
--- a/src/engine/bullet.c
+++ b/src/engine/bullet.c
@@ -1,44 +1,35 @@
#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;
- }
+
+// TODO: use hh_entity as bullet struct
+void shootBullet(vec2 playerPos, vec_cor cam_pos, hh_entity* bullet){
+ vec2 temp;
+ if(g_hh_controller_p1.button_secondary){
+ if(bullet->is_grounded){
+ bullet->is_grounded=false;
+ bullet->pos = playerPos;
+ }
+ }
+ else{
+ if(!bullet->is_grounded){
+ updateBullet(bullet , cam_pos, temp);
+ drawBullet(*bullet);
+ }
+ }
+
+
}
-void drawBullet(Bullet* bullet){
+void collision
+void updateBullet(hh_entity* bullet, vec_cor cam_pos, vec2 start){
+ 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);
- 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;
- });
+}
+void drawBullet(hh_entity bullet){
+ hh_s_ppu_loc_fam_entry temp = bullet.render.fam;
+ hh_ppu_update_foreground(10,temp);
}
diff --git a/src/engine/bullet.h b/src/engine/bullet.h
index ad67d84..5bbc031 100644
--- a/src/engine/bullet.h
+++ b/src/engine/bullet.h
@@ -1,16 +1,11 @@
#pragma once
#include "player_controller.h"
+#include "engine/sprite_controller.h"
+#include "input.h"
-typedef struct {
- int x;
- int y;
- int velocity;
- int isActive;
- int hit;
-} Bullet;
+void shootBullet(vec2 playerPos, vec_cor cam_pos, hh_entity*);
-//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);
+void updateBullet(hh_entity* , vec_cor, vec2 );
+
+void drawBullet(hh_entity);
diff --git a/src/engine/draw_screen.c b/src/engine/draw_screen.c
index 0c31bf6..4a5ca68 100644
--- a/src/engine/draw_screen.c
+++ b/src/engine/draw_screen.c
@@ -1,66 +1,77 @@
#include "engine/draw_screen.h"
-#include "engine/sprite_controller.h"
+//#include "engine/sprite_controller.h"
+hh_level_entity level;
+uint64_t offset=0;
uint8_t hh_world_to_tile(vec2 pos){
- //TODO: remove magic file name here
- FILE* level = fopen("static/level1_test.bin", "rb"); /* open binary file */
- if (!level) { /* check if file opened successfully */
- fprintf(stderr, "Error: Failed to open file.\n");
- return 0;
- }
- int index = ((pos.y/16)*40 + pos.x/16);//TODO: remove magic number(s)
- fseek(level, (index * sizeof(int)) + sizeof(int), SEEK_SET);
- int tile;// = (int*)malloc(sizeof(int));
- fread(&tile, sizeof(int), 1, level); // read 1 tile from binary
-
- fclose(level);
- // int val = tile;
- // free(tile);
+
+ int index = (((pos.y/16)*hh_max_x_size) + (pos.x/16));//TODO: remove magic number(s)
+ int tile= level.place[index];
return tile;
}
+void hh_update_screen(vec2 view, vec2 player){
+ int currentTileY = view.y / 16;
+ int offset_px = view.y-(offset / 40 * 16);
-// remeber old value to know which part to update.
-vec2 previousViewport = { .x = 0, .y = 0 };
-void hh_draw_screen(vec_cor viewport){
- if (viewport.x == previousViewport.x && viewport.y == previousViewport.y) return;
-
- hh_ppu_update_aux((hh_s_ppu_loc_aux){
- .bg_shift_x = viewport.x,
- .bg_shift_y = viewport.y,
- .fg_fetch = 0,
- .sysreset = 0,
- });
- // update previous viewport values
- previousViewport = viewport;
-}
+ // || (offset_px == 0 && lastUpdated != 0 && player.y/16 == currentTileY-5)
+ if( (offset_px > 230) ){
+ int size = (hh_max_x_size) * hh_max_y_size;
+ offset = currentTileY * level.x;
-void hh_setup_screen(){
- //(HH_map_size_X*HH_map_size_Y)
- int size = 2400; // max X = 40 en max Y = 80
- //TODO: remove magic file name here
- FILE* level = fopen("static/level1_test.bin", "rb"); /* open binary file */
- if (!level) { /* check if file opened successfully */
- fprintf(stderr, "Error: Failed to open level file.\n");
- return;
+ // Update the background screen
+ 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(level.place[offset+BAM_index]),
+ .tilemap_index = level.place[offset+BAM_index],
+ });
+ }
}
- 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);
-
+}
+void hh_setup_screen(hh_level_entity currentlevel){
+ hh_clear_screen();
+ hh_clear_sprite();
+ level = currentlevel;
+ int size = hh_max_x_size* hh_max_y_size;
+ printf("hier\n");
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],
+ .palette_index = hh_get_palette(currentlevel.place[BAM_index]),
+ .tilemap_index = currentlevel.place[BAM_index],
});
}
- free(tile);
}
+vec_cor hh_draw_screen(vec2 player) {
+ static vec_cor previousViewport = {0, 0};
+ int offset_px = offset/40*16;
+ //printf("%d\n",offset_px%2);
+ vec_cor viewport = hh_update_camera(player,(vec2){.x=0,.y=offset_px},(vec2){.x=20*16,.y=30*16+offset_px/4});//TODO: remove magic number(s)
+
+ hh_update_screen((vec2){.y=viewport.y,.x=0},player);
+ if (viewport.x == previousViewport.x && viewport.y == previousViewport.y){}
+ else{
+ // update previous viewport values
+
+ //printf("viewport y %d\n",viewport.y-(offset/40*16));
+ //printf("offset %d\n",offset);
+ hh_ppu_update_aux((hh_s_ppu_loc_aux){
+ .bg_shift_x = viewport.x - 0,
+ .bg_shift_y = viewport.y-(offset/40*16),
+ .fg_fetch = 0,
+ .sysreset = 0,
+ });
+ previousViewport = viewport;
+
+ }
+
+ return viewport;
+}
+
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++) {
diff --git a/src/engine/draw_screen.h b/src/engine/draw_screen.h
index 95765e5..0d1b68a 100644
--- a/src/engine/draw_screen.h
+++ b/src/engine/draw_screen.h
@@ -3,24 +3,32 @@
// every function call for drawing the screen goes here.
#include "engine/maths.h"
-#include "ppu/consts.h"
#include "ppu/ppu.h"
-
+#include "engine/level_const.h"
+#include "engine/sprite_controller.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
+#include "engine/camera.h"
-
-#define HH_map_size_X 80
-#define HH_map_size_Y 60
+#define hh_max_x_size 40
+#define hh_max_y_size 30
/** @brief return a single tile from world binary */
uint8_t hh_world_to_tile(vec2 pos);
+
/** @brief shift to level if viewport changed position */
-void hh_draw_screen(vec2 viewport);
+vec_cor hh_draw_screen(vec2 player);
+
/** @brief send data to BAM memory from binary level */
-void hh_setup_screen();
+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 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/level_const.c b/src/engine/level_const.c
new file mode 100644
index 0000000..b568e86
--- /dev/null
+++ b/src/engine/level_const.c
@@ -0,0 +1,22 @@
+#include "engine/level_const.h"
+
+
+hh_g_all_levels hh_init_game_levels(){
+ hh_g_all_levels levels;
+ levels.currentLevel=0;
+
+ levels.level[0].x=40;
+ levels.level[0].y=60;
+ levels.level[0].hh_level_completed=false;
+ FILE *fp = fopen("../test/bin/level1_test.bin", "rb");
+ fseek(fp, 0, SEEK_END);
+ int size = ftell(fp) / sizeof(int);
+ fseek(fp, (0 * sizeof(int)) + sizeof(int), SEEK_SET);
+ int* hh_game_level1 = malloc(size * sizeof(int));
+ fread(hh_game_level1, sizeof(int), size, fp);
+ fclose(fp);
+ levels.level[0].place = hh_game_level1;
+
+ return levels;
+}
+
diff --git a/src/engine/level_const.h b/src/engine/level_const.h
new file mode 100644
index 0000000..7a4aac5
--- /dev/null
+++ b/src/engine/level_const.h
@@ -0,0 +1,31 @@
+#pragma once
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <math.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;
+
+typedef struct {
+ int x;
+ int y;
+ int hh_total_enemies;
+ int* place;
+ bool hh_level_completed;
+}hh_level_entity;
+
+typedef struct {
+ hh_level_entity level[1];
+ int currentLevel;
+
+
+}hh_g_all_levels;
+
+hh_g_all_levels hh_init_game_levels();