Skip to content

Commit

Permalink
better syntax and fixes for stopped detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Oct 1, 2023
1 parent d7c538f commit 57f1750
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions rellax_demo/rellax.asc
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@ float _cam_tween_duration;
RellaxTweenEasingType _cam_tween_type;

// Previous character position
float _prev_c_x, _prev_c_y;
int _prev_c_x, _prev_c_y;
int _target_stop_counter;
bool _is_target_stopped;

// Camera offsets
int _off_x, _off_y;
int _off_applied_x, _off_applied_y;

// smoothed offset
int _look_ahead_x;
int _look_ahead_y;

// Interpolation factors
float _cam_lerp_factor_x;
float _cam_lerp_factor_y;
float _y_multiplier;

// Current camera position
PointF* _cam_pos;

// Partial character height
int _partial_c_height;

// Number of ticks for standing still
int _count_still_ticks;

// Flags
bool _is_doRoomSetup;
bool _DebugShow;
Expand Down Expand Up @@ -495,10 +495,10 @@ PointF* _doCameraTracking()
PointF* cam_target = PointF.New(_TargetPos.x + focus.x, _TargetPos.y + focus.y);
Point* game_camera = _get_camera_position();

bool is_outside_cam_window = FloatToInt(cam_target.x)-Game.Camera.Width/2-game_camera.x<=-_cam_window_w/2 ||
FloatToInt(cam_target.x)-Game.Camera.Width/2-game_camera.x>_cam_window_w/2 ||
FloatToInt(cam_target.y)-Game.Camera.Height/2-game_camera.y<=-_cam_window_h/2 ||
FloatToInt(cam_target.y)-Game.Camera.Height/2-game_camera.y>_cam_window_h/2;
int cam_window_check_x = FloatToInt(cam_target.x)-Game.Camera.Width/2-game_camera.x;
int cam_window_check_y = FloatToInt(cam_target.y)-Game.Camera.Height/2-game_camera.y;
bool is_outside_cam_window = (cam_window_check_x <= -_cam_window_w/2) || (cam_window_check_x > _cam_window_w/2) ||
(cam_window_check_y <= -_cam_window_h/2) || (cam_window_check_y > _cam_window_h/2);

if(is_outside_cam_window){
cam_target.x = _ClampFloat(cam_target.x - IntToFloat(Game.Camera.Width/2),
Expand All @@ -511,8 +511,16 @@ PointF* _doCameraTracking()
cam_target.y = IntToFloat(game_camera.y);
}

_prev_c_x = _TargetPos.x;
_prev_c_y = _TargetPos.y;
int c_new_x = FloatToInt(cam_target.x);
int c_new_y = FloatToInt(cam_target.y);
if((_prev_c_x == c_new_x) && (_prev_c_y == c_new_y)) {
_target_stop_counter++;
} else {
_target_stop_counter = 0;
}
_is_target_stopped = _target_stop_counter > 2;
_prev_c_x = c_new_x;
_prev_c_y = c_new_y;
return cam_target;
}

Expand Down Expand Up @@ -576,17 +584,20 @@ void _drawDebugOverlay()

void _quickAdjustToTarget()
{
PointF* p = _doCameraTracking();
_next_cam_pos.Set(p);
PointF* cam_target = _doCameraTracking();
_next_cam_pos.Set(cam_target);
_cam_pos.Set(_next_cam_pos);
_set_camera_position(FloatToInt(p.x), FloatToInt(p.y));
_set_camera_position(FloatToInt(cam_target.x), FloatToInt(cam_target.y));
}

void doSmoothCameraTracking()
{
if(_prev_c_x == _TargetPos.x && _prev_c_y == _TargetPos.y) {
if(_tween_cam_origin == null && _next_cam_pos != null) {
_tween_cam_origin = PointF.New(_next_cam_pos.x, _next_cam_pos.y);
PointF* cam_target = _doCameraTracking();
Point* game_camera = _get_camera_position();

if(_is_target_stopped ) {
if(_tween_cam_origin == null) {
_tween_cam_origin = PointF.New(IntToFloat(game_camera.x), IntToFloat(game_camera.y));
_cam_tween_elapsed = 0.0;
} else {
if(_cam_tween_elapsed < _cam_tween_duration) {
Expand All @@ -599,29 +610,34 @@ void doSmoothCameraTracking()
else {
_tween_cam_origin = null;
}

PointF* p = _doCameraTracking();
float filter_coefficient = 0.9;
Point* game_camera = _get_camera_position();

if(FloatToInt(p.x) != game_camera.x || FloatToInt(p.y) != game_camera.y) {

if(_tween_cam_origin != null) {
float tween_factor = _rellax_tween_GetValue(_cam_tween_elapsed, _cam_tween_duration, _cam_tween_type);
_next_cam_pos = _LerpPFEx(_tween_cam_origin, p, tween_factor, tween_factor);
} else {

if(_cam_lerp_factor_x > 0.04 && _cam_lerp_factor_y > 0.04) {
_next_cam_pos = _LerpPFEx(_cam_pos, p, _cam_lerp_factor_x, _cam_lerp_factor_y*_y_multiplier);

if(FloatToInt(cam_target.x) != game_camera.x || FloatToInt(cam_target.y) != game_camera.y)
{
if(_tween_cam_origin != null)
{
if(_cam_tween_elapsed < _cam_tween_duration)
{
float tween_factor = _rellax_tween_GetValue(_cam_tween_elapsed, _cam_tween_duration, _cam_tween_type);
_next_cam_pos = _LerpPF(_tween_cam_origin, cam_target, tween_factor);
} else {
float d_x = _ClampFloat( Maths.Sqrt((_cam_pos.x-p.x)*(_cam_pos.x-p.x)/4.0), 1.0, 8.0);
float d_y = _ClampFloat( Maths.Sqrt((_cam_pos.y-p.y)*(_cam_pos.y-p.y)/4.0), 1.0, 8.0);

_next_cam_pos.x = cam_target.x;
_next_cam_pos.y = cam_target.y;
}
}
else
{
if(_cam_lerp_factor_x > 0.04 && _cam_lerp_factor_y > 0.04)
{
_next_cam_pos = _LerpPFEx(_cam_pos, cam_target, _cam_lerp_factor_x, _cam_lerp_factor_y);
}
else
{
float d_x = _ClampFloat( Maths.Sqrt((_cam_pos.x-cam_target.x)*(_cam_pos.x-cam_target.x)/4.0), 1.0, 8.0);
float d_y = _ClampFloat( Maths.Sqrt((_cam_pos.y-cam_target.y)*(_cam_pos.y-cam_target.y)/4.0), 1.0, 8.0);
float clfx = 0.4/d_x;
float clfy = 0.4/d_y;

_next_cam_pos = _LerpPFEx(_cam_pos, p, clfx, clfy);
_next_cam_pos = _LerpPFEx(_cam_pos, cam_target, clfx, clfy);
}
}
}
Expand Down Expand Up @@ -856,18 +872,18 @@ void doRoomSetup()
// --- callbacks --------------------------------------------------------------

function on_event (EventType event, int data){
// player exits any room
if (event==eEventLeaveRoom){

if (event==eEventLeaveRoom)
{ // player exits any room
// reset the parallax object positions
for(int i=0; i<_pxo_count; i++){
_pxo[i].X=_pxoRoomStartX[i];
_pxo[i].Y=_pxoRoomStartY[i];
}
_is_doRoomSetup = false;
}

// player enters a room that's different from current
if (event==eEventEnterRoomBeforeFadein){
else if (event==eEventEnterRoomBeforeFadein)
{ // player enters a room that's different from current
if(!_is_doRoomSetup){
doRoomSetup();
}
Expand Down

0 comments on commit 57f1750

Please sign in to comment.