Dungeon Crawler

Previous topic - Next topic

erico

From the discussions we had here:
http://www.glbasic.com/forum/index.php?topic=9675.0

At some point, I thought to do a dungeon crawler visualization in 2d to boot.
Just some exercise.

Here the code, and the appended full project+compiled pc version + a screen shot.
Be aware that the way I did is rough coding, but hopefully this will give a head start/example should you strive for the same or similar. :good:

Currently only the first top left player is controllable with:
WASD as north,strife east,south,strife west.
QE as rotation.
RF as up and down.

Can we advance this?
Cheers.

Code (glbasic) Select
// --------------------------------- //
// Project: MATCHY ADV
// Start: Thursday, November 12, 2015
// IDE Version: 14.006


// --- BOOT
SETSCREEN 680,400,0 ;// final resolution
CREATESCREEN 0,0,170,100 ;// player view set to sprite 0
SMOOTHSHADING FALSE

// --- VARIABLES
GLOBAL ft ;// for/next timer/player set (1-4)
GLOBAL world[] ;// world 3d array
DIM world[61][61][61]
GLOBAL fov[] ;// fov 3d array
DIM fov[5][5][5]
GLOBAL wx,wy,wz ;// for/next world scanner
DIM px[5]; DIM py[5]; DIM pz[5] ;// players position
DIM pf[5] ;// players face direction (1-4) 1 north
GLOBAL px[], py[], pz[], pf[]

// --- LOAD ASSETS
SETCURRENTDIR("Media") ;// go to media files
LOADSPRITE "GUIDE BKG.png",1 ;// load background guide
LOADANIM "BLK001.png",2,170,100 ;// load block sprites
LOADANIM "FOG.png",3,170,100 ;// load depth fog


// --- WORLD GENERATION
FOR wx=5 TO 55
FOR wy=5 TO 55
FOR wz=5 TO 55
IF RND(4)=1 THEN world[wx][wy][wz]=1 ;// ramdom plot block
NEXT
NEXT
NEXT

// --- HARDCODE PLAYER POSITION
px[1]=25 ; py[1]=25 ; pz[1]=55 ;// set player 1 coord
pf[1]=1 ;// set player 1 orientation
px[2]=24 ; py[2]=25 ; pz[2]=55 ;// set player 2 coord
pf[2]=1 ;// set player 2 orientation
px[3]=23 ; py[3]=25 ; pz[3]=55 ;// set player 3 coord
pf[3]=1 ;// set player 3 orientation
px[4]=26 ; py[4]=25 ; pz[4]=55 ;// set player 4 coord
pf[4]=1 ;// set player 4 orientation
FOR wx=22 TO 27 ;// clear players start room
FOR wy=25 TO 27
FOR wz=52 TO 55
world[wx][wy][wz]=0
NEXT
NEXT
NEXT
FOR wx=22 TO 27
FOR wz=52 TO 55
world[wx][24][wz]=1
NEXT
NEXT

// ------------------------------------------------------------------------- MAIN LOOP
WHILE TRUE

// --- DISPLAY
FOR ft=1 TO 4 ;// loop through players
USESCREEN 0 ;// use virtual screen
DRAWSPRITE 1,0,0 ;// draw bkg guide

// --- DUMP FOV ARRAY
IF pf[ft]=1 ;// check orientation north
FOR wx=0 TO 4 ;// dump
FOR wy=0 TO 4
FOR wz=0 TO 4
fov[wx][wy][wz]=world[px[ft]+wx-2][py[ft]+wy-2][pz[ft]-wz]
NEXT
NEXT
NEXT
ENDIF

IF pf[ft]=2 ;// check orientation east
FOR wx=0 TO 4 ;// dump
FOR wy=0 TO 4
FOR wz=0 TO 4
fov[wx][wy][wz]=world[px[ft]+wz][py[ft]+wy-2][pz[ft]+wx-2]
NEXT
NEXT
NEXT
ENDIF
//
IF pf[ft]=3 ;// check orientation south
FOR wx=0 TO 4 ;// dump
FOR wy=0 TO 4
FOR wz=0 TO 4
fov[wx][wy][wz]=world[px[ft]-wx+2][py[ft]+wy-2][pz[ft]+wz]
NEXT
NEXT
NEXT
ENDIF

IF pf[ft]=4 ;// check orientation west
FOR wx=0 TO 4 ;// dump
FOR wy=0 TO 4
FOR wz=0 TO 4
fov[wx][wy][wz]=world[px[ft]-wz][py[ft]+wy-2][pz[ft]-wx+2]
NEXT
NEXT
NEXT
ENDIF

// --- DRAW FOV [layer4]
IF fov[0][4][4]=1 THEN STRETCHANIM 2,0,0,0,170,100
IF fov[1][4][4]=1 THEN STRETCHANIM 2,1,0,0,170,100
IF fov[4][4][4]=1 THEN STRETCHANIM 2,0,170,0,-170,100
IF fov[3][4][4]=1 THEN STRETCHANIM 2,1,170,0,-170,100
IF fov[2][4][4]=1 THEN STRETCHANIM 2,2,0,0,170,100

IF fov[0][3][4]=1 THEN STRETCHANIM 2,3,0,0,170,100
IF fov[1][3][4]=1 THEN STRETCHANIM 2,4,0,0,170,100
IF fov[4][3][4]=1 THEN STRETCHANIM 2,3,170,0,-170,100
IF fov[3][3][4]=1 THEN STRETCHANIM 2,4,170,0,-170,100
IF fov[2][3][4]=1 THEN STRETCHANIM 2,5,0,0,170,100

IF fov[0][0][4]=1 THEN STRETCHANIM 2,0,0,100,170,-100 ;// mirror down
IF fov[1][0][4]=1 THEN STRETCHANIM 2,1,0,100,170,-100
IF fov[4][0][4]=1 THEN STRETCHANIM 2,0,170,100,-170,-100
IF fov[3][0][4]=1 THEN STRETCHANIM 2,1,170,100,-170,-100
IF fov[2][0][4]=1 THEN STRETCHANIM 2,2,0,100,170,-100

IF fov[0][1][4]=1 THEN STRETCHANIM 2,3,0,100,170,-100
IF fov[1][1][4]=1 THEN STRETCHANIM 2,4,0,100,170,-100
IF fov[4][1][4]=1 THEN STRETCHANIM 2,3,170,100,-170,-100
IF fov[3][1][4]=1 THEN STRETCHANIM 2,4,170,100,-170,-100
IF fov[2][1][4]=1 THEN STRETCHANIM 2,5,0,100,170,-100

IF fov[0][2][4]=1 THEN STRETCHANIM 2,6,0,0,170,100 ;// in the midium!!
IF fov[1][2][4]=1 THEN STRETCHANIM 2,7,0,0,170,100
IF fov[4][2][4]=1 THEN STRETCHANIM 2,6,170,0,-170,100
IF fov[3][2][4]=1 THEN STRETCHANIM 2,7,170,0,-170,100
IF fov[2][2][4]=1 THEN STRETCHANIM 2,8,0,0,170,100
DRAWANIM 3,3,0,0 ;// hardfog
// --- DRAW FOV [layer3]
IF fov[0][4][3]=1 THEN STRETCHANIM 2,9,0,0,170,100
IF fov[1][4][3]=1 THEN STRETCHANIM 2,10,0,0,170,100
IF fov[4][4][3]=1 THEN STRETCHANIM 2,9,170,0,-170,100
IF fov[3][4][3]=1 THEN STRETCHANIM 2,10,170,0,-170,100
IF fov[2][4][3]=1 THEN STRETCHANIM 2,11,0,0,170,100

IF fov[0][3][3]=1 THEN STRETCHANIM 2,12,0,0,170,100
IF fov[1][3][3]=1 THEN STRETCHANIM 2,13,0,0,170,100
IF fov[4][3][3]=1 THEN STRETCHANIM 2,12,170,0,-170,100
IF fov[3][3][3]=1 THEN STRETCHANIM 2,13,170,0,-170,100
IF fov[2][3][3]=1 THEN STRETCHANIM 2,14,0,0,170,100

IF fov[0][0][3]=1 THEN STRETCHANIM 2,9,0,100,170,-100 ;// mirror down
IF fov[1][0][3]=1 THEN STRETCHANIM 2,10,0,100,170,-100
IF fov[4][0][3]=1 THEN STRETCHANIM 2,9,170,100,-170,-100
IF fov[3][0][3]=1 THEN STRETCHANIM 2,10,170,100,-170,-100
IF fov[2][0][3]=1 THEN STRETCHANIM 2,11,0,100,170,-100

IF fov[0][1][3]=1 THEN STRETCHANIM 2,12,0,100,170,-100
IF fov[1][1][3]=1 THEN STRETCHANIM 2,13,0,100,170,-100
IF fov[4][1][3]=1 THEN STRETCHANIM 2,12,170,100,-170,-100
IF fov[3][1][3]=1 THEN STRETCHANIM 2,13,170,100,-170,-100
IF fov[2][1][3]=1 THEN STRETCHANIM 2,14,0,100,170,-100

IF fov[0][2][3]=1 THEN STRETCHANIM 2,15,0,0,170,100 ;// in the midium!!
IF fov[1][2][3]=1 THEN STRETCHANIM 2,16,0,0,170,100
IF fov[4][2][3]=1 THEN STRETCHANIM 2,15,170,0,-170,100
IF fov[3][2][3]=1 THEN STRETCHANIM 2,16,170,0,-170,100
IF fov[2][2][3]=1 THEN STRETCHANIM 2,17,0,0,170,100
DRAWANIM 3,2,0,0 ;// midfog
// --- DRAW FOV [layer2]
IF fov[0][4][2]=1 THEN STRETCHANIM 2,18,0,0,170,100
IF fov[1][4][2]=1 THEN STRETCHANIM 2,19,0,0,170,100
IF fov[4][4][2]=1 THEN STRETCHANIM 2,18,170,0,-170,100
IF fov[3][4][2]=1 THEN STRETCHANIM 2,19,170,0,-170,100
IF fov[2][4][2]=1 THEN STRETCHANIM 2,20,0,0,170,100

IF fov[0][3][2]=1 THEN STRETCHANIM 2,21,0,0,170,100
IF fov[1][3][2]=1 THEN STRETCHANIM 2,22,0,0,170,100
IF fov[4][3][2]=1 THEN STRETCHANIM 2,21,170,0,-170,100
IF fov[3][3][2]=1 THEN STRETCHANIM 2,22,170,0,-170,100
IF fov[2][3][2]=1 THEN STRETCHANIM 2,23,0,0,170,100

IF fov[0][0][2]=1 THEN STRETCHANIM 2,18,0,100,170,-100 ;// mirror down
IF fov[1][0][2]=1 THEN STRETCHANIM 2,19,0,100,170,-100
IF fov[4][0][2]=1 THEN STRETCHANIM 2,18,170,100,-170,-100
IF fov[3][0][2]=1 THEN STRETCHANIM 2,19,170,100,-170,-100
IF fov[2][0][2]=1 THEN STRETCHANIM 2,20,0,100,170,-100

IF fov[0][1][2]=1 THEN STRETCHANIM 2,21,0,100,170,-100
IF fov[1][1][2]=1 THEN STRETCHANIM 2,22,0,100,170,-100
IF fov[4][1][2]=1 THEN STRETCHANIM 2,21,170,100,-170,-100
IF fov[3][1][2]=1 THEN STRETCHANIM 2,22,170,100,-170,-100
IF fov[2][1][2]=1 THEN STRETCHANIM 2,23,0,100,170,-100

IF fov[0][2][2]=1 THEN STRETCHANIM 2,24,0,0,170,100 ;// in the midium!!
IF fov[1][2][2]=1 THEN STRETCHANIM 2,25,0,0,170,100
IF fov[4][2][2]=1 THEN STRETCHANIM 2,24,170,0,-170,100
IF fov[3][2][2]=1 THEN STRETCHANIM 2,25,170,0,-170,100
IF fov[2][2][2]=1 THEN STRETCHANIM 2,26,0,0,170,100
DRAWANIM 3,1,0,0 ;// lightfog
// --- DRAW FOV [layer1]
IF fov[0][3][1]=1 THEN STRETCHANIM 2,27,0,0,170,100
IF fov[1][3][1]=1 THEN STRETCHANIM 2,28,0,0,170,100
IF fov[4][3][1]=1 THEN STRETCHANIM 2,27,170,0,-170,100
IF fov[3][3][1]=1 THEN STRETCHANIM 2,28,170,0,-170,100
IF fov[2][3][1]=1 THEN STRETCHANIM 2,29,0,0,170,100

IF fov[0][1][1]=1 THEN STRETCHANIM 2,27,0,100,170,-100 ;// mirror down
IF fov[1][1][1]=1 THEN STRETCHANIM 2,28,0,100,170,-100
IF fov[4][1][1]=1 THEN STRETCHANIM 2,27,170,100,-170,-100
IF fov[3][1][1]=1 THEN STRETCHANIM 2,28,170,100,-170,-100
IF fov[2][1][1]=1 THEN STRETCHANIM 2,29,0,100,170,-100

IF fov[0][2][1]=1 THEN STRETCHANIM 2,30,0,0,170,100 ;// in the midium!!
IF fov[1][2][1]=1 THEN STRETCHANIM 2,31,0,0,170,100
IF fov[4][2][1]=1 THEN STRETCHANIM 2,30,170,0,-170,100
IF fov[3][2][1]=1 THEN STRETCHANIM 2,31,170,0,-170,100
DRAWANIM 3,0,0,0 ;// super lightfog
IF fov[2][2][1]=1 THEN STRETCHANIM 2,32,0,0,170,100

// --- DRAW FOV [layer0]

IF fov[1][3][0]=1 THEN STRETCHANIM 2,33,0,0,170,100
IF fov[3][3][0]=1 THEN STRETCHANIM 2,33,170,0,-170,100
IF fov[2][3][0]=1 THEN STRETCHANIM 2,34,170,0,-170,100

IF fov[1][1][0]=1 THEN STRETCHANIM 2,33,0,100,170,-100 ;// mirror down
IF fov[3][1][0]=1 THEN STRETCHANIM 2,33,170,100,-170,-100
IF fov[2][1][0]=1 THEN STRETCHANIM 2,34,170,100,-170,-100

IF fov[1][2][0]=1 THEN STRETCHANIM 2,35,0,0,170,100 ;// in the midium!!
IF fov[3][2][0]=1 THEN STRETCHANIM 2,35,170,0,-170,100






PRINT "X="+px[ft],2,2 ;// player info
PRINT "Y="+py[ft],2,10
PRINT "Z="+pz[ft],2,18
PRINT "F="+pf[ft],2,26
// PRINT "2BA="+world[px[1]][py[1]][pz[1]-2],2,34
// PRINT "FOV="+fov[2][2][2],2,42



// ---
USESCREEN -1 ;// use backbuffer
IF ft=1 THEN ZOOMSPRITE 0,84,50,2,2 ;// scale output pl1
IF ft=2 THEN ZOOMSPRITE 0,424,50,2,2 ;// scale output pl2
IF ft=3 THEN ZOOMSPRITE 0,84,250,2,2 ;// scale output pl3
IF ft=4 THEN ZOOMSPRITE 0,424,250,2,2 ;// scale output pl4
IF ft=4 THEN DRAWRECT 338,0,3,400,RGB(126,126,126) ;// hud
IF ft=4 THEN DRAWRECT 0,198,680,3,RGB(126,126,126) ;// hud
NEXT
// --- CONTROLS
IF KEY(17)=1 ;// advance
IF pf[1]=1 THEN pz[1]=pz[1]-1
IF pf[1]=2 THEN px[1]=px[1]+1
IF pf[1]=3 THEN pz[1]=pz[1]+1
IF pf[1]=4 THEN px[1]=px[1]-1
SLEEP 200
ENDIF
IF KEY(31)=1 ;// retreat
IF pf[1]=1 THEN pz[1]=pz[1]+1
IF pf[1]=2 THEN px[1]=px[1]-1
IF pf[1]=3 THEN pz[1]=pz[1]-1
IF pf[1]=4 THEN px[1]=px[1]+1
SLEEP 200
ENDIF
IF KEY(32)=1 ;// strife right
IF pf[1]=1 THEN px[1]=px[1]+1
IF pf[1]=2 THEN pz[1]=pz[1]+1
IF pf[1]=3 THEN px[1]=px[1]-1
IF pf[1]=4 THEN pz[1]=pz[1]-1
SLEEP 200
ENDIF
IF KEY(30)=1 ;// strife left
IF pf[1]=1 THEN px[1]=px[1]-1
IF pf[1]=2 THEN pz[1]=pz[1]-1
IF pf[1]=3 THEN px[1]=px[1]+1
IF pf[1]=4 THEN pz[1]=pz[1]+1
SLEEP 200
ENDIF
IF KEY(18)=1 ;// rotate right
pf[1]=pf[1]+1
IF pf[1]=5 THEN pf[1]=1
SLEEP 200
ENDIF
IF KEY(16)=1 ;// rotate left
pf[1]=pf[1]-1
IF pf[1]=0 THEN pf[1]=4
SLEEP 200
ENDIF
IF KEY(19)=1 ;// up
py[1]=py[1]+1
SLEEP 200
ENDIF
IF KEY(33)=1 ;// down
py[1]=py[1]-1
SLEEP 200
ENDIF
// --- POSITION LIMITS
IF px[1]<5 THEN px[1]=5
IF px[1]>55 THEN px[1]=55
IF py[1]<5 THEN py[1]=5
IF py[1]>55 THEN py[1]=55
IF pz[1]<5 THEN pz[1]=5
IF pz[1]>55 THEN pz[1]=55

// --- DEBUG HUD

SHOWSCREEN
WEND
// ------------------------------------------------------------------------- MAIN LOOP [END]

Ian Price

 :good: That's very cool erico.  :good:
I came. I saw. I played.

UBERmonkeybot

#2
All i get is this...


DOH!   ignore that i didn't get the zip only the listing... :S

That's nice!


erico

Yep, the zip has 3 media files, the blocks, the background and the fog.
Background and fog are skipable, but you need the blocks to see stuff.

bigsofty

Doh, don't know how I missed this, it's an excellent effect Erico. Kinda reminds me of mono mode on the old Atari ST.  :D
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

erico

Thanks, I have been trying to find time to change this to a proper simple 4 players game but can´t seem to find so.
Apart from the perspective distortion, I think it is a better route to use 3d to draw the art anyways.
I have been doing a bunch of stuff in low res black and white lately, is that what recalled mono mode? What is that?

UBERmonkeybot


mentalthink

Wow I like it a lot, the effect like eyefish is really cool!!! and the simple graphics... very nice!!!

erico

thanks mental! Since it was going to be 2d, the perspective adds a nice twist, haven´t seen that before.

Quote from: monkeybot1968 on 2016-Feb-15
Mono = monochrome
https://en.wikipedia.org/wiki/Monochrome

I see, but what does that have to do with the atari st? Did it have a 2 color dither mode or something like that?
Mono = http://www.binaryzoo.com/mono_page/
:p