Skip to content

Instantly share code, notes, and snippets.

@devilstower
Created January 15, 2013 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilstower/4538835 to your computer and use it in GitHub Desktop.
Save devilstower/4538835 to your computer and use it in GitHub Desktop.
Endless early draft
--# Main
-- Use this function to perform your initial setup
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
textMode(CORNER)
textAlign(LEFT)
function setup()
watch("left")
watch("right")
watch("top")
watch("bottom")
posX, posY = 6, 6
map = {}
lightMap = {}
monster = {}
treasures = {}
floor = 0
generateMap()
oldX = 0
lbtn = Frame(20,500,70,550)
rbtn = Frame(120,500,170,550)
ubtn = Frame(70,550,120,600)
dbtn = Frame(70,450,120,500)
stats = Frame(0, 15, WIDTH, 285)
confront = Frame(0, 173, WIDTH, 385)
att = 10
def = 10
xp = 222
lvl = 1
hp = 10
end
function generateMap()
monsters = 0
floor = floor + 1
for y = 1, 52 do
map[y] = {}
for x = 1,52 do
map[y][x] = 2
end
end
for y = 1, 52 do
lightMap[y] = {}
for x = 1,52 do
lightMap[y][x] = 0
end
end
-- 6 by 6 array of rooms
--
for ry =1,6 do
for rx=1,6 do
left = rx * 8 - 6 + math.random(2)
right = left + math.random(2) + 3
bottom = ry * 8 - 6 + math.random(2)
top = bottom + math.random(2) + 3
-- draw a room
for y = bottom,top do
for x = left,right do
map[y][x] = 0
end
end
-- every room gets two openings
halls = 0
while halls < 2 do
side = math.random(4)
width = math.random(2)
if side == 1 and ry < 6 then
for x = left + 3, left + 3 + width do
map[top + 1][x] = 0
map[top + 2][x] = 0
map[top + 3][x] = 0
map[top + 4][x] = 0
end
halls = halls + 1
end
if side == 2 and rx < 6 then
for y = bottom + 3, bottom + 3 + width do
map[y][right + 1] = 0
map[y][right + 2] = 0
map[y][right + 3] = 0
map[y][right + 4] = 0
end
halls = halls + 1
end
if side == 3 and ry > 1 then
for x = left + 3, left + 3 + width do
map[bottom - 1][x] = 0
map[bottom - 2][x] = 0
map[bottom - 3][x] = 0
map[bottom - 4][x] = 0
end
halls = halls + 1
end
if side == 4 and rx > 1 then
for y = bottom + 3, bottom + 3 + width do
map[y][left - 1] = 0
map[y][left - 2] = 0
map[y][left - 3] = 0
map[y][left - 4] = 0
end
halls = halls + 1
end
end
if math.random(4) > 2 then
-- plant a monster
monsters = monsters + 1
monster[monsters] = Monster(left + 2, bottom + 2, floor)
end
end
end
map[5][5] = 5
treasures = {}
end
function drawStats()
pushStyle()
rect(0,HEIGHT-650,WIDTH,650)
fill(127, 127, 127, 255)
spriteMode(CORNER)
textMode(CORNER)
textAlign(LEFT)
tint(213, 213, 213, 255)
sprite("Planet Cute:Wood Block", 0, stats.bottom + 70, WIDTH, 130)
sprite("Planet Cute:Wood Block", 0, stats.bottom, WIDTH, 130)
sprite("Planet Cute:Wood Block", 0, stats.bottom-70, WIDTH, 130)
fill(199, 194, 193, 255)
fontSize(18)
text("Floor", 10, stats.bottom+100)
text("Level", 110, stats.bottom+100)
s = "("..xp.." of "..(lvl * 1024)..")"
text("Experience "..s,225,stats.bottom+100)
text("Health", 10, stats.bottom + 30)
text("Attack", 225, stats.bottom + 30)
text("Defense", 445, stats.bottom + 30)
fill(0, 0, 0, 255)
fontSize(28)
font("ArialRoundedMTBold")
fill(150, 150, 150, 255)
text(floor,24,stats.bottom+125)
text(lvl,124,stats.bottom+125)
fill(0, 0, 0, 255)
text(floor,22,stats.bottom+127)
text(lvl,122,stats.bottom+127)
fill(0, 0, 0, 255)
for i = hp,1,-1 do
tint(0, 0, 0, 255)
sprite("Planet Cute:Heart", i * 18 - 14, stats.bottom + 50, 24)
tint(255, 255, 255, 255)
sprite("Planet Cute:Heart", i * 18 - 16, stats.bottom + 52, 24)
end
for i = att,1,-1 do
tint(0, 0, 0, 255)
sprite("Small World:Sword", i * 18 + 213, stats.bottom + 54, 8)
tint(255, 255, 255, 255)
sprite("Small World:Sword", i * 18 + 211, stats.bottom + 56, 8)
end
for i = def,1,-1 do
tint(0, 0, 0, 255)
sprite("Planet Cute:Rock", i * 18 + 428, stats.bottom + 54, 24)
tint(255, 255, 255, 255)
sprite("Planet Cute:Rock", i * 18 + 426, stats.bottom + 56, 24)
end
for i = 20,1,-1 do
tint(0, 0, 0, 255)
sprite("Cargo Bot:Star Empty", i * 27 + 195,
stats.bottom + 120, 30)
end
x = math.floor(xp / (lvl * 1024) * 20)
for i = x,1,-1 do
tint(255, 255, 255, 255)
sprite("Cargo Bot:Star Filled", i * 27 + 195,
stats.bottom + 120, 30)
end
popStyle()
end
function drawConfront()
pushStyle()
fill(119, 110, 110, 255)
confront:draw()
tint(255, 255, 255, 49)
textAlign(CENTER)
textMode(CENTER)
clip(confront.left, confront.bottom, WIDTH, confront:height())
sprite("Planet Cute:Dirt Block", confront.left,
confront.bottom-90, confront:width()+25, confront:height()*2)
for i = 1,monsters do
confronted = 0
if monster[i]:near(posX, posY) then
fill(127, 127, 127, 101)
spriteMode(CORNER)
sprite("Planet Cute:Stone Block",
10 + 200 * confronted, confront.bottom + 10,
200 + 200 * confronted, confront:height() )
spriteMode(CENTER)
rect(10 + 200 * confronted, confront.bottom + 10,
200 + 200 * confronted, confront:height() - 20)
fill(0, 0, 0, 255)
text(monster[i].name, 110 + 200 * confronted,
confront.bottom + 30)
tint(monster[i].tint)
sprite(monster[i].img, 110 + 200 * confronted,
confront.bottom + 110, 75)
tint(255, 255, 255, 255)
sprite("Small World:Sword", confront.left + 40,
confront.top-30, 8)
sprite("Planet Cute:Heart", confront.left + 118,
confront.top-30, 24)
sprite("Planet Cute:Rock", confront.left + 185,
confront.top-24, 24)
text(monster[i].attack, 25 + 200 * confronted,
confront.top - 30)
text(monster[i].hp, 95 + 200 * confronted,
confront.top - 30)
text(monster[i].defense, 165 + 200 * confronted,
confront.top - 30)
end
end
noClip()
popStyle()
end
function drawMap()
sprite("SpaceCute:Background", 0, 0, WIDTH)
minX = posX - 8
if minX < 1 then minX = 1 end
if minX > table.getn(map[1]) - 16 then
minX = table.getn(map[1]) - 16
end
maxX = minX + 16
minY = posY - 8
if minY < 1 then minY = 1 end
if minY > table.getn(map) - 15 then
minY = table.getn(map) - 15
end
maxY = minY + 15
-- if maxX > string.len(map[1]) then maxX = string.len(map[1]) end
rectMode(CORNER)
spriteMode(CORNER)
for y = minY, maxY do
for x = minX, maxX do
noTint()
tile = map[y][x]
tileRight = 0
tileWest = 0
if y > 1 then
tileNorth = map[y-1][x]
end
if x > 1 then
tileWest = map[y][x-1]
end
gx = x * 50 - 50 - minX * 50
gy = HEIGHT - (y - minY) * 40 - 50
tint(lightMap[y][x] + 255 -
math.sqrt((math.abs(posX -x) * math.abs(posX -x))+
(math.abs(posY - y) * math.abs(posY - y)))*48)
if tile == 0 then
w,h = spriteSize("Planet Cute:Stone Block")
sprite("Planet Cute:Stone Block", gx, gy, 50)
if tileNorth == 2 then
sprite("Planet Cute:Shadow North", gx, gy, 50)
end
if tileWest == 2 then
sprite("Planet Cute:Shadow West", gx, gy, 50)
end
end
if tile == 2 then
--tint(127, 127, 127, 255)
w,h = spriteSize("Planet Cute:Wall Block Tall")
sprite("Planet Cute:Wall Block Tall", gx, gy+12, 50)
end
if tile == 4 then
w,h = spriteSize("Planet Cute:Door Tall Closed")
sprite("Planet Cute:Door Tall Closed", gx, gy+15, 50)
end
if tile == 5 then
w,h = spriteSize("Planet Cute:Roof South")
sprite("Planet Cute:Roof South", gx, gy+15, 50)
end
if posY == y and posX == x then
noTint()
gx = posX * 50 - 50 - minX * 50
gy = HEIGHT - (posY - minY) * 40 - 50
sprite("Planet Cute:Character Boy", gx, gy+25, 50)
sprite("Small World:Sword", gx+5, gy+ 45)
end
for i = 1, monsters do
if monster[i].x == x and monster[i].y == y then
monster[i]:draw(minX, minY)
end
end
end
end
noFill()
stroke(22, 34, 34, 255)
rect(0,HEIGHT-650,WIDTH,650)
tint(182, 168, 168, 93)
-- directional arrows
sprite("Cargo Bot:Command Left", 20, 500)
sprite("Cargo Bot:Command Right", 120,500)
sprite("Cargo Bot:Command Grab", 70,450)
translate(120,610)
rotate(180)
sprite("Cargo Bot:Command Grab", 0,0)
resetMatrix()
end
function checkMove(x, y)
if map[y][x] == 0 then
posX = x
posY = y
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
drawMap()
drawStats()
drawConfront()
-- Do your drawing here
if CurrentTouch.state == ENDED and CurrentTouch.x ~= oldX then
moved = false
if lbtn:touched(CurrentTouch) then
checkMove(posX - 1, posY)
moved = true
end
if rbtn:touched(CurrentTouch) then
checkMove(posX + 1, posY)
moved = true
end
if ubtn:touched(CurrentTouch) then
checkMove(posX, posY - 1)
moved = true
end
if dbtn:touched(CurrentTouch) then
checkMove(posX, posY + 1)
moved = true
end
if moved then
-- moved monsters
-- check for confrontations
confronted = 0
end
oldX = CurrentTouch.x
end
end
--# Frame
Frame = class()
-- Frame
-- ver. 1.0
-- a simple rectangle for holding controls.
-- ====================
function Frame:init(left, bottom, right, top)
self.left = left
self.right = right
self.bottom = bottom
self.top = top
end
function Frame:inset(dx, dy)
self.left = self.left + dx
self.right = self.right - dx
self.bottom = self.bottom + dy
self.top = self.top - dy
end
function Frame:offset(dx, dy)
self.left = self.left + dx
self.right = self.right + dx
self.bottom = self.bottom + dy
self.top = self.top + dy
end
function Frame:draw()
pushStyle()
rectMode(CORNERS)
rect(self.left, self.bottom, self.right, self.top)
popStyle()
end
function Frame:roundRect(r)
pushStyle()
insetPos = vec2(self.left + r,self.bottom + r)
insetSize = vec2(self:width() - 2 * r,self:height() - 2 * r)
rectMode(CORNER)
rect(insetPos.x, insetPos.y, insetSize.x, insetSize.y)
if r > 0 then
smooth()
lineCapMode(ROUND)
strokeWidth(r * 2)
line(insetPos.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y)
line(insetPos.x, insetPos.y,
insetPos.x, insetPos.y + insetSize.y)
line(insetPos.x, insetPos.y + insetSize.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
line(insetPos.x + insetSize.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
end
popStyle()
end
function Frame:gloss(baseclr)
local i, t, r, g, b, y
pushStyle()
if baseclr == nil then baseclr = color(194, 194, 194, 255) end
fill(baseclr)
rectMode(CORNERS)
rect(self.left, self.bottom, self.right, self.top)
r = baseclr.r
g = baseclr.g
b = baseclr.b
for i = 1 , self:height() / 2 do
r = r - 1
g = g - 1
b = b - 1
stroke(r, g, b, 255)
y = (self.bottom + self.top) / 2
line(self.left, y + i, self.right, y + i)
line(self.left, y - i, self.right, y - i)
end
popStyle()
end
function Frame:shade(base, step)
pushStyle()
strokeWidth(1)
for y = self.bottom, self.top do
i = self.top - y
stroke(base - i * step, base - i * step, base - i * step, 255)
line(self.left, y, self.right, y)
end
popStyle()
end
function Frame:touched(touch)
if touch.x >= self.left and touch.x <= self.right then
if touch.y >= self.bottom and touch.y <= self.top then
return true
end
end
return false
end
function Frame:ptIn(x, y)
if x >= self.left and x <= self.right then
if y >= self.bottom and y <= self.top then
return true
end
end
return false
end
function Frame:width()
return self.right - self.left
end
function Frame:height()
return self.top - self.bottom
end
function Frame:midX()
return (self.left + self.right) / 2
end
function Frame:midY()
return (self.bottom + self.top) / 2
end
--# Monster
Monster = class()
function Monster:init(x, y, l)
-- you can accept and set parameters here
self.x = x
self.y = y
self.attack = 0
self.armor = 0
self.ranged = false
-- set up a beastie by level
if l == 1 then
i = math.floor(math.random(3))
if i == 1 then
self.name = "Whispering Spider"
self.attack = 1
self.defense = 1
self.hp = 10 + math.random(6)
self.img ="Tyrian Remastered:Evil Head"
self.size = 25
self.tint = color(255, 255, 255, 255)
end
if i == 2 then
self.name = "Glistening Cube"
self.attack = 2
self.defense = 2
self.hp = 10 + math.random(6)
self.img ="Tyrian Remastered:Energy Cube"
self.size = 25
self.tint = color(224, 232, 234, 255)
end
if i == 3 then
self.name = "Boring Beetle"
self.attack = 1
self.defense = 2
self.ranged = false
self.hp = 10 + math.random(6)
self.img ="Planet Cute:Enemy Bug"
self.size = 25
self.tint = color(219, 217, 219, 255)
end
end
end
function Monster:draw(minX, minY)
-- Codea does not automatically call this method
gx = self.x * 50 - 50 - minX * 50
gy = HEIGHT - (self.y - minY) * 40 - 50
sprite(self.img, gx, gy+25, self.size)
end
function Monster:near(x, y)
if math.abs(self.x - x) < 2 and math.abs(self.y - y) < 2 then
return true
end
return false
end
--# Treasure
Treasure = class()
function Treasure:init(x)
-- you can accept and set parameters here
self.x = x
end
function Treasure:draw()
-- Codea does not automatically call this method
end
function Treasure:touched(touch)
-- Codea does not automatically call this method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment