Attribute VB_Name = "Raycast"
Global RAYSIZE

Type stat
        X As Integer
        Y As Integer
        va As Integer
End Type

'$DYNAMIC
'Screen 7, 0, 1, 0
Dim Shared sint(359), cost(359)
Dim Shared plr As stat, map(20, 20)
Const pi = 22 / 7



Rem $STATIC
Function Cg(va As Integer)
If va < 0 Then va = va + 360
If va > 359 Then va = va - 360
If map(plr.X - sint(va), plr.Y - cost(va)) > 0 Then Cg = 0 Else Cg = 1
If Cg = 0 Then crtouttext$ = "bump..."

End Function

Public Sub Engine()
u$ = Chr$(0) + "H": d$ = Chr$(0) + "P"
l$ = Chr$(0) + "K": r$ = Chr$(0) + "M"
endd$ = Chr$(0) + "O": pgd$ = Chr$(0) + "Q"

        Raycast
        
        G$ = grkeyascii$
        'If G$ = Chr$(27) Then End
        If G$ = u$ Or G$ = "I" Then
                If Cg(plr.va) Then
                        MoveIt (plr.va)
                End If
        ElseIf G$ = d$ Or G$ = "M" Then
                If Cg(plr.va - 180) Then
                        MoveIt (plr.va - 180)
                End If
        ElseIf G$ = l$ Or G$ = "J" Then
                plr.va = plr.va - 5
                If plr.va < 0 Then plr.va = plr.va + 360
                If plr.va > 359 Then plr.va = plr.va - 360
        ElseIf G$ = r$ Or G$ = "K" Then
                plr.va = plr.va + 5
                If plr.va < 0 Then plr.va = plr.va + 360
                If plr.va > 359 Then plr.va = plr.va - 360
        ElseIf G$ = endd$ Or G$ = "Z" Then
                If Cg(plr.va - 90) Then
                        MoveIt (plr.va - 90)
                End If
        ElseIf G$ = pgd$ Or G$ = "X" Then
                If Cg(plr.va + 90) Then
                        MoveIt (plr.va + 90)
                End If
        End If
        grkeyascii$ = ""
         moveupdown$ = ""
         moverightleft$ = ""
End Sub

Public Sub initengraycast()

For I = 0 To 359
        r = I * (pi / 180)
        sint(I) = Sin(r)
        cost(I) = Cos(r)
Next
Close
Open App.Path + "\raycast.dat" For Input As 1

For Y = 1 To 10: For X = 10 To 1 Step -1
        Input #1, a$: map(X, Y) = Val(a$): If map(X, Y) = 2 Then map(X, Y) = 0: plr.X = X: plr.Y = Y
Next: Next
Close
End Sub

Sub MoveIt(va As Integer)
If va < 0 Then va = va + 360
If va > 359 Then va = va - 360
plr.X = plr.X - sint(va)
plr.Y = plr.Y - cost(va)
End Sub

Sub Raycast()
GR.Cls
'WAIT &H3DA, 8
GR.Print plr.X; plr.Y; plr.va
ofs = 40: s = 10
For I = 30 To -30 Step -1
        ofs = ofs + 2
        a = plr.va - I
        If a < 0 Then a = a + 360
        If a > 359 Then a = a - 360
        X = plr.X * s: Y = plr.Y * s
        d = 0
        Do
                X = X - sint(a)
                Y = Y - cost(a)
                d = d + 1
        Loop Until map(X / s, Y / s)
        ds = (s ^ (2.5)) / d
        GR.Line (ofs * RAYSIZE, (90 - ds) * RAYSIZE)-((ofs + 1) * RAYSIZE, (90 + ds) * RAYSIZE), QBColor(map(X / s, Y / s)), BF
Next
'PCOPY 1, 0
End Sub

