Added a map in top corner with a player location
// Project: perlinnoise
// Created: 2018-02-10
// show all errors
SetErrorMode(2)
#constant width=1024
#constant height=768
// set window properties
SetWindowTitle( "perlinnoise" )
SetWindowSize( width, height, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( width, height ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 600, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
type _textures
id // texture store
r,g,b // colors
dr,dg,db // depths (because minecraft tiles is built with rectangular different shades of the color)
texturessize
endtype
global textures as _textures[8]
#constant mapheight=255
#constant mapmaxx=1024
#constant mapmaxy=1024
#constant playareax=50
#constant playareay=50
// map variables
global memblockid, imgwidth, imgheight, imgsize
global blocks as integer[mapmaxx,mapmaxy] //cubes
global totalblocks
global map as integer //the 2D map that was generated
global grasshalf, grassfull, dirtfull,player
global generating as integer
global cx#,cy#,cz#
generating=CreateText("Generating Terrain")
SetTextSize(generating,50)
SetTextPosition(generating, width/2-GetTextTotalWidth(generating)/2, height/2-GetTextTotalHeight(generating)/2)
// setup a skybox
SetSunActive(1)
SetSkyBoxHorizonSize(4.0,10)
SetSkyBoxHorizonColor(0,200,0)
SetSkyBoxSkyColor(0,0,255)
SetSkyBoxVisible(1)
SetFogColor(0,0,0)
SetFogRange(150,180)
SetFogMode(1)
definetextures()
grasshalf = CreateTextures(64,64,4,0,1) // sides
grassfull = CreateTextures(64,64,4,0,0) // top
dirtfull = CreateTextures(64,64,4,1,1) // bottom
generatemap()
DeleteText(generating)
player=CreateObjectSphere(.1,20,20)
// Get camera positioning
cx#=getobjectx(blocks[playareax/2,playareay/2])
cy#=getobjecty(blocks[playareax/2,playareay/2])+2
cz#=getobjectz(blocks[playareax/2,playareay/2])
SetObjectPosition(player,cx#,cy#,cz#)
RotateObjectLocalY(player,180)
SetCameraRange(1,.01,400)
//SetCameraRotation(1,0,90,0)
camerastep#=.01
do
//player
if GetRawKeyState(38) then MoveObjectLocalZ(player,camerastep#)
if GetRawKeyState(40) then MoveObjectLocalZ(player,-camerastep#)
if GetRawKeyState(37) then RotateObjectLocalY(player,-camerastep#*100)
if GetRawKeyState(39) then RotateObjectLocalY(player,camerastep#*100)
//A - rotate camera
if GetRawKeyState(65) then RotateObjectLocalY(player,-camerastep#*50)
if GetRawKeyState(68) then RotateObjectLocalY(player,camerastep#*50)
if GetRawKeyState(87) then RotateObjectLocalX(player,-camerastep#*5)
if GetRawKeyState(83) then RotateObjectLocalX(player,camerastep#*5)
if GetRawKeyState(69) then inc cy#,.3
if GetRawKeyState(67) then dec cy#,.3
//add
if GetRawKeyPressed(32) then addblock(blockpositionx,blockpositiony)
//movecameras
cx#=getobjectx(player)
cz#=getobjectz(player)
//cy#=getobjecty(player)
SetCameraPosition(1,cx#,cy#,cz#-2)
setcamerarotation(1,GetObjectAngleX(player),getobjectangley(player),GetObjectAngleZ(player))
blockpositionx=floor(cx#)
blockpositiony=floor(cz#)
//find location of block
// draw a dot to place position on the 2D map
SetspritePosition(map,width-100,0)
DrawEllipse(width-100+cx#,cz#,3,3,MakeColor(0,0,255),MakeColor(0,0,255),1)
cull_objs_behind_camera()
// SetSpritePosition(map,0,0)
Render2DBack()
Print( str(ScreenFPS()) + " - Use cursors to move player, WASD to move camera views")
print("Use E and C for a sky view")
print(" Last Key - " + str(GetRawLastKey())+ " - Blocks "+str(totalblocks)+" - " + str(GetPolygonsDrawn()) + " X - "+str(blockpositionx) + " - Y " + str(blockpositiony))
print(" More to come - Enjoy!")
Sync()
loop
function addblock(x,y)
blockpositionx=floor(cx#)
blockpositiony=floor(cz#)
bl=CreateObjectBox(1,1,1)
inc totalblocks
SetObjectPosition(bl,x,getobjecty(blocks[x,y])+1,y)
SetObjectImage(bl,grassfull,1)
cy#=getobjecty(blocks[x,y])+1
//SetObjectPosition(player,cx#,cy#+1,cz#)
endfunction
function generatemap()
swap()
DrawBox(0,0,mapmaxx,mapmaxy,MakeColor(mapheight,mapheight,mapheight),MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(mapheight,mapheight,mapheight),1)
render()
spr = GetImage(0,0,mapmaxx,mapmaxy)
map=CreateSprite(spr)
SetSpriteImage(map,spr,1)
SetSpriteSize(map,100,100)
// create memblock before loops to load faster
memblockid = CreateMemblockFromImage (spr)
imgwidth = GetMemblockInt(memblockid, 0)
imgheight = GetMemblockInt(memblockid, 4)
imgsize=GetMemblockSize(memblockid)
for x=1 to playareax
for y=1 to playareay
v=getimagepixel(x,y)
blocks[x,y]=CreateObjectBox(1,1,1)
inc totalblocks
SetObjectPosition(blocks[x,y],x,v/4,y)
SetObjectImage(blocks[x,y],grassfull,1)
next
next
// finish with the memblock
DeleteMemblock(memblockid)
endfunction
function getimagepixel(x,y)
offset=12 + ( ( x + ( y * imgwidth ) ) * 4)
value=GetMemblockByte(memblockid,offset)
endfunction value
function definetextures()
// grass=createtexture(64,64,0,200,0,8)
//grass
textures[0].r=0 :textures[0].dr=50
textures[0].g=80 :textures[0].dg=100
textures[0].b=0 :textures[0].db=50
//dirt
textures[1].r=139 :textures[1].dr=20
textures[1].g=69:textures[1].dg=20
textures[1].b=19:textures[1].db=20
//goldgold=createtexture(8,8,255,215,100,1)
textures[2].r=255 :textures[2].dr=100
textures[2].g=215 :textures[2].dg=1
textures[2].b=100 :textures[2].db=1
// water=createtexture(8,8,0,0,100,1)
textures[3].r=0 :textures[3].dr=50
textures[3].g=0 :textures[3].dg=1
textures[3].b=100 :textures[3].db=1
// emeralds=createtexture(8,8,39,89,45,1)
textures[4].r=39 :textures[4].dr=5
textures[4].g=89 :textures[4].dg=5
textures[4].b=45 :textures[4].db=5
endfunction
function createtextures(sizex# as float, sizey# as float,density, toptexture, bottomtexture)
/*
fire=createtexture(8,8,255,0,0,1)
sand=createtexture(8,8,194,178,128,1)
wood=createtexture(8,8,102,51,0,1)
grassfull=createtexture(64,64,0,200,0,8)
// SetObjectImage(plane,grassfull,1)
*/
if bottomtexture=0
//do a full block of the same texture
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
else
// do an half block with a different texture at bottom
texturecount=toptexture
swap()
for a=0 to sizex# step density
for b=0 to sizey#/3 step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
texturecount=bottomtexture
for a=0 to sizex# step density
for b=sizey#/3 to sizey# step density
rr=random(1,textures[texturecount].dr)
if textures[texturecount].dr-rr >0 then colorred=textures[texturecount].r - rr
if textures[texturecount].dg-rr >0 then colorgreen=textures[texturecount].g - rr
if textures[texturecount].db-rr >0 then colorblue=textures[texturecount].b - rr
if colorred<0 then colorred=textures[texturecount].r
if colorgreen<0 then colorgreen=textures[texturecount].g
if colorblue<0 then colorblue=textures[texturecount].b
drawbox(a,b,(A+density),(B+density), MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue),MakeColor(colorred,colorgreen,colorblue), 1)
next
next
render()
textures[texturecount].id = getimage(0,0,sizex#, sizey#)
endif
sleep(0)
endfunction textures[texturecount].id
function GetObjectInFront(obj, cam)
x1# = GetCameraX(cam)
y1# = GetCameraY(cam)
z1# = GetCameraZ(cam)
dx# = GetObjectX(obj) - x1#
dy# = GetObjectY(obj) - y1#
dz# = GetObjectZ(obj) - z1#
d# = sqrt(dx#*dx# + dy#*dy# + dz#*dz#)
ux1# = dx#/d#
uy1# = dy#/d#
uz1# = dz#/d#
d# = 3
MoveCameraLocalZ(cam, d#)
dx# = GetCameraX(cam) - x1#
dy# = GetCameraY(cam) - y1#
dz# = GetCameraZ(cam) - z1#
MoveCameraLocalZ(cam, -d#)
ux2# = dx#/d#
uy2# = dy#/d#
uz2# = dz#/d#
res# = ux1# * ux2# + uy1# * uy2# + uz1# * uz2#
angle# = acos(res#)
inFront = 0
////////CHANGE THIS////////////
if angle# < 90 then inFront = 1
endfunction inFront
function cull_objs_behind_camera() /// adapted to my own project but the jist is too cull all objects
for x = 0 to playareax
for y = 0 to playareay
if GetObjectExists( blocks[x,y] )
v = GetObjectInFront( blocks[x,y], 1 )
if v = 0 then SetBoxVisible( x,y, 0 )
if v = 1 then SetBoxVisible( x,y, 1 )
endif
next
next
endfunction
function SetBoxVisible (x,y, mode)
if mode=1
SetObjectVisible(blocks[x,y],1)
else
SetObjectVisible(blocks[x,y],0)
endif
endfunction
Enjoy for now!