_TITLE "Block mouse box entry to another box" ' demo for Prithak by B+ 2019-01-23 ' from Collision of Rectangular Images started by Johnno mod b+ 2018-06-10"
' 2018-06-10 mod by B+ change for x, y, w, h of images
' by readjusting all the variables and use STEP for box drawing
' Generalize the specific gosub routine from this one app so can reuse IN ANY APP using sprites / tiles / images
SCREEN _NEWIMAGE(800, 600, 32) '<<< something more standard center is 400, 300
' sprites / tiles / images are typically referred to by their left top corner ie X, Y and their width and height
'lets do the height and width first of the forbidden box
box1Width = 400 '<<< mod add this instead of above
box1Height = 100 '<<< mod add this instead of above
'now center the forbidden box
box1Left = 400 - box1Width / 2 'same as box1X
box1Top = 300 - box1Height / 2 'same as box1Y
'the box for the character at the mouse pointer
mouseboxWidth = 50 '<<< mod add these constants
mouseboxHeight = 40 '<<< mod add these constants
'box of forbidden entry
LINE (box1Left
, box1Top
)-STEP(box1Width
, box1Height
), _RGB32(255, 255, 255), BF
_PRINTSTRING (box1Left
+ 120, box1Top
+ 45), "Forbidden to Enter Box"
mouseboxX
= _MOUSEX - mouseboxWidth
/ 2 mouseboxY
= _MOUSEY - mouseboxHeight
/ 2
IF collision%
(box1Left
, box1Top
, box1Width
, box1Height
, mouseboxX
, mouseboxY
, mouseboxWidth
, mouseboxHeight
) = 1 THEN mouseboxX = oldMouseBoxX: mouseboxY = oldMouseBoxY
oldMouseBoxX = mouseboxX: oldMouseBoxY = mouseboxY
'draw character at mouse box
LINE (mouseboxX
, mouseboxY
)-STEP(mouseboxWidth
, mouseboxHeight
), _RGB32(255, 128, 0), BF
'<<< use step with width and height
'very handy function to detect if 2 boxes will overlap, given their top left corner and width and height, Thanks Johnno
FUNCTION collision%
(b1x
, b1y
, b1w
, b1h
, b2x
, b2y
, b2w
, b2h
) ' yes a type smaller than integer might be used
' x, y represent the box left most x and top most y
' w, h represent the box width and height which is the usual way sprites / tiles / images are described
' such that boxbottom = by + bh
' and boxright = bx + bw
'so the specific gosub above is gerealized to a function procedure here!
IF (b1y
+ b1h
< b2y
) OR (b1y
> b2y
+ b2h
) OR (b1x
> b2x
+ b2w
) OR (b1x
+ b1w
< b2x
) THEN collision% = 0
collision% = 1