Ill let you all take a peek on what have so far with the inventory and the player handling the inventory item and even a place holder to choose a block (will come) lots more to do on the block identifying (like with the height levels of the blocks etc)
// Project: Mesh Objects
// Created: 2018-08-04
// TO DO - Save Primative Objects to OBJ so can model them in blender and such like
// show all errors
SetErrorMode(2)
#constant PI# = 3.14159265359
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant size=16
// set window properties
SetWindowTitle( "Mesh Objects" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 1200, 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
global ang#, angx#, angy#, fDiffY#, fDiffX#,newX#,startx#, starty#, camerax#, cameray#, cameraz#,ix#,iy#,iz#,pointdistance#
global debugon=0
// *************************************************
// ** Perlin Setup - please dont upset these variables
// *************************************************
#constant perlinw = 1024 //power of 2
#constant perlinh = 1024 //power of 2
#constant perlinz = 0 // doesnt work yet - required 3D perlin noise - Yikes
#constant perlinheight=120.0
type _perlinmap
r,g, b, a, x, y
level
texture
noise
endtype
// *************************************************
// ** Mesh Memblock Types - please dont upset these variables
// *************************************************
global Cubed, VertexCount, MaxObjects, object, memblock
Type Vertex
x as float
y as float
z as float
nx as float
ny as float
nz as float
u as float
v as float
color as integer
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Mesh
VertexList as Vertex[]
TriangleList as Triangle[]
endtype
global MeshID as Mesh
type _vector
x#
y#
z#
polygon // 1 = yes - 0 = no
vindex
nindex
tindex
u#
v#
endtype
type _object
v as integer[] // the actual vertex on the mesh which need to save so can use SetMeshMemblockVertexPosition commands for example at a later time
vertex as _vector[] // eg v 1.276477 0.200934 -0.387665
normals as _vector[] // eg vn vn -0.5346 0.3304 -0.7779
faces as _vector[] // eg f 5//9 21//9 29//9
textures as _vector[] // eg vt 0.500 1 [0]
polygon as integer // 1=Yes , 0 = No
endtype
global objectmesh as _object[10] // upto 10 objects per app
type _meshmemblockvertexes
id
vfrom
vto
endtype
global meshmemblockvertexes as _meshmemblockvertexes[] // this will store the true vertex positions on a meshmemblock
type _farworlds
objID
x#
y#
z#
endtype
global farworlds as _farworlds[]
type _chunk
mesh
x#,y#,z#
air
number // this is the location on the chunk x and y position (by the selectedcube variable
cubesize // size of the cube
texturex // this is the texture number in the X from the tile map
texturey // this is the texture number in the Y on the tilemap
damage // how many times do we have to hit it before deletes
vertexposition // this is where that particular block vertex position starts on the mesh
faces as integer [5] // 6 faces to a cube - 0=top, 1=north, 2= east, 3=south, 4=west, 5=bottom - used for culling
fulltexture
alpha
inventorycounter
inventorytext
inventorycubeobj
endtype
global chunk as _chunk[50,size,128,size]
global inventoryitems as _chunk[]
// *************************************************
// ** END OF - Mesh Memblock Types - please dont upset these variables
// *************************************************
global coned
global objs as integer[]
global inventory as integer[]
global point, maxinventoryitems, inventorystep
global playblocks as integer[]
global selectedinventoryitem=0
global player as integer
debugon=0
coned=LoadImage("\media\test.png")
createdisplay(63) // even number here equals out and fits the boxes nicely on screen
wsize=1
maxchunks=0
for x=-wsize to wsize
for z=-wsize to wsize
if x=0 and z=0 // dont recreate the centre chunk - we alrady just did it,
buildchunk(maxchunks,x*size,0,z*size,size,size,size) // the depths of the surrounding chunks dont need to exceed 2
// print("biggerchunk")
else
buildchunk(maxchunks,x*size,0,z*size,size,1,size) // the depths of the surrounding chunks dont need to exceed 2
endif
inc maxchunks
next
next
global holder, sun, moon, earthcycleangle#, skybox_sky,skybox_earth, sky_speed#,mouse
sky_speed#=0.0005
holder=CreateObjectBox(1.1,1.1,1.1)
SetObjectRotation(holder,270,0,0)
SetObjectTransparency(holder,1)
SetObjectColor(holder,255,255,255,255)
setupcamera()
startgamefade=0
do
updatedisplayitems(inventorview)
updateearthcycle()
// start a fade up of all the chunks for the game to start
inc startgamefade,1
if startgamefade>=255
startgamefade=255
for i=0 to maxchunks-1
SetObjectTransparency(objs[i],0)
next
else
for i=0 to maxchunks-1
SetObjectColor(objs[i],255,255,255,startgamefade)
next
endif
movecamerawithmouse()
getplayerworldcoordinates()
if GetRawKeyPressed(82)
camerax#=0
cameray#=18
cameraz#=-10
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
endif
if GetRawMouseLeftState()
RotateObjectLocalX(player,.9)
RotateObjectLocalY(player,-.11)
//RotateObjectLocalZ(player,-1)
dec inventoryitems[selectedinventoryitem].inventorycounter
SetTextString(inventoryitems[selectedinventoryitem].inventorytext,str(inventoryitems[selectedinventoryitem].inventorycounter))
else
SetObjectRotation( player, -45, angy# + fDiffX#, 0 )
SetObjectPosition( player, getcamerax(1), getcameray(1)-2, getcameraz(1))
endif
if GetRawKeyPressed(73) // I = show invent
if inventorview=1
inventorview=2
else
inventorview=1
endif
endif
if inventorview=1
inc inventorystep,10
if inventorystep=>screenheight-10 then inventorystep=screenheight-10
endif
if inventorview=2
dec inventorystep,10
if inventorystep<0 then inventorystep=0
endif
if GetRawKeyState(78) then dec sky_speed#,.0005
if GetRawKeyState(77) then inc sky_speed#,.0005
if GetRawKeyPressed(79) then inc pointdistance#,.1
if GetRawKeyPressed(76) then dec pointdistance#,.1
if GetRawKeyState(38) then inc cameraz#,.1
if GetRawKeyState(40) then dec cameraz#,.1
if GetRawKeyState(37) then inc camerax#,.1
if GetRawKeyState(39) then dec camerax#,.1
if GetRawKeyState(65) then dec camerax#,.1
if GetRawKeyState(68) then inc camerax#,.1
if GetRawKeyState(87) then inc cameray#,.1
if GetRawKeyState(83) then dec cameray#,.1
if GetRawKeyPressed(27) then exit
// SetObjectPosition(player,camerax#,cameray#-4,cameraz#+4)
SetCameraPosition(1,camerax#,cameray#,cameraz#)
if debugon=1
print("FPS " + str(ScreenFPS()) + " - Last Key" + str(GetRawLastKey()))
print (maxobjects)
print("Cursors to move about, W and S to up and down")
print ("Day/Night Cycle Time " + str(sky_speed#) + " Press N and M to dec/inc")
print ("R to reset posiiton back on the main chunk")
endif
Render2DBack()
Sync()
loop
function updateearthcycle()
print("Time Of Day: " + GetCurrentTime())
distance#=4300
ex#=distance# * sin(earthcycleangle#)
ez#=distance# * cos(earthcycleangle#)
SetObjectPosition(sun,ex#, ez#, 0)
SetPointLightPosition(10000,ex#,ez#,0)
// SetSunDirection(ex#,0,ez#)
ex#=distance# * sin(earthcycleangle#-180)
ez#=distance# * cos(earthcycleangle#-180)
SetObjectPosition(moon,ex#, ez#, 0)
SetPointLightPosition(10001,ex#,ez#,0)
inc earthcycleangle#,sky_speed#
RotateObjectLocalZ(skybox_sky,-sky_speed#)
endfunction
function updatedisplayitems(inventoryview)
if selectedinventoryitem>=0
if inventoryview=1
for a=0 to 10
col = MakeColor(255-(a*(100/5)),0,0)
DrawBox(screenwidth/2+(selectedinventoryitem*GetSpriteWidth(inventory[selectedinventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[selectedinventoryitem]))+a,screenheight-inventorystep+a,screenwidth/2+(selectedinventoryitem*GetSpriteWidth(inventory[selectedinventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[selectedinventoryitem]))+100-a,screenheight-10-a,col,col,col,col,0)
next
endif
SetObjectPosition(inventoryitems[selectedinventoryitem].inventorycubeobj, .5,0,2)
FixObjectToObject(inventoryitems[selectedinventoryitem].inventorycubeobj,player)
SetObjectRotation(inventoryitems[selectedinventoryitem].inventorycubeobj,45,0,0 )
SetObjectScale(inventoryitems[selectedinventoryitem].inventorycubeobj,.3,.3,2)
// SetObjectRotation(inventoryitems[selectedinventoryitem].inventorycubeobj, newX#, angy# + fDiffX#, 0 )
// SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
SetObjectVisible(inventoryitems[selectedinventoryitem].inventorycubeobj,1)
endif
inventoryitem=0
for inx = 0 to 20
for invy=0 to 6
if inventoryitem<55
SetSpriteVisible(inventory[inventoryitem],1)
//SetSpritePosition(inventory[inventoryitem],screenwidth/2+(inx*GetSpriteWidth(inventory[inventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[inventoryitem])),screenheight-inventorystep+(invy))
SetSpritePosition(inventory[inventoryitem],inx*100,25+screenheight-inventorystep+(invy*100))
// SetTextPosition(inventoryitems[inventoryitem].inventorytext,screenwidth/2+(inventoryitem*GetSpriteWidth(inventory[inventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[inventoryitem]))+10,screenheight-inventorystep+10)
SetTextPosition(inventoryitems[inventoryitem].inventorytext,10+inx*100,35+screenheight-inventorystep+(invy*100))
if GetSpriteCollision(inventory[inventoryitem],point)
print("COLLIDED WITH " + str(inventoryitem))
if inventoryview=1
for a=0 to 1
col = MakeColor(255-(a*(100/5)),255-(a*(100/5)),0)
DrawBox(screenwidth/2+(inventoryitem*GetSpriteWidth(inventory[inventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[inventoryitem]))+a,screenheight-inventorystep+a,screenwidth/2+(inventoryitem*GetSpriteWidth(inventory[inventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(inventory[inventoryitem]))+100-a,screenheight-10-a,col,col,col,col,0)
next
endif
if GetRawMouseLeftPressed()
selectedinventoryitem=inventoryitem
for a=0 to maxinventoryitems
SetObjectVisible(inventoryitems[a].inventorycubeobj,0)
next
endif
endif
endif
inc inventoryitem
next
next
// SetSpritePosition(playblocks[inventoryitem],screenwidth/2+(inventoryitem*GetSpriteWidth(playblocks[inventoryitem]))-((maxinventoryitems/2)*GetSpriteWidth(playblocks[inventoryitem])),screenheight-80)
endfunction
function createdisplay(items)
point = CreateSprite(createpoint())
SetSpriteDepth(point,0)
maxinventoryitems=items
a=0
for tx=1 to 5
for ty=1 to 11
inventory.insert(CreateSprite(createinventory(a,1,tx,ty)))
SetSpriteVisible(inventory[a],0)
inc a
// inventory.insert(CreateSprite(createinventory(a,1,tx,ty)))
// SetSpriteVisible(inventory[a],1)
// inc a
next
next
print(inventory.length)
endfunction
function createinventory(i,fulltexture,texturex,texturey)
inv as _chunk // we create a block with the chunk block parameters
sizes=100
swap()
ClearScreen()
for a=0 to 10
col = MakeColor(255-(a*(sizes/5)),255-(a*(sizes/5)),255-(a*(sizes/5)))
DrawBox((screenwidth/2-50)+a,(screenheight/2-50)+a,((screenwidth/2-50)+sizes)-a,screenheight/2-50+((sizes*2)-sizes)-a,col,col,col,col,0)
next
DrawBox((screenwidth/2-50)+a,(screenheight/2-50)+a,((screenwidth/2-50)+sizes)-a+1,screenheight/2-50+((sizes*2)-sizes)-a,MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),1)
inv.texturex=texturex
inv.texturey=texturey
inv.air=1
inv.fulltexture=fulltexture
inv.cubesize=1
inv.x#=0
inv.y#=0
inv.z#=0
inv.faces[0]=1
inv.faces[1]=1
inv.faces[2]=1
inv.faces[3]=1
inv.faces[4]=1
inv.faces[5]=1
inv.inventorycounter=1
inv.inventorytext=CreateText(str(inv.inventorycounter))
SetTextSize(inv.inventorytext,20)
inventoryitems.insert(inv)
addcubed(inv)
inventoryitems[i].inventorycubeobj = CreateObjectFromMeshWithUVTexturing(meshid,coned)
SetObjectRotation(inventoryitems[i].inventorycubeobj,0,45,0)
// SetObjectScale(inventoryitems[i].inventorycubeobj,.3,.3,2)
emptymesh()
Render2DFront()
render()
id=GetImage(screenwidth/2-50,screenheight/2-50,sizes,sizes)
// take it way off the screen cause dont want to delete it but require to be able to make other objects in the centre
SetObjectPosition(inventoryitems[i].inventorycubeobj,10000,10000,10000)
endfunction id
function createpoint()
sizes=20
swap()
DrawEllipse(sizes/2,sizes/2,sizes/2,sizes/2,MakeColor(255,255,255),MakeColor(255,255,255),1)
for a=-2 to 2
DrawEllipse(sizes/2,sizes/2,sizes/2-a,sizes/2-a,MakeColor(0,0,0),MakeColor(0,0,0),0)
DrawLine(sizes/2+a,0,sizes/2+a,sizes,MakeColor(0,0,0),MakeColor(0,0,0))
DrawLine(0,sizes/2+a,sizes,sizes/2+a,MakeColor(0,0,0),MakeColor(0,0,0))
next
render()
id = GetImage(0,0,sizes+2,sizes+2)
SetRawMouseVisible(0)
endfunction id
function buildchunk(id,locx,locy,locz,sizex,sizey,sizez as integer)
// build chunk
for x=0 to sizex
for y=0 to sizey/2 // we start on a minecraft chunk being half of the whole chunk, so that we can add blocks above ground
for z=0 to sizez
if random(1,100)>80 or y=sizey/2
// InstanceMeshObject(0,1,locx+x*2,locy+y*2,locz+z*2,1,1,1,MakeColor(random(0,255),random(0,255),random(0,255)),0)
// top
// createMeshObjectPlaneWithTexture(locx+x*2,locy+y*2,locz+z*2,1,1)
chunk[id,x,y,z].air=1 // there is something occupying this slot
chunk[id,x,y,z].faces[0]=1
chunk[id,x,y,z].faces[1]=1
chunk[id,x,y,z].faces[2]=1
chunk[id,x,y,z].faces[3]=1
chunk[id,x,y,z].faces[4]=1
chunk[id,x,y,z].faces[5]=1
chunk[id,x,y,z].texturex=4
chunk[id,x,y,z].texturey=6
chunk[id,x,y,z].fulltexture=1
chunk[id,x,y,z].alpha=100
chunk[id,x,y,z].cubesize=2
chunk[id,x,y,z].x#=locx+x*2
chunk[id,x,y,z].y#=locy+y*2
chunk[id,x,y,z].z#=locz+z*2
addcubed(chunk[id,x,y,z])
inc maxobjects
else
chunk[id,x,y,z].air=0 // nothing is occupying this slot
endif
next
next
next
objs.insert(CreateObjectFromMeshWithUVTexturing(meshid,coned))
SetObjectPosition(objs[id],locx,locy,locz)
SetObjectColor(objs[id],255,255,255,0)
emptymesh()
endfunction
function emptymesh()
meshid.TriangleList.length=-1
meshid.VertexList.length=-1
endfunction
// add cube
// cb - bring in the cube data so that it can either create it from the x#,y#,z#, cubesize and faces data
// fulltextured - 1 if there is a different top and overhanging side - or 0 to make it the same texture all over
function addcubed(cb ref as _chunk)
// fulltextured = cb.fulltexture
// if top
// calculate the UVs - YIKES here we go
cubex#=((cb.texturex-1) * .5) + cb.texturex //
cubey#=(cb.texturey * 0.5)+ 0.5
if cb.fulltexture=1 //2nd texture on the tilesheet (or a look of the overhanging texture
// make cube with a different top and overhanging side texture feel
maxx# = ((100/15.00)/100) * cubex#
minx# = (maxx#-(100/15.00)/100)
halfx# = (maxx#/2 + minx# / 2)
// bottom texture
bottommaxx# = ((100/15.00)/100) * (cubex#+.5)
bottomminx# = (bottommaxx#-((100/15.00)/100))
bottomhalfx# = (bottommaxx#/2 + bottomminx# / 2)
maxy# = ((100/11.00)/100) * cubey#
miny# = (maxy#-(100/11.00)/100)
halfy# = (maxy#/2 + miny# / 2)
else
// make cube a full of the same texture
maxx# = ((100/15.00)/100) * (cubex#+0.5)
minx# = (maxx#-((100/15.00)/100))
halfx# = (maxx#/2 + minx# / 2)
// bottom texture
bottommaxx# = ((100/15.00)/100) * (cubex#+.5)
bottomminx# = (bottommaxx#-((100/15.00)/100))
bottomhalfx# = (bottommaxx#/2 + bottomminx# / 2)
maxy# = ((100/11.00)/100) * cubey#
miny# = (maxy#-(100/11.00)/100)
halfy# = (maxy#/2 + miny# / 2)
endif
alpha=255
color=MakeColor(alpha,alpha,alpha) // full alpha
// create the cube
cubesize=cb.cubesize
// faces as integer [5] // 6 faces to a cube - 0=top, 1=north, 2= east, 3=south, 4=west, 5=bottom - used for culling
if cb.faces[1] = 1 // north
AddVertex(meshID,cb.x# ,cb.y# ,cb.z# ,0,1,0, maxx#,halfy#,color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z# ,0,1,0, maxx#,miny#,color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z# ,0,1,0, halfx#,halfy#,color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z# ,0,1,0, halfx#,halfy#,color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z# ,0,1,0, maxx#,miny#,color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+cubesize ,cb.z# ,0,1,0, halfx#,miny#,color)
endif
if cb.faces[4] = 1 // west
AddVertex(meshID,cb.x#+cubesize ,cb.y#+cubesize ,cb.z# ,0,1,0 ,maxx# ,miny#, color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny#,color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z# ,0,1,0 ,maxx#,halfy#, color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z# ,0,1,0 ,maxx# ,halfy#, color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx# ,miny#,color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,halfx# ,halfy#, color)
endif
if cb.faces[5] = 1 // bottom
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,bottommaxx#,miny# , color)
AddVertex(meshID,cb.x# ,cb.y# ,cb.z# ,0,1,0 ,bottomhalfx#,halfy# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z# ,0,1,0 ,bottommaxx#,halfy#, color)
AddVertex(meshID,cb.x# ,cb.y# ,cb.z# ,0,1,0 ,bottomhalfx#,halfy# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,bottommaxx#,miny# , color)
AddVertex(meshID,cb.x# ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,bottomhalfx# ,miny# , color)
endif
if cb.faces[3] = 1 // south
AddVertex(meshID,cb.x# ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,halfx# ,halfy# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,maxx# ,halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,maxx#,miny# , color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx# ,miny# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,maxx# ,halfy# , color)
endif
if cb.faces[2] = 1 //east
AddVertex(meshID,cb.x# ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,maxx# ,halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z# ,0,1,0 ,halfx# ,miny# , color)
AddVertex(meshID,cb.x# ,cb.y# ,cb.z# ,0,1,0 ,halfx#,halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z# ,0,1,0 ,halfx# ,miny# , color)
AddVertex(meshID,cb.x# ,cb.y# ,cb.z#+-cubesize ,0,1,0 ,maxx# , halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+cubesize ,cb.z#+-cubesize ,0,1,0 ,maxx# , miny# , color)
endif
if cb.faces[0] = 1
if cb.fulltexture=1
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,minx#,miny#, color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,minx#,halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx# ,halfy# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny# , color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,minx#,halfy# , color)
else
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,maxx#,miny#, color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,maxx#,halfy# , color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx# ,halfy# , color)
AddVertex(meshID,cb.x#+cubesize ,cb.y#+Cubesize ,cb.z#+-cubesize ,0,1,0 ,halfx#,miny# , color)
AddVertex(meshID,cb.x# ,cb.y#+Cubesize ,cb.z# ,0,1,0 ,maxx#,halfy# , color)
endif
endif
endfunction
function setupcamera()
player = CreateObjectSphere(1,1,1)
camerax#=0
cameray#=18
cameraz#=-10
startx#=screenwidth/2
starty#=screenheight/2
SetRawMousePosition(startx#,starty#)
SetCameraRange(1,.01,9000)
// create an inverted skybox
CreateMeshObjectBoxInvertedWithTexture(0,0,0,9000,9000,9000,1,1,MakeColor(135,206,250))
skybox_sky=CreateObjectFromMeshWithColor(meshid)
SetObjectColor(skybox_sky,255,255,255,10)
emptymesh()
// create a sun
sun=CreateObjectSphere(150,20,20)
SetObjectColor(sun,253,184,19,255)
CreatePointLight(10000,0,1000,0,10000,0,0,0)
// create a moon
moon=CreateObjectSphere(150,20,20)
SetObjectColor(moon,255,255,255,255)
CreatePointLight(10001,0,-1000,0,50000,253,184,19)
endfunction
function CreateMeshObjectBoxInvertedWithTexture(X#, Y#, Z#, Width#, Height#, Depth#,top,bottom,color)
// Color= MakeColor(255,255,255) // This doesnt use color but AddVertex requires it
//Front Face
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2), 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2), 0,1,0, 0,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2), 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2), 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2), 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2), 0,1,0, 1,1,Color)
// Left
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2) , 0,1,0, 1,1,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth# , 0,1,0, 0,0,Color)
// Right
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2) , 0,1,0 , 0,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0 , 0,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0 , 1,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0 , 0,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth# , 0,1,0 , 1,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2)+depth#, 0,1,0 , 1,1,Color)
//back
AddVertex(MeshID, (X#-width#/2) ,(y# -height#/2) ,(Z#-depth#/2)+depth#, 0,1,0, 1,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth#, 0,1,0, 0,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth#, 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2)+depth#, 0,1,0, 1,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2)+depth#, 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth#, 0,1,0, 0,0,Color)
if top=1
// top
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0, 0,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth# , 0,1,0, 1,1,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2) , 0,1,0, 0,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth# , 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2)+height# ,(Z#-depth#/2)+depth# , 0,1,0, 1,1,Color)
endif
//bottom
if bottom=1
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2) , 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#+width#/2) ,(y#-height#/2) ,(Z#-depth#/2) , 0,1,0, 0,0,Color)
AddVertex(MeshID, (X#-width#/2)+width# ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2) , 0,1,0, 0,1,Color)
AddVertex(MeshID, (X#+width#/2) ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0, 1,0,Color)
AddVertex(MeshID, (X#-width#/2) ,(y#-height#/2) ,(Z#-depth#/2)+depth# , 0,1,0, 1,1,Color)
endif
endfunction MeshID
function creategradient()
swap()
for y = 0 to 256
for x = 0 to 256
diffX# = x - 127.0
diffY# = y - 127.0
dist# = Sqrt( diffX#*diffX# + diffY#*diffY# )
dist# = dist# * 2.0078125
if ( dist# > 255 ) then dist# = 255
color = MakeColor(255 - Round(dist#),255 - Round(dist#),255 - Round(dist#))
DrawBox(x,y,x+1,y+1,color,color,color,color,1)
//int index = y*256 + x
next
next
// sync()
//sleep(1000)
img = GetImage(0,0,256,256)
render()
endfunction img
// gets the players coordinates, so that we know the height of the block that the player is sitting on
// so can move the camera (on top of the cube)
function getplayerworldcoordinates()
chunkhit=(mouseover()-100000-12) * 2
//if chunkhit<0
// SetObjectVisible(holder,0)
// else
// SetObjectVisible(holder,1)
// endif
cubex=ix#-1
cubey=iz#-1
selectedcube = (cubex * 16) + cubey
if debugon=1 then print(selectedcube)
// position the block selector at the intersection point
newX# = round(ix# / 2) * 2
newY# = Round(iz# / 2) * 2
print(ix#)
print(iy#)
// if selectedcube>0 and selectedcube < numberofcubes
//move the cube locator and reposition the mouse to be in the same position so it doesnt look odd when you keep
// left mouse button pressed to rotate the camera
SetObjectPosition(holder,newX#+1, cameray#-16, NewY#+1)
SetSpritePosition(point,getpointerx(),getpointery())
//SetSpritePosition(mouse,getpointerx(),getpointery())
// SetRawMousePosition(NewX#, NewY#)
// endif
endfunction
function mouseOver()
myX as float
myY as float
my3dX as float
my3dY as float
my3dZ as float
rayStartX as float
rayStartY as float
rayStartZ as float
rayEndX as float
rayEndY as float
rayEndZ as float
myX=getpointerx()
myY=getpointery()
my3dX=Get3DVectorXFromScreen(myX,myY)
my3dY=Get3DVectorYFromScreen(myX,myY)
my3dZ=Get3DVectorZFromScreen(myX,myY)
rayStartX=my3dX+GetCameraX(1)
rayStartY=my3dY+GetCameraY(1)
rayStartZ=my3dZ+GetCameraZ(1)
rayEndX=my3dX+GetCameraX(1)
rayEndY=my3dY+GetCameraY(1)
rayEndZ=my3dZ+GetCameraZ(1)
theObjectHit= ObjectRayCast(0,rayStartX,rayStartY,rayStartZ,rayEndX,rayEndY,rayEndZ)
// Move camera with WASD
// Rotate Camera with Arrow Keys
MoveCameraLocalX(1,GetRawKeyState(68) - GetRawKeyState(65))
MoveCameraLocalZ(1,GetRawKeyState(87) - GetRawKeyState(83))
RotateCameraGlobalY(1,GetRawKeyState(39) - GetRawKeyState(37))
RotateCameraLocalX(1,GetRawKeyState(40) - GetRawKeyState(38))
cx# = GetCameraX(1)/2.0
cy# = GetCameraY(1)/2
cz# = GetCameraZ(1)/2
pxRaw# = Get3DVectorXFromScreen(myX,myY)
pyRaw# = Get3DVectorYFromScreen(myX,myY)
pzRaw# = Get3DVectorZFromScreen(myX,myY)
// get pointer vector y added to camera's y position
pyRelative# = pyRaw# + cy#
// find distance between camera's y position and pyRelative#
dist# = cy# - pyRelative#
// calculate scaling value by finding how many times dist# goes into the camera's y position
s# = cy# / dist#
// find the intersection point by multiplying pointer vector by s# and adding it to the camera position.
// ignoring Y in this case since it should be 0 anyway.
ix# = cx# + pxRaw# * s#
iz# = cz# + pzRaw# * s#
print (ix#)
endfunction theObjectHit
// Delete an object from a mesh
// ID = object to delete
// mesh = is the main meshmemblock that deletes from
function deletemeshobject(id, mesh, m ref as Mesh)
for i=meshmemblockvertexes[id].vfrom to meshmemblockvertexes[id].vto
SetMemblockFloat(memblock,72+i*36,0)
SetMemblockFloat(memblock,76+i*36,0)
SetMemblockFloat(memblock,80+i*36,0)
SetMemblockFloat(memblock,84+i*36,0)
SetMemblockFloat(memblock,88+i*36,0)
SetMemblockFloat(memblock,92+i*36,0)
SetMemblockFloat(memblock,96+i*36,0)
SetMemblockFloat(memblock,100+i*36,0)
SetMemblockInt(memblock,104+i*36,0) //maybe one day or year in 2019 lol
next
SetObjectMeshFromMemblock(mesh,1,memblock)
endfunction
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
// if ( newX# > 360 ) then newX# = 360
// if ( newX# < -360 ) then newX# = -360
// if we keep hold of the left mouse button then rotate the view otherwise the camera stays put
// so can concentrate on adding/removing blocks at that positon
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
Function AddVertex(m ref as Mesh, x as float, y as float, z as float, nx as float, ny as float, nz as float, u as float, v as float, color as integer)
vert as vertex
vert.x = x
vert.y = y
vert.z = z
vert.nx = nx
vert.ny = ny
vert.nz = nz
vert.u = u
vert.v = v
vert.color = color
m.VertexList.Insert(vert)
endfunction
Function AddTriangle(m ref as Mesh, v1 as integer, v2 as integer, v3 as integer)
t as Triangle
t.v1 = v1
t.v2 = v2
t.v3 = v3
m.TriangleList.Insert(t)
endfunction
Function CreateObjectFromMeshWithColor(m ref as mesh)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 2
IndexOffset = 72 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,4)
SetMemblockInt(memblock,12,36) // no color - 36 if color
SetmemblockInt(memblock,16,72)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
SetMemblockInt(memblock,60,0x08010401) // maybe one day or year in 2019 lol
SetMemblockString(memblock,64,"color") // maybe one day or year in 2019 lol
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,72+i*36,m.VertexList[i].x)
SetMemblockFloat(memblock,76+i*36,m.VertexList[i].y)
SetMemblockFloat(memblock,80+i*36,m.VertexList[i].z)
SetMemblockFloat(memblock,84+i*36,m.VertexList[i].nx)
SetMemblockFloat(memblock,88+i*36,m.VertexList[i].ny)
SetMemblockFloat(memblock,92+i*36,m.VertexList[i].nz)
SetMemblockFloat(memblock,96+i*36,m.VertexList[i].u)
SetMemblockFloat(memblock,100+i*36,m.VertexList[i].v)
SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
/* for i = 0 to m.TriangleList.Length
SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
next
*/
id = CreateObjectFromMeshMemblock(memblock)
// DeleteMemblock(memblock)
endfunction id
Function CreateObjectFromMeshWithUVTexturing(m ref as mesh, texture)
DeleteMemblock(memblock)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 2
IndexOffset = 72 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,4)
SetMemblockInt(memblock,12,36) // no color - 36 if color
SetmemblockInt(memblock,16,60)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
SetMemblockInt(memblock,60,0x08010401) // maybe one day or year in 2019 lol
SetMemblockString(memblock,64,"color") // maybe one day or year in 2019 lol
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,72+i*36,m.VertexList[i].x)
SetMemblockFloat(memblock,76+i*36,m.VertexList[i].y)
SetMemblockFloat(memblock,80+i*36,m.VertexList[i].z)
SetMemblockFloat(memblock,84+i*36,m.VertexList[i].nx)
SetMemblockFloat(memblock,88+i*36,m.VertexList[i].ny)
SetMemblockFloat(memblock,92+i*36,m.VertexList[i].nz)
SetMemblockFloat(memblock,96+i*36,m.VertexList[i].u)
SetMemblockFloat(memblock,100+i*36,m.VertexList[i].v)
SetMemblockInt(memblock,104+i*36,m.VertexList[i].color) //maybe one day or year in 2019 lol
next
// for i = 0 to m.TriangleList.Length
// SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
// SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
// SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
// next
DeleteObject(id)
id = CreateObjectFromMeshMemblock(memblock)
SetObjectImage(id,texture,0)
SetObjectUVScale(id,0,2,2)
SetImageMagFilter(texture,0) // no blurring on edges
SetImageMinFilter(texture,0) // no blurring on edges
SetObjectTransparency(id,1)
// if mode=1 // creates the world or it creates for other small things - like explosion blocks
// we need to keep the world chunk in memory so we can keep updating the vertices
meshmemory = memblock
// endif
// DeleteMemblock(memblock)
endfunction id
I have just slightly tweaked the atlas map for the tiles - so has one tile in the graphic that looks like tree bark and is easily identify the blocks on the main chunk when it loads in
Press "I" to load the Inventory and select
Press Left mouse button to use the iitem
Lots more work to do though, but can have a go at tweaking it to your refinements if you like
Enjoy and Catch up soon
EDIT - Janbo... I shall do the drectional faces and cull the blocks too - promise - its easy to do - but been side tracked to get inventory system at work.