diff --git a/guicanvas.cfg b/guicanvas.cfg new file mode 100644 index 0000000..793b40f --- /dev/null +++ b/guicanvas.cfg @@ -0,0 +1,392 @@ +// GUICANVAS by @SalatielSauer (01/09/2023) +// github.com/CubeScript/Sauer-GUICANVAS +// the screen consists of a model with dozens of meshes, each mesh has an index that is used as a pixel; +// the pixel color/texture is set using objskin ; +// the model can be loaded in game with `mmodel guicanvas` or in a menu with `guimodelpreview "guicanvas"`; +// this file must be executed (/exec guicanvas.cfg) at least once to enable the guicanvas.* commands. + +guicanvas.previewMode = 0 +_guicanvasEditor = [ + notepadfile = $arg1 + newgui _ [ guistayopen [ + guinoautotab [ + guieditor $notepadfile -80 20 + textinit $notepadfile $notepadfile + guilist [ + guilist [ + guifield notepadfile -25 + guilist [ + guibutton "^f8save file" [textfocus $notepadfile; textsave $notepadfile] 0 + guibar + guibutton "^f1exec file" [textfocus $notepadfile; textexec $notepadfile] 0 + guibar + guibutton "^f8load file" [textfocus $notepadfile; textload $notepadfile] 0 + ] + ] + guibar + guilist [ + guibutton "^f1copy all" [textfocus $notepadfile; textselectall; textcopy] 0 + guibutton "^f1paste" [textfocus $notepadfile; textpaste] 0 + ] + guibar + guilist [ + guibutton "^f3clear all" [textfocus $notepadfile; textclear] 0 + ] + guibar + guilist [ + guicheckbox "^f8show preview" guicanvas.previewMode + guibutton "^f1exec & preview" [textfocus $notepadfile; textexec $notepadfile; sleep 0 [guicanvas.refresh]; guicanvas.previewMode = 1] 0 + ] + ] + guistrut -2 + guialign 1 [guitextbox "GUICANVAS Editor 1.0" 0 0 0xccaacc] + + if (guicanvas.previewMode) [ + guistrut -28 + guialign 1 [ + guistrut 10 + guimodelpreview "../../packages/models/guicanvas" [] 1 10 + guistrut -8 + ] + guistrut 8 + ] + ] + ]] 0 + showgui _ +] + +_guicanvas = [ + if (|| (! (getalias guicanvas)) (=s (getalias guicanvas) "")) [ + guicanvas = [ + if (=s $arg1 "notepad") [_guicanvasEditor] [ + local canvas + canvas = [] + loopfiles cfg "guicanvas" "cfg" [append canvas $cfg] + inputcommand $arg1 [ + local isNum + isNum = (&& (>= (strcode $commandbuf) 48) (<= (strcode $commandbuf) 57)) + if (! $isNum) [ + local newCanvas saveCanvas + newCanvas = (concatword "guicanvas/" (strreplace $commandbuf ".cfg" "") ".cfg") + saveCanvas = [ + _filesystem.save @newCanvas (escape "exec ^"guicanvas.cfg^"^n^nguicanvas.size 32 32 // this will try to use an existing screen or generate a new one^nguicanvas.background = ^"packages/textures/default.png^"^n^nguicanvas.refreshRate = 0^nguicanvas.play [^n^t// your render commands go here^n^t// they will run every millisecond set by $guicanvas.refreshRate^n^n] [^n^t// this is the interface body^n^tguititle ^"my canvas^"^n] 0 0^n^nguicanvas.refresh // this will draw the background and the pixels set with guicanvas.setPixel") [ + _guicanvasEditor @@newCanvas + textfocus @@newCanvas + textload @@newCanvas + ] + ] + if (findfile $newCanvas) [ + inputcommand "0" [ + if (= $commandbuf 0) [ + _guicanvasEditor @@newCanvas + ] [ + @@saveCanvas + ] + ] (format "^f8[GUICANVAS] ^f3warning^n^f7there is already a canvas with the same name in ^f5%1^f7^nuse ^f50 ^f~to load it or ^f51 ^f~to overwrite with preset.^n^f8>^f5" @newCanvas) + ] [ + @saveCanvas + ] + + ] [ + exec (concatword "guicanvas/" (at (concat @@canvas) @commandbuf) ".cfg") + ] + ] (concat "^f8[GUICANVAS]^n^f7type a name to create a canvas, or a number to load:^n" (loopconcat i (listlen $canvas) [result (format "^f1%1^f7: ^f8%2^n^f5" $i (at $canvas $i))])) + ] + ] + guicanvas.width = 32 + guicanvas.height = 32 + guicanvas.pixels = [] + guicanvas.background = 1 + guicanvas.keepPixels = 0 + guicanvas.opacity = 1 + guicanvas.refreshRate = -1 + + // most of these are used only once and shouldn't be changed accidentally, + // but for the sake of variability options: + _guicanvas.screenDir = "packages/models/guicanvas" + _guicanvas.baseTextureBlack = "packages/skyboxes/black_up.jpg" + _guicanvas.baseTextureWhite = "packages/skyboxes/white_ft.jpg" + _guicanvas.baseTextureOpacity = "packages/skyboxes/cloud_ring.png" + + _guicanvas.usedIndexes = [] + _guicanvas.lastRefresh = (getmillis) + + guicanvas.resetPixels = [ + guicanvas.pixels = [] + _guicanvas.usedIndexes = [] + ] + + guicanvas.setPixel = [ + local pixel_index pixel_texture + pixel_index = (+ (* $arg2 $guicanvas.width) $arg1) + if (= (indexof $_guicanvas.usedIndexes $pixel_index) -1) [ + // if arg3 is -1: arg3 is baseTextureOpacity, else: if arg4 is defined: arg3 arg4 arg5 are colors (baseTextureWhite), else: arg3 is custom texture path. + pixel_texture = (? (= $arg3 -1) $_guicanvas.baseTextureOpacity (? (!=s $arg4 "") (concatword "" $_guicanvas.baseTextureWhite) $arg3)) + guicanvas.pixels = (concatword $guicanvas.pixels "objskin " $pixel_index " ^"" $pixel_texture "^";") + append _guicanvas.usedIndexes $pixel_index // prevents duplicate items in pixels list + ] + ] + + guicanvas.refresh = [ + clearmodel (format "../../%1" $_guicanvas.screenDir) + ] + + guicanvas.fillRect = [ + local x y width height r g b + x = $arg1 + y = $arg2 + width = $arg3 + height = $arg4 + r = $arg5 + g = $arg6 + b = $arg7 + loop i $width [ + loop j $height [ + guicanvas.setPixel (+ $x $i) (+ $y $j) $r $g $b + ] + ] + ] + + guicanvas.perspective = [ + modelpreviewpitch $arg1 + modelpreviewfov (max 10 $arg2) + ] + + guicanvas.onupdate = [ + if (!= $guicanvas.refreshRate -1) [ + if (>= (getmillis) $_guicanvas.lastRefresh) [ + _guicanvas.lastRefresh = (+ (getmillis) $guicanvas.refreshRate) + do $arg1 + ] + ] + ] + + guicanvas.play = [ + local onupdate action actionX actionY + if (= $numargs 0) [ + arg1 = (getalias _guicanvas.playarg1) + arg2 = (getalias _guicanvas.playarg2) + arg3 = (getalias _guicanvas.playarg3) + arg4 = (getalias _guicanvas.playarg4) + ] + onupdate = $arg1; _guicanvas.playarg1 = $arg1 + action = $arg2; _guicanvas.playarg2 = $arg2 + actionX = $arg3; _guicanvas.playarg3 = $arg3 + actionY = $arg4; _guicanvas.playarg4 = $arg4 + + _guicanvas.lastRefresh = (getmillis) + + newgui guicanvas [ + guistrut -30 + guialign 0 [ + guistrut -35 + guialign 0 [ + guistrut 22 + guimodelpreview (format "../../%1" $_guicanvas.screenDir) "mapmodel" [] 15 0 + guistrut -10 + ] + guistrut -35 + ] + + guialign -1 [ + guilist [ + guistrut (+ -12 @@@actionY) + guilist [ + guistrut @@@@actionX + @@@@action + guistrut (* @@@@actionX -1) + ] + guistrut (- 13 @@@actionY) + ] + guistrut 20 + ] + + guicanvas.onupdate [ + @@onupdate + ] + ] 0 + showgui guicanvas + ] + + guicanvas.size = [ + if (= $numargs 0) [ + inputcommand (concat $guicanvas.width $guicanvas.height) [ + guicanvas.size (at $commandbuf 0) (at $commandbuf 1) + sleep 0 [echo (concat "^f8[GUICANVAS] ^f1screen size set to" $guicanvas.width $guicanvas.height)] + ] "^f8[GUICANVAS] ^f7screen size:^f5" + ] [ + guicanvas.width = @arg1 + guicanvas.height = (? (=s @arg2 "") $guicanvas.width @arg2) + ] + + if (! (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $guicanvas.width $guicanvas.height))) [ + sleep 0 [cleargui] + + inputcommand " " [ + echo (format "^f8[GUICANVAS] ^f5generating file for screen %1x%2, this may take a while.." $guicanvas.width $guicanvas.height) + sleep 0 [ + _guicanvas.genScreen $guicanvas.width $guicanvas.height 1 1 [ + local restart + restart = [ + if (!=s (getalias _guicanvas.playarg1) "") [ + echo "^f8[GUICANVAS] ^f7restarting canvas" + guicanvas.play $_guicanvas.playarg1 $_guicanvas.playarg2 $_guicanvas.playarg3 $_guicanvas.playarg4 + if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []] + ] + ] + if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [ + _filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [ + restart + ] + ] [restart] + + ] + ] + ] (format "^f8[GUICANVAS]^t^n> ^f7The screen size ^f5%1^f7x^f5%2 ^f7is not yet available^npress ^f0enter ^f~to generate it or ^f3esc ^f~to cancel." $guicanvas.width $guicanvas.height) + ] [if (getalias guicanvas.onstart) [do $guicanvas.onstart; guicanvas.onstart = []]] + ] + + guicanvas.genScreen = [ + local _obj warning checkobj + _gscw = (? (=s $arg1 "") $guicanvas.width $arg1) + _gsch = (? (=s $arg2 "") $guicanvas.height $arg2) + _gscl = 1 + _gsct = 1 + _obj = (format "canvas_%1x%2.obj" $_gscw $_gsch) + checkobj = [ + if (findfile (concatword $_guicanvas.screenDir "/" $_obj)) [ + warning = (concat "^f~The^f8" $_obj "^f~file ^f2is already in place^f~, are you sure you want to rewrite it? ") + ] [ + warning = (format "You are about to ^f2create^f~ a ^f5%1^f~x^f5%2 ^f~screen, " $_gscw $_gsch) + ] + ] + checkobj + newgui _ [ guistayopen [ + guistrut -3 + guititle (format "^f8GUICANVAS screen generator^n^f5%1^f~x^f5%2 ^f~contains ^f5%3 ^f~pixels" $_gscw $_gsch (* $_gscw $_gsch)) + guistrut 2 + guialign 0 [ + guitext "^f5width: " 0 + guifield _gscw -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch] + guistrut 1 + guitext "^f5height: " 0 + guifield _gsch -5 [sleep 500 [guifadein @guifadein]; guifadein 0; guicanvas.genScreen $_gscw $_gsch] + guistrut 1 + guitext "^f5layers: " 0 + guifield _gscl -5 + ] + guistrut 0.3 + guialign 0 [ + guicheckbox "textures support" _gsct 1 0 + ] + guistrut 0.3 + guitextbox (concat @@warning "this operation may take a while depending on the screen size..") 45 0 0xffffff + guialign 0 [ + guibutton (concat "^f8continue anyway") [ + _guicanvas.genScreen $_gscw $_gsch $_gscl $_gsct + ] + guistrut 1 + guibutton "^f3cancel" [cleargui] "exit" + ] + guistrut 1 + ]] 0 + showgui _ + ] + + _guicanvas.genScreen = [ + local width height layers textures index x_offset y_offset buffer callback + width = $arg1 + height = $arg2 + layers = $arg3 + textures = $arg4 + callback = $arg5 + // offsets for centering + x_offset = (*f (+f (divf $width 2.0) 0.5) -1) + y_offset = (-f (divf $height 2.0) 0.5) + index = 0 + buffer = "# generated with GUICANVAS Screen Generator by @SalatielSauer^n" + loop layer $layers [ + loop ri $height [ + local i + i = (- $height $ri) // start from top row + loop j $width [ + local v1 v2 v3 v4 uv start_vertex + + // define the 4 vertices for the current plane, considering offsets for centering + v1 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f $i $y_offset) $layer) + v2 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f $i $y_offset) $layer) + v3 = (format "v %1 %2 0.%3" (+f (+f $j 1) $x_offset) (+f (+f $i 1) $y_offset) $layer) + v4 = (format "v %1 %2 0.%3" (+f $j $x_offset) (+f (+f $i 1) $y_offset) $layer) + + // UV coordinates (standard plane unwrapping) + uv = "vt 0 0^nvt 1 0^nvt 1 1^nvt 0 1^n" + + // write the face (polygon) and UV using the above vertices + // 'g' is equivalent to "export as groups" in Blender + buffer = (format "%7g %1 ^n%2 ^n%3 ^n%4 ^n%5^n%6" $index $v1 $v2 $v3 $v4 (? $textures $uv "") $buffer) + start_vertex = (+ (* $index 4) 1) + index = (+ $index 1) + buffer = (format "%5f %1/1 %2/2 %3/3 %4/4^n" $start_vertex (+ $start_vertex 1) (+ $start_vertex 2) (+ $start_vertex 3) $buffer) + ] + ] + ] + + _filesystem.save (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir $width $height) (escape $buffer) [ + if (findfile (format "%1/canvas_%2x%3.obj" $_guicanvas.screenDir @width @height)) [ + echo (format "^f8[GUICANVAS] ^f1screen saved successfully at ^f7%1/canvas_%2x%3.obj" $_guicanvas.screenDir @@width @@height) + @@callback + ] [ + error "^f8[GUICANVAS] ^f3could not save screen, try again" + ] + ] + ] + + _filesystem.write = [if (!=s $arg1 "") [append _filesystem.linesbuff (concatword "[" $arg1 "]")]] + _filesystem.open = [_filesystem.linesbuff = []; newgui _ [guieditor @arg1 -1 1; textfocus @arg1; textload @arg1; cleargui]; showgui _; if $arg2 [sleep 0 [arg1 = (escape $textshow); arg2 = @arg1; nodebug [do (substr (strreplace (strreplace (strreplace (strreplace (concatword "^n" $textshow) ";" "^n") "//" "^^/^^/") "^n" "]^n _filesystem.write [") "[ ]" "^^n") 1)]; @arg2]]] + _filesystem.save = [newgui _ [local content; content = @arg2; guifield content 1; textfocus content; textsave @arg1; cleargui];showgui _; sleep 0 [@arg3]] + + _guicanvas.loadScreen = 1 + _guicanvas.screenCFG = [ + // the actual model configuration: + objload (concatword "canvas_" $guicanvas.width "x" $guicanvas.height ".obj") + mdlscale 100 + mdlspin -36 + mdlyaw -90 + mdlfullbright 1 + mdlalphatest $guicanvas.opacity + + objcullface * 0 + + objdir "../.." // start at home/installation folder + if (|| (!= $guicanvas.background 0) (!=s $guicanvas.background 0)) [ + objskin * (? (= $guicanvas.background 1) $_guicanvas.baseTextureBlack $guicanvas.background) // use black background as default (1) + ] [ + objskin * $_guicanvas.baseTextureOpacity + ] + + do $guicanvas.pixels + if (! $guicanvas.keepPixels) [ + guicanvas.pixels = [] + _guicanvas.usedIndexes = [] + ] + ] + ] [ + if (_guicanvas.loadScreen) [ + do $_guicanvas.screenCFG + ] [_guicanvas.loadScreen = 1] + ] +] + +_guicanvas.loadScreen = 0 +_guicanvas + +if (! (findfile (format "%1/obj.cfg" $_guicanvas.screenDir))) [ + // create model configuration file if not available + _filesystem.save (format "%1/obj.cfg" $_guicanvas.screenDir) (escape $_guicanvas.screenCFG) [ + if (findfile (format "%1/obj.cfg" $_guicanvas.screenDir)) [ + echo "^f8[GUICANVAS] ^f7loaded successfully" + ] [ + error (format "^f8[GUICANVAS] ^f3could not write ^f7%1/obj.cfg" $_guicanvas.screenDir) + ] + ] +] [echo "^f7GUICANVAS version: 1.0"] \ No newline at end of file diff --git a/guicanvas/guicanvas_circle.cfg b/guicanvas/guicanvas_circle.cfg new file mode 100644 index 0000000..24cabdc --- /dev/null +++ b/guicanvas/guicanvas_circle.cfg @@ -0,0 +1,18 @@ +// GUICANVAS: example of a colored circle, adapted by @SalatielSauer (20/09/2023) + + + +guicanvas.background = 0 +guicanvas.opacity = 1 +loop x 32 [ loop y 32 [ + local center distance + center = 16 + distance = (sqrt (+f (pow (-f $x $center) 2) (pow (-f $y $center) 2))) + if (< $distance 14) [ + guicanvas.setPixel $x $y (divf (rnd 200 150) (* $x $y)) (divf (rnd 64 20) (* $y $x)) 0 + ] [ + guicanvas.setPixel $x $y -1 + ] +]] + +if (! $guicanvas.previewMode) [ guicanvas.size 32 32; guicanvas.play ] [ guicanvas "guicanvas_circle" ] \ No newline at end of file diff --git a/guicanvas/guicanvas_raycast.cfg b/guicanvas/guicanvas_raycast.cfg new file mode 100644 index 0000000..00b1009 --- /dev/null +++ b/guicanvas/guicanvas_raycast.cfg @@ -0,0 +1,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 "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 "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] +] diff --git a/guicanvas/guicanvas_snake.cfg b/guicanvas/guicanvas_snake.cfg new file mode 100644 index 0000000..2326cc8 --- /dev/null +++ b/guicanvas/guicanvas_snake.cfg @@ -0,0 +1,176 @@ +// GUICANVAS: snake demo by @SalatielSauer (06/09/2023) + +_tileSize = 1; +_tileCount = (- (div $guicanvas.width $_tileSize) 2); +_snake = [[0 10] [0 9] [0 8]] +_apple = [15 15] +_yVelocity = 0 +_xVelocity = 1 +_lavaFrames = (loopconcat i 25 [result $i]) +_lavaFrame = 0 +_RENDER = [ + local headX headY + _lavaFrame = (max 0 (at $_lavaFrames (+ (indexof $_lavaFrames $_lavaFrame) 1))) + guicanvas.fillRect 0 0 1 $guicanvas.width (format "packages/dg/floor_lava2.jpg" (* $_lavaFrame 20)) + guicanvas.fillRect 0 0 $guicanvas.width 1 (format "packages/dg/floor_lava2.jpg" (* $_lavaFrame 20)) + guicanvas.fillRect $guicanvas.width 0 1 $guicanvas.width (format "packages/dg/floor_lava2.jpg" (* $_lavaFrame 20)) + guicanvas.fillRect $guicanvas.width 30 $guicanvas.width 1 (format "packages/dg/floor_lava2.jpg" (* $_lavaFrame 20)) + guicanvas.fillRect 31 0 1 32 (format "packages/dg/floor_lava2.jpg" (* $_lavaFrame 20)) + // move the snake + headX = (+ (at (at $_snake 0) 0) $_xVelocity) + headY = (+ (at (at $_snake 0) 1) $_yVelocity) + + _snake = (format "[%1 %2] %3" $headX $headY (sublist $_snake 0 (- (listlen $_snake) 1))) // unshift + pop + + // check for collision with apple + if (&& (= $headX (at $_apple 0)) (= $headY (at $_apple 1))) [ + _apple = [-1 -1] + sleep (max 0 (- 3000 (* (listlen $_snake) 100))) [ + _apple = (format "%1 %2" (rnd $_tileCount 1) (rnd $_tileCount 1)) // new apple + sound 62 + ] + append _snake "[]" + sound 60 + if (= (- (listlen $_snake) 3) $_snakeRec) [ + sound 12 + clearsleep + _apple = (format "%1 %2" (rnd $_tileCount 1) (rnd $_tileCount 1)) // new apple + sound 62 + guicanvas.background = (format "packages/textures/default.png" (divf (rnd 100) 100) (divf (rnd 100) 100) (divf (rnd 100) 100)) + + ] + ] + + // check for collisions with walls or self + local wallCollision + wallCollision = (|| (<= $headX 0) (<= $headY 0) (>= $headX $_tileCount) (>= $headY $_tileCount)) + if (|| (_checkSnakeCollision $headX $headY) $wallCollision) [ + _snakeDead = 1 + sound (rnd 47 33) + sound 63 + if $wallCollision [sound 65] // toast + ] + + if (! $_snakeDead) [ + // draw apple + guicanvas.fillRect (* (at $_apple 0) $_tileSize) (* (at $_apple 1) $_tileSize) $_tileSize $_tileSize 1 0 0 + ] + + // draw snake + loop i (listlen $_snake) [ + local color + color = (? (>= (listlen $_snake) 32) (mod $i 2) (divf 1 $i)) + guicanvas.fillRect (* (at (at $_snake $i) 0) $_tileSize) (* (at (at $_snake $i) 1) $_tileSize) $_tileSize $_tileSize (format "%2" $color (? $_snakeDead "packages/hud/blip_red_skull.png" (? (=s $i 0) (format "packages/icons/%1.jpg" (playermodelicon)) "packages/skyboxes/white_ft.jpg"))) + ] + +] + +_checkSnakeCollision = [ + local x y collision + x = $arg1 + y = $arg2 + collision = 0 + loop i (listlen $_snake) [ + i = (+ $i 1) + if (= $collision 0) [ + collision = (&& (= (at (at $_snake $i) 0) $x) (= (at (at $_snake $i) 1) $y)) + i = (listlen $_snake) + ] + ] + result $collision +] + +_resetGame = [ + local startpos + startpos = (rnd 30 1) + _snakeDead = 0 + _snake = (format "[3 %1] [2 %1] [1 %1]" $startpos (- $startpos 1) (- $startpos 2) (- $startpos 3)) + _apple = [15 15] + _xVelocity = 1 + _yVelocity = 0 + guicanvas.background = (format "packages/textures/default.png" (divf (rnd 100) 100) (divf (rnd 100) 100) (divf (rnd 100) 100)) + +] + + +bind up [ + if (= $_yVelocity 0) [ + _xVelocity = 0 + _yVelocity = -1 + ] +] + +bind down [ + if (= $_yVelocity 0) [ + _xVelocity = 0 + _yVelocity = 1 + ] +] + +bind left [ + if (= $_xVelocity 0) [ + _xVelocity = -1 + _yVelocity = 0 + ] +] + +bind right [ + if (= $_xVelocity 0) [ + _xVelocity = 1 + _yVelocity = 0 + ] +] + +guicanvas.size 32 32 +guicanvas.perspective 0 15 // pitch, fov +guicanvas.background = (format "packages/textures/default.png" (divf (rnd 100) 100) (divf (rnd 100) 100) (divf (rnd 100) 100)) +guicanvas.refreshRate = 50 +guicanvas.refresh +if (=s (getalias _snakeRec) "") [_snakeRec = 0] +_snakeDead = 0 +guicanvas.play [ + if (! $_snakeDead) [ + _RENDER + guicanvas.refresh + _y = (-f (min (max (at (at $_snake 0) 1) 0) 20) 6) + _x = (-f (at (at $_snake 0) 0) 6) + _y = (-f (+f 2 (divf (at (at $_snake 0) 1) 2)) 2) + clearconsole + ] +] [ + local points + points = (- (listlen $_snake) 3) + _snakeRec = (? (> $points $_snakeRec) $points $_snakeRec) + + guialign 0 [ + guistrut -3 + if (_snakeDead) [ + guistrut -3 + guialign 0 [guiimage "packages/hud/blip_red_skull.png" [] 0.5 0] + guistrut -0.2 + guialign 0 [guititle "^f4RIP"] + guititle (format "^f3Apples: ^f0%1^f4/%2" $points $_snakeRec) + guistrut 0.9 + guistayopen [guibutton "try again?" [_resetGame] "../mitaman/mm-auggiedog.jpg"] + _x = 9 + _y = 5 + guialign 0 [ + guistrut -23 + guialign 0 [ + guistrut -10.4 + guiimage (format "packages/hud/damage.png" $points) [] 8.7 0 + guistrut -11 + ] + guistrut -25 + ] + guistrut -4.5 + ] [ + guitextbox (format "^n^nApples: ^f0%1^f4/%2" $points $_snakeRec) 0 0 0xff0000 + if (&& (isconnected) (= $editing 1)) [ + guitextbox "you are in edit mode" 0 0 "0xffff00" + ] + ] + ] +] @_x @_y + +//if (! $isconnected) [sleep 500 [coop; showgui guicanvas]; showgui guicanvas] // if for some reason /sound doesn't work properly in the main menu \ No newline at end of file