Skip to content

Instantly share code, notes, and snippets.

@GlueBalloon
Last active March 18, 2021 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GlueBalloon/2f75849193aeedf0b508 to your computer and use it in GitHub Desktop.
Save GlueBalloon/2f75849193aeedf0b508 to your computer and use it in GitHub Desktop.
Xavier-tile-box-maze
--# Main
-- Use this function to perform your initial setup
displayMode(OVERLAY)
supportedOrientations(LANDSCAPE_ANY)
function setup()
stick = Stick()
empty = Floor()
unerasable = Wall("Cargo Bot:Crate Yellow 2")
pylon = Wall("Cargo Bot:Crate Red 2")
wall = Wall("Cargo Bot:Crate Yellow 2")
floor = Floor("Cargo Bot:Crate Green 2")
secretDoor = Wall("Cargo Bot:Crate Yellow 2")
secretFloor = Floor("Cargo Bot:Crate Green 2")
goal = Floor("Project:Goal")
local goalImage = makeGoalImage()
goal.model.texture = goalImage
world = World()
hero = Hero(4,17)
gameWon = false
winBanner = YouWinBanner()
TO_DEG = 180/math.pi
eraseMode = false
eraseModeToggle = function ()
if eraseMode == false then
eraseMode = true
world.editMode = true
print("erase mode ON")
return
end
eraseMode = false
print("erase mode OFF")
end
writeMap = function()
local dataAsString = getString(world.activeData,"saveData")
saveProjectTab("WorldData", "savedMap = "..dataAsString)
print("map saved")
end
parameter.action("tap to turn erase on/off", function () eraseModeToggle() end)
parameter.action("tap to save map", function () writeMap() end)
parameter.action("tap to load map", function () world.activeData = savedMap end)
parameter.action("tap to reset map", function () restart() end)
parameter.action("tap to clear map", function ()
world.activeData = world.clearMap
world.editMode = true
end)
end
function makeGoalImage()
local goalImage = readImage("Cargo Bot:Crate Green 1")
setContext(goalImage)
tint(21, 21, 21, 255)
sprite("SpaceCute:Star", goalImage.width/2,(goalImage.height/2)-2,goalImage.width, goalImage.height)
noTint()
sprite("SpaceCute:Star", goalImage.width/2,goalImage.height/2,goalImage.width, goalImage.height)
setContext()
return goalImage
end
-- This function gets called once every frame
function draw()
background(0)
local TO_DEG = TO_DEG
local hero = hero
perspective(60)
camera(hero.x, 3, 1 + hero.z, hero.x, 0, hero.z, 0, 1, 0)
-- Draw world
pushMatrix()
world:draw()
popMatrix()
-- Draw hero
translate(hero.x, hero.y, hero.z)
rotate(stick.direction*TO_DEG, 0, 1, 0)
-- roll animation
if stick.active then
rotate(-ElapsedTime*10*TO_DEG, 0, 0, 1)
end
scale(.25, .25, .25)
hero:draw()
-- Restore orthographic projection
ortho()
viewMatrix(matrix())
resetMatrix()
-- corner fade overlay
sprite("Cargo Bot:Background Fade", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
if gameWon then
winBanner:draw()
end
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 + mvtx
hero.z = hero.z + mvtz
-- convert to table coordinates
hero.px = ceil(hero.x - .5)
hero.py = ceil(hero.z - .5)
-- lazy collision check
if eraseMode and world.activeData[hero.py][hero.px] ~= 5 then
world.activeData[hero.py][hero.px] = -1
elseif world.activeData[hero.py][hero.px] == 3 then
world.activeData = world.data2
elseif world.activeData[hero.py][hero.px] == 4 then
world.activeData = world.data
elseif world.editMode and world.activeData[hero.py][hero.px] == -1 then
world.activeData[hero.py][hero.px] = 0
elseif world.activeData[hero.py][hero.px] == 6 then
gameWon = true
elseif world.activeData[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)
end
function offsetFromCenter(touch)
local centerX, centerY = WIDTH/2,HEIGHT/2
return touch.x - centerX, touch.y - centerY
end
function worldCoordinatesAt(touch)
local offsetX, offsetY = offsetFromCenter(touch)
local totalSquaresHigh, squaresAbove, squaresBelow = 5, 2.5, 1.5
local approxTileScreenHeight = HEIGHT/totalSquaresHigh
local screenHeightAboveAndBelowCenterTile = (HEIGHT - approxTileScreenHeight)/2
local upperTileApproxScreenHeight = screenHeightAboveAndBelowCenterTile / squaresAbove
local lowerTileApproxScreenHeight = screenHeightAboveAndBelowCenterTile / squaresBelow
local totalSquaresWide, squaresLeftOrRight = 6, 2.5
local approxTileScreenWidth = WIDTH/totalSquaresWide
local verticalTileOffset, horizontalTileOffset
if offsetX < 0 then
end
end
--# World
World = class()
function World:init()
self.editMode = false
-- define the world
self.data =
{
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{5, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1, 0,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0, 0, 0, 0, 0, 0, 0,-1, 0,-1, 0,-1,-1, 0,-1,-1,-1,-1,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1, 0,-1, 1,-1, 0,-1, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0, 0, 0,-1, 1,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1, 0,-1, 0,-1, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1, 0,-1, 0, 0, 0,-1,-1,-1,-1,-1,-1, 5},
{5, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 5},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
}
self.activeData = self.data
self.tilesFromCenterToRender = 6
self.centerTileScreenWidth = WIDTH/6 -- hard coded here, should really be calculated
end
function createBox(x,y,w,h)
-- polygons are defined by a series of points in counter-clockwise order
local box = physics.body(POLYGON, vec2(-w/2,h/2), vec2(-w/2,-h/2), vec2(w/2,-h/2), vec2(w/2,h/2))
box.interpolate = true
box.x = x
box.y = y
box.restitutions = 0.25
box.sleepingAllowed = false
box.type = STATIC
return box
end
function World:draw()
local floor, wall, pylon = floor, wall, pylon
local offSet = self.tilesFromCenterToRender
local px, py = hero.px, hero.py
-- look around the hero to draw whatever is around him
translate(px - offSet, 0, py - offSet)
shiftTo2D = vec3(2,-1,1)
for y = py - offSet, py + offSet do
for x = px - offSet, px + offSet do
if self.activeData[y] then
local val = self.activeData[y][x]
if val == -1 then
if world.editMode then
empty:draw()
else
wall:draw()
end
elseif val == 0 then
floor:draw()
elseif val == 1 then
wall:draw()
elseif val == 2 then
pylon:draw()
elseif val == 3 then
secretDoor:draw()
elseif val == 4 then
secretFloor:draw()
elseif val == 5 then
unerasable:draw()
elseif val == 6 then
goal:draw()
end
end
translate(1,0,0)
end
translate(-(1 + 2 * offSet), 0, 1)
end
end
function World:tileAtPosition(position)
return self.activeData[position.y][position.x]
end
function World:tilePositionAtXY(givenScreenXY)
return hero:actualWorldPosition()
end
function World:flipTileAtPosition(position)
local currentValue = self:tileAtPosition(position)
local defaultFlipTo = 0
local newValue
if currentValue == 0 then
newValue = -1
else
newValue = defaultFlipTo
end
self.activeData[position.y][position.x] = newValue
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("Cargo Bot:Crate Blue 2")
end
function Hero:actualWorldPosition()
return vec2(self.px - 1, self.py - 1)
end
function Hero:draw()
self.mdl:draw()
end
--# Wall
Wall = class()
function Wall:init(tex)
-- all the unique vertices that make up a cube
local vertices =
{
vec3(-0.5,-0.5, 0.5), -- Left bottom front
vec3( 0.5, -0.5, 0.5), -- Right bottom front
vec3( 0.5, 0.5, 0.5), -- Right top front
vec3(-0.5, 0.5, 0.5), -- Left top front
vec3(-0.5, -0.5, -0.5), -- Left bottom back
vec3( 0.5, -0.5, -0.5), -- Right bottom back
vec3( 0.5, 0.5, -0.5), -- Right top back
vec3(-0.5, 0.5, -0.5), -- Left top back
}
-- now construct a cube out of the vertices above
local verts =
{
-- Front
vertices[1], vertices[2], vertices[3],
vertices[1], vertices[3], vertices[4],
-- Right
vertices[2], vertices[6], vertices[7],
vertices[2], vertices[7], vertices[3],
-- Back
vertices[6], vertices[5], vertices[8],
vertices[6], vertices[8], vertices[7],
-- Left
vertices[5], vertices[1], vertices[4],
vertices[5], vertices[4], vertices[8],
-- Top
vertices[4], vertices[3], vertices[7],
vertices[4], vertices[7], vertices[8],
-- Bottom
vertices[5], vertices[6], vertices[2],
vertices[5], vertices[2], vertices[1],
}
-- all the unique texture positions needed
local texvertices =
{
vec2(0,0),
vec2(1,0),
vec2(0,1),
vec2(1,1)
}
-- apply the texture coordinates to each triangle
local texCoords =
{
-- Front
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Right
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Back
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Left
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Top
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Bottom
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
}
self.model = mesh()
self.model.vertices = verts
self.model.texture = tex
self.model.texCoords = texCoords
self.model:setColors(255,255,255,255)
end
function Wall:draw()
self.model:draw()
end
--# Floor
Floor = class()
function Floor:init(tex)
-- all the unique vertices that make up a cube
local vertices =
{
vec3( 0.5, -0.5, 0.5), -- Right top front
vec3(-0.5, -0.5, 0.5), -- Left top front
vec3( 0.5, -0.5, -0.5), -- Right top back
vec3(-0.5, -0.5, -0.5), -- Left top back
}
-- now construct a cube out of the vertices above
local verts =
{
-- Bottom
vertices[3], vertices[4], vertices[2],
vertices[3], vertices[2], vertices[1],
}
-- all the unique texture positions needed
local texvertices =
{
vec2(1,1),
vec2(0,1),
vec2(1,0),
vec2(0,0),
--[[
vec2(0,0),
vec2(1,0),
vec2(0,1),
vec2(1,1)
]]
}
local texCoords =
{
-- Bottom
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
}
self.model = mesh()
self.model.vertices = verts
self.model.texture = tex
self.model.texCoords = texCoords
if tex ~= nil then
self.model:setColors(255,255,255,255)
else
self.model:setColors(0,0,0,255)
end
end
function Floor:draw()
self.model:draw()
end
--# SerializeLib
-- SerializeLib
-- getString
-- Converts the contents of a table to a simple text format. Child class names will
-- be prefixed with a "-".
-- Arguments
-- obj: the object or table to encode
-- name: the name of the object, will be embedded in the returned data
-- returns: string containing the JSON encoded value of obj
function getString(obj,name,i)
if i == nil then i=-1 end
local ret=""
local val=""
if type(obj) == "nil" then
val = "nil"
elseif type(obj) == "number" or type(obj) == "boolean" then
val = tostring(obj)
elseif type(obj) == "string" then
val = escape(obj)
elseif type(obj) == "userdata" then
name="userdata:" .. name
val = escape(tostring(obj))
elseif type(obj) == "table" then
if name ~= nil and obj.is_a ~= nil then
name = "class:" .. name
end
if name ~= nil then val = "{" end
for k,v in pairs(obj) do
if tostring(k):sub(1,1) ~= "_" then
local s=getString(v,k,i+1)
if s ~= "" then
val = val .. s
end
end
end
if name ~= nil then
val = val .. string.rep(" ",i) .. "}\n"
end
else
name=""
val=""
end
if name ~= nil and name ~= "" then
-- ret = ret .. string.rep(" ",i) .. name .. "="
ret = ret .. string.rep(" ",i)
end
if val ~= "" then
ret = ret .. val .. ","
end
-- ******* pretty much only good for use in this project
ret = string.gsub(ret, ",}\n,}\n,", "}}")
return ret
end
-- escape special characters in a string
function escape(str)
local ret=str
ret = ret:gsub("\\","\\\\")
ret = ret:gsub("\"","\\\"")
ret = ret:gsub("\n","\\n")
ret = ret:gsub("\r","\\r")
return "\"" .. ret .. "\""
end
function unescape(str)
local ret=str
if ret:sub(1,1)=="\"" then ret=ret:sub(2) end
if ret:sub(-1,-1)=="\"" then ret=ret:sub(1,-2) end
ret = ret:gsub("\\\\","\\")
ret = ret:gsub("\\\"","\"")
ret = ret:gsub("\\n","\n")
ret = ret:gsub("\\r","\r")
return ret
end
-- loadTable
-- loads a table with the values from data
-- child tables will be loaded in to new tables
-- Example:
-- data="{letters:{1:"a",2:"b"},numbers(1:1,2:2}}"
-- t={}
-- loadTable(t,data)
--
-- results:
-- t.letters={"a","b"}
-- t.numbers={1,2}
-- Arguments:
-- myTable: the table or object to populate
-- data: string data encoded with getString
-- Returns: nothing
function loadTable(myTable,data,startPos)
if startPos == nil then startPos=1 end
local len=data:len()
local pos=0
local key=""
local val=""
while pos ~= nil and startPos ~= nil and startPos <= len do
while data:sub(startPos,startPos) <= " " and startPos <= len do
startPos = startPos + 1
end
if data:sub(startPos,startPos) == "}" then
return startPos+1
end
if startPos > len then
return startPos
end
pos=data:find("=",startPos,true)
if pos ~= nil then
key=data:sub(startPos,pos-1)
local n=tonumber(key)
if n ~= nil then key = n end
startPos=pos+1
pos=data:find("\n",startPos,true)
if pos == nil then pos=len+1 end
val=data:sub(startPos,pos-1)
startPos=pos+1
if val == "class {" then
local child={}
myTable["class:" .. key]=child
startPos = loadTable(child,data,startPos)
elseif val == "{" then
local child={}
myTable[key]=child
startPos = loadTable(child,data,startPos)
elseif val:sub(1,1) == "\"" then
myTable[key]=unescape(val)
elseif val == "true" then
myTable[key]=true
elseif val == "false" then
myTable[key]=false
elseif val:sub(1,8)=="userdata" then
myTable["userdata:key"]=val:sub(10)
else
myTable[key]=tonumber(val)
end
end
end
return startPos
end
--# Stick
Stick = class()
function Stick:init()
self.direction = 0
self.dist = 0
self.active = false
self.origin = vec2(150, 150)
self.center = self.origin
self.pos = self.origin
self.stick_bg = readImage("Space Art:Eclipse")
self.stick = readImage(asset.builtin.UI.Blue_Box_Tick)
end
function Stick:draw()
scaler = .9
sprite(self.stick_bg, self.center.x, self.center.y)
sprite(self.stick, self.pos.x, self.pos.y, self.stick.width * scaler, self.stick.height * scaler)
end
function Stick:touched(touch)
if touch.state == BEGAN then
self.center = vec2(touch.x, touch.y)
self.active = true
end
self.pos = vec2(touch.x, touch.y)
self.direction = math.atan2(self.pos.y - self.center.y, self.pos.x - self.center.x)
self.dist = math.min(2, self.pos:dist(self.center)/32)
if touch.state == ENDED then
self.center = self.origin
self.pos = self.center
self.active = false
end
end
--# WorldData
savedMap =
{{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,}
,{ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 5,}
,{ 5, 0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 0,-1,-1,-1, 0,-1,-1,-1,-1,-1, 0, 0, 0,-1,-1, 5,}
,{ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0,-1,-1,-1, 0,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,}
,{ 5,-1,-1, 0,-1, 0,-1,-1,-1,-1,-1,-1, 0,-1,-1,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0, 5,}
,{ 5, 0,-1, 0,-1, 0,-1, 0, 0, 0, 0,-1, 0,-1,-1,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0, 5,}
,{ 5, 0,-1, 0,-1, 0,-1, 0,-1,-1,-1,-1, 0,-1,-1,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0, 5,}
,{ 5, 0,-1, 0,-1, 0,-1, 0,-1,-1,-1, 0, 0,-1,-1,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0,-1, 0, 5,}
,{ 5, 0,-1, 0,-1, 0, 0, 0, 0, 0,-1, 0, 0,-1,-1,-1, 0,-1, 0,-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= class()
function YouWinBanner:init(x)
-- you can accept and set parameters here
self.fontColor = color(49, 255, 0, 255)
self.textPosition = vec2(WIDTH/2,HEIGHT/2)
end
function YouWinBanner:draw()
pushStyle()
fill(self.fontColor)
textAlign(CENTER)
fontSize(100)
font("Optima-ExtraBlack")
text("YOU WIN!!", self.textPosition.x, self.textPosition.y)
popStyle()
self:randomizeColor()
end
function YouWinBanner:randomizeColor(touch)
local randR, randG, randB = math.random(256) - 1, math.random(256) - 1, math.random(256) - 1
self.fontColor = color(randR, randG, randB, 255)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment