/
guicanvas_raycast.cfg
151 lines (139 loc) · 4.7 KB
/
guicanvas_raycast.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// GUICANVAS: simple raycast demo, ported to cubescript by @SalatielSauer (01/09/2023)
_mapPreview = 0
guicanvas.size 32 32
guicanvas.perspective 0 15 // pitch, fov
guicanvas.background = 1
guicanvas.refreshRate = 30
guicanvas.play [
if (_mapPreview) [
guicanvas.setPixel (at $_PLAYER 0) (+ (at $_PLAYER 1) 10) (format "packages/icons/%1.jpg" (playermodelicon))
loop y (listlen $_MAP) [
loop x (listlen (at $_MAP $y)) [
local cell
cell = (at (at $_MAP $y) $x)
if $cell [
guicanvas.setPixel $x (+ $y 10) (format "<mad:1/%1/1>packages/skyboxes/white_ft.jpg" (? (&& (!=s $_lastCell "^f4void") (= $y (at $_lastCell 2)) (= $x (at $_lastCell 3))) (+f 0 (divf (at $_lastCell 0) 7)) 1))
]
]
]
]
] [
guistrut -13
guilist [
guistrut -2
guitextbox (concat "x:^f8" (at $_PLAYER 0) "^n^f~y:^f8" (at $_PLAYER 1) "^n^f~a:^f8" (at $_PLAYER 2) "^n^f~cell:^f8" $_lastCell) 25 0
guilist [guistayopen [
guibutton "^f5respawn" [_PLAYER = [16 5 93.13]; sleep 0 [_RENDER]] 0
guistrut 1
guibutton (format "%1dig" (? (<= (at $_lastCell 0) 1.5) "^f1" "^f4")) [
local cell
cell = $_lastCell
_MAP = (loopconcat my (listlen $_MAP) [
format "[%1]" (
loopconcat mx (listlen (at $_MAP $my)) [
if (&& (<= (at $cell 0) 1.5) (= $my (at $cell 2)) (= $mx (at $cell 3))) [
result "0"
] [result (at (at $_MAP $my) $mx)]
]
)]
)
_RENDER
] 0
guistrut 1
guicheckbox "^f0map" _mapPreview 1 0 [sleep 100 [_RENDER]]
]]
guistrut 10
guilist [
guistrut 10
guitextbox "ray caster demo" 14 0 0x111122
]
guilist [
guistrut 10
guitextbox "avoid walls :)" 14 0 0x111111
]
guistrut -13
]
] 12 0
_MAP = [
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
]
_GETMAPCELL = [at (at $_MAP $arg1) $arg2]
_SCREEN_WIDTH = $guicanvas.height
_SCREEN_HEIGHT = $guicanvas.height
_PI = 3.141592653589793
_PI_OVER_180 = 57.29577951308232
_PI_OVER_16 = 0.19634954084936207
_PI_OVER_4 = 0.7853981633974483
_PI_OVER_2 = 1.5707963267948966
_PLAYER = [16 5 93.13]
_MOVEMENT_SPEED = 0.5
_STEPSIZE = 0.1
_RADIANSTODEGREES = [ *f $arg1 $_PI_OVER_180 ]
_CASTRAY = [
local x y angle _sin _cos dist _result break cellValue
x = $arg1
y = $arg2
angle = $arg3
_sin = (sin (_RADIANSTODEGREES $angle))
_cos = (cos (_RADIANSTODEGREES $angle))
_result = 0
break = 0
while [ (&& (< $dist 10) (! $break)) ] [
x = (+f $x (*f $_cos $_STEPSIZE))
y = (+f $y (*f $_sin $_STEPSIZE))
dist = (+f $dist $_STEPSIZE)
mapX = (floor $x)
mapY = (floor $y)
cellValue = (_GETMAPCELL $mapY $mapX)
if (!= $cellValue 0) [
_result = (concat $dist $cellValue $mapY $mapX)
break = 1
]
]
result $_result;
]
_lastCell = 0
_RENDER = [
local rayAngle cell dist sliceHeight playerX playerY playerDir colorValue
playerX = (at $_PLAYER 0)
playerY = (at $_PLAYER 1)
playerDir = (at $_PLAYER 2)
loop x $_SCREEN_WIDTH [
rayAngle = (+f (-f $playerDir $_PI_OVER_4) (*f (divf $x $_SCREEN_WIDTH) $_PI_OVER_2))
cell = (_CASTRAY $playerX $playerY $rayAngle)
dist = (at $cell 0)
if (= $x (div $_SCREEN_WIDTH 2)) [_lastCell = (? $dist $cell "^f4void")]
colorValue = (divf (- 155 (*f $dist 15)) 255)
sliceHeight = (divf $_SCREEN_HEIGHT $dist)
guicanvas.fillRect $x (divf (-f $_SCREEN_HEIGHT $sliceHeight) 2) 1 $sliceHeight (format "<mad:%1/%1/%1><offset:%2>packages/textures/%3" $colorValue (* 23 $dist) (at ["" "default.png" "nieb/box2.jpg"] (at $cell 1)))
]
guicanvas.refresh
clearconsole
//echo (concat "x:" (at $_PLAYER 0) "y:" (at $_PLAYER 1) "d:" (at $_PLAYER 2))
]
_RENDER
bind right [
_PLAYER = (concat (at $_PLAYER 0) (at $_PLAYER 1) (+f (at $_PLAYER 2) $_PI_OVER_16))
sleep 0 [_RENDER]
]
bind left [
_PLAYER = (concat (at $_PLAYER 0) (at $_PLAYER 1) (-f (at $_PLAYER 2) $_PI_OVER_16))
sleep 0 [_RENDER]
]
bind up [
_PLAYER = (concat (+f (at $_PLAYER 0) (*f $_MOVEMENT_SPEED (cos (_RADIANSTODEGREES (at $_PLAYER 2))))) (+f (at $_PLAYER 1) (*f $_MOVEMENT_SPEED (sin (_RADIANSTODEGREES (at $_PLAYER 2))))) (at $_PLAYER 2))
sleep 0 [_RENDER]
]
bind down [
_PLAYER = (concat (-f (at $_PLAYER 0) (*f $_MOVEMENT_SPEED (cos (_RADIANSTODEGREES (at $_PLAYER 2))))) (-f (at $_PLAYER 1) (*f $_MOVEMENT_SPEED (sin (_RADIANSTODEGREES (at $_PLAYER 2))))) (at $_PLAYER 2))
sleep 0 [_RENDER]
]