Browse Source

incremental improvement

pixelbath 1 year ago
parent
commit
971cbb98f8
1 changed files with 74 additions and 16 deletions
  1. 74 16
      drop.lua

+ 74 - 16
drop.lua

@@ -4,6 +4,7 @@
 -- script: lua
 
 t=0
+dt=0
 x=96
 y=24
 
@@ -115,23 +116,80 @@ function draw_balls_and_lasers()
   end
 end
 
+function circleRect(cx, cy, radius, rx, ry, rw, rh)
+
+  -- temporary variables to set edges for testing
+  local testX = cx
+  local testY = cy
+
+  -- which edge is closest?
+  if cx < rx then         testX = rx          -- test left edge
+  elseif cx > rx+rw then  testX = rx+rw end   -- right edge
+  if cy < ry then          testY = ry         -- top edge
+  elseif cy > ry+rh then  testY = ry+rh end   -- bottom edge
+
+  -- get distance from closest edges
+  local distX = cx-testX
+  local distY = cy-testY
+  local distance = math.sqrt( (distX*distX) + (distY*distY) )
+
+  -- if the distance is less than the radius, collision!
+  if distance <= radius then
+    return true
+  end
+  return false
+end
+
 function check_ball_collisions(ball)
   for row=0,num_rows-1 do
     for col=0,num_cols-1 do
       blockVal = blocks[row][col]
       if blockVal > 0 then
-        if ball.x >= col * blockWidth and ball.x <= col * blockWidth + blockWidth then
-          if ball.y >= row * blockHeight and ball.y <= row * blockHeight + blockHeight then
-            if row * blockHeight - ball.y <= 1 or row * blockHeight + blockHeight - ball.y <= 1 then
-              ball.vy = ball.vy * -1
-              blocks[row][col] = blockVal - 1
-              freeze = true
-            else
-              ball.vx = ball.vx * -1
-              blocks[row][col] = blockVal - 1
-              freeze = true
-            end
+        local blockX = col * blockWidth
+        local blockY = row * blockHeight
+        -- if ball.x >= blockX then
+
+        -- end
+        -- if ball.x >= col * blockWidth and ball.x <= col * blockWidth + blockWidth then
+        --   if ball.y >= row * blockHeight and ball.y <= row * blockHeight + blockHeight then
+        --     if row * blockHeight - ball.y <= 1 or row * blockHeight + blockHeight - ball.y <= 1 then
+        --       ball.vy = ball.vy * -1
+        --       blocks[row][col] = blockVal - 1
+        --       freeze = true
+        --     else
+        --       ball.vx = ball.vx * -1
+        --       blocks[row][col] = blockVal - 1
+        --       freeze = true
+        --     end
+        --   end
+        -- end
+
+        if circleRect(ball.x, ball.y, 2, blockX, blockY, blockWidth, blockHeight) then
+          -- bottom
+          if ball.y <= blockY - (blockHeight/2) then
+            -- ball.y = blockY+1
+            ball.vy = ball.vy * -1
+            return
+          end
+          -- top
+          if ball.y >= blockY + (blockHeight/2) then
+            -- ball.y = blockY-1
+            ball.vy = ball.vy * -1
+            return
+          end
+          -- left
+          if ball.x < blockX - (blockWidth/2) then
+            -- ball.x = blockX-1
+            ball.vx = ball.vx * -1
+            return
           end
+          -- right
+          if ball.x > blockX then
+            -- ball.x = blockX+1
+            ball.vx = ball.vx * -1
+            return
+          end
+          freeze = true
         end
       end
     end
@@ -212,8 +270,13 @@ end
 generate_grid()
 
 function TIC()
+  if not freeze then
+    t=time()
+  end
   cls(0)
 
+  draw_blocks()
+
   if launchMode then
     if launchBalls > 0 then
       if launchBallTimer == 0 then
@@ -246,12 +309,7 @@ function TIC()
     draw_preview()
   end
 
-  draw_blocks()
   draw_debug()
-
-  if not freeze then
-    t=t+1
-  end
 end
 
 -- <TILES>