Twine Version: 2.3.9
Story Format: 2.31.1
Hi folks,
I am trying to extend the “navigating a dungeon” tutorial from the Twine Cookbook to include a rudimentary light casting system, as if the player character had a flashlight. My aim is to only have the dungeon tiles that the player character can “see” be printed. I’ve implemented something similar with the libtcod library in the past, but that library has built in support for this sort of thing.
In any case, I am not trying to build a complex roguelike so much as to use a simple roguelike as the vehicle to tell my story.
Here’s what I have so far:
"<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 "@">>
<<elseif $mapArray[$i][$k] eq 1 and $lightRadius.includesAny($mapArray[$i][$k])>>
<<print ".">>
<<elseif $mapArray[$i][$k] eq 0 and $lightRadius.includesAny($mapArray.indexOf($i,$k))>>
<<print "#">>
<<elseif $mapArray[$i][$k] eq 2 and $lightRadius.includesAny($mapArray.indexOf($i,$k))>>
<<print "E">>
<<else>>
<<print "\xB0">>
<</if>>
<</for>>
<<print "<br>">>
<</for>>
<</nobr>>
</span>"
And here is $lightRadius (initialized in another passage):
<<set $lightRadius to [
[$positionX-1, $positionY-1],[$positionX-1, $positionY,],[$positionX-1, $positioY+1],[$positionX, $positionY-1],[$positionX, $positionY],[$positionX, $positionY+1],[$positionX+1, $positionY-1],[$positionX+1, $positionY],[$positionX+1, $positionY+1]]>>"
Here is the output I get:
° ° ° ° ° ° ° ° ° ° °
° ° ° @ ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
° ° ° ° ° ° ° ° ° ° °
So, am I barking up the right tree, here? What I am attempting is to access the indices of the nested mapArray array and check it against another array that has the xy positions around the player character. Then the Location passage would only print those squares, with the remainder being another ASCII character (I was aiming for the one that is like a dot filled rectangle, so still working on figuring out how to display extended ASCII).
Obviously I am not understanding how to properly access arrays and/or use the appropriate methods. Or, perhaps my approach is completely wrong.
Any tips or direction would be greatly appreciated – thank!
–Matt