Skip to content

Commit

Permalink
Rollback ex9
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed May 15, 2018
1 parent 59ae40e commit 74d33d1
Showing 1 changed file with 0 additions and 86 deletions.
86 changes: 0 additions & 86 deletions src/exercise/9-combining-observables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ function getMousePos(canvas: HTMLCanvasElement, evt: MouseEvent) {
function drawPaddle(game: GameUI, side: 'left' | 'right') {
let y = side === 'left' ? game.paddleL : game.paddleR;
let { ctxt } = game;
// if (side === 'left') {
// ctxt.clearRect(0, 0, 25, game.board.size.h);
// } else {
// ctxt.clearRect(game.board.size.w - 25, 0, 25, game.board.size.h);
// }
ctxt.beginPath();
ctxt.fillStyle = 'grey';
if (side === 'left') {
Expand Down Expand Up @@ -148,25 +143,6 @@ function setupGame(cnv: HTMLCanvasElement): GameUI {
return game;
}

function boundedMouseMoveObservable(
game: GameUI
): Observable<{ x: number; y: number }> {
const { cnv } = game;
return fromEvent<MouseEvent>(window, 'mousemove').pipe(
map(m => getMousePos(cnv, m)),
map(m => {
let xp = 35 * game.board.size.ratio;
let y = Math.max(35, Math.min(m.y, game.board.size.h - 35));
let x = Math.max(xp, Math.min(m.x, game.board.size.w - xp));
return {
y,
x
};
}),
distinctUntilChanged()
);
}

window.onload = () => {
const cnv: HTMLCanvasElement | null = document.querySelector('.board');
if (cnv === null) {
Expand All @@ -175,25 +151,6 @@ window.onload = () => {
let game = setupGame(cnv);

let gameSubject = new BehaviorSubject<GameUI>(game);
gameSubject.forEach((g: GameUI) => {
let $scoreL = document.querySelector('.score.score-l');
let $scoreR = document.querySelector('.score.score-r');
if ($scoreL) {
$scoreL.innerHTML = `${g.scoreL}`;
}
if ($scoreR) {
$scoreR.innerHTML = `${g.scoreR}`;
}
});

boundedMouseMoveObservable(game)
.pipe(withLatestFrom(gameSubject))
.subscribe(([p, g]) => {
let paddleL = p.y;
let paddleR = p.x / g.board.size.ratio;
let newGame = { ...g, paddleL, paddleR };
gameSubject.next(newGame);
});

let {
board: {
Expand All @@ -205,47 +162,4 @@ window.onload = () => {
y: h / 2,
theta: 0.3
});

interval(30)
.pipe(withLatestFrom(ballSubject, gameSubject))
.forEach(([_, ball, g]) => {
let { x, y, theta } = ball;
x += 8 * Math.cos(theta);
y -= 8 * Math.sin(theta);
if (
x <= 20 + 2 * BALL_RADIUS &&
(y < g.paddleL + PADDLE_HEIGHT && y > g.paddleL - PADDLE_HEIGHT)
) {
theta = Math.PI - theta;
} else if (
x > g.board.size.w - 20 - 2 * BALL_RADIUS &&
(y < g.paddleR + PADDLE_HEIGHT && y > g.paddleR - PADDLE_HEIGHT)
) {
theta = Math.PI - theta;
} else if (x <= 5 || x > g.board.size.w - 10) {
// left/right wall bounce
theta = Math.PI - theta;
if (x <= 5) {
g.scoreR++;
x = 16;
} else {
g.scoreL++;
x = g.board.size.w - 16;
}
gameSubject.next(g);
} else if (y <= 13 || y > g.board.size.h - 13) {
// top/bottom wall bounce
theta = -theta;
}
ballSubject.next({ x, y, theta });
});

ballSubject.pipe(withLatestFrom(gameSubject)).subscribe(([b, g]) => {
let { x, y, theta } = b;
g.ctxt.clearRect(5, 0, g.cnv.width - 10, g.cnv.height);
drawPaddle(g, 'left');
drawPaddle(g, 'right');
drawBall(g, { x, y });
drawBallMotionVector(g, { x, y }, theta);
});
};

0 comments on commit 74d33d1

Please sign in to comment.