@SugarRay Here’s a version of my code that uses buttons.
Replaced below code to reduce button code in button:touched.
viewer.mode=FULLSCREEN
function setup()
btn={}
rectMode(CENTER)
assert(craft, "Include Craft as a dependency")
ex,ey,ez=0,45,0
speed,xx,yy=0,0,0
dir=0
cameraX,cameraY,cameraZ=-205,1,-205
scene = craft.scene()
scene.camera.position = vec3(cameraX,0,cameraZ)
scene.camera.eulerAngles=vec3(ex,ey,ez)
scene.sun.rotation = quat.eulerAngles(45,0,45)
scene.ambientColor = color(90,90,90)
skyMaterial = scene.sky.material
skyMaterial.horizon = color(0, 203, 255, 255)
mtl = craft.material(asset.builtin.Materials.Standard)
img = readImage(asset.builtin.Surfaces.Basic_Bricks_AO)
createFloor()
-- create random walls
for z=1,100 do
xPos=math.random(-200,200)
zPos=math.random(-200,200)
createWall(xPos,zPos,math.random(20,60),.1)
xPos=math.random(-200,200)
zPos=math.random(-200,200)
createWall(xPos,zPos,.1,math.random(20,60))
end
table.insert(btn,button(WIDTH/2-100,100,"Left",function() xx=-.3 end))
table.insert(btn,button(WIDTH/2+100,100,"Right",function() xx=.3 end))
table.insert(btn,button(WIDTH/2,150,"Forward",function() speed=.1 end))
table.insert(btn,button(WIDTH/2,50,"Backward",function() speed=-.1 end))
table.insert(btn,button(WIDTH/2-100,150,"Cam Up",function() dir=.1 end))
table.insert(btn,button(WIDTH/2-100,50,"Cam Down",function() dir=-.1 end))
table.insert(btn,button(WIDTH/2+100,150,"Look Up",function() yy=-.2 end))
table.insert(btn,button(WIDTH/2+100,50,"Look Down",function() yy=.2 end))
end
function draw()
background(0)
update(DeltaTime)
scene:draw()
updateCameraPos()
for a,b in pairs(btn) do
b:draw()
end
end
function update(dt)
scene:update(dt)
scene.camera.position = vec3(cameraX,cameraY,cameraZ)
scene.camera.eulerAngles=vec3(ex,ey,ez)
end
function touched(t)
for a,b in pairs(btn) do
b:touched(t)
end
end
function updateCameraPos()
ex=ex+yy
ey=ey-xx
x=speed*math.sin(math.rad(ey))
z=speed*math.cos(math.rad(ey))
cameraX=cameraX+x
cameraY=cameraY+dir
if cameraY<1 then
cameraY=1
end
cameraZ=cameraZ+z
end
function createFloor()
local c1=scene:entity()
c1.model = craft.model.cube(vec3(400,.1,400))
c1.position=vec3(0,0,0)
c1.material = craft.material(asset.builtin.Materials.Standard)
c1.material.map = readImage(asset.builtin.Surfaces.Desert_Cliff_Color)
end
function createWall(xp,zp,xs,zs)
local c1=scene:entity()
c1.material=mtl
c1.material.map=img
c1.model=craft.model.cube(vec3(xs,6,zs))
c1.position=vec3(xp,3,zp)
end
button=class()
function button:init(x,y,n,a)
self.x=x
self.y=y
self.name=n
self.action=a
end
function button:draw()
fill(255)
rect(self.x,self.y,90,40)
fill(0)
text(self.name,self.x,self.y)
end
function button:touched(t)
if t.x>self.x-45 and t.x<self.x+45 and t.y>self.y-20 and t.y<self.y+20 then
if t.state==BEGAN then
self.action()
end
if t.state==ENDED then
xx,yy,speed,dir=0,0,0,0
end
end
end