From ef62c52f4ef990c5f51dd3a3d38c90757890b898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sun, 31 Jan 2021 10:14:22 +0100 Subject: Fix edge case --- pong/pong.tdy | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pong/pong.tdy b/pong/pong.tdy index 9778dfc..cb861f9 100644 --- a/pong/pong.tdy +++ b/pong/pong.tdy @@ -45,6 +45,13 @@ or := fn a: bool, b: bool -> bool { ret b } +sign := fn a: float -> float { + if a < 0.0 { + ret -1.0 + } + ret 1.0 +} + rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: float, bw: float, bh: float -> bool { acx := ax + aw / 2. acy := ay + ah / 2. @@ -59,7 +66,11 @@ rect_overlap := fn ax: float, ay: float, aw: float, ah: float, bx: float, by: fl ball_and_paddle_check := fn pad: Paddle, ball: Ball { if rect_overlap(ball.x, ball.y, 0.2, 0.2, pad.x, pad.y, 0.2, 1.) { - ball.vx = -ball.vx + if ball.x < pad.x { + ball.vx = -abs(ball.vx) + } else { + ball.vx = abs(ball.vx) + } } } -- cgit v1.2.1