- if ((newdir ? (base.xm < -SKID_XM) : (base.xm > SKID_XM)) && !timer_started(&skidding_timer) &&
- dir == !newdir && on_ground())
- {
- timer_start(&skidding_timer, SKID_TIME);
-
- play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
-
- }
- dir = newdir;
-
-
- if ((newdir ? (base.xm < 0) : (base.xm > 0)) && !isice(base.x, base.y + base.height) &&
- !timer_started(&skidding_timer))
- {
- base.xm = 0;
- }
-
- if (!duck)
- {
- if (dir == newdir)
- {
- /* Facing the direction we're jumping? Go full-speed: */
-
- if (input.fire == UP)
- {
- base.xm = base.xm + ( newdir ? WALK_SPEED : -WALK_SPEED) * frame_ratio;
-
- if(newdir)
- {
- if (base.xm > MAX_WALK_XM)
- base.xm = MAX_WALK_XM;
- }
- else
- {
- if (base.xm < -MAX_WALK_XM)
- base.xm = -MAX_WALK_XM;
- }
- }
- else if ( input.fire == DOWN)
- {
- base.xm = base.xm + ( newdir ? RUN_SPEED : -RUN_SPEED) * frame_ratio;
-
- if(newdir)
- {
- if (base.xm > MAX_RUN_XM)
- base.xm = MAX_RUN_XM;
- }
- else
- {
- if (base.xm < -MAX_RUN_XM)
- base.xm = -MAX_RUN_XM;
- }
- }
- else
- {
- /* Not facing the direction we're jumping?
- Go half-speed: */
-
- base.xm = base.xm + ( newdir ? (WALK_SPEED / 2) : -(WALK_SPEED / 2)) * frame_ratio;
-
- if(newdir)
- {
- if (base.xm > MAX_WALK_XM / 2)
- base.xm = MAX_WALK_XM / 2;
- }
- else
- {
- if (base.xm < -MAX_WALK_XM / 2)
- base.xm = -MAX_WALK_XM / 2;
- }
- }
- }
-
- }
+ float vx = physic.get_velocity_x();
+ float vy = physic.get_velocity_y();
+ float ax = physic.get_acceleration_x();
+ float ay = physic.get_acceleration_y();
+
+ float dirsign = 0;
+ if(!duck && input.left == DOWN && input.right == UP) {
+ dir = LEFT;
+ dirsign = -1;
+ } else if(!duck && input.left == UP && input.right == DOWN) {
+ dir = RIGHT;
+ dirsign = 1;
+ }
+
+ if (input.fire == UP) {
+ ax = dirsign * WALK_ACCELERATION_X;
+ // limit speed
+ if(vx >= MAX_WALK_XM && dirsign > 0) {
+ vx = MAX_WALK_XM;
+ ax = 0;
+ } else if(vx <= -MAX_WALK_XM && dirsign < 0) {
+ vx = -MAX_WALK_XM;
+ ax = 0;
+ }
+ } else {
+ ax = dirsign * RUN_ACCELERATION_X;
+ // limit speed
+ if(vx >= MAX_RUN_XM && dirsign > 0) {
+ vx = MAX_RUN_XM;
+ ax = 0;
+ } else if(vx <= -MAX_RUN_XM && dirsign < 0) {
+ vx = -MAX_RUN_XM;
+ ax = 0;
+ }
+ }
+
+ // we can reach WALK_SPEED without any acceleration
+ if(dirsign != 0 && fabs(vx) < WALK_SPEED) {
+ vx = dirsign * WALK_SPEED;
+ }
+
+ // changing directions?
+ if(on_ground() && ((vx < 0 && dirsign >0) || (vx>0 && dirsign<0))) {
+ if(fabs(vx)>SKID_XM && !skidding_timer.check()) {
+ skidding_timer.start(SKID_TIME);
+ play_sound(sounds[SND_SKID], SOUND_CENTER_SPEAKER);
+ ax *= 2.5;
+ } else {
+ ax *= 2;
+ }
+ }
+
+ // we get slower when not pressing any keys
+ if(dirsign == 0) {
+ if(fabs(vx) < WALK_SPEED) {
+ vx = 0;
+ ax = 0;
+ } else if(vx < 0) {
+ ax = WALK_ACCELERATION_X * 1.5;
+ } else {
+ ax = WALK_ACCELERATION_X * -1.5;
+ }
+ }
+
+ // if we're on ice slow down acceleration or deceleration
+ if (isice(base.x, base.y + base.height))
+ {
+ /* the acceleration/deceleration rate on ice is inversely proportional to
+ * the current velocity.
+ */
+
+ // increasing 1 will increase acceleration/deceleration rate
+ // decreasing 1 will decrease acceleration/deceleration rate
+ // must stay above zero, though
+ if (ax != 0) ax *= 1 / fabs(vx);
+ }
+
+ physic.set_velocity(vx, vy);
+ physic.set_acceleration(ax, ay);