Compare commits
6 Commits
e1a7774bfb
...
9c2e21f4d2
Author | SHA1 | Date |
---|---|---|
|
9c2e21f4d2 | |
|
b1a6430eb7 | |
|
d2de5cfed9 | |
|
237bee41a9 | |
|
bc7f9ef777 | |
|
4478a512a3 |
|
@ -2,3 +2,4 @@
|
|||
build/
|
||||
compile_commands.json
|
||||
res/
|
||||
.gdb_history
|
||||
|
|
|
@ -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,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
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "game_systems.h"
|
||||
#include "constants.h"
|
||||
#include "ent_impl.h"
|
||||
#include "mempool.h"
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include <stdio.h>
|
||||
|
@ -52,7 +53,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
|
||||
if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL)
|
||||
{
|
||||
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y});
|
||||
draw_sprite(data->tile_sprites[tilemap.tiles[i].tile_type], (Vector2){x,y}, false);
|
||||
}
|
||||
else if (tilemap.tiles[i].tile_type == SOLID_TILE)
|
||||
{
|
||||
|
@ -78,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)
|
||||
{
|
||||
|
@ -93,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)
|
||||
|
@ -118,14 +119,14 @@ 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)
|
||||
{
|
||||
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);
|
||||
draw_sprite(spr.sprite, pos, p_cspr->flip_x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -177,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);
|
||||
|
@ -214,7 +215,7 @@ 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 = create_crate(&scene->ent_manager, &scene->engine->assets, metal);
|
||||
|
||||
CTransform_t* p_ctransform = get_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;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
#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,
|
||||
|
@ -117,14 +124,14 @@ typedef struct _SpriteRenderInfo
|
|||
{
|
||||
Sprite_t* sprite;
|
||||
Vector2 offset;
|
||||
bool flip_x;
|
||||
bool flip_y;
|
||||
} 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;
|
||||
} CSprite_t;
|
||||
|
||||
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
||||
|
@ -134,4 +141,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)
|
||||
{
|
||||
|
@ -83,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;
|
||||
}
|
||||
|
||||
|
@ -108,7 +109,7 @@ 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;
|
||||
|
@ -116,23 +117,23 @@ void* add_component(EntityManager_t* p_manager, Entity_t* p_entity, ComponentEnu
|
|||
if (p_comp)
|
||||
{
|
||||
p_entity->components[comp_type] = comp_idx;
|
||||
sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
|
||||
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);
|
||||
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;
|
||||
if (p_entity->components[comp_type] == MAX_COMP_POOL_SIZE) return;
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
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]);
|
||||
}
|
||||
|
|
|
@ -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,21 +0,0 @@
|
|||
#ifndef __ENTITY_H
|
||||
#define __ENTITY_H
|
||||
#include <stdbool.h>
|
||||
|
||||
#define N_TAGS 4
|
||||
#define N_COMPONENTS 10
|
||||
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;
|
||||
unsigned long components[N_COMPONENTS];
|
||||
|
||||
} Entity_t;
|
||||
#endif // __ENTITY_H
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -151,12 +151,12 @@ Font* get_font(Assets_t* assets, const char* name)
|
|||
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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#ifndef __ENT_IMPL_H
|
||||
#define __ENT_IMPL_H
|
||||
#include "entManager.h"
|
||||
#include "assets.h"
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "game_systems.h"
|
||||
#include "AABB.h"
|
||||
#include "EC.h"
|
||||
#include "constants.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -66,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)
|
||||
|
@ -307,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)
|
||||
|
@ -477,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;
|
||||
|
@ -526,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;
|
||||
|
@ -547,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
|
||||
|
@ -587,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,
|
||||
|
@ -698,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
|
||||
|
@ -717,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;
|
||||
|
@ -733,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))
|
||||
{
|
||||
|
@ -790,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};
|
||||
|
@ -838,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);
|
||||
|
@ -874,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(
|
||||
|
@ -927,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
|
||||
|
@ -974,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 = {
|
||||
|
@ -1003,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;
|
||||
|
@ -1020,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
|
||||
|
@ -1031,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)
|
||||
|
@ -1095,7 +1096,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;
|
||||
}
|
||||
|
|
|
@ -4,16 +4,16 @@
|
|||
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(ent_manager, p_crate, CBBOX_COMP_T);
|
||||
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(ent_manager, p_crate, CTRANSFORM_COMP_T);
|
||||
add_component(ent_manager, p_crate, CMOVEMENTSTATE_T);
|
||||
add_component(ent_manager, p_crate, CTILECOORD_COMP_T);
|
||||
CHurtbox_t* p_hurtbox = add_component(ent_manager, p_crate, CHURTBOX_T);
|
||||
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;
|
||||
|
|
|
@ -2,38 +2,55 @@
|
|||
#include "constants.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "raymath.h"
|
||||
|
||||
#define N_PLAYER_SPRITES 2
|
||||
#define N_PLAYER_SPRITES 4
|
||||
enum PlayerSpriteEnum
|
||||
{
|
||||
SPR_PLAYER_STAND = 0,
|
||||
SPR_PLAYER_RUN
|
||||
SPR_PLAYER_RUN,
|
||||
SPR_PLAYER_JUMP,
|
||||
SPR_PLAYER_FALL,
|
||||
};
|
||||
|
||||
static SpriteRenderInfo_t player_sprite_map[N_PLAYER_SPRITES] = {0};
|
||||
|
||||
static unsigned int player_sprite_transition_func(Entity_t* ent)
|
||||
{
|
||||
return SPR_PLAYER_RUN;
|
||||
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);
|
||||
if (p_ctrans->velocity.x > 0) p_spr->flip_x = true;
|
||||
else if (p_ctrans->velocity.x < 0) p_spr->flip_x = false;
|
||||
|
||||
if (p_move->ground_state & 1)
|
||||
{
|
||||
if (Vector2LengthSqr(p_ctrans->velocity) > 1000.0f)
|
||||
{
|
||||
return SPR_PLAYER_RUN;
|
||||
}
|
||||
return SPR_PLAYER_STAND;
|
||||
}
|
||||
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(ent_manager, p_ent, CBBOX_COMP_T);
|
||||
CBBox_t* p_bbox = add_component(p_ent, CBBOX_COMP_T);
|
||||
|
||||
set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT);
|
||||
add_component(ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||
add_component(p_ent, CTRANSFORM_COMP_T);
|
||||
|
||||
CJump_t* p_cjump = add_component(ent_manager, p_ent, CJUMP_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(ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
add_component(ent_manager, p_ent, CTILECOORD_COMP_T);
|
||||
add_component(ent_manager, p_ent, CMOVEMENTSTATE_T);
|
||||
CHitBoxes_t* p_hitbox = add_component(ent_manager, p_ent, CHITBOXES_T);
|
||||
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,
|
||||
|
@ -47,7 +64,7 @@ Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets)
|
|||
.width = p_bbox->size.x + 2,
|
||||
.height = p_bbox->size.y - 1,
|
||||
};
|
||||
CSprite_t* p_cspr = add_component(ent_manager, p_ent, CSPRITE_T);
|
||||
CSprite_t* p_cspr = add_component(p_ent, CSPRITE_T);
|
||||
p_cspr->sprites = player_sprite_map;
|
||||
p_cspr->transition_func = &player_sprite_transition_func;
|
||||
|
||||
|
|
Loading…
Reference in New Issue