Compare commits
17 Commits
bafb32501f
...
937f63b0ca
Author | SHA1 | Date |
---|---|---|
|
937f63b0ca | |
|
c0aa37886f | |
|
9c2e21f4d2 | |
|
b1a6430eb7 | |
|
d2de5cfed9 | |
|
237bee41a9 | |
|
bc7f9ef777 | |
|
4478a512a3 | |
|
e1a7774bfb | |
|
b0e1d33ad6 | |
|
98450d0bba | |
|
0f485d89f2 | |
|
57c9eb0216 | |
|
e9d9ffff20 | |
|
3580209f71 | |
|
7b3685b792 | |
|
ee4313adbb |
|
@ -2,3 +2,4 @@
|
|||
build/
|
||||
compile_commands.json
|
||||
res/
|
||||
.gdb_history
|
||||
|
|
|
@ -11,17 +11,11 @@ if (${CMAKE_BUILD_TYPE} STREQUAL tile16)
|
|||
endif()
|
||||
|
||||
set(GAME_LIBS
|
||||
lib_EC
|
||||
lib_scenes
|
||||
sc_queue
|
||||
sc_map
|
||||
sc_array
|
||||
raylib
|
||||
m
|
||||
)
|
||||
|
||||
|
||||
add_subdirectory(engine)
|
||||
add_subdirectory(scenes)
|
||||
add_executable(${PROJECT_NAME}
|
||||
main.c
|
||||
)
|
||||
|
@ -29,11 +23,6 @@ add_executable(${PROJECT_NAME}
|
|||
target_include_directories(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_link_directories(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
@ -48,17 +37,9 @@ target_link_options(EntManager_test PRIVATE -fsanitize=address -gdwarf-4)
|
|||
target_include_directories(EntManager_test
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_link_directories(EntManager_test
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
target_link_libraries(EntManager_test
|
||||
lib_EC
|
||||
lib_scenes
|
||||
sc_queue
|
||||
sc_map
|
||||
${GAME_LIBS}
|
||||
)
|
||||
|
||||
add_executable(scene_test
|
||||
|
@ -68,17 +49,11 @@ add_executable(scene_test
|
|||
target_include_directories(scene_test
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
|
||||
target_compile_options(scene_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
target_link_options(scene_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
|
||||
target_link_directories(scene_test
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
|
||||
target_link_libraries(scene_test
|
||||
${GAME_LIBS}
|
||||
)
|
||||
|
@ -89,13 +64,9 @@ add_executable(scene_test_mem
|
|||
target_include_directories(scene_test_mem
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_link_options(scene_test_mem PRIVATE -Xlinker -Map=scene_test.map)
|
||||
target_link_directories(scene_test_mem
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
|
||||
target_link_libraries(scene_test_mem
|
||||
${GAME_LIBS}
|
||||
)
|
||||
|
@ -106,14 +77,9 @@ add_executable(menu_test
|
|||
target_include_directories(menu_test
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_compile_options(menu_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
target_link_options(menu_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
target_link_directories(menu_test
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
target_link_libraries(menu_test
|
||||
${GAME_LIBS}
|
||||
)
|
||||
|
@ -124,14 +90,9 @@ add_executable(assets_test
|
|||
target_include_directories(assets_test
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_compile_options(assets_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
target_link_options(assets_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||
target_link_directories(assets_test
|
||||
PRIVATE
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
target_link_libraries(assets_test
|
||||
${GAME_LIBS}
|
||||
)
|
||||
|
|
|
@ -43,8 +43,8 @@ int main(void)
|
|||
DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED);
|
||||
|
||||
// Draw the static Sprite and animated Sprite
|
||||
draw_sprite(spr, (Vector2){64,128});
|
||||
draw_sprite(spr2, (Vector2){64,180});
|
||||
draw_sprite(spr, (Vector2){64,128}, false);
|
||||
draw_sprite(spr2, (Vector2){64,180}, true);
|
||||
EndDrawing();
|
||||
|
||||
// Update the animated Sprite
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
#ifndef __ENTITY_MANAGER_H
|
||||
#define __ENTITY_MANAGER_H
|
||||
#include "sc/queue/sc_queue.h"
|
||||
#include "sc/map/sc_map.h"
|
||||
#include "mempool.h" // includes entity and components
|
||||
|
||||
typedef struct EntityManager {
|
||||
// All fields are Read-Only
|
||||
struct sc_map_64v entities; // ent id : entity
|
||||
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}]
|
||||
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...]
|
||||
struct sc_queue_uint to_add;
|
||||
struct sc_queue_uint to_remove;
|
||||
} EntityManager_t;
|
||||
|
||||
void init_entity_manager(EntityManager_t* p_manager);
|
||||
void update_entity_manager(EntityManager_t* p_manager);
|
||||
void clear_entity_manager(EntityManager_t* p_manager);
|
||||
void free_entity_manager(EntityManager_t* p_manager);
|
||||
|
||||
Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
|
||||
void remove_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
|
||||
void* add_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
|
||||
void* get_component(EntityManager_t* p_manager, Entity_t *entity, ComponentEnum_t comp_type);
|
||||
void remove_component(EntityManager_t* p_manager, Entity_t* entity, ComponentEnum_t comp_type);
|
||||
|
||||
#endif // __ENTITY_MANAGER_H
|
|
@ -1,20 +0,0 @@
|
|||
#ifndef __ENTITY_H
|
||||
#define __ENTITY_H
|
||||
#include <stdbool.h>
|
||||
#include "sc/map/sc_map.h"
|
||||
|
||||
#define N_TAGS 4
|
||||
typedef enum EntityTag {
|
||||
NO_ENT_TAG,
|
||||
PLAYER_ENT_TAG,
|
||||
ENEMY_ENT_TAG,
|
||||
CRATES_ENT_TAG,
|
||||
} EntityTag_t;
|
||||
|
||||
typedef struct Entity {
|
||||
unsigned long m_id;
|
||||
EntityTag_t m_tag;
|
||||
bool m_alive;
|
||||
struct sc_map_64 components;
|
||||
} Entity_t;
|
||||
#endif // __ENTITY_H
|
|
@ -1,7 +0,0 @@
|
|||
#include "assets_maps.h"
|
||||
|
||||
|
||||
const char* const player_sprite_map[N_PLAYER_SPRITES] = {
|
||||
"plr_stand",
|
||||
"plr_run",
|
||||
};
|
|
@ -1,13 +0,0 @@
|
|||
#ifndef __ASSETS_MAPS_H
|
||||
#define __ASSETS_MAPS_H
|
||||
|
||||
#define N_PLAYER_SPRITES 2
|
||||
enum PlayerSpriteEnum
|
||||
{
|
||||
SPR_PLAYER_STAND = 0,
|
||||
SPR_PLAYER_RUN
|
||||
};
|
||||
|
||||
extern const char* const player_sprite_map[N_PLAYER_SPRITES];
|
||||
|
||||
#endif // __ASSETS_MAPS_H
|
|
@ -1,5 +1,4 @@
|
|||
#include "mempool.h"
|
||||
#include "entManager.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
@ -12,10 +11,10 @@ int main(void)
|
|||
|
||||
puts("Creating two entities");
|
||||
Entity_t *p_ent = add_entity(&manager, PLAYER_ENT_TAG);
|
||||
CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||
CBBox_t * p_bbox = (CBBox_t *)add_component(p_ent, CBBOX_COMP_T);
|
||||
p_bbox->size.x = 15;
|
||||
p_ent = add_entity(&manager, ENEMY_ENT_TAG);
|
||||
p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||
p_bbox = (CBBox_t *)add_component(p_ent, CBBOX_COMP_T);
|
||||
p_bbox->size.y = 40;
|
||||
update_entity_manager(&manager);
|
||||
|
||||
|
@ -23,7 +22,7 @@ int main(void)
|
|||
unsigned long idx = 0;
|
||||
sc_map_foreach(&manager.entities, idx, p_ent)
|
||||
{
|
||||
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
||||
p_bbox = (CBBox_t *)get_component(p_ent, CBBOX_COMP_T);
|
||||
printf("BBOX: %f,%f\n", p_bbox->size.x, p_bbox->size.y);
|
||||
remove_entity(&manager, idx);
|
||||
}
|
||||
|
@ -33,7 +32,7 @@ int main(void)
|
|||
puts("Print again, should show nothing");
|
||||
sc_map_foreach(&manager.entities, idx, p_ent)
|
||||
{
|
||||
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
||||
p_bbox = (CBBox_t *)get_component(p_ent, CBBOX_COMP_T);
|
||||
printf("BBOX: %f,%f\n", p_bbox->size.x, p_bbox->size.y);
|
||||
remove_entity(&manager, idx);
|
||||
}
|
||||
|
|
1
main.c
1
main.c
|
@ -1,5 +1,6 @@
|
|||
#include "raylib.h"
|
||||
#include "scene_impl.h"
|
||||
#include "mempool.h"
|
||||
#include <stdio.h>
|
||||
#define N_SCENES 3
|
||||
|
||||
|
|
17
scene_test.c
17
scene_test.c
|
@ -1,5 +1,7 @@
|
|||
#include "mempool.h"
|
||||
#include "scene_impl.h"
|
||||
#include "ent_impl.h"
|
||||
#include "assets_loader.h"
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -23,23 +25,18 @@ int main(void)
|
|||
init_memory_pools();
|
||||
|
||||
init_assets(&engine.assets);
|
||||
Texture2D* tex = add_texture(&engine.assets, "plr_tex", "res/test_tex.png");
|
||||
Sprite_t* spr = add_sprite(&engine.assets, "plr_stand", tex);
|
||||
spr->origin = (Vector2){0, 0};
|
||||
spr->frame_size = (Vector2){32, 32};
|
||||
|
||||
spr = add_sprite(&engine.assets, "plr_run", tex);
|
||||
spr->frame_count = 4;
|
||||
spr->origin = (Vector2){0, 0};
|
||||
spr->frame_size = (Vector2){32, 32};
|
||||
spr->speed = 15;
|
||||
load_from_infofile("res/assets.info", &engine.assets);
|
||||
init_player_creation("res/player_spr.info", &engine.assets);
|
||||
|
||||
LevelScene_t scene;
|
||||
scene.scene.engine = &engine;
|
||||
init_level_scene(&scene);
|
||||
scene.data.tile_sprites[ONEWAY_TILE] = get_sprite(&engine.assets, "tl_owp");
|
||||
scene.data.tile_sprites[LADDER] = get_sprite(&engine.assets, "tl_ldr");
|
||||
scenes[0] = &scene.scene;
|
||||
change_scene(&engine, 0);
|
||||
|
||||
|
||||
while(true)
|
||||
{
|
||||
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
add_subdirectory(EC)
|
||||
add_subdirectory(engine)
|
||||
add_library(lib_scenes STATIC
|
||||
engine.c
|
||||
assets.c
|
||||
assets_maps.c
|
||||
assets_loader.c
|
||||
player_ent.c
|
||||
items_ent.c
|
||||
editor_scene.c
|
||||
menu_scene.c
|
||||
game_systems.c
|
||||
AABB.c
|
||||
gui.c
|
||||
)
|
||||
target_include_directories(lib_scenes
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_link_libraries(lib_scenes
|
||||
PRIVATE
|
||||
lib_EC
|
||||
raylib
|
||||
PUBLIC
|
||||
lib_engine
|
||||
)
|
|
@ -0,0 +1,124 @@
|
|||
#include "assets_loader.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum AssetInfoType
|
||||
{
|
||||
TEXTURE_INFO,
|
||||
SPRITE_INFO,
|
||||
INVALID_INFO
|
||||
}AssetInfoType_t;
|
||||
|
||||
typedef struct SpriteInfo
|
||||
{
|
||||
char* tex;
|
||||
Vector2 origin;
|
||||
Vector2 frame_size;
|
||||
int speed;
|
||||
int frame_count;
|
||||
}SpriteInfo_t;
|
||||
|
||||
static bool parse_sprite_info(char* sprite_info_str, SpriteInfo_t* spr_info)
|
||||
{
|
||||
char* tex_name = strtok(sprite_info_str, ",");
|
||||
if (tex_name == NULL) return false;
|
||||
spr_info->tex = tex_name;
|
||||
char* spr_data = tex_name + strlen(tex_name) + 1;
|
||||
int data_count = sscanf(
|
||||
spr_data, "%f,%f,%f,%f,%d,%d",
|
||||
&spr_info->origin.x, &spr_info->origin.y,
|
||||
&spr_info->frame_size.x, &spr_info->frame_size.y,
|
||||
&spr_info->frame_count, &spr_info->speed
|
||||
);
|
||||
return data_count == 6;
|
||||
}
|
||||
|
||||
bool load_from_infofile(const char* file, Assets_t* assets)
|
||||
{
|
||||
FILE* in_file = fopen(file, "r");
|
||||
if (in_file == NULL)
|
||||
{
|
||||
printf("Unable to open file %s\n", file);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
size_t line_num = 0;
|
||||
AssetInfoType_t info_type = INVALID_INFO;
|
||||
|
||||
while (true)
|
||||
{
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
|
||||
if (tmp[0] == '-')
|
||||
{
|
||||
tmp++;
|
||||
if (strcmp(tmp, "Texture") == 0)
|
||||
{
|
||||
info_type = TEXTURE_INFO;
|
||||
}
|
||||
else if (strcmp(tmp, "Sprite") == 0)
|
||||
{
|
||||
info_type = SPRITE_INFO;
|
||||
}
|
||||
else
|
||||
{
|
||||
info_type = INVALID_INFO;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) continue;
|
||||
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
switch(info_type)
|
||||
{
|
||||
case TEXTURE_INFO:
|
||||
{
|
||||
if (add_texture(assets, name, info_str) == NULL)
|
||||
{
|
||||
printf("Unable to add texture at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
printf("Added texture %s as %s\n", info_str, name);
|
||||
//strcpy(tmp2, name);
|
||||
}
|
||||
break;
|
||||
case SPRITE_INFO:
|
||||
{
|
||||
SpriteInfo_t spr_info = {0};
|
||||
if (!parse_sprite_info(info_str, &spr_info))
|
||||
{
|
||||
printf("Unable to parse info for sprite at line %lu\n", line_num);
|
||||
break;
|
||||
}
|
||||
//printf("Compare %s,%s = %d\n", tmp2, spr_info.tex, strcmp(tmp2, spr_info.tex));
|
||||
Texture2D* tex = get_texture(assets, spr_info.tex);
|
||||
if (tex == NULL)
|
||||
{
|
||||
printf("Unable to get texture info %s for sprite %s\n", spr_info.tex, name);
|
||||
break;
|
||||
}
|
||||
printf("Added Sprite %s from texture %s\n", name, spr_info.tex);
|
||||
Sprite_t* spr = add_sprite(assets, name, tex);
|
||||
spr->origin = spr_info.origin;
|
||||
spr->frame_size = spr_info.frame_size;
|
||||
spr->frame_count = spr_info.frame_count;
|
||||
spr->speed = spr_info.speed;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
line_num++;
|
||||
}
|
||||
fclose(in_file);
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
#ifndef __ASSETS_LOADER_H
|
||||
#define __ASSETS_LOADER_H
|
||||
#include "assets.h"
|
||||
|
||||
bool load_from_infofile(const char* file, Assets_t* assets);
|
||||
|
||||
#endif // __ASSETS_LOADER_H
|
|
@ -4,7 +4,7 @@
|
|||
#ifndef TILE16_SIZE
|
||||
#define TILE_SIZE 32
|
||||
#define DEFAULT_MAP_WIDTH 48
|
||||
#define DEFAULT_MAP_HEIGHT 16
|
||||
#define DEFAULT_MAP_HEIGHT 22
|
||||
#define VIEWABLE_MAP_WIDTH 32
|
||||
#define VIEWABLE_MAP_HEIGHT 16
|
||||
#else
|
||||
|
@ -22,7 +22,7 @@
|
|||
|
||||
#ifndef TILE16_SIZE
|
||||
#define PLAYER_WIDTH 30
|
||||
#define PLAYER_HEIGHT 55
|
||||
#define PLAYER_HEIGHT 42
|
||||
#define PLAYER_C_WIDTH 30
|
||||
#define PLAYER_C_HEIGHT 30
|
||||
#else
|
|
@ -1,7 +1,8 @@
|
|||
#include "scene_impl.h"
|
||||
#include "game_systems.h"
|
||||
#include "constants.h"
|
||||
#include "assets_maps.h"
|
||||
#include "ent_impl.h"
|
||||
#include "mempool.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include <stdio.h>
|
||||
|
@ -16,6 +17,7 @@ enum EntitySpawnSelection {
|
|||
SPAWN_CRATE,
|
||||
SPAWN_METAL_CRATE,
|
||||
};
|
||||
|
||||
#define MAX_SPAWN_TYPE 5
|
||||
static unsigned int current_spawn_selection = 0;
|
||||
|
||||
|
@ -40,7 +42,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
Entity_t* p_ent;
|
||||
|
||||
BeginTextureMode(data->game_viewport);
|
||||
ClearBackground(RAYWHITE);
|
||||
ClearBackground(WHITE);
|
||||
BeginMode2D(data->cam);
|
||||
for (size_t i = 0; i < tilemap.n_tiles; ++i)
|
||||
{
|
||||
|
@ -49,7 +51,11 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
int y = (i / tilemap.width) * TILE_SIZE;
|
||||
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
|
||||
|
||||
if (tilemap.tiles[i].tile_type == SOLID_TILE)
|
||||
if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL)
|
||||
{
|
||||
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, false);
|
||||
}
|
||||
else if (tilemap.tiles[i].tile_type == SOLID_TILE)
|
||||
{
|
||||
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK);
|
||||
}
|
||||
|
@ -73,8 +79,8 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
char buffer[64] = {0};
|
||||
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
|
||||
{
|
||||
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
Color colour;
|
||||
switch(p_ent->m_tag)
|
||||
{
|
||||
|
@ -88,8 +94,8 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
colour = BLACK;
|
||||
}
|
||||
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour);
|
||||
CHurtbox_t* p_hurtbox = get_component(&scene->ent_manager, p_ent, CHURTBOX_T);
|
||||
CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOXES_T);
|
||||
CHurtbox_t* p_hurtbox = get_component(p_ent, CHURTBOX_T);
|
||||
CHitBoxes_t* p_hitbox = get_component(p_ent, CHITBOXES_T);
|
||||
if (p_hitbox != NULL)
|
||||
{
|
||||
for (uint8_t i = 0;i < p_hitbox->n_boxes; ++i)
|
||||
|
@ -113,11 +119,15 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
};
|
||||
DrawRectangleLinesEx(rec, 1.5, PURPLE);
|
||||
}
|
||||
CSprite_t* p_cspr = get_component(&scene->ent_manager, p_ent, CSPRITE_T);
|
||||
CSprite_t* p_cspr = get_component(p_ent, CSPRITE_T);
|
||||
if (p_cspr != NULL)
|
||||
{
|
||||
Vector2 pos = Vector2Add(p_ct->position, p_cspr->offset);
|
||||
draw_sprite(p_cspr->sprite, pos);
|
||||
const SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
|
||||
if (spr.sprite != NULL)
|
||||
{
|
||||
Vector2 pos = Vector2Add(p_ct->position, spr.offset);
|
||||
draw_sprite(spr.sprite, pos, p_cspr->flip_x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -168,10 +178,10 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
|
||||
{
|
||||
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CJump_t* p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
|
||||
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
|
||||
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
|
||||
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
|
||||
DrawText(buffer, gui_x, 15, 12, BLACK);
|
||||
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
|
||||
|
@ -203,57 +213,11 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
Entity_t* p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG);
|
||||
CBBox_t* p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T);
|
||||
Entity_t* p_crate = create_crate(&scene->ent_manager, &scene->engine->assets, metal);
|
||||
|
||||
set_bbox(p_bbox, TILE_SIZE, TILE_SIZE);
|
||||
p_bbox->solid = true;
|
||||
p_bbox->fragile = !metal;
|
||||
|
||||
CTransform_t* p_ctransform = add_component(&scene->ent_manager, p_crate, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_crate, CTRANSFORM_COMP_T);
|
||||
p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE;
|
||||
p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE;
|
||||
add_component(&scene->ent_manager, p_crate, CMOVEMENTSTATE_T);
|
||||
add_component(&scene->ent_manager, p_crate, CTILECOORD_COMP_T);
|
||||
CHurtbox_t* p_hurtbox = add_component(&scene->ent_manager, p_crate, CHURTBOX_T);
|
||||
p_hurtbox->size = p_bbox->size;
|
||||
p_hurtbox->fragile = !metal;
|
||||
}
|
||||
|
||||
static void spawn_player(Scene_t* scene)
|
||||
{
|
||||
Entity_t* p_ent = add_entity(&scene->ent_manager, PLAYER_ENT_TAG);
|
||||
CBBox_t* p_bbox = add_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
|
||||
set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT);
|
||||
add_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
|
||||
CJump_t* p_cjump = add_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
|
||||
p_cjump->jump_speed = 680;
|
||||
p_cjump->jumps = 1;
|
||||
p_cjump->max_jumps = 1;
|
||||
p_cjump->jump_ready = true;
|
||||
add_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
add_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
|
||||
add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
|
||||
CHitBoxes_t* p_hitbox = add_component(&scene->ent_manager, p_ent, CHITBOXES_T);
|
||||
p_hitbox->n_boxes = 2;
|
||||
p_hitbox->boxes[0] = (Rectangle) {
|
||||
.x = 0,
|
||||
.y = -1,
|
||||
.width = p_bbox->size.x - 1,
|
||||
.height = p_bbox->size.y + 2,
|
||||
};
|
||||
p_hitbox->boxes[1] = (Rectangle) {
|
||||
.x = -1,
|
||||
.y = 0,
|
||||
.width = p_bbox->size.x + 2,
|
||||
.height = p_bbox->size.y - 1,
|
||||
};
|
||||
CSprite_t* p_cspr = add_component(&scene->ent_manager, p_ent, CSPRITE_T);
|
||||
p_cspr->sprite = get_sprite(&scene->engine->assets, "plr_stand");
|
||||
p_cspr->sprites_map = player_sprite_map;
|
||||
p_cspr->transition_func = &player_sprite_transition_func;
|
||||
}
|
||||
|
||||
static void toggle_block_system(Scene_t* scene)
|
||||
|
@ -422,7 +386,8 @@ void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
|
|||
|
||||
void init_level_scene(LevelScene_t* scene)
|
||||
{
|
||||
init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func, &level_do_action);
|
||||
//init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func, &level_do_action);
|
||||
init_scene(&scene->scene, &level_scene_render_func, &level_do_action);
|
||||
|
||||
init_level_scene_data(&scene->data);
|
||||
// insert level scene systems
|
||||
|
@ -458,6 +423,7 @@ void init_level_scene(LevelScene_t* scene)
|
|||
scene->data.tilemap.n_tiles = scene->data.tilemap.width * scene->data.tilemap.height;
|
||||
assert(scene->data.tilemap.n_tiles <= MAX_N_TILES);
|
||||
scene->data.tilemap.tiles = all_tiles;
|
||||
memset(scene->data.tile_sprites, 0, sizeof(scene->data.tile_sprites));
|
||||
for (size_t i = 0; i < MAX_N_TILES;i++)
|
||||
{
|
||||
all_tiles[i].solid = NOT_SOLID;
|
||||
|
@ -471,7 +437,7 @@ void init_level_scene(LevelScene_t* scene)
|
|||
all_tiles[tile_idx].tile_type = SOLID_TILE; // for testing
|
||||
}
|
||||
|
||||
spawn_player(&scene->scene);
|
||||
create_player(&scene->scene.ent_manager, &scene->scene.engine->assets);
|
||||
update_entity_manager(&scene->scene.ent_manager);
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
add_subdirectory(EC)
|
||||
add_library(lib_engine STATIC
|
||||
assets.c
|
||||
AABB.c
|
||||
gui.c
|
||||
engine.c
|
||||
)
|
||||
target_include_directories(lib_engine
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
target_link_libraries(lib_engine
|
||||
PUBLIC
|
||||
lib_EC
|
||||
)
|
|
@ -8,7 +8,16 @@ target_include_directories(lib_EC
|
|||
${CMAKE_CURRENT_LIST_DIR}
|
||||
${RAYLIB_DIR}/include
|
||||
)
|
||||
target_link_libraries(lib_EC
|
||||
PRIVATE
|
||||
raylib
|
||||
|
||||
target_link_directories(lib_EC
|
||||
PUBLIC
|
||||
${RAYLIB_DIR}/lib
|
||||
)
|
||||
target_link_libraries(lib_EC
|
||||
PUBLIC
|
||||
raylib
|
||||
sc_queue
|
||||
sc_map
|
||||
sc_array
|
||||
m
|
||||
)
|
|
@ -1,11 +1,17 @@
|
|||
#ifndef __COMPONENTS_H
|
||||
#define __COMPONENTS_H
|
||||
#include "raylib.h"
|
||||
#ifndef __ENTITY_H
|
||||
#define __ENTITY_H
|
||||
#include <stdint.h>
|
||||
#include "entity.h"
|
||||
// TODO: Look at sc to use macros to auto generate functions
|
||||
#include <stdbool.h>
|
||||
#include "raylib.h"
|
||||
#include "sc/map/sc_map.h"
|
||||
#include "sc/queue/sc_queue.h"
|
||||
|
||||
#define N_TAGS 4
|
||||
#define N_COMPONENTS 10
|
||||
#define MAX_COMP_POOL_SIZE 1024
|
||||
typedef struct EntityManager EntityManager_t;
|
||||
typedef struct Entity Entity_t;
|
||||
|
||||
typedef enum ComponentEnum {
|
||||
CBBOX_COMP_T,
|
||||
CTRANSFORM_COMP_T,
|
||||
|
@ -114,12 +120,19 @@ typedef struct Sprite {
|
|||
} Sprite_t;
|
||||
|
||||
typedef unsigned int (*sprite_transition_func_t)(Entity_t *ent); // Transition requires knowledge of the entity
|
||||
typedef struct _CSprite_t {
|
||||
const char * const *sprites_map; //Array of all sprite names associated
|
||||
typedef struct _SpriteRenderInfo
|
||||
{
|
||||
Sprite_t* sprite;
|
||||
sprite_transition_func_t transition_func;
|
||||
Vector2 offset;
|
||||
} SpriteRenderInfo_t;
|
||||
|
||||
typedef struct _CSprite_t {
|
||||
SpriteRenderInfo_t* sprites;
|
||||
sprite_transition_func_t transition_func;
|
||||
unsigned int current_idx;
|
||||
bool flip_x;
|
||||
bool flip_y;
|
||||
bool pause;
|
||||
} CSprite_t;
|
||||
|
||||
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
||||
|
@ -129,4 +142,42 @@ static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
|||
p_bbox->half_size.x = (unsigned int)(x / 2);
|
||||
p_bbox->half_size.y = (unsigned int)(y / 2);
|
||||
}
|
||||
#endif // __COMPONENTS_H
|
||||
|
||||
typedef enum EntityTag {
|
||||
NO_ENT_TAG,
|
||||
PLAYER_ENT_TAG,
|
||||
ENEMY_ENT_TAG,
|
||||
CRATES_ENT_TAG,
|
||||
} EntityTag_t;
|
||||
|
||||
struct Entity {
|
||||
unsigned long m_id;
|
||||
EntityTag_t m_tag;
|
||||
bool m_alive;
|
||||
unsigned long components[N_COMPONENTS];
|
||||
EntityManager_t* manager;
|
||||
};
|
||||
|
||||
struct EntityManager {
|
||||
// All fields are Read-Only
|
||||
struct sc_map_64v entities; // ent id : entity
|
||||
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}]
|
||||
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...]
|
||||
struct sc_queue_uint to_add;
|
||||
struct sc_queue_uint to_remove;
|
||||
};
|
||||
|
||||
void init_entity_manager(EntityManager_t* p_manager);
|
||||
void update_entity_manager(EntityManager_t* p_manager);
|
||||
void clear_entity_manager(EntityManager_t* p_manager);
|
||||
void free_entity_manager(EntityManager_t* p_manager);
|
||||
|
||||
Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
|
||||
void remove_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
|
||||
void* add_component(Entity_t *entity, ComponentEnum_t comp_type);
|
||||
void* get_component(Entity_t *entity, ComponentEnum_t comp_type);
|
||||
void remove_component(Entity_t* entity, ComponentEnum_t comp_type);
|
||||
|
||||
#endif // __ENTITY_H
|
|
@ -1,4 +1,4 @@
|
|||
#include "entManager.h"
|
||||
#include "mempool.h"
|
||||
|
||||
void init_entity_manager(EntityManager_t* p_manager)
|
||||
{
|
||||
|
@ -21,20 +21,19 @@ void update_entity_manager(EntityManager_t* p_manager)
|
|||
// It does not make new entities, but will free entity
|
||||
// New entities are assigned during add_entity
|
||||
unsigned long e_idx;
|
||||
unsigned long comp_type_idx;
|
||||
unsigned long comp_idx;
|
||||
|
||||
sc_queue_foreach (&p_manager->to_remove, e_idx)
|
||||
{
|
||||
Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx);
|
||||
if (!p_entity) continue;
|
||||
sc_map_foreach (&p_entity->components, comp_type_idx, comp_idx)
|
||||
for (size_t i = 0; i < N_COMPONENTS; ++i)
|
||||
{
|
||||
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx);
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], e_idx);
|
||||
if (p_entity->components[i] == MAX_COMP_POOL_SIZE) continue;
|
||||
|
||||
free_component_to_mempool((ComponentEnum_t)i, p_entity->components[i]);
|
||||
sc_map_del_64v(&p_manager->component_map[i], e_idx);
|
||||
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
|
||||
}
|
||||
sc_map_clear_64(&p_entity->components);
|
||||
free_entity_to_mempool(e_idx);
|
||||
sc_map_del_64v(&p_manager->entities, e_idx);
|
||||
}
|
||||
|
@ -84,6 +83,7 @@ Entity_t *add_entity(EntityManager_t* p_manager, EntityTag_t tag)
|
|||
{
|
||||
sc_queue_add_last(&p_manager->to_add, e_idx);
|
||||
}
|
||||
p_ent->manager = p_manager;
|
||||
return p_ent;
|
||||
}
|
||||
|
||||
|
@ -109,33 +109,31 @@ Entity_t* get_entity(EntityManager_t* p_manager, unsigned long id)
|
|||
|
||||
// Components are not expected to be removed
|
||||
// So, no need to extra steps to deal with iterator invalidation
|
||||
void* add_component(EntityManager_t* p_manager, Entity_t* p_entity, ComponentEnum_t comp_type)
|
||||
void* add_component(Entity_t* p_entity, ComponentEnum_t comp_type)
|
||||
{
|
||||
unsigned long comp_type_idx = (unsigned long)comp_type;
|
||||
unsigned long comp_idx = 0;
|
||||
void* p_comp = new_component_from_mempool(comp_type, &comp_idx);
|
||||
if (p_comp)
|
||||
{
|
||||
sc_map_put_64(&p_entity->components, comp_type_idx, comp_idx);
|
||||
sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
|
||||
p_entity->components[comp_type] = comp_idx;
|
||||
sc_map_put_64v(&p_entity->manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
|
||||
}
|
||||
return p_comp;
|
||||
}
|
||||
|
||||
void* get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
void* get_component(Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
{
|
||||
unsigned long comp_type_idx = (unsigned long)comp_type;
|
||||
void * p_comp = sc_map_get_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
//unsigned long comp_idx = sc_map_get_64(&p_entity->components, comp_type_idx);
|
||||
if (!sc_map_found(&p_manager->component_map[comp_type_idx])) return NULL;
|
||||
void * p_comp = sc_map_get_64v(&p_entity->manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
if (!sc_map_found(&p_entity->manager->component_map[comp_type_idx])) return NULL;
|
||||
return p_comp;
|
||||
}
|
||||
|
||||
void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
void remove_component(Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
{
|
||||
unsigned long comp_type_idx = (unsigned long)comp_type;
|
||||
unsigned long comp_idx = sc_map_del_64(&p_entity->components, comp_type_idx);
|
||||
if (!sc_map_found(&p_entity->components)) return;
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
free_component_to_mempool(comp_type, comp_idx);
|
||||
if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return;
|
||||
sc_map_del_64v(&p_entity->manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
free_component_to_mempool(comp_type, p_entity->components[comp_type]);
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
// Static allocate buffers
|
||||
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
||||
|
@ -118,7 +119,6 @@ void init_memory_pools(void)
|
|||
for (size_t i = 0; i < ent_mempool.max_size; ++i)
|
||||
{
|
||||
entity_buffer[i].m_id = i;
|
||||
sc_map_init_64(&entity_buffer[i].components, 16 ,0);
|
||||
ent_mempool.free_list.buffer[i] = i;
|
||||
}
|
||||
ent_mempool.free_list.count = ent_mempool.max_size;
|
||||
|
@ -138,10 +138,6 @@ void free_memory_pools(void)
|
|||
free(ent_mempool.use_list);
|
||||
cb_free(&ent_mempool.free_list);
|
||||
|
||||
for (int i = 0; i < MAX_COMP_POOL_SIZE; ++i)
|
||||
{
|
||||
sc_map_term_64(&entity_buffer[i].components);
|
||||
}
|
||||
pool_inited = false;
|
||||
}
|
||||
}
|
||||
|
@ -154,7 +150,10 @@ Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
|
|||
*e_idx_ptr = e_idx;
|
||||
ent_mempool.use_list[e_idx] = true;
|
||||
Entity_t* ent = entity_buffer + e_idx;
|
||||
sc_map_clear_64(&ent->components);
|
||||
for (size_t j = 0; j< N_COMPONENTS; j++)
|
||||
{
|
||||
ent->components[j] = MAX_COMP_POOL_SIZE;
|
||||
}
|
||||
ent->m_alive = true;
|
||||
ent->m_tag = NO_ENT_TAG;
|
||||
return ent;
|
|
@ -1,8 +1,6 @@
|
|||
#ifndef __MEMPOOL_H
|
||||
#define __MEMPOOL_H
|
||||
#include "entity.h"
|
||||
#include "components.h"
|
||||
#define MAX_COMP_POOL_SIZE 1024
|
||||
#include "EC.h"
|
||||
void init_memory_pools(void);
|
||||
void free_memory_pools(void);
|
||||
|
|
@ -5,23 +5,50 @@
|
|||
#define MAX_SPRITES 16
|
||||
#define MAX_SOUNDS 16
|
||||
#define MAX_FONTS 4
|
||||
#define MAX_NAME_LEN 32
|
||||
uint8_t free_idx[4] = {0};
|
||||
|
||||
// Hard limit number of
|
||||
static Texture2D textures[MAX_TEXTURES];
|
||||
static Font fonts[MAX_FONTS];
|
||||
static Sound sfx[MAX_SOUNDS];
|
||||
static Sprite_t sprites[MAX_SPRITES];
|
||||
typedef struct TextureData
|
||||
{
|
||||
Texture2D texture;
|
||||
char name[MAX_NAME_LEN];
|
||||
}TextureData_t;
|
||||
typedef struct SpriteData
|
||||
{
|
||||
Sprite_t sprite;
|
||||
char name[MAX_NAME_LEN];
|
||||
}SpriteData_t;
|
||||
typedef struct FontData
|
||||
{
|
||||
Font font;
|
||||
char name[MAX_NAME_LEN];
|
||||
}FontData_t;
|
||||
typedef struct SoundData
|
||||
{
|
||||
Sound sound;
|
||||
char name[MAX_NAME_LEN];
|
||||
}SoundData_t;
|
||||
|
||||
|
||||
static TextureData_t textures[MAX_TEXTURES];
|
||||
static FontData_t fonts[MAX_FONTS];
|
||||
static SoundData_t sfx[MAX_SOUNDS];
|
||||
static SpriteData_t sprites[MAX_SPRITES];
|
||||
|
||||
// Maybe need a circular buffer??
|
||||
Texture2D* add_texture(Assets_t* assets, const char* name, const char* path)
|
||||
{
|
||||
uint8_t tex_idx = free_idx[0];
|
||||
assert(tex_idx < MAX_TEXTURES);
|
||||
textures[tex_idx] = LoadTexture(path);
|
||||
sc_map_put_s64(&assets->m_textures, name, tex_idx);
|
||||
Texture2D tex = LoadTexture(path);
|
||||
if (tex.width == 0 || tex.height == 0) return NULL;
|
||||
|
||||
textures[tex_idx].texture = tex;
|
||||
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
|
||||
free_idx[0]++;
|
||||
return textures + tex_idx;
|
||||
return &textures[tex_idx].texture;
|
||||
}
|
||||
|
||||
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
|
||||
|
@ -29,30 +56,33 @@ Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
|
|||
uint8_t spr_idx = free_idx[1];
|
||||
assert(spr_idx < MAX_SPRITES);
|
||||
memset(sprites + spr_idx, 0, sizeof(Sprite_t));
|
||||
sprites[spr_idx].texture = texture;
|
||||
sc_map_put_s64(&assets->m_sprites, name, spr_idx);
|
||||
sprites[spr_idx].sprite.texture = texture;
|
||||
strncpy(sprites[spr_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_sprites, sprites[spr_idx].name, spr_idx);
|
||||
free_idx[1]++;
|
||||
return sprites + spr_idx;
|
||||
return &sprites[spr_idx].sprite;
|
||||
}
|
||||
|
||||
Sound* add_sound(Assets_t* assets, const char* name, const char* path)
|
||||
{
|
||||
uint8_t snd_idx = free_idx[2];
|
||||
assert(snd_idx < MAX_SOUNDS);
|
||||
sfx[snd_idx] = LoadSound(path);
|
||||
sc_map_put_s64(&assets->m_sounds, name, snd_idx);
|
||||
sfx[snd_idx].sound = LoadSound(path);
|
||||
strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx);
|
||||
free_idx[2]++;
|
||||
return sfx + snd_idx;
|
||||
return &sfx[snd_idx].sound;
|
||||
}
|
||||
|
||||
Font* add_font(Assets_t* assets, const char* name, const char* path)
|
||||
{
|
||||
uint8_t fnt_idx = free_idx[3];
|
||||
assert(fnt_idx < MAX_FONTS);
|
||||
fonts[fnt_idx] = LoadFont(path);
|
||||
sc_map_put_s64(&assets->m_fonts, name, fnt_idx);
|
||||
fonts[fnt_idx].font = LoadFont(path);
|
||||
strncpy(fonts[fnt_idx].name, name, MAX_NAME_LEN);
|
||||
sc_map_put_s64(&assets->m_fonts, fonts[fnt_idx].name, fnt_idx);
|
||||
free_idx[3]++;
|
||||
return fonts + fnt_idx;
|
||||
return &fonts[fnt_idx].font;
|
||||
}
|
||||
|
||||
void init_assets(Assets_t* assets)
|
||||
|
@ -86,7 +116,7 @@ Texture2D* get_texture(Assets_t* assets, const char* name)
|
|||
uint8_t tex_idx = sc_map_get_s64(&assets->m_textures, name);
|
||||
if (sc_map_found(&assets->m_textures))
|
||||
{
|
||||
return textures + tex_idx;
|
||||
return &textures[tex_idx].texture;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -96,7 +126,7 @@ Sprite_t* get_sprite(Assets_t* assets, const char* name)
|
|||
uint8_t spr_idx = sc_map_get_s64(&assets->m_sprites, name);
|
||||
if (sc_map_found(&assets->m_sprites))
|
||||
{
|
||||
return sprites + spr_idx;
|
||||
return &sprites[spr_idx].sprite;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -106,7 +136,7 @@ Sound* get_sound(Assets_t* assets, const char* name)
|
|||
uint8_t snd_idx = sc_map_get_s64(&assets->m_sounds, name);
|
||||
if (sc_map_found(&assets->m_sounds))
|
||||
{
|
||||
return sfx + snd_idx;
|
||||
return &sfx[snd_idx].sound;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -116,17 +146,17 @@ Font* get_font(Assets_t* assets, const char* name)
|
|||
uint8_t fnt_idx = sc_map_get_s64(&assets->m_fonts, name);
|
||||
if (sc_map_found(&assets->m_fonts))
|
||||
{
|
||||
return fonts + fnt_idx;
|
||||
return &fonts[fnt_idx].font;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void draw_sprite(Sprite_t* spr, Vector2 pos)
|
||||
void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x)
|
||||
{
|
||||
Rectangle rec = {
|
||||
spr->origin.x + spr->frame_size.x * spr->current_frame,
|
||||
spr->origin.y,
|
||||
spr->frame_size.x,
|
||||
spr->frame_size.x * (flip_x ? -1:1),
|
||||
spr->frame_size.y
|
||||
};
|
||||
DrawTextureRec(*spr->texture, rec, pos, WHITE);
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef __ASSETS_H
|
||||
#define __ASSETS_H
|
||||
#include "sc/map/sc_map.h"
|
||||
#include "components.h"
|
||||
#include "EC.h"
|
||||
#include "raylib.h"
|
||||
|
||||
typedef struct Assets
|
||||
|
@ -28,5 +28,5 @@ Sprite_t* get_sprite(Assets_t* assets, const char* name);
|
|||
Sound* get_sound(Assets_t* assets, const char* name);
|
||||
Font* get_font(Assets_t* assets, const char* name);
|
||||
|
||||
void draw_sprite(Sprite_t* spr, Vector2 pos);
|
||||
void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x);
|
||||
#endif // __ASSETS_H
|
|
@ -7,13 +7,14 @@ void change_scene(GameEngine_t* engine, unsigned int idx)
|
|||
engine->scenes[engine->curr_scene]->state = SCENE_PLAYING;
|
||||
}
|
||||
|
||||
void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func)
|
||||
//void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func)
|
||||
void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func)
|
||||
{
|
||||
sc_map_init_64(&scene->action_map, 32, 0);
|
||||
sc_array_init(&scene->systems);
|
||||
init_entity_manager(&scene->ent_manager);
|
||||
|
||||
scene->scene_type = scene_type;
|
||||
//scene->scene_type = scene_type;
|
||||
scene->render_function = render_func;
|
||||
scene->action_function = action_func;
|
||||
scene->state = SCENE_ENDED;
|
|
@ -1,6 +1,5 @@
|
|||
#ifndef __ENGINE_H
|
||||
#define __ENGINE_H
|
||||
#include "entManager.h"
|
||||
#include "actions.h"
|
||||
#include "sc/array/sc_array.h"
|
||||
#include "assets.h"
|
||||
|
@ -15,10 +14,10 @@ typedef struct GameEngine {
|
|||
} GameEngine_t;
|
||||
void change_scene(GameEngine_t* engine, unsigned int idx);
|
||||
|
||||
typedef enum SceneType {
|
||||
LEVEL_SCENE = 0,
|
||||
MENU_SCENE,
|
||||
}SceneType_t;
|
||||
//typedef enum SceneType {
|
||||
// LEVEL_SCENE = 0,
|
||||
// MENU_SCENE,
|
||||
//}SceneType_t;
|
||||
|
||||
typedef enum SceneState {
|
||||
SCENE_PLAYING = 0,
|
||||
|
@ -36,7 +35,7 @@ struct Scene {
|
|||
system_func_t render_function;
|
||||
action_func_t action_function;
|
||||
EntityManager_t ent_manager;
|
||||
SceneType_t scene_type;
|
||||
//SceneType_t scene_type;
|
||||
SceneState_t state;
|
||||
GameEngine_t *engine;
|
||||
};
|
||||
|
@ -46,7 +45,8 @@ extern void update_scene(Scene_t* scene);
|
|||
extern void render_scene(Scene_t* scene);
|
||||
extern void do_action(Scene_t* scene, ActionType_t action, bool pressed);
|
||||
|
||||
void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func);
|
||||
//void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func);
|
||||
void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func);
|
||||
void free_scene(Scene_t* scene);
|
||||
|
||||
#endif // __ENGINE_H
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __ENT_IMPL_H
|
||||
#define __ENT_IMPL_H
|
||||
#include "assets.h"
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets);
|
||||
Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets);
|
||||
Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool metal);
|
||||
|
||||
#endif // __ENT_IMPL_H
|
|
@ -1,7 +1,7 @@
|
|||
#include "game_systems.h"
|
||||
#include "AABB.h"
|
||||
#include "EC.h"
|
||||
#include "constants.h"
|
||||
#include "assets_maps.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
|
||||
|
@ -67,8 +67,8 @@ static bool check_collision(const CollideEntity_t* ent, TileGrid_t* grid, Entity
|
|||
{
|
||||
if (ent->idx == ent_idx) continue;
|
||||
Entity_t * p_ent = get_entity(p_manager, ent_idx);
|
||||
CTransform_t *p_ctransform = get_component(p_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t *p_bbox = get_component(p_manager, p_ent, CBBOX_COMP_T);
|
||||
CTransform_t *p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t *p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
if (p_bbox == NULL || p_ctransform == NULL) continue;
|
||||
//if (p_bbox->solid && !p_bbox->fragile)
|
||||
if (p_bbox->solid)
|
||||
|
@ -308,10 +308,10 @@ void player_movement_input_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
|
||||
{
|
||||
Entity_t* p_player = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T);
|
||||
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
|
||||
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T);
|
||||
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_player, CBBOX_COMP_T);
|
||||
CJump_t* p_cjump = get_component(p_player, CJUMP_COMP_T);
|
||||
CMovementState_t* p_mstate = get_component(p_player, CMOVEMENTSTATE_T);
|
||||
|
||||
// Ladder handling
|
||||
if (!p_pstate->ladder_state)
|
||||
|
@ -478,10 +478,10 @@ void player_bbox_update_system(Scene_t* scene)
|
|||
Entity_t* p_player;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
{
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T);
|
||||
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T);
|
||||
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_player, CBBOX_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_player, CPLAYERSTATE_T);
|
||||
CMovementState_t* p_mstate = get_component(p_player, CMOVEMENTSTATE_T);
|
||||
|
||||
Vector2 new_bbox;
|
||||
Vector2 offset;
|
||||
|
@ -527,7 +527,7 @@ void player_bbox_update_system(Scene_t* scene)
|
|||
p_ctransform->position = Vector2Add(p_ctransform->position, offset);
|
||||
}
|
||||
|
||||
CHitBoxes_t* p_hitbox = get_component(&scene->ent_manager, p_player, CHITBOXES_T);
|
||||
CHitBoxes_t* p_hitbox = get_component(p_player, CHITBOXES_T);
|
||||
p_hitbox->boxes[0].height = p_bbox->size.y + 2;
|
||||
//p_hitbox->boxes[1].y = p_bbox->size.y / 4;
|
||||
p_hitbox->boxes[1].height = p_bbox->size.y - 1;
|
||||
|
@ -548,7 +548,7 @@ void tile_collision_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CBBOX_COMP_T], ent_idx, p_bbox)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
// Get the occupied tiles
|
||||
// For each tile, loop through the entities, check collision and move
|
||||
// exclude self
|
||||
|
@ -588,10 +588,10 @@ void tile_collision_system(Scene_t* scene)
|
|||
Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
|
||||
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
|
||||
if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check
|
||||
CBBox_t *p_other_bbox = get_component(&scene->ent_manager, p_other_ent, CBBOX_COMP_T);
|
||||
CBBox_t *p_other_bbox = get_component(p_other_ent, CBBOX_COMP_T);
|
||||
if (p_other_bbox == NULL) continue;
|
||||
|
||||
CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t *p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T);
|
||||
check_collision_and_move(
|
||||
&scene->ent_manager, &tilemap, ent_idx,
|
||||
p_ctransform, p_bbox->size, p_other_ct->position,
|
||||
|
@ -699,7 +699,7 @@ void friction_coefficient_update_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ct)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
|
||||
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
|
||||
|
||||
|
||||
// Friction
|
||||
|
@ -718,7 +718,7 @@ void friction_coefficient_update_system(Scene_t* scene)
|
|||
p_ct->fric_coeff = (Vector2){-3.3, -1};
|
||||
}
|
||||
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
|
||||
if (p_pstate != NULL && (p_pstate->is_crouch & 1))
|
||||
{
|
||||
p_ct->fric_coeff.x -= 4;
|
||||
|
@ -734,8 +734,8 @@ void global_external_forces_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
|
||||
if (!(p_mstate->ground_state & 1))
|
||||
{
|
||||
|
@ -791,7 +791,7 @@ void global_external_forces_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
if (p_pstate->ladder_state)
|
||||
{
|
||||
p_ctransform->accel = (Vector2){0,0};
|
||||
|
@ -839,9 +839,9 @@ void player_ground_air_transition_system(Scene_t* scene)
|
|||
Entity_t* p_player;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
{
|
||||
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
|
||||
CMovementState_t* p_mstate = get_component(&scene->ent_manager, p_player, CMOVEMENTSTATE_T);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T);
|
||||
CJump_t* p_cjump = get_component(p_player, CJUMP_COMP_T);
|
||||
CMovementState_t* p_mstate = get_component(p_player, CMOVEMENTSTATE_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_player, CPLAYERSTATE_T);
|
||||
|
||||
// Handle Ground<->Air Transition
|
||||
bool in_water = (p_mstate->water_state & 1);
|
||||
|
@ -875,8 +875,8 @@ void state_transition_update_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
if (p_ctransform == NULL || p_bbox == NULL) continue;
|
||||
|
||||
bool on_ground = check_on_ground(
|
||||
|
@ -928,9 +928,9 @@ void update_tilemap_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CTILECOORD_COMP_T], ent_idx, p_tilecoord)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
if (p_ctransform == NULL) continue;
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
if (p_bbox == NULL) continue;
|
||||
|
||||
// Update tilemap position
|
||||
|
@ -975,7 +975,7 @@ void hitbox_update_system(Scene_t* scene)
|
|||
sc_map_foreach(&scene->ent_manager.component_map[CHITBOXES_T], ent_idx, p_hitbox)
|
||||
{
|
||||
Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
for (uint8_t i = 0; i < p_hitbox->n_boxes; ++i)
|
||||
{
|
||||
Vector2 hitbox_pos = {
|
||||
|
@ -1004,9 +1004,9 @@ void hitbox_update_system(Scene_t* scene)
|
|||
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
|
||||
if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check
|
||||
|
||||
CHurtbox_t* p_other_hurtbox = get_component(&scene->ent_manager, p_other_ent, CHURTBOX_T);
|
||||
CHurtbox_t* p_other_hurtbox = get_component(p_other_ent, CHURTBOX_T);
|
||||
if (p_other_hurtbox == NULL) continue;
|
||||
CTransform_t* p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T);
|
||||
Vector2 hurtbox_pos = Vector2Add(p_other_ct->position, p_other_hurtbox->offset);
|
||||
|
||||
Vector2 overlap;
|
||||
|
@ -1021,8 +1021,8 @@ void hitbox_update_system(Scene_t* scene)
|
|||
if (p_other_ent->m_tag == CRATES_ENT_TAG)
|
||||
{
|
||||
|
||||
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
|
||||
if (
|
||||
// TODO: Check Material of the crates
|
||||
p_ctransform->position.y + p_bbox->size.y <= p_other_ct->position.y
|
||||
|
@ -1032,14 +1032,14 @@ void hitbox_update_system(Scene_t* scene)
|
|||
if (p_pstate->jump_pressed)
|
||||
{
|
||||
p_ctransform->velocity.y = -600;
|
||||
CJump_t * p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
|
||||
CJump_t * p_cjump = get_component(p_ent, CJUMP_COMP_T);
|
||||
p_cjump->short_hop = false;
|
||||
p_cjump->jumped = true;
|
||||
}
|
||||
}
|
||||
|
||||
CTileCoord_t* p_tilecoord = get_component(
|
||||
&scene->ent_manager, p_other_ent, CTILECOORD_COMP_T
|
||||
p_other_ent, CTILECOORD_COMP_T
|
||||
);
|
||||
|
||||
for (size_t i = 0;i < p_tilecoord->n_tiles; ++i)
|
||||
|
@ -1072,22 +1072,19 @@ void sprite_animation_system(Scene_t* scene)
|
|||
unsigned int spr_idx = p_cspr->transition_func(p_ent);
|
||||
if (p_cspr->current_idx != spr_idx)
|
||||
{
|
||||
Sprite_t* new_spr = get_sprite(&scene->engine->assets, p_cspr->sprites_map[spr_idx]);
|
||||
if (new_spr != NULL)
|
||||
{
|
||||
p_cspr->sprite = new_spr;
|
||||
p_cspr->current_idx = spr_idx;
|
||||
p_cspr->sprite->current_frame = 0;
|
||||
}
|
||||
p_cspr->current_idx = spr_idx;
|
||||
p_cspr->sprites[spr_idx].sprite->current_frame = 0;
|
||||
}
|
||||
}
|
||||
if (p_cspr->pause) return;
|
||||
SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
|
||||
// Animate it (handle frame count)
|
||||
p_cspr->sprite->elapsed++;
|
||||
if (p_cspr->sprite->elapsed == p_cspr->sprite->speed)
|
||||
spr.sprite->elapsed++;
|
||||
if (spr.sprite->elapsed == spr.sprite->speed)
|
||||
{
|
||||
p_cspr->sprite->current_frame++;
|
||||
p_cspr->sprite->current_frame %= p_cspr->sprite->frame_count;
|
||||
p_cspr->sprite->elapsed = 0;
|
||||
spr.sprite->current_frame++;
|
||||
spr.sprite->current_frame %= spr.sprite->frame_count;
|
||||
spr.sprite->elapsed = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1100,7 +1097,7 @@ void camera_update_system(Scene_t* scene)
|
|||
const int height = lvl_scene->data.game_rec.height;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
{
|
||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
||||
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
|
||||
lvl_scene->data.cam.offset = (Vector2){ width/2.0f, height/2.0f };
|
||||
lvl_scene->data.cam.target = p_ctransform->position;
|
||||
}
|
||||
|
@ -1134,8 +1131,3 @@ void term_level_scene_data(LevelSceneData_t* data)
|
|||
//sc_map_term_32(&data->collision_events);
|
||||
UnloadRenderTexture(data->game_viewport); // Unload render texture
|
||||
}
|
||||
|
||||
unsigned int player_sprite_transition_func(Entity_t* ent)
|
||||
{
|
||||
return SPR_PLAYER_RUN;
|
||||
}
|
|
@ -16,6 +16,4 @@ void update_tilemap_system(Scene_t* scene);
|
|||
void hitbox_update_system(Scene_t* scene);
|
||||
void sprite_animation_system(Scene_t* scene);
|
||||
void camera_update_system(Scene_t* scene);
|
||||
|
||||
unsigned int player_sprite_transition_func(Entity_t* ent);
|
||||
#endif // __GAME_SYSTEMS_H
|
|
@ -0,0 +1,20 @@
|
|||
#include "ent_impl.h"
|
||||
#include "constants.h"
|
||||
|
||||
Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool metal)
|
||||
{
|
||||
Entity_t* p_crate = add_entity(ent_manager, CRATES_ENT_TAG);
|
||||
CBBox_t* p_bbox = add_component(p_crate, CBBOX_COMP_T);
|
||||
|
||||
set_bbox(p_bbox, TILE_SIZE, TILE_SIZE);
|
||||
p_bbox->solid = true;
|
||||
p_bbox->fragile = !metal;
|
||||
|
||||
add_component(p_crate, CTRANSFORM_COMP_T);
|
||||
add_component(p_crate, CMOVEMENTSTATE_T);
|
||||
add_component(p_crate, CTILECOORD_COMP_T);
|
||||
CHurtbox_t* p_hurtbox = add_component(p_crate, CHURTBOX_T);
|
||||
p_hurtbox->size = p_bbox->size;
|
||||
p_hurtbox->fragile = !metal;
|
||||
return p_crate;
|
||||
}
|
|
@ -118,7 +118,8 @@ static void gui_loop(Scene_t* scene)
|
|||
|
||||
void init_menu_scene(MenuScene_t* scene)
|
||||
{
|
||||
init_scene(&scene->scene, MENU_SCENE, &menu_scene_render_func, &menu_do_action);
|
||||
//init_scene(&scene->scene, MENU_SCENE, &menu_scene_render_func, &menu_do_action);
|
||||
init_scene(&scene->scene, &menu_scene_render_func, &menu_do_action);
|
||||
|
||||
sc_array_add(&scene->scene.systems, &gui_loop);
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
#include "ent_impl.h"
|
||||
#include "constants.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "raymath.h"
|
||||
|
||||
#define N_PLAYER_SPRITES 7
|
||||
enum PlayerSpriteEnum
|
||||
{
|
||||
SPR_PLAYER_STAND = 0,
|
||||
SPR_PLAYER_RUN,
|
||||
SPR_PLAYER_JUMP,
|
||||
SPR_PLAYER_FALL,
|
||||
SPR_PLAYER_LADDER,
|
||||
SPR_PLAYER_CROUCH,
|
||||
SPR_PLAYER_SWIM,
|
||||
};
|
||||
|
||||
static SpriteRenderInfo_t player_sprite_map[N_PLAYER_SPRITES] = {0};
|
||||
|
||||
static unsigned int player_sprite_transition_func(Entity_t* ent)
|
||||
{
|
||||
CTransform_t* p_ctrans = get_component(ent, CTRANSFORM_COMP_T);
|
||||
CMovementState_t* p_move = get_component(ent, CMOVEMENTSTATE_T);
|
||||
CSprite_t* p_spr = get_component(ent, CSPRITE_T);
|
||||
CPlayerState_t* p_plr = get_component(ent, CPLAYERSTATE_T);
|
||||
if (p_ctrans->velocity.x > 0) p_spr->flip_x = true;
|
||||
else if (p_ctrans->velocity.x < 0) p_spr->flip_x = false;
|
||||
|
||||
p_spr->pause = false;
|
||||
|
||||
if (p_move->ground_state & 1)
|
||||
{
|
||||
if (p_plr->is_crouch)
|
||||
{
|
||||
return SPR_PLAYER_CROUCH;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Vector2LengthSqr(p_ctrans->velocity) > 1000.0f)
|
||||
{
|
||||
return SPR_PLAYER_RUN;
|
||||
}
|
||||
return SPR_PLAYER_STAND;
|
||||
}
|
||||
}
|
||||
else if (p_plr->ladder_state)
|
||||
{
|
||||
p_spr->pause = Vector2LengthSqr(p_ctrans->velocity) < 10.0f;
|
||||
return SPR_PLAYER_LADDER;
|
||||
}
|
||||
else if (p_move->water_state & 1)
|
||||
{
|
||||
return SPR_PLAYER_SWIM;
|
||||
}
|
||||
return (p_ctrans->velocity.y < 0) ? SPR_PLAYER_JUMP : SPR_PLAYER_FALL;
|
||||
}
|
||||
|
||||
Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets)
|
||||
{
|
||||
Entity_t* p_ent = add_entity(ent_manager, PLAYER_ENT_TAG);
|
||||
CBBox_t* p_bbox = add_component(p_ent, CBBOX_COMP_T);
|
||||
|
||||
set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT);
|
||||
add_component(p_ent, CTRANSFORM_COMP_T);
|
||||
|
||||
CJump_t* p_cjump = add_component(p_ent, CJUMP_COMP_T);
|
||||
p_cjump->jump_speed = 680;
|
||||
p_cjump->jumps = 1;
|
||||
p_cjump->max_jumps = 1;
|
||||
p_cjump->jump_ready = true;
|
||||
add_component(p_ent, CPLAYERSTATE_T);
|
||||
add_component(p_ent, CTILECOORD_COMP_T);
|
||||
add_component(p_ent, CMOVEMENTSTATE_T);
|
||||
CHitBoxes_t* p_hitbox = add_component(p_ent, CHITBOXES_T);
|
||||
p_hitbox->n_boxes = 2;
|
||||
p_hitbox->boxes[0] = (Rectangle) {
|
||||
.x = 0,
|
||||
.y = -1,
|
||||
.width = p_bbox->size.x - 1,
|
||||
.height = p_bbox->size.y + 2,
|
||||
};
|
||||
p_hitbox->boxes[1] = (Rectangle) {
|
||||
.x = -1,
|
||||
.y = 0,
|
||||
.width = p_bbox->size.x + 2,
|
||||
.height = p_bbox->size.y - 1,
|
||||
};
|
||||
CSprite_t* p_cspr = add_component(p_ent, CSPRITE_T);
|
||||
p_cspr->sprites = player_sprite_map;
|
||||
p_cspr->transition_func = &player_sprite_transition_func;
|
||||
|
||||
return p_ent;
|
||||
}
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets)
|
||||
{
|
||||
static bool already_init = false;
|
||||
|
||||
if (already_init) return false;
|
||||
|
||||
FILE* in_file = fopen(info_file, "r");
|
||||
if (in_file == NULL)
|
||||
{
|
||||
printf("Unable to open file %s\n", info_file);
|
||||
return false;
|
||||
}
|
||||
|
||||
char buffer[256];
|
||||
char* tmp;
|
||||
size_t line_num = 0;
|
||||
uint8_t i = 0;
|
||||
while (true)
|
||||
{
|
||||
tmp = fgets(buffer, 256, in_file);
|
||||
if (tmp == NULL) break;
|
||||
tmp[strcspn(tmp, "\r\n")] = '\0';
|
||||
|
||||
if (i == N_PLAYER_SPRITES) break;
|
||||
|
||||
char* name = strtok(buffer, ":");
|
||||
char* info_str = strtok(NULL, ":");
|
||||
if (name == NULL || info_str == NULL) return false;
|
||||
|
||||
while(*name == ' ' || *name == '\t') name++;
|
||||
while(*info_str == ' ' || *info_str == '\t') info_str++;
|
||||
|
||||
Vector2 offset;
|
||||
int data_count = sscanf(
|
||||
info_str, "%f,%f",
|
||||
&offset.x, &offset.y
|
||||
);
|
||||
if (data_count !=2)
|
||||
{
|
||||
printf("Unable to parse info for player at line %lu\n", line_num);
|
||||
return false;
|
||||
}
|
||||
Sprite_t* spr = get_sprite(assets, name);
|
||||
player_sprite_map[i].sprite = spr;
|
||||
player_sprite_map[i].offset = offset;
|
||||
i++;
|
||||
}
|
||||
already_init = true;
|
||||
return true;
|
||||
}
|
|
@ -17,7 +17,7 @@ typedef enum TileType {
|
|||
ONEWAY_TILE,
|
||||
LADDER
|
||||
} TileType_t;
|
||||
|
||||
#define MAX_TILE_TYPES 4
|
||||
typedef enum SolidType
|
||||
{
|
||||
NOT_SOLID = 0,
|
||||
|
@ -45,6 +45,7 @@ typedef struct LevelSceneData {
|
|||
RenderTexture2D game_viewport;
|
||||
Rectangle game_rec;
|
||||
Camera2D cam;
|
||||
Sprite_t* tile_sprites[MAX_TILE_TYPES];
|
||||
}LevelSceneData_t;
|
||||
|
||||
typedef struct LevelScene {
|
Loading…
Reference in New Issue