thoughtram / reactive-snake

Taming snakes with reactive streams
83 stars 27 forks source link

there is a bug #2

Open kiyonlin opened 5 years ago

kiyonlin commented 5 years ago

Suppose now that the snake is moving to the "right". I quickly press the "up" and "left" directions keys in the speed time. The direction$ will push the "up" and "left", but withLatestFrom only Taking "Left", "Up" will be ignored, causing the snake to immediately go to the "Left" and collide with itself.

So both ticks$ and direction$ should make the snake moving.

I think change snake$ to be like this will resolve this bug:

let snake$: Observable<Array<Point2D>> = combineLatest(ticks$, direction$).pipe(
    withLatestFrom(snakeLength$, ([_, direction], snakeLength) => [direction, snakeLength]),
    scan(move, generateSnake()),
    share()
  );
CHBaker commented 5 years ago

@kiyonlin I noticed the bug too, tried your fix. The problem is now the speed of turning is much faster than the speed of going straight, and the game gets choppy

Vikasg7 commented 3 years ago

The problem with both is:-
combineLatest - Next tick is not respecting keyevents resulting in multiple steps while making turns. withLatestFrom - Key events are getting lost between ticks.

So both combineLatest and withLatestFrom are not ideal for this. I think, we need some observable that resets the timer/interval on each key event so that the next tick is generated after N milliseconds of previous keyevent/tick.

PS:- Wrote an operator to achieve the required behaviour. Link