Compare commits
3 Commits
a057835e65
...
b5da3f216c
Author | SHA1 | Date |
---|---|---|
|
b5da3f216c | |
|
c6ed46c6e7 | |
|
64466128bc |
12
components.h
12
components.h
|
@ -3,12 +3,11 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
// TODO: Look at sc to use macros to auto generate functions
|
// TODO: Look at sc to use macros to auto generate functions
|
||||||
|
|
||||||
#define N_COMPONENTS 5
|
#define N_COMPONENTS 4
|
||||||
enum ComponentEnum
|
enum ComponentEnum
|
||||||
{
|
{
|
||||||
CBBOX_COMP_T,
|
CBBOX_COMP_T,
|
||||||
CTRANSFORM_COMP_T,
|
CTRANSFORM_COMP_T,
|
||||||
CTILECOORD_COMP_T,
|
|
||||||
CJUMP_COMP_T,
|
CJUMP_COMP_T,
|
||||||
CPLAYERSTATE_T
|
CPLAYERSTATE_T
|
||||||
};
|
};
|
||||||
|
@ -59,4 +58,13 @@ typedef struct _CPlayerState_t
|
||||||
unsigned int is_crouch: 1;
|
unsigned int is_crouch: 1;
|
||||||
unsigned int in_water:1;
|
unsigned int in_water:1;
|
||||||
}CPlayerState_t;
|
}CPlayerState_t;
|
||||||
|
|
||||||
|
|
||||||
|
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
||||||
|
{
|
||||||
|
p_bbox->size.x = x;
|
||||||
|
p_bbox->size.y = y;
|
||||||
|
p_bbox->half_size.x = (unsigned int)(x / 2);
|
||||||
|
p_bbox->half_size.y = (unsigned int)(y / 2);
|
||||||
|
}
|
||||||
#endif // __COMPONENTS_H
|
#endif // __COMPONENTS_H
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Constants to be used in game
|
||||||
|
#define TILE_SIZE 32
|
||||||
|
|
||||||
|
#define GRAV_ACCEL 1500
|
||||||
|
#define JUMP_SPEED 70
|
||||||
|
#define MOVE_ACCEL 1000
|
||||||
|
#define FRICTION 0.98
|
||||||
|
#define PLAYER_STAND_WIDTH 30
|
||||||
|
|
||||||
|
#define PLAYER_WIDTH 30
|
||||||
|
#define PLAYER_HEIGHT 55
|
||||||
|
|
||||||
|
#define PLAYER_C_WIDTH 48
|
||||||
|
#define PLAYER_C_HEIGHT 30
|
||||||
|
#define PLAYER_C_YOFFSET 25
|
||||||
|
#define PLAYER_C_XOFFSET 9
|
||||||
|
|
||||||
|
#define PLAYER_MAX_SPEED 1000
|
||||||
|
#define Y_FRICTION 0.98
|
||||||
|
#define X_FRICTION 0.85
|
||||||
|
|
||||||
|
#define MAX_WATER_LEVEL 4
|
|
@ -6,7 +6,7 @@
|
||||||
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
|
||||||
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
|
static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE];
|
||||||
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
|
static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE];
|
||||||
static CTileCoord_t ctilecoord_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 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
|
static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
|
||||||
{
|
{
|
||||||
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), {0}, {0}},
|
{bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), {0}, {0}},
|
||||||
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), {0}, {0}},
|
{ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), {0}, {0}},
|
||||||
{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), {0}, {0}},
|
//{ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), {0}, {0}},
|
||||||
{cjump_buffer, 1, sizeof(CJump_t), {0}, {0}},
|
{cjump_buffer, 1, sizeof(CJump_t), {0}, {0}},
|
||||||
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), {0}, {0}},
|
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), {0}, {0}},
|
||||||
};
|
};
|
||||||
|
|
438
scene_impl.c
438
scene_impl.c
|
@ -9,6 +9,14 @@ static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
|
||||||
static Tile_t all_tiles[MAX_N_TILES] = {0};
|
static Tile_t all_tiles[MAX_N_TILES] = {0};
|
||||||
|
|
||||||
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
|
||||||
|
static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 0.7};
|
||||||
|
|
||||||
|
static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width)
|
||||||
|
{
|
||||||
|
unsigned int tile_x = x / TILE_SIZE;
|
||||||
|
unsigned int tile_y = y / TILE_SIZE;
|
||||||
|
return tile_y * tilemap_width + tile_x;
|
||||||
|
}
|
||||||
|
|
||||||
static bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap)
|
static bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap)
|
||||||
{
|
{
|
||||||
|
@ -86,6 +94,86 @@ static inline bool check_on_ground(Vector2 pos, Vector2 bbox_sz, TileGrid_t* gri
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef enum AnchorPoint
|
||||||
|
{
|
||||||
|
AP_TOP_LEFT,
|
||||||
|
AP_TOP_CENTER,
|
||||||
|
AP_TOP_RIGHT,
|
||||||
|
AP_MID_LEFT,
|
||||||
|
AP_MID_CENTER,
|
||||||
|
AP_MID_RIGHT,
|
||||||
|
AP_BOT_LEFT,
|
||||||
|
AP_BOT_CENTER,
|
||||||
|
AP_BOT_RIGHT,
|
||||||
|
}AnchorPoint_t;
|
||||||
|
|
||||||
|
static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
|
||||||
|
{
|
||||||
|
Vector2 p1;
|
||||||
|
Vector2 p2;
|
||||||
|
|
||||||
|
Vector2 offset = {0};
|
||||||
|
switch (anchor)
|
||||||
|
{
|
||||||
|
case AP_TOP_LEFT:
|
||||||
|
// When resizing bbox, it is implicitly assumed that to be already in topleft
|
||||||
|
// due to the coordindate system (+ve towards right and downwards)
|
||||||
|
// So do nothing
|
||||||
|
return offset;
|
||||||
|
case AP_TOP_CENTER:
|
||||||
|
p1.x = bbox.x / 2;
|
||||||
|
p1.y = 0;
|
||||||
|
p2.x = new_bbox.x / 2;
|
||||||
|
p2.y = 0;
|
||||||
|
break;
|
||||||
|
case AP_TOP_RIGHT:
|
||||||
|
p1.x = bbox.x;
|
||||||
|
p1.y = 0;
|
||||||
|
p2.x = new_bbox.x;
|
||||||
|
p2.y = 0;
|
||||||
|
break;
|
||||||
|
case AP_MID_LEFT:
|
||||||
|
p1.x = 0;
|
||||||
|
p1.y = bbox.y / 2;
|
||||||
|
p2.x = 0;
|
||||||
|
p2.y = new_bbox.y / 2;
|
||||||
|
break;
|
||||||
|
case AP_MID_CENTER:
|
||||||
|
p1.x = bbox.x / 2;
|
||||||
|
p1.y = bbox.y / 2;
|
||||||
|
p2.x = new_bbox.x / 2;
|
||||||
|
p2.y = new_bbox.y / 2;
|
||||||
|
break;
|
||||||
|
case AP_MID_RIGHT:
|
||||||
|
p1.x = bbox.x;
|
||||||
|
p1.y = bbox.y / 2;
|
||||||
|
p2.x = new_bbox.x;
|
||||||
|
p2.y = new_bbox.y / 2;
|
||||||
|
break;
|
||||||
|
case AP_BOT_LEFT:
|
||||||
|
p1.x = 0;
|
||||||
|
p1.y = bbox.y;
|
||||||
|
p2.x = 0;
|
||||||
|
p2.y = new_bbox.y;
|
||||||
|
break;
|
||||||
|
case AP_BOT_CENTER:
|
||||||
|
p1.x = bbox.x / 2;
|
||||||
|
p1.y = bbox.y;
|
||||||
|
p2.x = new_bbox.x / 2;
|
||||||
|
p2.y = new_bbox.y;
|
||||||
|
break;
|
||||||
|
case AP_BOT_RIGHT:
|
||||||
|
p1.x = bbox.x;
|
||||||
|
p1.y = bbox.y;
|
||||||
|
p2.x = new_bbox.x;
|
||||||
|
p2.y = new_bbox.y;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
offset.x = p1.x - p2.x;
|
||||||
|
offset.y = p1.y - p2.y;
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
static void level_scene_render_func(Scene_t* scene)
|
static void level_scene_render_func(Scene_t* scene)
|
||||||
{
|
{
|
||||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||||
|
@ -105,6 +193,11 @@ static void level_scene_render_func(Scene_t* scene)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Draw water tile
|
||||||
|
if (tilemap.tiles[i].water_level > 0)
|
||||||
|
{
|
||||||
|
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLUE);
|
||||||
|
}
|
||||||
DrawText(buffer, x, y, 10, BLACK);
|
DrawText(buffer, x, y, 10, BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,6 +235,8 @@ static void level_scene_render_func(Scene_t* scene)
|
||||||
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK);
|
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 60, 12, BLACK);
|
||||||
sprintf(buffer, "Crouch: %s", p_pstate->is_crouch? "YES":"NO");
|
sprintf(buffer, "Crouch: %s", p_pstate->is_crouch? "YES":"NO");
|
||||||
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
|
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 90, 12, BLACK);
|
||||||
|
sprintf(buffer, "Water: %s", p_pstate->in_water? "YES":"NO");
|
||||||
|
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 120, 12, BLACK);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,11 +253,19 @@ static void player_movement_input_system(Scene_t* scene)
|
||||||
{
|
{
|
||||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
||||||
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_COMP_T);
|
CJump_t* p_cjump = get_component(&scene->ent_manager, p_player, CJUMP_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);
|
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.x = 0;
|
||||||
p_ctransform->accel.y = 0;
|
p_ctransform->accel.y = 0;
|
||||||
p_ctransform->accel = Vector2Scale(Vector2Normalize(data->player_dir), MOVE_ACCEL);
|
if (!p_pstate->in_water)
|
||||||
|
{
|
||||||
|
data->player_dir.y = 0;
|
||||||
|
p_ctransform->accel = Vector2Scale(Vector2Normalize(data->player_dir), MOVE_ACCEL/1.2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
p_ctransform->accel = Vector2Scale(Vector2Normalize(data->player_dir), MOVE_ACCEL);
|
||||||
|
}
|
||||||
|
|
||||||
// Short Hop
|
// Short Hop
|
||||||
if (p_cjump->jumped && p_ctransform->velocity.y < 0)
|
if (p_cjump->jumped && p_ctransform->velocity.y < 0)
|
||||||
|
@ -174,23 +277,25 @@ static void player_movement_input_system(Scene_t* scene)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!p_ctransform->on_ground)
|
if (!p_ctransform->on_ground)
|
||||||
{
|
{
|
||||||
p_ctransform->accel = Vector2Add(p_ctransform->accel, GRAVITY);
|
if (p_pstate->in_water)
|
||||||
if(p_pstate->is_crouch)
|
|
||||||
{
|
{
|
||||||
p_pstate->is_crouch = false;
|
p_ctransform->accel = Vector2Add(p_ctransform->accel, UPTHRUST);
|
||||||
p_ctransform->position.x += PLAYER_C_XOFFSET;
|
|
||||||
p_ctransform->position.y -= PLAYER_C_YOFFSET;
|
|
||||||
p_bbox->size.y = PLAYER_HEIGHT;
|
|
||||||
p_bbox->size.x = PLAYER_WIDTH;
|
|
||||||
}
|
}
|
||||||
|
p_ctransform->accel = Vector2Add(p_ctransform->accel, GRAVITY);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// All these actions can only be done on ground
|
||||||
|
|
||||||
|
// Check if possible to jump when jump is pressed
|
||||||
if (data->jumped_pressed && p_cjump->jumps > 0)
|
if (data->jumped_pressed && p_cjump->jumps > 0)
|
||||||
{
|
{
|
||||||
bool jump_valid = true;
|
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_pos = p_ctransform->position;
|
||||||
|
@ -201,6 +306,7 @@ static void player_movement_input_system(Scene_t* scene)
|
||||||
jump_valid = !check_collision_at(test_pos, test_bbox, &tilemap, top);
|
jump_valid = !check_collision_at(test_pos, test_bbox, &tilemap, top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jump okay
|
||||||
if (jump_valid)
|
if (jump_valid)
|
||||||
{
|
{
|
||||||
p_ctransform->velocity.y -= p_cjump->jump_speed;
|
p_ctransform->velocity.y -= p_cjump->jump_speed;
|
||||||
|
@ -208,30 +314,26 @@ static void player_movement_input_system(Scene_t* scene)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->crouch_pressed && !p_pstate->is_crouch)
|
}
|
||||||
{
|
|
||||||
p_ctransform->position.x -= PLAYER_C_XOFFSET;
|
// Friction
|
||||||
p_ctransform->position.y += PLAYER_C_YOFFSET;
|
if (p_pstate->in_water)
|
||||||
p_bbox->size.x = PLAYER_C_WIDTH;
|
{
|
||||||
p_bbox->size.y = PLAYER_C_HEIGHT;
|
// Apply water friction
|
||||||
p_pstate->is_crouch = data->crouch_pressed;
|
// Consistent in all direction
|
||||||
}
|
p_ctransform->accel = Vector2Add(
|
||||||
else if (!data->crouch_pressed && p_pstate->is_crouch)
|
p_ctransform->accel,
|
||||||
{
|
Vector2Scale(p_ctransform->velocity, -5.5)
|
||||||
Vector2 test_pos = p_ctransform->position;
|
);
|
||||||
Vector2 test_bbox = {PLAYER_WIDTH, PLAYER_HEIGHT};
|
}
|
||||||
Vector2 top = {0, -1};
|
else
|
||||||
test_pos.x += PLAYER_C_XOFFSET;
|
{
|
||||||
test_pos.y -= PLAYER_C_YOFFSET;
|
// For game feel, y is set to air resistance only
|
||||||
if (!check_collision_at(test_pos, test_bbox, &tilemap, top))
|
// x is set to ground resistance (even in air)
|
||||||
{
|
// If not, then player is can go faster by bunny hopping
|
||||||
p_ctransform->position.x += PLAYER_C_XOFFSET;
|
// which is fun but not quite beneficial here
|
||||||
p_ctransform->position.y -= PLAYER_C_YOFFSET;
|
p_ctransform->accel.x += p_ctransform->velocity.x * -3.3;
|
||||||
p_bbox->size.x = PLAYER_WIDTH;
|
p_ctransform->accel.y += p_ctransform->velocity.y * -1;
|
||||||
p_bbox->size.y = PLAYER_HEIGHT;
|
|
||||||
p_pstate->is_crouch = data->crouch_pressed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data->player_dir.x = 0;
|
data->player_dir.x = 0;
|
||||||
|
@ -239,6 +341,74 @@ static void player_movement_input_system(Scene_t* scene)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void player_bbox_update_system(Scene_t *scene)
|
||||||
|
{
|
||||||
|
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
||||||
|
TileGrid_t tilemap = data->tilemap;
|
||||||
|
|
||||||
|
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);
|
||||||
|
if (p_ctransform->on_ground)
|
||||||
|
{
|
||||||
|
AnchorPoint_t anchor = AP_BOT_CENTER;
|
||||||
|
int dx[3] = {1, 0, 2};
|
||||||
|
for (size_t i=0; i<3; i++)
|
||||||
|
{
|
||||||
|
unsigned int tile_idx = get_tile_idx
|
||||||
|
(
|
||||||
|
p_ctransform->position.x + p_bbox->half_size.x * dx[i],
|
||||||
|
p_ctransform->position.y + p_bbox->size.y,
|
||||||
|
tilemap.width
|
||||||
|
);
|
||||||
|
if (tilemap.tiles[tile_idx].solid)
|
||||||
|
{
|
||||||
|
switch(i)
|
||||||
|
{
|
||||||
|
case 1: anchor = AP_BOT_LEFT;break;
|
||||||
|
case 2: anchor = AP_BOT_RIGHT;break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Vector2 new_bbox;
|
||||||
|
if (p_pstate->is_crouch)
|
||||||
|
{
|
||||||
|
new_bbox.x = PLAYER_C_WIDTH;
|
||||||
|
new_bbox.y = PLAYER_C_HEIGHT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_bbox.x = PLAYER_WIDTH;
|
||||||
|
new_bbox.y = PLAYER_HEIGHT;
|
||||||
|
}
|
||||||
|
Vector2 offset = shift_bbox(p_bbox->size, new_bbox, anchor);
|
||||||
|
set_bbox(p_bbox, new_bbox.x, new_bbox.y);
|
||||||
|
p_ctransform->position = Vector2Add(p_ctransform->position, offset);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (p_pstate->in_water)
|
||||||
|
{
|
||||||
|
Vector2 new_bbox = {PLAYER_C_WIDTH, PLAYER_C_HEIGHT};
|
||||||
|
Vector2 offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER);
|
||||||
|
set_bbox(p_bbox, PLAYER_C_WIDTH, PLAYER_C_HEIGHT);
|
||||||
|
p_ctransform->position = Vector2Add(p_ctransform->position, offset);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Vector2 new_bbox = {PLAYER_WIDTH, PLAYER_HEIGHT};
|
||||||
|
Vector2 offset = shift_bbox(p_bbox->size, new_bbox, AP_MID_CENTER);
|
||||||
|
set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT);
|
||||||
|
p_ctransform->position = Vector2Add(p_ctransform->position, offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void movement_update_system(Scene_t* scene)
|
static void movement_update_system(Scene_t* scene)
|
||||||
{
|
{
|
||||||
// Update movement
|
// Update movement
|
||||||
|
@ -251,8 +421,7 @@ static void movement_update_system(Scene_t* scene)
|
||||||
p_ctransform->velocity,
|
p_ctransform->velocity,
|
||||||
Vector2Scale(p_ctransform->accel, delta_time)
|
Vector2Scale(p_ctransform->accel, delta_time)
|
||||||
);
|
);
|
||||||
p_ctransform->velocity.x *= X_FRICTION;
|
|
||||||
p_ctransform->velocity.y *= Y_FRICTION;
|
|
||||||
float mag = Vector2Length(p_ctransform->velocity);
|
float mag = Vector2Length(p_ctransform->velocity);
|
||||||
p_ctransform->velocity = Vector2Scale(Vector2Normalize(p_ctransform->velocity), (mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag);
|
p_ctransform->velocity = Vector2Scale(Vector2Normalize(p_ctransform->velocity), (mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag);
|
||||||
|
|
||||||
|
@ -280,63 +449,40 @@ static void player_ground_air_transition_system(Scene_t* scene)
|
||||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
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);
|
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);
|
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 on_ground = check_on_ground(p_ctransform->position, p_bbox->size, &data->tilemap);
|
||||||
|
bool in_water = false;
|
||||||
|
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
|
// Handle Ground<->Air Transition
|
||||||
if (!on_ground && p_ctransform->on_ground)
|
if (!on_ground && p_ctransform->on_ground)
|
||||||
{
|
{
|
||||||
p_cjump->jumps--;
|
p_cjump->jumps--;
|
||||||
}
|
}
|
||||||
else if (on_ground && !p_ctransform->on_ground)
|
else if ((on_ground && !p_ctransform->on_ground) || in_water)
|
||||||
{
|
{
|
||||||
p_cjump->jumps = p_cjump->max_jumps;
|
p_cjump->jumps = p_cjump->max_jumps;
|
||||||
p_cjump->jumped = false;
|
p_cjump->jumped = false;
|
||||||
p_cjump->short_hop = false;
|
p_cjump->short_hop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Handle in water <-> out of water transition
|
||||||
|
|
||||||
p_ctransform->on_ground = on_ground;
|
p_ctransform->on_ground = on_ground;
|
||||||
}
|
p_pstate->in_water = in_water;
|
||||||
}
|
|
||||||
|
|
||||||
static void update_tilemap_system(Scene_t *scene)
|
|
||||||
{
|
|
||||||
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
|
|
||||||
TileGrid_t tilemap = data->tilemap;
|
|
||||||
|
|
||||||
Entity_t *p_ent;
|
|
||||||
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
|
|
||||||
{
|
|
||||||
CTileCoord_t * p_tilecoord = get_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
|
|
||||||
if (p_tilecoord == NULL) continue;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,7 +497,6 @@ static void player_collision_system(Scene_t *scene)
|
||||||
{
|
{
|
||||||
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_player, CTRANSFORM_COMP_T);
|
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);
|
CBBox_t* p_bbox = get_component(&scene->ent_manager, p_player, CBBOX_COMP_T);
|
||||||
CTileCoord_t* p_tilecoord = get_component(&scene->ent_manager, p_player, CTILECOORD_COMP_T);
|
|
||||||
// Get the occupied tiles
|
// Get the occupied tiles
|
||||||
// For each tile, loop through the entities and find overlaps
|
// For each tile, loop through the entities and find overlaps
|
||||||
// exclude self
|
// exclude self
|
||||||
|
@ -360,60 +505,70 @@ static void player_collision_system(Scene_t *scene)
|
||||||
// Resolve collision via moving player by the overlap amount
|
// Resolve collision via moving player by the overlap amount
|
||||||
Vector2 overlap = {0};
|
Vector2 overlap = {0};
|
||||||
Vector2 prev_overlap = {0};
|
Vector2 prev_overlap = {0};
|
||||||
for (size_t i=0;i<p_tilecoord->n_tiles;++i)
|
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++)
|
||||||
{
|
{
|
||||||
unsigned int tile_idx = p_tilecoord->tiles[i];
|
for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++)
|
||||||
Vector2 other;
|
|
||||||
if(tilemap.tiles[tile_idx].solid)
|
|
||||||
{
|
{
|
||||||
|
unsigned int tile_idx = tile_y * tilemap.width + tile_x;
|
||||||
other.x = (tile_idx % tilemap.width) * TILE_SIZE;
|
//for (size_t i=0;i<p_tilecoord->n_tiles;++i)
|
||||||
other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional
|
//{
|
||||||
if (find_AABB_overlap(p_ctransform->position, p_bbox->size, other, TILE_SZ, &overlap))
|
//unsigned int tile_idx = p_tilecoord->tiles[i];
|
||||||
|
Vector2 other;
|
||||||
|
if(tilemap.tiles[tile_idx].solid)
|
||||||
{
|
{
|
||||||
find_AABB_overlap(p_ctransform->prev_position, p_bbox->size, other, TILE_SZ, &prev_overlap);
|
|
||||||
if (fabs(prev_overlap.y) > fabs(prev_overlap.x))
|
other.x = (tile_idx % tilemap.width) * TILE_SIZE;
|
||||||
|
other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional
|
||||||
|
if (find_AABB_overlap(p_ctransform->position, p_bbox->size, other, TILE_SZ, &overlap))
|
||||||
{
|
{
|
||||||
p_ctransform->position.x += overlap.x;
|
find_AABB_overlap(p_ctransform->prev_position, p_bbox->size, other, TILE_SZ, &prev_overlap);
|
||||||
p_ctransform->velocity.x = 0;
|
if (fabs(prev_overlap.y) > fabs(prev_overlap.x))
|
||||||
}
|
{
|
||||||
else if (fabs(prev_overlap.x) > fabs(prev_overlap.y))
|
p_ctransform->position.x += overlap.x;
|
||||||
{
|
p_ctransform->velocity.x = 0;
|
||||||
p_ctransform->position.y += overlap.y;
|
}
|
||||||
p_ctransform->velocity.y = 0;
|
else if (fabs(prev_overlap.x) > fabs(prev_overlap.y))
|
||||||
}
|
{
|
||||||
else if (fabs(overlap.x) < fabs(overlap.y))
|
p_ctransform->position.y += overlap.y;
|
||||||
{
|
p_ctransform->velocity.y = 0;
|
||||||
p_ctransform->position.x += overlap.x;
|
}
|
||||||
p_ctransform->velocity.x = 0;
|
else if (fabs(overlap.x) < fabs(overlap.y))
|
||||||
}
|
{
|
||||||
else
|
p_ctransform->position.x += overlap.x;
|
||||||
{
|
p_ctransform->velocity.x = 0;
|
||||||
p_ctransform->position.y += overlap.y;
|
}
|
||||||
p_ctransform->velocity.y = 0;
|
else
|
||||||
|
{
|
||||||
|
p_ctransform->position.y += overlap.y;
|
||||||
|
p_ctransform->velocity.y = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
else
|
{
|
||||||
{
|
// TODO: check against the entities of each involved tiles
|
||||||
// TODO: check against the entities of each involved tiles
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Level boundary collision
|
// Level boundary collision
|
||||||
unsigned int level_width = tilemap.width * TILE_SIZE;
|
unsigned int level_width = tilemap.width * TILE_SIZE;
|
||||||
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width)
|
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width)
|
||||||
{
|
{
|
||||||
p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x;
|
p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x;
|
||||||
p_ctransform->position.x = (p_ctransform->position.x + p_bbox->size.x > level_width) ? level_width - p_bbox->size.x : p_ctransform->position.x;
|
p_ctransform->position.x = (p_ctransform->position.x + p_bbox->size.x > level_width) ? level_width - p_bbox->size.x : p_ctransform->position.x;
|
||||||
p_ctransform->velocity.x *= -1;
|
p_ctransform->velocity.x *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int level_height = tilemap.height * TILE_SIZE;
|
unsigned int level_height = tilemap.height * TILE_SIZE;
|
||||||
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height)
|
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height)
|
||||||
{
|
{
|
||||||
p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y;
|
p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y;
|
||||||
p_ctransform->position.y = (p_ctransform->position.y + p_bbox->size.y > level_height) ? level_height - p_bbox->size.y : p_ctransform->position.y;
|
p_ctransform->position.y = (p_ctransform->position.y + p_bbox->size.y > level_height) ? level_height - p_bbox->size.y : p_ctransform->position.y;
|
||||||
p_ctransform->velocity.y *= -1;
|
p_ctransform->velocity.y *= -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Deal with float precision, by rounding when it is near to an integer enough by 2 dp
|
// Deal with float precision, by rounding when it is near to an integer enough by 2 dp
|
||||||
|
@ -451,12 +606,28 @@ static void toggle_block_system(Scene_t *scene)
|
||||||
|
|
||||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
||||||
{
|
{
|
||||||
int mouse_x = GetMouseX() / TILE_SIZE;
|
unsigned int tile_idx = get_tile_idx(GetMouseX(), GetMouseY(), tilemap.width);
|
||||||
int mouse_y = GetMouseY() / TILE_SIZE;
|
|
||||||
unsigned int tile_idx = mouse_y * tilemap.width + mouse_x;
|
|
||||||
if (tile_idx != last_tile_idx)
|
if (tile_idx != last_tile_idx)
|
||||||
{
|
{
|
||||||
tilemap.tiles[tile_idx].solid = !tilemap.tiles[tile_idx].solid;
|
tilemap.tiles[tile_idx].solid = !tilemap.tiles[tile_idx].solid;
|
||||||
|
tilemap.tiles[tile_idx].water_level = 0;
|
||||||
|
last_tile_idx = tile_idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: Check for right click to change to water, also update above
|
||||||
|
else if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON))
|
||||||
|
{
|
||||||
|
unsigned int tile_idx = get_tile_idx(GetMouseX(), GetMouseY(), tilemap.width);
|
||||||
|
if (tile_idx != last_tile_idx)
|
||||||
|
{
|
||||||
|
if (tilemap.tiles[tile_idx].water_level == 0)
|
||||||
|
{
|
||||||
|
tilemap.tiles[tile_idx].water_level = MAX_WATER_LEVEL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tilemap.tiles[tile_idx].water_level = 0;
|
||||||
|
}
|
||||||
last_tile_idx = tile_idx;
|
last_tile_idx = tile_idx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -472,9 +643,11 @@ void level_do_action(Scene_t *scene, ActionType_t action, bool pressed)
|
||||||
switch(action)
|
switch(action)
|
||||||
{
|
{
|
||||||
case ACTION_UP:
|
case ACTION_UP:
|
||||||
|
data->player_dir.y = (pressed)? -1 : 0;
|
||||||
data->jumped_pressed = pressed;
|
data->jumped_pressed = pressed;
|
||||||
break;
|
break;
|
||||||
case ACTION_DOWN:
|
case ACTION_DOWN:
|
||||||
|
data->player_dir.y = (pressed)? 1 : 0;
|
||||||
data->crouch_pressed = pressed;
|
data->crouch_pressed = pressed;
|
||||||
break;
|
break;
|
||||||
case ACTION_LEFT:
|
case ACTION_LEFT:
|
||||||
|
@ -497,8 +670,9 @@ void init_level_scene(LevelScene_t *scene)
|
||||||
|
|
||||||
// insert level scene systems
|
// insert level scene systems
|
||||||
sc_array_add(&scene->scene.systems, &player_movement_input_system);
|
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, &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, &player_collision_system);
|
||||||
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
|
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
|
||||||
sc_array_add(&scene->scene.systems, &toggle_block_system);
|
sc_array_add(&scene->scene.systems, &toggle_block_system);
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
typedef struct Tile
|
typedef struct Tile
|
||||||
{
|
{
|
||||||
bool solid;
|
bool solid;
|
||||||
|
unsigned int water_level;
|
||||||
struct sc_map_64 entities_set;
|
struct sc_map_64 entities_set;
|
||||||
}Tile_t;
|
}Tile_t;
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,8 @@ int main(void)
|
||||||
Entity_t *p_ent = add_entity(&scene.scene.ent_manager, PLAYER_ENT_TAG);
|
Entity_t *p_ent = add_entity(&scene.scene.ent_manager, PLAYER_ENT_TAG);
|
||||||
|
|
||||||
CBBox_t *p_bbox = add_component(&scene.scene.ent_manager, p_ent, CBBOX_COMP_T);
|
CBBox_t *p_bbox = add_component(&scene.scene.ent_manager, p_ent, CBBOX_COMP_T);
|
||||||
p_bbox->size.x = 30;
|
set_bbox(p_bbox, 30, 45);
|
||||||
p_bbox->size.y = 45;
|
|
||||||
add_component(&scene.scene.ent_manager, p_ent, CTRANSFORM_COMP_T);
|
add_component(&scene.scene.ent_manager, p_ent, CTRANSFORM_COMP_T);
|
||||||
add_component(&scene.scene.ent_manager, p_ent, CTILECOORD_COMP_T);
|
|
||||||
CJump_t *p_cjump = add_component(&scene.scene.ent_manager, p_ent, CJUMP_COMP_T);
|
CJump_t *p_cjump = add_component(&scene.scene.ent_manager, p_ent, CJUMP_COMP_T);
|
||||||
p_cjump->jump_speed = 680;
|
p_cjump->jump_speed = 680;
|
||||||
p_cjump->jumps = 1;
|
p_cjump->jumps = 1;
|
||||||
|
|
Loading…
Reference in New Issue