Compare commits

..

No commits in common. "0a4c700bf6941efe44d004c82b5518649fbfa39c" and "533e2998bc97e452df3b1477a8fe237480c80b9a" have entirely different histories.

7 changed files with 27 additions and 92 deletions

View File

@ -15,7 +15,6 @@
#define VIEWABLE_MAP_HEIGHT 16
#endif
#define DELTA_T 0.017
#define GRAV_ACCEL 1500
#define JUMP_SPEED 70
#define MOVE_ACCEL 1300

View File

@ -86,13 +86,13 @@ static void level_scene_render_func(Scene_t* scene)
TileGrid_t tilemap = data->tilemap;
Entity_t* p_ent;
Vector2 min = GetScreenToWorld2D((Vector2){data->game_rec.x, data->game_rec.y}, data->camera.cam);
Vector2 min = GetScreenToWorld2D((Vector2){data->game_rec.x, data->game_rec.y}, data->cam);
Vector2 max = GetScreenToWorld2D(
(Vector2){
data->game_rec.x + data->game_rec.width,
data->game_rec.y + data->game_rec.height
},
data->camera.cam
data->cam
);
min = Vector2Scale(min, 1.0f/tilemap.tile_size);
max = Vector2Scale(max, 1.0f/tilemap.tile_size);
@ -103,7 +103,7 @@ static void level_scene_render_func(Scene_t* scene)
BeginTextureMode(data->game_viewport);
ClearBackground(WHITE);
BeginMode2D(data->camera.cam);
BeginMode2D(data->cam);
for (int tile_y = min.y; tile_y <= max.y; tile_y++)
{
for (int tile_x = min.x; tile_x <= max.x; tile_x++)
@ -533,7 +533,7 @@ static void toggle_block_system(Scene_t* scene)
&& raw_mouse_pos.y < data->game_rec.height
)
{
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->camera.cam);
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->cam);
unsigned int tile_idx = get_tile_idx(mouse_pos.x, mouse_pos.y, &tilemap);
if (tile_idx >= (tilemap.n_tiles - tilemap.width)) return;
if (tile_idx == last_tile_idx) return;

View File

@ -19,7 +19,7 @@ static void level_scene_render_func(Scene_t* scene)
BeginTextureMode(data->game_viewport);
ClearBackground(WHITE);
BeginMode2D(data->camera.cam);
BeginMode2D(data->cam);
for (size_t i = 0; i < tilemap.n_tiles; ++i)
{
char buffer[6] = {0};

View File

@ -362,10 +362,11 @@ void player_movement_input_system(Scene_t* scene)
// Check if possible to jump when jump is pressed
if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
{
p_pstate->ladder_state = false;
p_cjump->jumps--;
if (!in_water)
{
if (p_mstate->ground_state & 1 || p_cjump->coyote_timer > 0)
if (p_mstate->ground_state & 1)
{
p_ctransform->velocity.y = -p_cjump->jump_speed;
}
@ -379,7 +380,6 @@ void player_movement_input_system(Scene_t* scene)
p_ctransform->velocity.y = -p_cjump->jump_speed / 1.75;
}
p_pstate->ladder_state = false;
p_cjump->coyote_timer = 0;
p_cjump->jumped = true;
p_cjump->jump_ready = false;
@ -1125,7 +1125,7 @@ void movement_update_system(Scene_t* scene)
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap;
// Update movement
float delta_time = DELTA_T; // TODO: Will need to think about delta time handling
float delta_time = 0.017; // TODO: Will need to think about delta time handling
CTransform_t * p_ctransform;
unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ctransform)
@ -1795,77 +1795,29 @@ void sprite_animation_system(Scene_t* scene)
void camera_update_system(Scene_t* scene)
{
LevelSceneData_t* data = &CONTAINER_OF(scene, LevelScene_t, scene)->data;
LevelScene_t* lvl_scene = CONTAINER_OF(scene, LevelScene_t, scene);
Entity_t* p_player;
const int width = data->game_rec.width;
const int height =data->game_rec.height;
data->camera.cam.offset = (Vector2){ width/2.0f, height/2.0f };
Vector2 target_vel = {0};
const int width = lvl_scene->data.game_rec.width;
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(p_player, CTRANSFORM_COMP_T);
data->camera.target_pos.x = p_ctransform->position.x;
target_vel = p_ctransform->velocity;
CMovementState_t* p_movement = get_component(p_player, CMOVEMENTSTATE_T);
CPlayerState_t* p_pstate = get_component(p_player, CPLAYERSTATE_T);
data->camera.target_pos.x += (p_movement->x_dir == 1) ? width/6: -width/6;
if (p_movement->ground_state == 0b01
|| (p_movement->water_state & 1)
|| (p_pstate->ladder_state & 1)
)
{
data->camera.base_y = p_ctransform->position.y;
}
if (p_ctransform->position.y >= data->camera.base_y)
{
data->camera.target_pos.y = p_ctransform->position.y;
data->camera.target_pos.y += p_ctransform->velocity.y * 0.2;
}
lvl_scene->data.cam.offset = (Vector2){ width/2.0f, height/2.0f };
lvl_scene->data.cam.target = p_ctransform->position;
}
data->camera.target_pos.x = Clamp(data->camera.target_pos.x, data->game_rec.width / 2,
fmax(data->tilemap.width * TILE_SIZE, data->game_rec.width) - data->game_rec.width / 2);
data->camera.target_pos.y = Clamp(data->camera.target_pos.y, data->game_rec.height / 2,
fmax(data->tilemap.height * TILE_SIZE, data->game_rec.width) - data->game_rec.height / 2);
// Mass-Spring damper update in x direction
float x = data->camera.target_pos.x - data->camera.cam.target.x;
float v = data->camera.current_vel.x - target_vel.x;
float F = data->camera.k * x - data->camera.c * v;
// Kinematics update
const float dt = DELTA_T;
float a_dt = F * dt / data->camera.mass;
data->camera.cam.target.x += (data->camera.current_vel.x + a_dt * 0.5) * dt;
data->camera.current_vel.x += a_dt;
// Simple lerp for y direction
float dy = data->camera.target_pos.y - data->camera.cam.target.y;
data->camera.cam.target.y += dy * 0.1;
Vector2 max = GetWorldToScreen2D(
(Vector2){
fmax(data->tilemap.width * TILE_SIZE, data->game_rec.width),
fmax(data->tilemap.height * TILE_SIZE, data->game_rec.height)
fmax(lvl_scene->data.tilemap.width * TILE_SIZE, lvl_scene->data.game_rec.width),
fmax(lvl_scene->data.tilemap.height * TILE_SIZE, lvl_scene->data.game_rec.height)
},
data->camera.cam
lvl_scene->data.cam
);
Vector2 min = GetWorldToScreen2D((Vector2){0, 0}, data->camera.cam);
if (max.x < width)
{
data->camera.cam.offset.x = width - (max.x - width/2.0f);
data->camera.cam.target.x = fmax(data->tilemap.width * TILE_SIZE, data->game_rec.width) - width / 2;
data->camera.current_vel.x = 0;
}
if (min.x > 0)
{
data->camera.cam.offset.x = width/2.0f - min.x;
data->camera.cam.target.x = width / 2;
data->camera.current_vel.x = 0;
}
Vector2 min = GetWorldToScreen2D((Vector2){0, 0}, lvl_scene->data.cam);
if (max.x < width) lvl_scene->data.cam.offset.x = width - (max.x - width/2.0f);
if (max.y < height) lvl_scene->data.cam.offset.y = height - (max.y - height/2.0f);
if (min.x > 0) lvl_scene->data.cam.offset.x = width/2.0f - min.x;
if (min.y > 0) lvl_scene->data.cam.offset.y = height/2.0f - min.y;
}
void level_end_detection_system(Scene_t* scene)

View File

@ -27,23 +27,11 @@ typedef struct CoinCounter
uint16_t total;
}CoinCounter_t;
typedef struct LevelCamera {
Camera2D cam;
Vector2 target_pos;
float base_y;
//Vector2 prev_pos;
Vector2 current_vel;
float mass;
float c; // damping factor
float k; // spring constant
}LevelCamera_t;
typedef struct LevelSceneData {
TileGrid_t tilemap;
RenderTexture2D game_viewport;
Rectangle game_rec;
//Camera2D cam;
LevelCamera_t camera;
Camera2D cam;
Sprite_t* tile_sprites[MAX_TILE_SPRITES];
LevelPack_t* level_pack;
unsigned int current_level;

View File

@ -10,13 +10,9 @@ void init_level_scene_data(LevelSceneData_t* data, uint32_t max_tiles, Tile_t* t
//data->game_rec = (Rectangle){25, 25, VIEWABLE_MAP_WIDTH*TILE_SIZE, VIEWABLE_MAP_HEIGHT*TILE_SIZE};
data->game_viewport = LoadRenderTexture(view_zone.width, view_zone.height);
data->game_rec = view_zone;
//data->camera.cam = (Camera2D){0};
memset(&data->camera, 0, sizeof(LevelCamera_t));
data->camera.cam.rotation = 0.0f;
data->camera.cam.zoom = 1.0f;
data->camera.mass = 0.2f;
data->camera.k = 6.2f;
data->camera.c = 2.2f;
data->cam = (Camera2D){0};
data->cam.rotation = 0.0f;
data->cam.zoom = 1.0f;
data->tilemap.max_tiles = max_tiles;
if (tiles != NULL)

View File

@ -34,7 +34,7 @@ static void level_scene_render_func(Scene_t* scene)
BeginTextureMode(data->game_viewport);
ClearBackground(WHITE);
BeginMode2D(data->camera.cam);
BeginMode2D(data->cam);
for (size_t i = 0; i < tilemap.n_tiles; ++i)
{
char buffer[6] = {0};
@ -240,7 +240,7 @@ static void toggle_block_system(Scene_t* scene)
&& raw_mouse_pos.y < data->game_rec.height
)
{
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->camera.cam);
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->cam);
unsigned int tile_idx = get_tile_idx(mouse_pos.x, mouse_pos.y, &tilemap);
if (tile_idx >= MAX_N_TILES) return;
if (tile_idx == last_tile_idx) return;