Krystman

Pico-8 Hero - Breakout - Deflection Function Test

Nov 25th, 2016
7,467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. box_x = 32
  2. box_y = 58
  3. box_w = 64
  4. box_h = 12
  5.  
  6. rayx = 0
  7. rayy = 0
  8. raydx = -2
  9. raydy = -2
  10.  
  11. debug1 = "debug"
  12.  
  13. function _init()
  14. end
  15.  
  16. function _update()
  17.  if btn(1) then
  18.   rayx+=1
  19.  end
  20.  if btn(0) then
  21.   rayx-=1
  22.  end
  23.  if btn(2) then
  24.   rayy-=1
  25.  end
  26.  if btn(3) then
  27.   rayy+=1
  28.  end
  29. end
  30.  
  31. function _draw()
  32.  cls()
  33.  rect(box_x, box_y, box_x+box_w, box_y+box_h, 7)
  34.  local px, py = rayx, rayy
  35.  repeat
  36.   pset(px, py, 8)
  37.   px+=raydx
  38.   py+=raydy
  39.  until px<0 or px>128 or py < 0 or py > 128
  40.  if deflx_ballbox(rayx,rayy,raydx,raydy,box_x,box_y,box_w,box_h) then
  41.   print("horizontal")
  42.  else
  43.   print("vertical")
  44.  end
  45.  print(debug1)
  46. end
  47.  
  48. function hit_ballbox(bx,by,tx,ty,tw,th)
  49.  if bx+ball_r < tx then return false end
  50.  if by+ball_r < ty then return false end
  51.  if bx-ball_r > tx+tw then return false end
  52.  if by-ball_r > ty+th then return false end
  53.  return true
  54. end
  55.  
  56. function deflx_ballbox(bx,by,bdx,bdy,tx,ty,tw,th)
  57.  -- calculate wether to deflect the ball
  58.  -- horizontally or vertically when it hits a box
  59.  if bdx == 0 then
  60.   -- moving vertically
  61.   return false
  62.  elseif bdy == 0 then
  63.   -- moving horizontally
  64.   return true
  65.  else
  66.   -- moving diagonally
  67.   -- calculate slope
  68.   local slp = bdy / bdx
  69.   local cx, cy
  70.   -- check variants
  71.   if slp > 0 and bdx > 0 then
  72.    -- moving down right
  73.    debug1="q1"
  74.    cx = tx-bx
  75.    cy = ty-by
  76.    if cx<=0 then
  77.     return false
  78.    elseif cy/cx < slp then
  79.     return true
  80.    else
  81.     return false
  82.    end
  83.   elseif slp < 0 and bdx > 0 then
  84.    debug1="q2"
  85.    -- moving up right
  86.    cx = tx-bx
  87.    cy = ty+th-by
  88.    if cx<=0 then
  89.     return false
  90.    elseif cy/cx < slp then
  91.     return false
  92.    else
  93.     return true
  94.    end
  95.   elseif slp > 0 and bdx < 0 then
  96.    debug1="q3"
  97.    -- moving left up
  98.    cx = tx+tw-bx
  99.    cy = ty+th-by
  100.    if cx>=0 then
  101.     return false
  102.    elseif cy/cx > slp then
  103.     return false
  104.    else
  105.     return true
  106.    end
  107.   else
  108.    -- moving left down
  109.    debug1="q4"
  110.    cx = tx+tw-bx
  111.    cy = ty-by
  112.    if cx>=0 then
  113.     return false
  114.    elseif cy/cx < slp then
  115.     return false
  116.    else
  117.     return true
  118.    end
  119.   end
  120.  end
  121.  return false
  122. end
Advertisement
Add Comment
Please, Sign In to add comment