Dungeon Crawler -- Part 2

Building on our last video, we are using three main mechanics for our dungeon crawler. * Health system * Maze-like map * Encounters However, before we can move forward from what was setup in the last video, we need to think about our project as a whole. Since we have numerous variables, we should think about having some "init" passage as a place where all initial values are set. <<silently>> <<display "init">> <</silently>> [[Map]] Health: <<print $health>> <<silently>> <<if $health lte 0>> <<goto "Death">> <</if>> <</silently>>We also need a better way to figure out our position in the map. While using just the values of positionX and positionY can be helpful, it would be better to see the whole map as well. To do that, we need to use the < for > macro in SugarCube to move through the items in each array. Starting with the top array, we move through its own arrays one by one. In each, we test the values. If it is a 0, we write a hash. If it is a 1, we write a period. If it is a 2, we write a E. If the position is the same as positionX AND positionY, we write a P. [[Encounter system]] <<display "Location">> <<display "Grue">> <<display "Health">> <<nobr>> <<if $mapArray[$positionY-1][$positionX] eq 1>> [[North]] | <<elseif $mapArray[$positionY-1][$positionX] eq 2>> [[Exit]] | <<endif>> <<if $mapArray[$positionY][$positionX+1] eq 1>> [[East]] | <<elseif $mapArray[$positionY][$positionX+1] eq 2>> [[Exit]] | <<endif>> <<if $mapArray[$positionY+1][$positionX] eq 1>> [[South]] | <<elseif $mapArray[$positionY+1][$positionX] eq 2>> [[Exit]] | <<endif>> <<if $mapArray[$positionY][$positionX-1] eq 1>> [[West]] | <<elseif $mapArray[$positionY][$positionX-1] eq 2>> [[Exit]] | <<endif>> <</nobr>><<set $positionY -= 1>> <<display "Map System">><<set $positionY += 1>> <<display "Map System">><<set $positionX -= 1>> <<display "Map System">><<set $positionX += 1>> <<display "Map System">> <<display "Map System">> <span id="map"> <<nobr>> <<for $i to 0; $i lt $mapArray.length; $i++>> <<for $k to 0; $k lt $mapArray[$i].length; $k++>> <<if $k eq $positionX and $i eq $positionY>> <<print "P">> <<elseif $mapArray[$i][$k] eq 1>> <<print "&#46;">> <<elseif $mapArray[$i][$k] eq 0>> <<print "&#35;">> <<elseif $mapArray[$i][$k] eq 2>> <<print "E">> <</if>> <</for>> <<print "<br>">> <</for>> <</nobr>> </span> <<set $health to 100>> <<set $mapArray to [[0,0,0,0,0,0,0,0,0,0,0], [0,1,1,1,0,1,1,1,1,1,0], [0,0,0,1,0,0,0,0,0,1,0], [0,1,0,1,1,1,1,1,0,1,0], [0,1,0,0,0,0,0,1,0,1,0], [0,1,1,1,1,1,1,1,0,1,0], [0,0,0,0,0,0,0,1,0,1,0], [0,1,0,1,1,1,1,1,1,1,0], [0,1,0,1,0,0,0,1,0,0,0], [0,1,1,1,0,1,1,1,1,2,0], [0,0,0,0,0,0,0,0,0,0,0]]>> <<set $positionX to 1>> <<set $positionY to 1>> <<set $attackNumber to 10>> <<silently>> <<if $attackNumber lte 0>> <<set $attackNumber to 10>> <</if>> <<set $attackNumber -= 1>> <<set $chance to Math.round(Math.random() * $attackNumber)>> <</silently>> <<if $chance eq 1>> The grue attacks! Lose 10 health! <<set $attackNumber to 0>> <<set $health -= 10>> <</if>> You escaped! <<display "Health">>Since we also wanted an encounter system, we borrow the code from an older video on re-creating the Grue from <em>Zork</em>. For every step we take, we have a random but increasing chance of having the grue attack. It will always eventually attack within ten movements. However, once it has attacked, it will reset. [[Testing our map]]Oh no! You were eaten by the grue!