Compare commits
7 Commits
533e2998bc
...
0a4c700bf6
Author | SHA1 | Date |
---|---|---|
|
0a4c700bf6 | |
|
ae730ce029 | |
|
b56e0e7f10 | |
|
d1d8033b77 | |
|
f8eab8acec | |
|
835b88f1f4 | |
|
064341e2eb |
|
@ -15,6 +15,7 @@
|
|||
#define VIEWABLE_MAP_HEIGHT 16
|
||||
#endif
|
||||
|
||||
#define DELTA_T 0.017
|
||||
#define GRAV_ACCEL 1500
|
||||
#define JUMP_SPEED 70
|
||||
#define MOVE_ACCEL 1300
|
||||
|
|
|
@ -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->cam);
|
||||
Vector2 min = GetScreenToWorld2D((Vector2){data->game_rec.x, data->game_rec.y}, data->camera.cam);
|
||||
Vector2 max = GetScreenToWorld2D(
|
||||
(Vector2){
|
||||
data->game_rec.x + data->game_rec.width,
|
||||
data->game_rec.y + data->game_rec.height
|
||||
},
|
||||
data->cam
|
||||
data->camera.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->cam);
|
||||
BeginMode2D(data->camera.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->cam);
|
||||
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->camera.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;
|
||||
|
|
|
@ -19,7 +19,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
|
||||
BeginTextureMode(data->game_viewport);
|
||||
ClearBackground(WHITE);
|
||||
BeginMode2D(data->cam);
|
||||
BeginMode2D(data->camera.cam);
|
||||
for (size_t i = 0; i < tilemap.n_tiles; ++i)
|
||||
{
|
||||
char buffer[6] = {0};
|
||||
|
|
|
@ -362,11 +362,10 @@ 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)
|
||||
if (p_mstate->ground_state & 1 || p_cjump->coyote_timer > 0)
|
||||
{
|
||||
p_ctransform->velocity.y = -p_cjump->jump_speed;
|
||||
}
|
||||
|
@ -380,6 +379,7 @@ 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 = 0.017; // TODO: Will need to think about delta time handling
|
||||
float delta_time = DELTA_T; // 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,29 +1795,77 @@ void sprite_animation_system(Scene_t* scene)
|
|||
|
||||
void camera_update_system(Scene_t* scene)
|
||||
{
|
||||
LevelScene_t* lvl_scene = CONTAINER_OF(scene, LevelScene_t, scene);
|
||||
LevelSceneData_t* data = &CONTAINER_OF(scene, LevelScene_t, scene)->data;
|
||||
Entity_t* p_player;
|
||||
const int width = lvl_scene->data.game_rec.width;
|
||||
const int height = lvl_scene->data.game_rec.height;
|
||||
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};
|
||||
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);
|
||||
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 = 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;
|
||||
}
|
||||
}
|
||||
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(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)
|
||||
fmax(data->tilemap.width * TILE_SIZE, data->game_rec.width),
|
||||
fmax(data->tilemap.height * TILE_SIZE, data->game_rec.height)
|
||||
},
|
||||
lvl_scene->data.cam
|
||||
data->camera.cam
|
||||
);
|
||||
Vector2 min = GetWorldToScreen2D((Vector2){0, 0}, 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;
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -27,11 +27,23 @@ 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;
|
||||
//Camera2D cam;
|
||||
LevelCamera_t camera;
|
||||
Sprite_t* tile_sprites[MAX_TILE_SPRITES];
|
||||
LevelPack_t* level_pack;
|
||||
unsigned int current_level;
|
||||
|
|
|
@ -10,9 +10,13 @@ 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->cam = (Camera2D){0};
|
||||
data->cam.rotation = 0.0f;
|
||||
data->cam.zoom = 1.0f;
|
||||
//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->tilemap.max_tiles = max_tiles;
|
||||
if (tiles != NULL)
|
||||
|
|
|
@ -34,7 +34,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
|
||||
BeginTextureMode(data->game_viewport);
|
||||
ClearBackground(WHITE);
|
||||
BeginMode2D(data->cam);
|
||||
BeginMode2D(data->camera.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->cam);
|
||||
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->camera.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;
|
||||
|
|
Loading…
Reference in New Issue