Sorceress

Raycatter Dev : Strafing Test

Dec 13th, 2020
1,117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '(c) 2020, @_sorceress
  2.  
  3. SCREEN _NEWIMAGE(640, 480, 256)
  4. _TITLE "Raycatter Dev : Strafing Test"
  5.  
  6. 'generate random test map
  7. RANDOMIZE TIMER
  8. DIM g(15, 15)
  9. FOR x = 0 TO 15: FOR y = 0 TO 15
  10.     IF RND < .3 THEN gv = 1 ELSE gv = 0
  11.     IF x MOD 15 = 0 OR y MOD 15 = 0 THEN gv = 1
  12.     IF ABS(x - 5) + ABS(y - 5) < 3 THEN gv = 0
  13.     g(x, y) = gv
  14. NEXT: NEXT
  15.  
  16. 'player behaviour
  17. px = 5: py = 5: pa = 0: pelev = 0
  18. turnspeed = 5
  19. walkspeed = 2.5
  20.  
  21. FPS = 30
  22. dt = 1 / FPS
  23.  
  24. DO
  25.   '### input
  26.   fv = 0: sv = 0
  27.   IF ScanKey%(17) THEN fv = fv + walkspeed
  28.   IF ScanKey%(31) THEN fv = fv - walkspeed
  29.   IF ScanKey%(30) THEN sv = sv + walkspeed
  30.   IF ScanKey%(32) THEN sv = sv - walkspeed
  31.   WHILE _MOUSEINPUT
  32.     pa = pa + turnspeed * (_MOUSEMOVEMENTX / 1000)
  33.     pelev = pelev + turnspeed * (_MOUSEMOVEMENTY / 1000)
  34.     IF ABS(pelev) > 1 THEN pelev = SGN(pelev)
  35.   WEND
  36.  
  37.   '### physics
  38.   pacos = COS(pa): pasin = SIN(pa)
  39.   xvt = fv * pacos + sv * pasin
  40.   yvt = fv * pasin - sv * pacos
  41.   xv = xv + SGN(xvt - xv) * 4 * dt
  42.   yv = yv + SGN(yvt - yv) * 4 * dt
  43.   px1 = px + xv * dt: py1 = py + yv * dt
  44.   x1 = px1: y1 = py: GOSUB cbox: IF ok = 0 THEN px = px1
  45.   x1 = px: y1 = py1: GOSUB cbox: IF ok = 0 THEN py = py1
  46.  
  47.   '### raycatter
  48.   h0 = 240 - 240 * pelev
  49.   LINE (0, 0)-(639, h0), 11, BF
  50.   LINE (0, h0)-(639, 479), 8, BF
  51.   x = 320 * (1 - TAN(pa - 3.141 / 2)): y = h0 - 320
  52.   IF pasin > 0 THEN CIRCLE (x, y), 32, 14: PAINT (x, y + 30), 14, 14
  53.   FOR sx = 0 TO 639: e = sx / 320 - 1 'x-fov
  54.     x = px: y = py 'ray origin
  55.     rx = pacos - e * pasin: ry = pasin + e * pacos 'ray vector
  56.     d = 0 'terribly inefficient way of catting rays
  57.     WHILE g(INT(x), INT(y)) = 0
  58.       x = x + rx * .01: y = y + ry * .01: d = d + .01
  59.     WEND
  60.     h = 100 / d 'draw column
  61.     LINE (sx, h0 - h)-(sx, h0 + h), (3 * INT(x) + INT(y)) MOD 7 + 1
  62.   NEXT
  63.  
  64.   '### draw minimap
  65.   m = 8
  66.   FOR x = 0 TO 15: FOR y = 0 TO 15
  67.       LINE (x * m, y * m)-(x * m + m, y * m + m), g(x, y) * 8, BF
  68.   NEXT: NEXT
  69.   x = m * px: y = m * py: ux = m * pacos: uy = m * pasin
  70.   LINE (x - ux, y - uy)-(x + ux, y + uy), 3: LINE -(x - uy, y + ux), 3: LINE -(x + uy, y - ux), 3: LINE -(x + ux, y + uy), 3
  71.  
  72.   '### text feedback
  73.   LOCATE 1, 20: PRINT px; py; pa; pelev
  74.   _DISPLAY: _LIMIT FPS
  75. LOOP
  76.  
  77. '################################################################################
  78.  
  79. 'collision box test. a must be smaller than 0.5 to work properly.
  80. cbox:
  81. a = .4
  82. ok = g(INT(x1 - a), INT(y1 - a)) + g(INT(x1 + a), INT(y1 - a)) + g(INT(x1 - a), INT(y1 + a)) + g(INT(x1 + a), INT(y1 + a))
  83. RETURN
  84.  
  85. 'is key down?
  86. FUNCTION ScanKey% (scancode%)
  87.   STATIC Ready%, keyflags%()
  88.   IF NOT Ready% THEN REDIM keyflags%(0 TO 127): Ready% = -1
  89.   i% = INP(&H60)
  90.   IF (i% AND 128) THEN keyflags%(i% XOR 128) = 0
  91.   IF (i% AND 128) = 0 THEN keyflags%(i%) = -1
  92.   K$ = INKEY$
  93.   ScanKey% = keyflags%(scancode%)
  94. END FUNCTION
  95.  
RAW Paste Data