indexing description: "[ Let yout EM_DRAWABLE inherit from this class so that you can set it's movement to a constant speed. The movement is not influenced by the framerate. ]" date: "$Date$" revision: "$Revision$" class EM_NORMALIZED_SPEED inherit EM_TIME_SINGLETON feature -- status set_speed (a_speed: INTEGER) is -- Set `speed' to `a_speed'. do if speed = 0 and a_speed /= 0 then last_time_pos_changed := time.ticks end speed := a_speed ensure speed_set: speed = a_speed end speed: INTEGER -- Speed in distance (i.e. pixels) per second. get_delta: INTEGER is -- Distance `Current' has to move to reflect `speed' local cur_time: INTEGER pixel_count: INTEGER do cur_time := time.ticks if cur_time - last_time_pos_changed>2000 then last_time_pos_changed := cur_time end -- I need to smooth out here since ticks is not precise enough -- the result is a very smooth motion but it is required that -- the frame rate is stable which will be the case in most situations. -- If not decrease the smoothing_parameter that is set to 95% per default interval := interval * smoothing_parameter + ( cur_time - last_time_pos_changed) * (1 - smoothing_parameter) last_time_pos_changed := cur_time -- I have to trick a lot here to make motion slower then one pixel per -- frame possible position := position + ( (interval / 1000) * speed).abs if position - last_pixel_count > 1.0 then pixel_count := (position - last_pixel_count).rounded last_pixel_count := last_pixel_count + pixel_count if speed < 0 then pixel_count := - pixel_count end end result := pixel_count end feature {NONE} -- implementation smoothing_parameter: DOUBLE is 0.9 -- See comment in `update_cur_position' last_time_pos_changed: INTEGER -- The last time `cur_position' was updated last_pixel_count: INTEGER -- The Last over all position quantizised by pixel position: DOUBLE -- The last over all position precise interval: DOUBLE -- Every `interval' ms a call to get_delta is made end