Advertisement
Stargrove

Untitled

Nov 18th, 2017
363
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 1 0
  1. /// scr_gridCollisionLine(startX, startY, endX, endY, tileType);
  2. // REQUIRED MACROS TILESIZE => size of tiles and TILESIZE1 => one pixel less!
  3. /// @param startx
  4. /// @param starty
  5. /// @param endx
  6. /// @param endy
  7. /// @param tiletype
  8.  
  9. var sx = argument0;
  10. var sy = argument1;
  11. var tx = argument2;
  12. var ty = argument3;
  13. var tileType = argument4;
  14.  
  15. // === scr_tileCheck Start + Endpoints === //
  16. if (scr_tileCheck(sx, sy, tileType) || scr_tileCheck(tx, ty, tileType)) return true;
  17.  
  18. // === Iterate X-Axis === //: scr_tileCheck only precise axis intersections
  19. var deltax=abs(tx div TILESIZE - sx div TILESIZE);
  20. if deltax>0{// if start + end not in the same cell
  21. var xslope = ((ty-sy)/(tx-sx)),// Determine Linear Equation Slope, solving y(x);
  22. dirx = sign(tx-sx),// get direction along xaxis
  23. cx = (sx&~TILESIZE1)+TILESIZE1*(dirx>0),// From Start X get nearest intersection with grid - LEFT or RIGHT (does not escape the start cell);
  24. cy = sy+xslope*(cx-sx);// Mathematically determine the appropriate Y coordinate
  25. if scr_tileCheck(cx, cy, tileType) return true;
  26.  
  27. repeat(deltax-1){// repeat as often as there are as of yet unchecked cells between Start X and End X;
  28. cx+=TILESIZE*dirx;// If no collision move along X on by given grid_dimensions of 32px;
  29. cy+=xslope*TILESIZE*dirx;
  30. if scr_tileCheck(cx, cy, tileType) return true;
  31. }
  32. }
  33.  
  34. // === Iterate X-Axis === //: scr_tileCheck only precise axis intersections
  35. var deltay=abs((ty div TILESIZE) - (sy div TILESIZE));
  36. if deltay>0{
  37. var yslope=((tx-sx)/(ty-sy)),
  38. diry = sign(ty-sy),
  39. cy=(sy&~TILESIZE1)+TILESIZE1*(diry>0),
  40. cx=sx+yslope*(cy-sy);
  41. if scr_tileCheck(cx, cy, tileType) return true;
  42.  
  43. repeat(deltay-1){
  44. cy+=TILESIZE*diry;
  45. cx+=yslope*TILESIZE*diry;
  46. if scr_tileCheck(cx, cy, tileType) return true;
  47. }
  48. }
  49.  
  50. return false; // if all checks passed!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement