How would I add camera movement to this code?
Code PART.1
--# Main
-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
function setup()
parameter.number(cameraangle, 0, 100, 1)
music("A Hero's Quest:Dungeon", true)
stick = Stick()
pylon = Wall("Documents:wall")
wall = Wall("Documents:slate")
floor = Floor("Documents:stone")
sprite("Documents:white floor")
world = World()
hero = Hero(3,3)
time = 0
TO_DEG = 180/math.pi
end
-- This function gets called once every frame
function draw()
background(0, 0, 0, 100)
font("Arial-BoldMT")
fill(0, 0, 0, 255)
fontSize(20)
time = time +0.02
text("Time "..time,WIDTH/2, HEIGHT-100)
local TO_DEG = TO_DEG
local hero = hero
perspective(60)
camera(hero.x, 0, hero.z, hero.x, 0, hero.z, 0, 1, 0)
-- Draw world
pushMatrix()
world:draw()
popMatrix()
-- Draw hero
-- roll animation
if stick.active then
rotate(-ElapsedTime*10*TO_DEG/2, 0, 0, 1)
end
scale(.25, .25, .25)
hero:draw()
-- Restore orthographic projection
ortho()
viewMatrix(matrix())
resetMatrix()
--fade out overlay
sprite("Cargo Bot:Background Fade", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
if stick.active then
local ceil = math.ceil
stick:draw()
-- move hero based on stick direction
local mvtx = math.cos(stick.direction)/50*stick.dist
local mvtz = -math.sin(stick.direction)/50*stick.dist
hero.x = hero.x + 0-mvtx
hero.z = hero.z + 0-mvtz
-- convert to table coordinates
hero.px = ceil(hero.x - .5)
hero.py = ceil(hero.z - .5)
-- lazy collision check
if world.data[hero.py][hero.px] ~= 0 then
hero.x = hero.x + mvtx
hero.z = hero.z + mvtz
hero.px = ceil(hero.x + .5)
hero.py = ceil(hero.z + .5)
end
end
end
function touched(touch)
stick:touched(touch)
if touch.x<WIDTH/2 then Stick:touched(touch) else
end
end
--# World
World = class()
function World:init()
-- define the world
self.data =
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 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},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
}
end
function World:draw()
local floor, wall, pylon = floor, wall, pylon
local offSet = 3
local px, py = hero.px, hero.py
-- look around the hero to draw whatever is around him
translate(px - offSet, 0, py - offSet)
for y = py - offSet, py + offSet do
for x = px - offSet, px + offSet do
if self.data[y] then
local val = self.data[y][x]
if val == 0 then
floor:draw()
elseif val == 1 then
wall:draw()
elseif val == 2 then
pylon:draw()
end
end
translate(1,0,0)
end
translate(-(1 + 2 * offSet), 0, 1)
end
end
--# Hero
Hero = class()
function Hero:init(x, z)
self.x, self.y, self.z = x,0,z
self.px, self.py = math.ceil(.5+x), math.ceil(.5+z)
self.mdl = Wall("Documents:marble")
sprite("Documents:new wall")
end
function Hero:draw()
self.mdl:draw()
end