Compare commits
6 Commits
5b3da5c94f
...
68bb9f941c
Author | SHA1 | Date |
---|---|---|
|
68bb9f941c | |
|
25870309c0 | |
|
3c02068ca6 | |
|
0169a90ee8 | |
|
b2a5e29ce4 | |
|
1ba786a91e |
12
components.h
12
components.h
|
@ -1,13 +1,15 @@
|
|||
#ifndef __COMPONENTS_H
|
||||
#define __COMPONENTS_H
|
||||
#include "raylib.h"
|
||||
#include <stdint.h>
|
||||
// TODO: Look at sc to use macros to auto generate functions
|
||||
|
||||
#define N_COMPONENTS 4
|
||||
#define N_COMPONENTS 5
|
||||
enum ComponentEnum
|
||||
{
|
||||
CBBOX_COMP_T,
|
||||
CTRANSFORM_COMP_T,
|
||||
CTILECOORD_COMP_T,
|
||||
CJUMP_COMP_T,
|
||||
CPLAYERSTATE_T
|
||||
};
|
||||
|
@ -26,7 +28,8 @@ typedef struct _CTransform_t
|
|||
Vector2 position;
|
||||
Vector2 velocity;
|
||||
Vector2 accel;
|
||||
bool on_ground;
|
||||
uint8_t ground_state:2;
|
||||
uint8_t water_state:2;
|
||||
}CTransform_t;
|
||||
|
||||
// This is to store the occupying tiles
|
||||
|
@ -56,8 +59,9 @@ typedef enum PlayerState
|
|||
|
||||
typedef struct _CPlayerState_t
|
||||
{
|
||||
unsigned int is_crouch: 1;
|
||||
unsigned int in_water:1;
|
||||
Vector2 player_dir;
|
||||
uint8_t jump_pressed: 1;
|
||||
uint8_t is_crouch: 1;
|
||||
}CPlayerState_t;
|
||||
|
||||
|
||||
|
|
18
entManager.c
18
entManager.c
|
@ -97,6 +97,13 @@ void remove_entity(EntityManager_t *p_manager, unsigned long id)
|
|||
sc_queue_add_last(&p_manager->to_remove, id);
|
||||
}
|
||||
|
||||
Entity_t *get_entity(EntityManager_t *p_manager, unsigned long id)
|
||||
{
|
||||
Entity_t *p_entity = sc_map_get_64v(&p_manager->entities, id);
|
||||
if(!sc_map_found(&p_manager->entities)) return NULL;
|
||||
return p_entity;
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
@ -107,7 +114,7 @@ void *add_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
|
|||
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], comp_idx, p_comp);
|
||||
sc_map_put_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id, p_comp);
|
||||
}
|
||||
return p_comp;
|
||||
}
|
||||
|
@ -115,9 +122,10 @@ void *add_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnu
|
|||
void *get_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
{
|
||||
unsigned long comp_type_idx = (unsigned long)comp_type;
|
||||
unsigned long comp_idx = sc_map_get_64(&p_entity->components, comp_type_idx);
|
||||
if (!sc_map_found(&p_entity->components)) return NULL;
|
||||
return sc_map_get_64v(&p_manager->component_map[comp_type_idx], comp_idx);
|
||||
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;
|
||||
return p_comp;
|
||||
}
|
||||
|
||||
void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentEnum_t comp_type)
|
||||
|
@ -125,6 +133,6 @@ void remove_component(EntityManager_t *p_manager, Entity_t *p_entity, ComponentE
|
|||
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], comp_idx);
|
||||
sc_map_del_64v(&p_manager->component_map[comp_type_idx], p_entity->m_id);
|
||||
free_component_to_mempool(comp_type, comp_idx);
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ 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);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
||||
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
|
||||
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
|
||||
static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE];
|
||||
static CJump_t cjump_buffer[1]; // Only player is expected to have this
|
||||
static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this
|
||||
|
||||
|
@ -29,6 +30,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
|
|||
{
|
||||
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}},
|
||||
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}},
|
||||
{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}},
|
||||
{cjump_buffer, 1, sizeof(CJump_t), NULL, {0}},
|
||||
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}},
|
||||
};
|
||||
|
|
257
scene_impl.c
257
scene_impl.c
|
@ -9,7 +9,7 @@ static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
|
|||
static Tile_t all_tiles[MAX_N_TILES] = {0};
|
||||
|
||||
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
||||
static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 0.7};
|
||||
static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1};
|
||||
|
||||
static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width)
|
||||
{
|
||||
|
@ -237,7 +237,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
DrawText(buffer, (tilemap.width + 3) * TILE_SIZE + 1, 60, 12, BLACK);
|
||||
sprintf(buffer, "Crouch: %s", p_pstate->is_crouch? "YES":"NO");
|
||||
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
|
||||
sprintf(buffer, "Water: %s", p_pstate->in_water? "YES":"NO");
|
||||
sprintf(buffer, "Water: %s", p_ct->water_state & 1? "YES":"NO");
|
||||
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 120, 12, BLACK);
|
||||
}
|
||||
}
|
||||
|
@ -248,32 +248,33 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
TileGrid_t tilemap = data->tilemap;
|
||||
|
||||
// Deal with player acceleration/velocity via inputs first
|
||||
float mag = Vector2Length(data->player_dir);
|
||||
mag = (mag == 0)? 1 : mag;
|
||||
Entity_t * p_player;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
CPlayerState_t* p_pstate;
|
||||
unsigned int ent_idx;
|
||||
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
|
||||
{
|
||||
float mag = Vector2Length(p_pstate->player_dir);
|
||||
mag = (mag == 0)? 1 : mag;
|
||||
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);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T);
|
||||
p_pstate->is_crouch = data->crouch_pressed;
|
||||
p_ctransform->accel.x = 0;
|
||||
p_ctransform->accel.y = 0;
|
||||
if (!p_pstate->in_water)
|
||||
|
||||
bool in_water = (p_ctransform->water_state & 1);
|
||||
if (!in_water)
|
||||
{
|
||||
data->player_dir.y = 0;
|
||||
p_ctransform->accel = Vector2Scale(Vector2Normalize(data->player_dir), MOVE_ACCEL/1.2);
|
||||
p_pstate->player_dir.y = 0;
|
||||
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL/1.2);
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->accel = Vector2Scale(Vector2Normalize(data->player_dir), MOVE_ACCEL);
|
||||
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL);
|
||||
}
|
||||
|
||||
// Short Hop
|
||||
if (p_cjump->jumped && p_ctransform->velocity.y < 0)
|
||||
{
|
||||
if (!data->jumped_pressed && !p_cjump->short_hop)
|
||||
if (!p_pstate->jump_pressed && !p_cjump->short_hop)
|
||||
{
|
||||
p_cjump->short_hop = true;
|
||||
p_ctransform->velocity.y /= 2;
|
||||
|
@ -281,37 +282,18 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
}
|
||||
|
||||
|
||||
if (!p_ctransform->on_ground)
|
||||
{
|
||||
// Only apply upthrust if center is in water
|
||||
// If need more accuracy, need to find area of overlap
|
||||
if (p_pstate->in_water)
|
||||
{
|
||||
unsigned int tile_idx = get_tile_idx(
|
||||
p_ctransform->position.x + p_bbox->size.x / 2,
|
||||
p_ctransform->position.y + p_bbox->size.y / 2,
|
||||
tilemap.width
|
||||
);
|
||||
|
||||
if (tilemap.tiles[tile_idx].water_level > 0)
|
||||
{
|
||||
p_ctransform->accel = Vector2Add(p_ctransform->accel, UPTHRUST);
|
||||
}
|
||||
}
|
||||
p_ctransform->accel = Vector2Add(p_ctransform->accel, GRAVITY);
|
||||
}
|
||||
//else
|
||||
{
|
||||
if (p_cjump->cooldown_timer > 0) p_cjump->cooldown_timer--;
|
||||
// Jumps is possible as long as you have a jump
|
||||
|
||||
// Check if possible to jump when jump is pressed
|
||||
if (data->jumped_pressed && p_cjump->jumps > 0 && p_cjump->cooldown_timer == 0)
|
||||
if (p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->cooldown_timer == 0)
|
||||
{
|
||||
bool jump_valid = true;
|
||||
|
||||
// Check Jump from crouch
|
||||
if (p_pstate->is_crouch)
|
||||
if(p_pstate->is_crouch)
|
||||
{
|
||||
Vector2 test_pos = p_ctransform->position;
|
||||
Vector2 test_bbox = {PLAYER_WIDTH, PLAYER_HEIGHT};
|
||||
|
@ -324,7 +306,7 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
// Jump okay
|
||||
if (jump_valid)
|
||||
{
|
||||
if (!p_pstate->in_water)
|
||||
if (!in_water)
|
||||
{
|
||||
p_ctransform->velocity.y -= p_cjump->jump_speed;
|
||||
}
|
||||
|
@ -334,7 +316,6 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
}
|
||||
|
||||
p_cjump->jumped = true;
|
||||
p_cjump->jumps--;
|
||||
p_cjump->cooldown_timer = 15;
|
||||
}
|
||||
}
|
||||
|
@ -342,7 +323,7 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
}
|
||||
|
||||
// Friction
|
||||
if (p_pstate->in_water)
|
||||
if (in_water)
|
||||
{
|
||||
// Apply water friction
|
||||
// Consistent in all direction
|
||||
|
@ -360,10 +341,9 @@ static void player_movement_input_system(Scene_t* scene)
|
|||
p_ctransform->accel.x += p_ctransform->velocity.x * -3.3;
|
||||
p_ctransform->accel.y += p_ctransform->velocity.y * -1;
|
||||
}
|
||||
p_pstate->player_dir.x = 0;
|
||||
p_pstate->player_dir.y = 0;
|
||||
}
|
||||
data->player_dir.x = 0;
|
||||
data->player_dir.y = 0;
|
||||
|
||||
}
|
||||
|
||||
static void player_bbox_update_system(Scene_t *scene)
|
||||
|
@ -377,7 +357,7 @@ static void player_bbox_update_system(Scene_t *scene)
|
|||
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);
|
||||
if (p_ctransform->on_ground)
|
||||
if (p_ctransform->ground_state & 1)
|
||||
{
|
||||
AnchorPoint_t anchor = AP_BOT_CENTER;
|
||||
int dx[3] = {1, 0, 2};
|
||||
|
@ -400,7 +380,7 @@ static void player_bbox_update_system(Scene_t *scene)
|
|||
}
|
||||
}
|
||||
Vector2 new_bbox;
|
||||
if (p_pstate->is_crouch)
|
||||
if(p_pstate->is_crouch)
|
||||
{
|
||||
new_bbox.x = PLAYER_C_WIDTH;
|
||||
new_bbox.y = PLAYER_C_HEIGHT;
|
||||
|
@ -416,7 +396,7 @@ static void player_bbox_update_system(Scene_t *scene)
|
|||
}
|
||||
else
|
||||
{
|
||||
if (p_pstate->in_water)
|
||||
if (p_ctransform->water_state & 1)
|
||||
{
|
||||
Vector2 new_bbox = {PLAYER_C_WIDTH, PLAYER_C_HEIGHT};
|
||||
Vector2 offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER);
|
||||
|
@ -436,11 +416,38 @@ static void player_bbox_update_system(Scene_t *scene)
|
|||
|
||||
static void movement_update_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
// Update movement
|
||||
float delta_time = 0.017; // TODO: Will need to think about delta time handling
|
||||
CTransform_t * p_ctransform;
|
||||
sc_map_foreach_value(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], p_ctransform)
|
||||
unsigned long ent_idx;
|
||||
sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ctransform)
|
||||
{
|
||||
Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
|
||||
if (p_ctransform == NULL) continue;
|
||||
|
||||
if (!(p_ctransform->ground_state & 1))
|
||||
{
|
||||
// Only apply upthrust if center is in water
|
||||
// If need more accuracy, need to find area of overlap
|
||||
if (p_ctransform->water_state & 1)
|
||||
{
|
||||
unsigned int tile_idx = get_tile_idx(
|
||||
p_ctransform->position.x + p_bbox->half_size.x,
|
||||
p_ctransform->position.y + p_bbox->half_size.y,
|
||||
data->tilemap.width
|
||||
);
|
||||
|
||||
if (data->tilemap.tiles[tile_idx].water_level > 0)
|
||||
{
|
||||
p_ctransform->accel = Vector2Add(p_ctransform->accel, UPTHRUST);
|
||||
}
|
||||
}
|
||||
p_ctransform->accel = Vector2Add(p_ctransform->accel, GRAVITY);
|
||||
}
|
||||
|
||||
p_ctransform->velocity =
|
||||
Vector2Add(
|
||||
p_ctransform->velocity,
|
||||
|
@ -461,54 +468,25 @@ static void movement_update_system(Scene_t* scene)
|
|||
p_ctransform->position,
|
||||
Vector2Scale(p_ctransform->velocity, delta_time)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void player_ground_air_transition_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
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);
|
||||
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
|
||||
CPlayerState_t* p_pstate = get_component(&scene->ent_manager, p_player, CPLAYERSTATE_T);
|
||||
|
||||
// State checks
|
||||
bool on_ground = check_on_ground(p_ctransform->position, p_bbox->size, &data->tilemap);
|
||||
bool in_water = false;
|
||||
if (!p_pstate->in_water)
|
||||
{
|
||||
unsigned int tile_idx = get_tile_idx(
|
||||
p_ctransform->position.x + p_bbox->size.x / 2,
|
||||
p_ctransform->position.y + p_bbox->size.y / 2,
|
||||
data->tilemap.width
|
||||
);
|
||||
|
||||
in_water = (data->tilemap.tiles[tile_idx].water_level > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE;
|
||||
unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE;
|
||||
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE;
|
||||
unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
|
||||
for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++)
|
||||
{
|
||||
for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++)
|
||||
{
|
||||
unsigned int tile_idx = tile_y * data->tilemap.width + tile_x;
|
||||
in_water |= data->tilemap.tiles[tile_idx].water_level > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle Ground<->Air Transition
|
||||
//if (!on_ground && p_ctransform->on_ground)
|
||||
if ((on_ground && !p_ctransform->on_ground) || in_water)
|
||||
bool in_water = (p_ctransform->water_state & 1);
|
||||
// Landing or in water
|
||||
if (p_ctransform->ground_state == 0b01 || in_water)
|
||||
{
|
||||
// Recover jumps
|
||||
p_cjump->jumps = p_cjump->max_jumps;
|
||||
p_cjump->jumped = false;
|
||||
p_cjump->short_hop = false;
|
||||
|
@ -517,13 +495,10 @@ static void player_ground_air_transition_system(Scene_t* scene)
|
|||
}
|
||||
|
||||
// TODO: Handle in water <-> out of water transition
|
||||
if (p_pstate->in_water && !in_water)
|
||||
if (p_ctransform->water_state == 0b10 || p_ctransform->ground_state == 0b10)
|
||||
{
|
||||
p_cjump->jumps -= (p_cjump->jumps > 0)? 1:0;
|
||||
}
|
||||
|
||||
p_ctransform->on_ground = on_ground;
|
||||
p_pstate->in_water = in_water;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -636,11 +611,104 @@ static void player_collision_system(Scene_t *scene)
|
|||
p_ctransform->position.y = decimal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void state_transition_update_system(Scene_t *scene)
|
||||
{
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
//Entity_t* p_ent;
|
||||
|
||||
CBBox_t *p_bbox;
|
||||
unsigned long ent_idx;
|
||||
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);
|
||||
if (p_ctransform == NULL) continue;
|
||||
|
||||
bool on_ground = check_on_ground(p_ctransform->position, p_bbox->size, &data->tilemap);
|
||||
bool in_water = false;
|
||||
if (!(p_ctransform->water_state & 1))
|
||||
{
|
||||
unsigned int tile_idx = get_tile_idx(
|
||||
p_ctransform->position.x + p_bbox->half_size.x,
|
||||
p_ctransform->position.y + p_bbox->half_size.y,
|
||||
data->tilemap.width
|
||||
);
|
||||
|
||||
in_water = (data->tilemap.tiles[tile_idx].water_level > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE;
|
||||
unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE;
|
||||
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE;
|
||||
unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
|
||||
for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++)
|
||||
{
|
||||
for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++)
|
||||
{
|
||||
unsigned int tile_idx = tile_y * data->tilemap.width + tile_x;
|
||||
in_water |= data->tilemap.tiles[tile_idx].water_level > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
p_ctransform->ground_state <<= 1;
|
||||
p_ctransform->ground_state |= on_ground? 1:0;
|
||||
p_ctransform->ground_state &= 3;
|
||||
p_ctransform->water_state <<= 1;
|
||||
p_ctransform->water_state |= in_water? 1:0;
|
||||
p_ctransform->water_state &= 3;
|
||||
}
|
||||
}
|
||||
|
||||
static void update_tilemap_system(Scene_t *scene)
|
||||
{
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
TileGrid_t tilemap = data->tilemap;
|
||||
|
||||
CTileCoord_t *p_tilecoord;
|
||||
unsigned long ent_idx;
|
||||
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);
|
||||
if (p_ctransform == NULL) continue;
|
||||
CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
|
||||
if (p_bbox == NULL) continue;
|
||||
|
||||
// Update tilemap position
|
||||
for (size_t i=0;i<p_tilecoord->n_tiles;++i)
|
||||
{
|
||||
// Use previously store tile position
|
||||
// Clear from those positions
|
||||
unsigned int tile_idx = p_tilecoord->tiles[i];
|
||||
sc_map_del_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id);
|
||||
}
|
||||
p_tilecoord->n_tiles = 0;
|
||||
|
||||
// Compute new occupied tile positions and add
|
||||
// Extend the check by a little to avoid missing
|
||||
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE;
|
||||
unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE;
|
||||
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE;
|
||||
unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
|
||||
|
||||
for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++)
|
||||
{
|
||||
for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++)
|
||||
{
|
||||
unsigned int tile_idx = tile_y * tilemap.width + tile_x;
|
||||
p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx;
|
||||
sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void toggle_block_system(Scene_t *scene)
|
||||
{
|
||||
// TODO: This system is not good as the interface between raw input and actions is broken
|
||||
static unsigned int last_tile_idx = MAX_N_TILES;
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
TileGrid_t tilemap = data->tilemap;
|
||||
|
@ -680,42 +748,43 @@ static void toggle_block_system(Scene_t *scene)
|
|||
|
||||
void level_do_action(Scene_t *scene, ActionType_t action, bool pressed)
|
||||
{
|
||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||
CPlayerState_t *p_playerstate;
|
||||
sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate)
|
||||
{
|
||||
switch(action)
|
||||
{
|
||||
case ACTION_UP:
|
||||
data->player_dir.y = (pressed)? -1 : 0;
|
||||
p_playerstate->player_dir.y = (pressed)? -1 : 0;
|
||||
break;
|
||||
case ACTION_DOWN:
|
||||
data->player_dir.y = (pressed)? 1 : 0;
|
||||
data->crouch_pressed = pressed;
|
||||
p_playerstate->player_dir.y = (pressed)? 1 : 0;
|
||||
p_playerstate->is_crouch = pressed;
|
||||
break;
|
||||
case ACTION_LEFT:
|
||||
data->player_dir.x = (pressed)? -1 : 0;
|
||||
p_playerstate->player_dir.x = (pressed)? -1 : 0;
|
||||
break;
|
||||
case ACTION_RIGHT:
|
||||
data->player_dir.x = (pressed)? 1 : 0;
|
||||
p_playerstate->player_dir.x = (pressed)? 1 : 0;
|
||||
break;
|
||||
case ACTION_JUMP:
|
||||
data->jumped_pressed = pressed;
|
||||
p_playerstate->jump_pressed = pressed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_level_scene(LevelScene_t *scene)
|
||||
{
|
||||
init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func, &level_do_action);
|
||||
scene->scene.scene_data = &scene->data;
|
||||
scene->data.jumped_pressed = false;
|
||||
scene->data.crouch_pressed = false;
|
||||
memset(&scene->data.player_dir, 0, sizeof(Vector2));
|
||||
|
||||
// insert level scene systems
|
||||
sc_array_add(&scene->scene.systems, &player_movement_input_system);
|
||||
sc_array_add(&scene->scene.systems, &player_bbox_update_system);
|
||||
sc_array_add(&scene->scene.systems, &movement_update_system);
|
||||
//sc_array_add(&scene->scene.systems, &update_tilemap_system);
|
||||
sc_array_add(&scene->scene.systems, &update_tilemap_system);
|
||||
sc_array_add(&scene->scene.systems, &player_collision_system);
|
||||
sc_array_add(&scene->scene.systems, &state_transition_update_system);
|
||||
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
|
||||
sc_array_add(&scene->scene.systems, &toggle_block_system);
|
||||
|
||||
|
|
|
@ -22,10 +22,7 @@ typedef struct TileGrid
|
|||
|
||||
typedef struct LevelSceneData
|
||||
{
|
||||
Vector2 player_dir;
|
||||
TileGrid_t tilemap;
|
||||
bool jumped_pressed;
|
||||
bool crouch_pressed;
|
||||
}LevelSceneData_t;
|
||||
|
||||
typedef struct LevelScene
|
||||
|
|
|
@ -24,6 +24,7 @@ int main(void)
|
|||
p_cjump->jumps = 1;
|
||||
p_cjump->max_jumps = 1;
|
||||
add_component(&scene.scene.ent_manager, p_ent, CPLAYERSTATE_T);
|
||||
add_component(&scene.scene.ent_manager, p_ent, CTILECOORD_COMP_T);
|
||||
update_entity_manager(&scene.scene.ent_manager);
|
||||
//for (size_t step = 0; step < 6000; step++)
|
||||
while(true)
|
||||
|
|
Loading…
Reference in New Issue