Floor = class()
function Floor:init()
-- params
self.dialog = Dialog(100, 400, "Go down a level?")
self.dialog.hasCancel = true
self.showDialog = false
self.tiles = {}
self.roomCoords = {}
self.ox = 0
self.oy = 0
self.stairsX = 0
self.stairsY = 0
for y = 1, 50 do
self.tiles[y] = {}
for x = 1, 50 do
self.tiles[y][x] = "0"
end
end
-- add some rooms
numRooms = math.random(10) + 5
for i= 1, numRooms do
self:addRoom(i)
end
for i= 1, numRooms do
self:carveTunnel(i)
end
found = 0
while found < 2 do
x = math.random(50)
y = math.random(50)
if self.tiles[y][x] == "-" and self.tiles[y - 1][x] == "-"then
self.tiles[y][x] = "x"
found = found + 1
end
end
found = false
while not found do
x = math.random(50)
y = math.random(50)
if self.tiles[y][x] == "-" and self.tiles[y - 1][x] == "-" and
self.tiles[y + 1][x + 1] == "-" and self.tiles[y - 1][x - 1] == "-" then
self.tiles[y][x] = "X"
found = true
end
end
found = false
while not found do
x = math.random(50)
y = math.random(50)
if self.tiles[y][x] == "-" and self.tiles[y - 1][x] == "-" and
self.tiles[y + 1][x + 1] == "-" and self.tiles[y - 1][x - 1] == "-" then
self.tiles[y][x] = "c"
found = true
end
end
monsters = nil
monsters = {}
for i = 1, 1 + floorNum do
m = Monster(floorNum, self)
table.insert(monsters, m)
end
end
function Floor:addRoom(n)
-- attempt to create a random room
tries = 0
while tries < 50 do
w = math.random(6) + 1
h = math.random(6) + 1
x = math.random(49 - w)
y = math.random(49 - h)
if self.tiles[y][x] == "0" and self.tiles[y][x + w] == "0" and
self.tiles[y + h][x] == "0" and self.tiles[y + h][x + w] == "0" and
self.tiles[y + (h // 2) ][x + (w // 2)] == "0"
then
for rx = x, x + w do
for ry = y, y + h do
if rx > 1 and rx < 50 and ry > 1 and ry < 50 then
self.tiles[ry][rx] = "-"
end
end
end
self.roomCoords[n] = {x + math.random(w), y + math.random(h)}
return
end
tries = tries + 1
end
end
function Floor:carveTunnel(n)
for i, r in ipairs(self.roomCoords) do
r2 = nil
if i < #self.roomCoords then
r2 = self.roomCoords[i + 1]
else
r2 = self.roomCoords[1]
end
sx = r[1]
sy = r[2]
ex = r2[1]
ey = r2[2]
-- draw a horizontal tunnel
if ex < sx then
for x = ex, sx do
self.tiles[sy][x] = "-"
end
else
for x = sx, ex do
self.tiles[sy][x] = "-"
end
end
-- draw a vertical tunnel
if ey < sy then
for y = ey, sy do
self.tiles[y][ex] = "-"
end
else
for y = sy, ey do
self.tiles[y][ex] = "-"
end
end
end
end
function Floor:draw(cx, cy)
-- move to keep character more centered
cols = WIDTH // 48
rows = (HEIGHT - 200) // 48
ox = cx - cols // 2
oy = cy - rows // 2
if ox < 0 then ox = 0 elseif ox > 50 - cols then ox = 50 - cols end
if oy < 0 then oy = 0 elseif oy > 51 - rows then oy = 51 - rows end
self.ox = ox
self.oy = oy
-- draw the section of floor around the cx, cy localtion
my = 50
mx = 50
dx = 0
dy = 200
while self.ox + cols > 50 do
self.ox = self.ox - 1
end
for y= my, self.oy + 1, -1 do
for x = 1 + self.ox, mx do
noTint()
dist = math.sqrt((x - cx) * (x- cx) + (y - cy) * (y - cy))
t = 255 - dist * 40
tint(t,t,t,255)
c = self.tiles[y][x]
local bx, by = screenMap(x, y)
if c == "-" then
sprite("Blocks:Greysand",bx,by,48,80)
end
if c == "x" then
sprite("Blocks:Greysand",bx,by,48,80)
sprite("Planet Cute:Key",bx,by + 16,48,48)
end
if c == "X" then
sprite("Blocks:Greysand",bx,by,48,80)
sprite("Planet Cute:Door Tall Closed",bx,by + 16,48,80)
end
if c == "S" then
tint(0, 0, 0, 255)
sprite("Blocks:Greysand",bx,by,48,80)
noTint()
tint(127, 127, 127, 255)
sprite("Cargo Bot:Crate Blue 1", bx,by + 16,48,64)
noTint()
end
if c == "0" then
sprite("Planet Cute:Stone Block",bx,by + 32,48,96)
end
if c == "W" then
sprite("Planet Cute:Wall Block",bx,by + 32,48,96)
end
if c == "c" then
sprite("Planet Cute:Chest Closed",bx,by + 32,48,96)
end
if c == "C" then
sprite("Planet Cute:Chest Open",bx,by + 32,48,96)
end
if c == "d" then
sprite("Blocks:Gravel Dirt",bx,by,48,80)
end
end
end
noTint()
for i, m in ipairs(monsters) do
m:draw(self.ox, self.oy)
end
for i, t in ipairs(treasures) do
t:draw(self.ox, self.oy)
end
bx, by = screenMap(cx, cy)
if health < 1 then
tint(127, 127, 127, 255)
sprite("Cargo Bot:Condition Yellow", bx, by + 32, 48, 48)
fill(52, 52, 52, 255)
textMode(CORNER)
text("RIP", bx - 15, by + 24)
floor.tiles[cy][cx] = "d"
else
tint(hitTint)
sprite("Planet Cute:Character Boy",bx,by + 26,48,64)
if weapon > 1 then
sprite("Small World:Sword", bx + 16, by + 28, 9, 32)
end
end
noTint()
hitTint = color(255, 255, 255, 255)
if self.showDialog then self.dialog:draw() end
end
function Floor:check(x, y)
c = self.tiles[y][x]
if c == "-" then return true end
if c == "x" then
sound("Game Sounds One:Bell 2")
keys = keys + 1
self.tiles[y][x] = "-"
return true
end
if c == "X" and keys > 0 then
sound("A Hero's Quest:Door Close")
keys = keys - 1
self.tiles[y][x] = "S"
end
if c == "S" then
self.showDialog =true
end
if c == "c" and keys > 0 then
sound("A Hero's Quest:Door Open")
keys = keys - 1
self.tiles[y][x] = "C"
t = Treasure(x, y)
table.insert(treasures, t)
-- reward routine
end
if c == "C" then
--return true
for i, t in ipairs(treasures) do
if t.x == x and t.y == y then
if t.t == 1 then
-- coins
gold = gold + math.random(59 * floorNum)
sound("A Hero's Quest:Bottle Break 1")
fm = FloatingMessage("+" .. gold .. " gold",
screenMap(t.x, t.y))
saveLocalData("GOLD", gold)
fm.y = fm.y + 25
fm.tint = color(224, 192, 132, 255)
table.insert(messages, fm)
end
if t.t == 2 then
-- weapon
weapon = 1 + math.random(floorNum)
sound("A Hero's Quest:Sword Hit 4")
fm = FloatingMessage("+" .. weapon .. " sword",
screenMap(t.x, t.y))
fm.y = fm.y + 25
fm.x = fm.x - 25
fm.tint = color(131, 203, 224, 255)
table.insert(messages, fm)
end
if t.t == 3 then
-- heart
health = health + 1
sound("A Hero's Quest:Defensive Cast 1")
fm = FloatingMessage("+ 1 health",screenMap(t.x, t.y))
fm.y = fm.y + 25
fm.tint = color(223, 170, 186, 255)
table.insert(messages, fm)
end
table.remove(treasures, i)
end
end
end
return false
end
function Floor:touched(touch)
local i
i = self.dialog:touched(touch)
if i == 1 then
return true
end
if i == 2 then
self.showDialog = false
end
return false
end