Skip to content

Instantly share code, notes, and snippets.

/Alien.lua

Created May 13, 2012
Embed
What would you like to do?
Ludum Dare #23 - Tiny World Travel - Codea - Lua Game
Alien = class()
-- Alien class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
ALIEN_DEAD = 0
ALIEN_LIVE = 1
function Alien:init()
self.position = vec2(WIDTH,math.random(-6,6)+HEIGHT/2)
self.state = ALIEN_LIVE
self.speed = math.random(1,level+2)
self.R = math.random(255)
self.G = math.random(255)
self.B = math.random(255)
end
function Alien:draw()
if self.state == ALIEN_DEAD then return end
-- check distance with the main tiny world house:
if self.position:dist(tiny_world.houses[tiny_world.destiny].position)<55 then
self.state = ALIEN_DEAD
tiny_world.frame = 0
tiny_world.state = WORLD_BREAK
return
end
self.position.x = self.position.x - self.speed
local d = math.sin((self.position.x-0.66)/33)
if self.position.y <= tiny_world.houses[tiny_world.destiny].position.y then
d = -d
end
self.position.y = self.position.y + d
pushMatrix()
pushStyle()
-- Set up basic graphical style
lineCapMode(ROUND)
strokeWidth(8)
stroke(self.R,self.G,self.B, 255)
smooth()
noFill()
-- Transform to pos
translate(self.position.x, self.position.y)
rotate(90)
-- Draw our triangle invader
-- 60 pixels high, 40 pixels wide
line( 0, 30, 20, -30 )
line( 0, 30,-20, -30 )
line( -20, -30, 20, -30 )
popMatrix()
popStyle()
end
function Alien:touched(touch)
if self.state ~= ALIEN_DEAD and vec2(touch.x,touch.y):dist(self.position)<20 then
self.state = ALIEN_DEAD
sound(SOUND_EXPLODE, 14589)
return true
end
return false
end
Asteroid = class()
-- Asteroid class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
ASTEROID_DEAD = 0
ASTEROID_LIVE = 1
function Asteroid:init(avoidpos)
self.state = ASTEROID_LIVE
self.R = math.random(255)
self.G = math.random(255)
self.B = math.random(255)
self.img = AsteroidImg()
self.angle= 1
self.position = vec2(math.random(WIDTH),math.random(HEIGHT)) - avoidpos
self.angle = math.random(360)
local r = math.random(1,7)
if r == 1 then
self.speed = vec2(1,1)
elseif r == 2 then
self.speed = vec2(1,0)
elseif r ==3 then
self.speed = vec2(0,1)
elseif r == 5 then
self.speed = vec2(-1,-1)
elseif r == 6 then
self.speed = vec2(-1,0)
else
self.speed = vec2(0,-1)
end
self.speed = self.speed * level
end
function Asteroid:update()
self.angle = self.angle + 1
if self.angle == 360 then self.angle = 0 end
self.position = self.position + self.speed
if self.position.x > WIDTH then
self.position.x = 0
elseif self.position.x < 0 then
self.position.x = WIDTH
end
if self.position.y > HEIGHT then
self.position.y = 0
elseif self.position.y < 0 then
self.position.y = HEIGHT
end
end
function Asteroid:draw()
if self.state == ASTEROID_DEAD then return end
self:update()
-- check distance with the main tiny world house:
if self.position:dist(tiny_world.houses[tiny_world.destiny].position)<22 then
self.state = ASTEROID_DEAD
tiny_world.frame = 0
tiny_world.state = WORLD_BREAK
return
end
-- draw
pushMatrix()
-- Transform to pos
translate(self.position.x, self.position.y)
self.angle = self.angle + 0.4
rotate(self.angle)
tint(self.R,self.G,self.B,math.random(130,255))
sprite(self.img)
noTint()
popMatrix()
end
function Asteroid:touched(touch)
if self.state ~= ASTEROID_DEAD and vec2(touch.x,touch.y):dist(self.position)<30 then
self.state = ASTEROID_DEAD
sound(SOUND_EXPLODE, 14589)
return true
end
return false
end
function AsteroidImg()
local img = image(32, 32)
img:set(1,13,255,255,255,255)
img:set(1,14,255,255,255,255)
img:set(1,15,255,255,255,255)
img:set(1,16,255,255,255,255)
img:set(1,17,255,255,255,255)
img:set(2,10,255,255,255,255)
img:set(2,11,255,255,255,255)
img:set(2,12,255,255,255,255)
img:set(2,13,255,255,255,255)
img:set(2,14,255,255,255,255)
img:set(2,15,255,255,255,255)
img:set(2,16,255,255,255,255)
img:set(2,17,255,255,255,255)
img:set(2,18,255,255,255,255)
img:set(2,19,255,255,255,255)
img:set(2,20,255,255,255,255)
img:set(3,9,255,255,255,255)
img:set(3,10,255,255,255,255)
img:set(3,11,255,255,255,255)
img:set(3,12,255,255,255,255)
img:set(3,13,255,255,255,255)
img:set(3,14,255,255,255,255)
img:set(3,15,255,255,255,255)
img:set(3,16,255,255,255,255)
img:set(3,17,127,127,127,255)
img:set(3,18,127,127,127,255)
img:set(3,19,255,255,255,255)
img:set(3,20,255,255,255,255)
img:set(3,21,255,255,255,255)
img:set(3,22,255,255,255,255)
img:set(4,8,255,255,255,255)
img:set(4,9,255,255,255,255)
img:set(4,10,255,255,255,255)
img:set(4,11,255,255,255,255)
img:set(4,12,255,255,255,255)
img:set(4,13,255,255,255,255)
img:set(4,14,255,255,255,255)
img:set(4,15,255,255,255,255)
img:set(4,16,255,255,255,255)
img:set(4,17,127,127,127,255)
img:set(4,18,255,255,255,255)
img:set(4,19,127,127,127,255)
img:set(4,20,255,255,255,255)
img:set(4,21,255,255,255,255)
img:set(4,22,255,255,255,255)
img:set(4,23,255,255,255,255)
img:set(4,24,255,255,255,255)
img:set(5,7,255,255,255,255)
img:set(5,8,255,255,255,255)
img:set(5,9,200,120,98,255)
img:set(5,10,255,128,0,255)
img:set(5,11,127,127,127,255)
img:set(5,12,127,127,127,255)
img:set(5,13,127,127,127,255)
img:set(5,14,255,255,255,255)
img:set(5,15,255,255,255,255)
img:set(5,16,127,127,127,255)
img:set(5,17,127,127,127,255)
img:set(5,18,255,255,255,255)
img:set(5,19,255,255,255,255)
img:set(5,20,127,127,127,255)
img:set(5,21,127,127,127,255)
img:set(5,22,255,255,255,255)
img:set(5,23,255,255,255,255)
img:set(5,24,255,255,255,255)
img:set(5,25,255,255,255,255)
img:set(6,6,255,255,255,255)
img:set(6,7,255,255,255,255)
img:set(6,8,255,128,0,255)
img:set(6,9,200,120,98,255)
img:set(6,10,255,128,0,255)
img:set(6,11,255,255,255,255)
img:set(6,12,255,255,255,255)
img:set(6,13,255,255,255,255)
img:set(6,14,138,128,0,255)
img:set(6,15,127,127,127,255)
img:set(6,16,255,255,255,255)
img:set(6,17,127,127,127,255)
img:set(6,18,255,255,255,255)
img:set(6,19,255,255,255,255)
img:set(6,20,127,127,127,255)
img:set(6,21,127,127,127,255)
img:set(6,22,255,255,255,255)
img:set(6,23,255,255,255,255)
img:set(6,24,255,255,255,255)
img:set(6,25,255,255,255,255)
img:set(6,26,255,255,255,255)
img:set(7,6,255,255,255,255)
img:set(7,7,255,255,255,255)
img:set(7,8,200,120,98,255)
img:set(7,9,127,127,127,255)
img:set(7,10,255,255,255,255)
img:set(7,11,255,128,0,255)
img:set(7,12,127,127,127,255)
img:set(7,13,255,255,255,255)
img:set(7,14,255,255,255,255)
img:set(7,15,127,127,127,255)
img:set(7,16,127,127,127,255)
img:set(7,17,127,127,127,255)
img:set(7,18,255,255,255,255)
img:set(7,19,255,255,255,255)
img:set(7,20,127,127,127,255)
img:set(7,21,127,127,127,255)
img:set(7,22,255,255,255,255)
img:set(7,23,255,255,255,255)
img:set(7,24,127,127,127,255)
img:set(7,25,255,255,255,255)
img:set(7,26,255,255,255,255)
img:set(7,27,255,255,255,255)
img:set(8,6,255,255,255,255)
img:set(8,7,255,128,0,255)
img:set(8,8,200,120,98,255)
img:set(8,9,255,255,255,255)
img:set(8,10,255,255,255,255)
img:set(8,11,255,128,0,255)
img:set(8,12,255,255,255,255)
img:set(8,13,255,255,255,255)
img:set(8,14,138,128,0,255)
img:set(8,15,127,127,127,255)
img:set(8,16,127,127,127,255)
img:set(8,17,255,255,255,255)
img:set(8,18,127,127,127,255)
img:set(8,19,255,255,255,255)
img:set(8,20,255,255,255,255)
img:set(8,21,127,127,127,255)
img:set(8,22,127,127,127,255)
img:set(8,23,255,255,255,255)
img:set(8,24,255,255,255,255)
img:set(8,25,255,255,255,255)
img:set(8,26,255,255,255,255)
img:set(8,27,255,255,255,255)
img:set(8,28,255,255,255,255)
img:set(9,5,255,255,255,255)
img:set(9,6,255,255,255,255)
img:set(9,7,255,255,255,255)
img:set(9,8,127,127,127,255)
img:set(9,9,255,128,0,255)
img:set(9,10,255,255,255,255)
img:set(9,11,127,127,127,255)
img:set(9,12,255,255,255,255)
img:set(9,13,138,128,0,255)
img:set(9,14,255,255,255,255)
img:set(9,15,255,255,255,255)
img:set(9,16,127,127,127,255)
img:set(9,17,255,255,255,255)
img:set(9,18,255,255,255,255)
img:set(9,19,255,255,255,255)
img:set(9,20,255,255,255,255)
img:set(9,21,255,255,255,255)
img:set(9,22,127,127,127,255)
img:set(9,23,255,255,255,255)
img:set(9,24,255,255,255,255)
img:set(9,25,255,255,255,255)
img:set(9,26,255,255,255,255)
img:set(9,27,255,255,255,255)
img:set(9,28,255,255,255,255)
img:set(10,4,255,255,255,255)
img:set(10,5,255,255,255,255)
img:set(10,6,200,120,98,255)
img:set(10,7,200,120,98,255)
img:set(10,8,255,255,255,255)
img:set(10,9,255,128,0,255)
img:set(10,10,255,255,255,255)
img:set(10,11,255,255,255,255)
img:set(10,12,255,255,255,255)
img:set(10,13,255,255,255,255)
img:set(10,14,255,255,255,255)
img:set(10,15,255,255,255,255)
img:set(10,16,255,255,255,255)
img:set(10,17,255,255,255,255)
img:set(10,18,255,255,255,255)
img:set(10,19,127,127,127,255)
img:set(10,20,255,255,255,255)
img:set(10,21,255,255,255,255)
img:set(10,22,255,255,255,255)
img:set(10,23,127,127,127,255)
img:set(10,24,255,255,255,255)
img:set(10,25,127,127,127,255)
img:set(10,26,255,255,255,255)
img:set(10,27,255,255,255,255)
img:set(10,28,255,255,255,255)
img:set(10,29,255,255,255,255)
img:set(11,3,255,255,255,255)
img:set(11,4,255,255,255,255)
img:set(11,5,200,120,98,255)
img:set(11,6,200,120,98,255)
img:set(11,7,255,255,255,255)
img:set(11,8,255,128,0,255)
img:set(11,9,255,255,255,255)
img:set(11,10,255,255,255,255)
img:set(11,11,255,255,255,255)
img:set(11,12,138,128,0,255)
img:set(11,13,255,255,255,255)
img:set(11,14,255,255,255,255)
img:set(11,15,255,255,255,255)
img:set(11,16,255,255,255,255)
img:set(11,17,255,255,255,255)
img:set(11,18,127,127,127,255)
img:set(11,19,255,255,255,255)
img:set(11,20,127,127,127,255)
img:set(11,21,255,255,255,255)
img:set(11,22,255,255,255,255)
img:set(11,23,127,127,127,255)
img:set(11,24,127,127,127,255)
img:set(11,25,255,255,255,255)
img:set(11,26,255,255,255,255)
img:set(11,27,255,255,255,255)
img:set(11,28,255,255,255,255)
img:set(11,29,255,255,255,255)
img:set(12,3,255,255,255,255)
img:set(12,4,200,120,98,255)
img:set(12,5,255,255,255,255)
img:set(12,6,127,127,127,255)
img:set(12,7,255,255,255,255)
img:set(12,8,255,128,0,255)
img:set(12,9,255,255,255,255)
img:set(12,10,127,127,127,255)
img:set(12,11,255,255,255,255)
img:set(12,12,255,255,255,255)
img:set(12,13,255,255,255,255)
img:set(12,14,255,255,255,255)
img:set(12,15,255,255,255,255)
img:set(12,16,255,255,255,255)
img:set(12,17,255,255,255,255)
img:set(12,18,255,255,255,255)
img:set(12,19,127,127,127,255)
img:set(12,20,127,127,127,255)
img:set(12,21,255,255,255,255)
img:set(12,22,255,255,255,255)
img:set(12,23,255,255,255,255)
img:set(12,24,127,127,127,255)
img:set(12,25,255,255,255,255)
img:set(12,26,255,255,255,255)
img:set(12,27,255,255,255,255)
img:set(12,28,255,255,255,255)
img:set(12,29,255,255,255,255)
img:set(12,30,255,255,255,255)
img:set(13,3,255,255,255,255)
img:set(13,4,200,120,98,255)
img:set(13,5,200,120,98,255)
img:set(13,6,200,120,98,255)
img:set(13,7,255,255,255,255)
img:set(13,8,255,128,0,255)
img:set(13,9,255,255,255,255)
img:set(13,10,255,255,255,255)
img:set(13,11,138,128,0,255)
img:set(13,12,255,255,255,255)
img:set(13,13,255,255,255,255)
img:set(13,14,255,255,255,255)
img:set(13,15,255,255,255,255)
img:set(13,16,255,255,255,255)
img:set(13,17,127,127,127,255)
img:set(13,18,127,127,127,255)
img:set(13,19,255,255,255,255)
img:set(13,20,255,255,255,255)
img:set(13,21,255,255,255,255)
img:set(13,22,255,255,255,255)
img:set(13,23,255,255,255,255)
img:set(13,24,127,127,127,255)
img:set(13,25,255,255,255,255)
img:set(13,26,255,255,255,255)
img:set(13,27,127,127,127,255)
img:set(13,28,255,255,255,255)
img:set(13,29,255,255,255,255)
img:set(13,30,255,255,255,255)
img:set(14,3,255,255,255,255)
img:set(14,4,200,120,98,255)
img:set(14,5,200,120,98,255)
img:set(14,6,255,255,255,255)
img:set(14,7,255,128,0,255)
img:set(14,8,255,128,0,255)
img:set(14,9,255,255,255,255)
img:set(14,10,138,128,0,255)
img:set(14,11,255,255,255,255)
img:set(14,12,255,255,255,255)
img:set(14,13,255,255,255,255)
img:set(14,14,255,255,255,255)
img:set(14,15,255,255,255,255)
img:set(14,16,127,127,127,255)
img:set(14,17,255,255,255,255)
img:set(14,18,255,255,255,255)
img:set(14,19,255,255,255,255)
img:set(14,20,255,255,255,255)
img:set(14,21,255,255,255,255)
img:set(14,22,127,127,127,255)
img:set(14,23,255,255,255,255)
img:set(14,24,255,255,255,255)
img:set(14,25,255,255,255,255)
img:set(14,26,255,255,255,255)
img:set(14,27,255,255,255,255)
img:set(14,28,255,255,255,255)
img:set(14,29,255,255,255,255)
img:set(14,30,255,255,255,255)
img:set(14,31,255,255,255,255)
img:set(15,3,138,128,0,255)
img:set(15,4,200,120,98,255)
img:set(15,5,200,120,98,255)
img:set(15,6,255,255,255,255)
img:set(15,7,255,255,255,255)
img:set(15,8,255,255,255,255)
img:set(15,9,255,255,255,255)
img:set(15,10,127,127,127,255)
img:set(15,11,255,255,255,255)
img:set(15,12,255,255,255,255)
img:set(15,13,255,255,255,255)
img:set(15,14,255,255,255,255)
img:set(15,15,255,255,255,255)
img:set(15,16,255,255,255,255)
img:set(15,17,255,255,255,255)
img:set(15,18,255,255,255,255)
img:set(15,19,255,255,255,255)
img:set(15,20,255,255,255,255)
img:set(15,21,255,255,255,255)
img:set(15,22,255,255,255,255)
img:set(15,23,127,127,127,255)
img:set(15,24,255,255,255,255)
img:set(15,25,127,127,127,255)
img:set(15,26,255,255,255,255)
img:set(15,27,127,127,127,255)
img:set(15,28,255,255,255,255)
img:set(15,29,255,255,255,255)
img:set(15,30,255,255,255,255)
img:set(15,31,255,255,255,255)
img:set(16,3,138,128,0,255)
img:set(16,4,200,120,98,255)
img:set(16,5,200,120,98,255)
img:set(16,6,255,255,255,255)
img:set(16,7,255,128,0,255)
img:set(16,8,138,128,0,255)
img:set(16,9,255,255,255,255)
img:set(16,10,255,255,255,255)
img:set(16,11,255,255,255,255)
img:set(16,12,255,255,255,255)
img:set(16,13,255,255,255,255)
img:set(16,14,255,255,255,255)
img:set(16,15,255,255,255,255)
img:set(16,16,255,255,255,255)
img:set(16,17,255,255,255,255)
img:set(16,18,255,255,255,255)
img:set(16,19,255,255,255,255)
img:set(16,20,255,255,255,255)
img:set(16,21,255,255,255,255)
img:set(16,22,255,255,255,255)
img:set(16,23,255,255,255,255)
img:set(16,24,255,255,255,255)
img:set(16,25,127,127,127,255)
img:set(16,26,255,255,255,255)
img:set(16,27,255,255,255,255)
img:set(16,28,255,255,255,255)
img:set(16,29,255,255,255,255)
img:set(16,30,255,255,255,255)
img:set(16,31,255,255,255,255)
img:set(17,3,138,128,0,255)
img:set(17,4,138,128,0,255)
img:set(17,5,200,120,98,255)
img:set(17,6,255,255,255,255)
img:set(17,7,255,255,255,255)
img:set(17,8,255,255,255,255)
img:set(17,9,255,255,255,255)
img:set(17,10,255,255,255,255)
img:set(17,11,255,255,255,255)
img:set(17,12,127,127,127,255)
img:set(17,13,255,255,255,255)
img:set(17,14,255,255,255,255)
img:set(17,15,255,255,255,255)
img:set(17,16,255,255,255,255)
img:set(17,17,255,255,255,255)
img:set(17,18,255,255,255,255)
img:set(17,19,255,255,255,255)
img:set(17,20,127,127,127,255)
img:set(17,21,127,127,127,255)
img:set(17,22,255,255,255,255)
img:set(17,23,127,127,127,255)
img:set(17,24,127,127,127,255)
img:set(17,25,127,127,127,255)
img:set(17,26,127,127,127,255)
img:set(17,27,127,127,127,255)
img:set(17,28,255,255,255,255)
img:set(17,29,255,255,255,255)
img:set(17,30,255,255,255,255)
img:set(17,31,255,255,255,255)
img:set(18,3,138,128,0,255)
img:set(18,4,138,128,0,255)
img:set(18,5,255,255,255,255)
img:set(18,6,127,127,127,255)
img:set(18,7,255,255,255,255)
img:set(18,8,255,255,255,255)
img:set(18,9,127,127,127,255)
img:set(18,10,255,255,255,255)
img:set(18,11,255,255,255,255)
img:set(18,12,255,255,255,255)
img:set(18,13,255,255,255,255)
img:set(18,14,255,255,255,255)
img:set(18,15,127,127,127,255)
img:set(18,16,255,255,255,255)
img:set(18,17,255,255,255,255)
img:set(18,18,255,255,255,255)
img:set(18,19,127,127,127,255)
img:set(18,20,255,255,255,255)
img:set(18,21,255,255,255,255)
img:set(18,22,255,255,255,255)
img:set(18,23,255,255,255,255)
img:set(18,24,255,255,255,255)
img:set(18,25,255,255,255,255)
img:set(18,26,127,127,127,255)
img:set(18,27,255,255,255,255)
img:set(18,28,255,255,255,255)
img:set(18,29,255,255,255,255)
img:set(18,30,255,255,255,255)
img:set(18,31,255,255,255,255)
img:set(19,3,255,255,255,255)
img:set(19,4,138,128,0,255)
img:set(19,5,255,255,255,255)
img:set(19,6,138,128,0,255)
img:set(19,7,255,255,255,255)
img:set(19,8,255,255,255,255)
img:set(19,9,255,255,255,255)
img:set(19,10,255,255,255,255)
img:set(19,11,255,255,255,255)
img:set(19,12,255,255,255,255)
img:set(19,13,127,127,127,255)
img:set(19,14,255,255,255,255)
img:set(19,15,255,255,255,255)
img:set(19,16,255,255,255,255)
img:set(19,17,255,255,255,255)
img:set(19,18,255,255,255,255)
img:set(19,19,127,127,127,255)
img:set(19,20,255,255,255,255)
img:set(19,21,255,255,255,255)
img:set(19,22,255,255,255,255)
img:set(19,23,255,255,255,255)
img:set(19,24,255,255,255,255)
img:set(19,25,127,127,127,255)
img:set(19,26,255,255,255,255)
img:set(19,27,255,255,255,255)
img:set(19,28,255,255,255,255)
img:set(19,29,255,255,255,255)
img:set(19,30,255,255,255,255)
img:set(20,3,255,255,255,255)
img:set(20,4,138,128,0,255)
img:set(20,5,255,255,255,255)
img:set(20,6,255,255,255,255)
img:set(20,7,255,255,255,255)
img:set(20,8,255,255,255,255)
img:set(20,9,255,255,255,255)
img:set(20,10,255,255,255,255)
img:set(20,11,255,255,255,255)
img:set(20,12,255,255,255,255)
img:set(20,13,255,255,255,255)
img:set(20,14,255,255,255,255)
img:set(20,15,255,255,255,255)
img:set(20,16,255,255,255,255)
img:set(20,17,255,255,255,255)
img:set(20,18,127,127,127,255)
img:set(20,19,127,127,127,255)
img:set(20,20,127,127,127,255)
img:set(20,21,127,127,127,255)
img:set(20,22,127,127,127,255)
img:set(20,23,127,127,127,255)
img:set(20,24,255,255,255,255)
img:set(20,25,127,127,127,255)
img:set(20,26,255,255,255,255)
img:set(20,27,255,255,255,255)
img:set(20,28,255,255,255,255)
img:set(20,29,255,255,255,255)
img:set(20,30,255,255,255,255)
img:set(21,4,255,255,255,255)
img:set(21,5,138,128,0,255)
img:set(21,6,255,255,255,255)
img:set(21,7,255,255,255,255)
img:set(21,8,138,128,0,255)
img:set(21,9,255,255,255,255)
img:set(21,10,127,127,127,255)
img:set(21,11,255,255,255,255)
img:set(21,12,255,255,255,255)
img:set(21,13,255,255,255,255)
img:set(21,14,255,255,255,255)
img:set(21,15,255,255,255,255)
img:set(21,16,255,255,255,255)
img:set(21,17,127,127,127,255)
img:set(21,18,127,127,127,255)
img:set(21,19,127,127,127,255)
img:set(21,20,127,127,127,255)
img:set(21,21,255,255,255,255)
img:set(21,22,127,127,127,255)
img:set(21,23,255,255,255,255)
img:set(21,24,127,127,127,255)
img:set(21,25,255,255,255,255)
img:set(21,26,255,255,255,255)
img:set(21,27,255,255,255,255)
img:set(21,28,255,255,255,255)
img:set(21,29,255,255,255,255)
img:set(22,5,255,255,255,255)
img:set(22,6,255,255,255,255)
img:set(22,7,255,255,255,255)
img:set(22,8,255,255,255,255)
img:set(22,9,255,255,255,255)
img:set(22,10,255,255,255,255)
img:set(22,11,255,255,255,255)
img:set(22,12,255,255,255,255)
img:set(22,13,255,255,255,255)
img:set(22,14,255,255,255,255)
img:set(22,15,255,255,255,255)
img:set(22,16,255,255,255,255)
img:set(22,17,127,127,127,255)
img:set(22,18,255,255,255,255)
img:set(22,19,255,255,255,255)
img:set(22,20,127,127,127,255)
img:set(22,21,127,127,127,255)
img:set(22,22,127,127,127,255)
img:set(22,23,127,127,127,255)
img:set(22,24,255,255,255,255)
img:set(22,25,255,255,255,255)
img:set(22,26,255,255,255,255)
img:set(22,27,255,255,255,255)
img:set(22,28,255,255,255,255)
img:set(22,29,255,255,255,255)
img:set(23,6,255,255,255,255)
img:set(23,7,138,128,0,255)
img:set(23,8,255,255,255,255)
img:set(23,9,255,255,255,255)
img:set(23,10,127,127,127,255)
img:set(23,11,255,255,255,255)
img:set(23,12,255,255,255,255)
img:set(23,13,255,255,255,255)
img:set(23,14,127,127,127,255)
img:set(23,15,127,127,127,255)
img:set(23,16,255,255,255,255)
img:set(23,17,255,255,255,255)
img:set(23,18,255,255,255,255)
img:set(23,19,127,127,127,255)
img:set(23,20,127,127,127,255)
img:set(23,21,255,255,255,255)
img:set(23,22,255,255,255,255)
img:set(23,23,255,255,255,255)
img:set(23,24,255,255,255,255)
img:set(23,25,255,255,255,255)
img:set(23,26,255,255,255,255)
img:set(23,27,255,255,255,255)
img:set(23,28,255,255,255,255)
img:set(24,6,255,255,255,255)
img:set(24,7,255,255,255,255)
img:set(24,8,255,255,255,255)
img:set(24,9,138,128,0,255)
img:set(24,10,255,255,255,255)
img:set(24,11,255,255,255,255)
img:set(24,12,255,255,255,255)
img:set(24,13,127,127,127,255)
img:set(24,14,255,255,255,255)
img:set(24,15,127,127,127,255)
img:set(24,16,127,127,127,255)
img:set(24,17,255,255,255,255)
img:set(24,18,255,255,255,255)
img:set(24,19,255,255,255,255)
img:set(24,20,255,255,255,255)
img:set(24,21,127,127,127,255)
img:set(24,22,127,127,127,255)
img:set(24,23,255,255,255,255)
img:set(24,24,255,255,255,255)
img:set(24,25,255,255,255,255)
img:set(24,26,255,255,255,255)
img:set(24,27,255,255,255,255)
img:set(24,28,255,255,255,255)
img:set(25,8,255,255,255,255)
img:set(25,9,255,255,255,255)
img:set(25,10,138,128,0,255)
img:set(25,11,255,255,255,255)
img:set(25,12,127,127,127,255)
img:set(25,13,255,255,255,255)
img:set(25,14,255,255,255,255)
img:set(25,15,255,255,255,255)
img:set(25,16,255,255,255,255)
img:set(25,17,255,255,255,255)
img:set(25,18,127,127,127,255)
img:set(25,19,255,255,255,255)
img:set(25,20,255,255,255,255)
img:set(25,21,255,255,255,255)
img:set(25,22,255,255,255,255)
img:set(25,23,255,255,255,255)
img:set(25,24,255,255,255,255)
img:set(25,25,255,255,255,255)
img:set(25,26,255,255,255,255)
img:set(25,27,255,255,255,255)
img:set(26,9,255,255,255,255)
img:set(26,10,255,255,255,255)
img:set(26,11,255,255,255,255)
img:set(26,12,138,128,0,255)
img:set(26,13,255,255,255,255)
img:set(26,14,255,255,255,255)
img:set(26,15,255,255,255,255)
img:set(26,16,255,255,255,255)
img:set(26,17,138,128,0,255)
img:set(26,18,255,255,255,255)
img:set(26,19,255,255,255,255)
img:set(26,20,255,255,255,255)
img:set(26,21,255,255,255,255)
img:set(26,22,255,255,255,255)
img:set(26,23,255,255,255,255)
img:set(26,24,255,255,255,255)
img:set(26,25,255,255,255,255)
img:set(27,11,255,255,255,255)
img:set(27,12,255,255,255,255)
img:set(27,13,255,255,255,255)
img:set(27,14,255,255,255,255)
img:set(27,15,255,255,255,255)
img:set(27,16,138,128,0,255)
img:set(27,17,255,255,255,255)
img:set(27,18,255,255,255,255)
img:set(27,19,138,128,0,255)
img:set(27,20,255,255,255,255)
img:set(27,21,255,255,255,255)
img:set(27,22,255,255,255,255)
img:set(27,23,255,255,255,255)
img:set(28,13,255,255,255,255)
img:set(28,14,255,255,255,255)
img:set(28,15,255,255,255,255)
img:set(28,16,255,255,255,255)
img:set(28,17,255,255,255,255)
img:set(28,18,255,255,255,255)
img:set(28,19,255,255,255,255)
img:set(28,20,255,255,255,255)
img:set(28,21,255,255,255,255)
img:set(29,15,255,255,255,255)
img:set(29,16,255,255,255,255)
return img
end
Fireworks = class()
-- Fireworks class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
function Fireworks:init(ox,oy,R,G,B)
self.debris_list = {}
self.clumps= 1
self.points = 66
self.clump_factor = self.clumps / self.points
self.maxvel= 6
self.maxcycles=66
self.gdiv = 5
self.active = true
self.ox = ox
self.oy = oy
self:new(ox,oy)
self.color = color(R,G,B,255)
end
function Fireworks:new(ox,oy)
for i= 1, self.points do
self.debris_list[i]=self:add_debris(ox,oy)
end
end
function Fireworks:add_debris(ox,oy)
local vel = math.random() * self.maxvel
local angle = math.random() * 2 * math.pi
return {x = ox or 0, y = oy or 0, dx = vel *math.cos(angle),
dy=vel*math.sin(angle),active=true, cycles=math.random(1,self.maxcycles)}
end
-- This function gets called once every frame
function Fireworks:draw()
self.clump_factor=self.clumps/self.points
strokeWidth(4)
lineCapMode(ROUND)
-- background(10,10,20)
local done = true
for i, debris in ipairs(self.debris_list) do
stroke(self.color.r, self.color.g, self.color.b,
--stroke (255*math.random(),255*math.random(),255*math.random(),
255 - (debris.cycles / self.maxcycles) * 255)
if debris.active then
done=false
line(debris.x,debris.y, debris.x, debris.y)
debris.x = debris.x + debris.dx
debris.y = debris.y + debris.dy
debris.dy = debris.dy + Gravity.y / self.gdiv
debris.cycles = debris.cycles + 1
if debris.cycles > self.maxcycles
or math.abs(debris.x) > WIDTH -- either side
or debris.y < -HEIGHT -- bottom
then
debris.active = false
end
end
end
if done then
self.active = false
end
end
House = class()
-- House class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
function House:init(pos)
-- you can accept and set parameters here
self.position = pos
self.state = WORLD_START
self.time = 0
self.frame = 0
end
function House:draw(isDestiny,R,G,B)
if self.state == WORLD_WAIT then
self.position.x = self.position.x - math.sin(self.position.x)*self.time*math.pi
self.time = self.time + 1/3
if (self.position.x<0 or self.position.x>WIDTH) then
self.state = WORLD_TRAVEL
self.position.x = 0
tiny_world:travelStart()
end
elseif self.state == WORLD_TRAVEL then
self.position.x = self.position.x + 0.8
self.position.y = HEIGHT/2 - math.sin(self.position.x)*math.pi/3
if self.position.x>WIDTH+10 then
tiny_world.frame = 6
tiny_world.state = WORLD_SAVED
self.state = 0
return
end
self.frame = (self.frame+1)%128
if self.frame%100==0 then
sound(DATA, "ZgBADgBAP1AaOWlEvl09O2DUrDwylEs/QQAhVUVyQEBIIQgn")
end
end
-- tiny world with a colored house
if self.state == WORLD_TRAVEL then
sprite("juaxix:tiny_world",self.position.x,self.position.y,128,146)
elseif self.state == WORLD_START or self.state == WORLD_WAIT then
sprite("juaxix:tiny_world",self.position.x,self.position.y)
if isDestiny then
tint(R,G,B, 128)
end
sprite("juaxix:house",self.position.x,self.position.y)
font("Arial-BoldMT")
fontSize(12)
fill(255, 255, 255, 255)
noTint()
text("House ,level: "..level,self.position.x-6,self.position.y-27)
end
end
function House:touched(touch)
-- Codea does not automatically call this method
end
-- Main
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
function setup()
displayMode(FULLSCREEN)
score = 0
level = 1 -- start in level 1 , easy
highscore = readLocalData("highscore",0)
tiny_world = World(4+(level*2)) -- start the tiny world with 6 persons
end
function draw()
tiny_world:draw()
end
function touched(touch)
tiny_world:touched(touch)
end
Person = class()
-- Person class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
function Person:init(pos)
self.position = pos
self.with_us = false
self.friend = nil
end
function Person:draw(lost,R,G,B)
if lost then
tint(R,G,B,255)
end
if self.friend ~= nil then
self.position.x = self.friend.position.x+math.random(-6,6)
self.position.y = self.friend.position.y+math.random(-6,6)
end
sprite("juaxix:juax",self.position.x, self.position.y)
noTint()
end
function Person:touched(touch)
end
Road = class()
-- Road class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
ROAD_EMPTY = 0
ROAD_DRAW = 1
ROAD_RUN = 2
ROAD_END = 3
function Road:init()
-- you can accept and set parameters here
self.touches = {}
self.colours = {}
self.person = nil
self.house = nil
self.time = 0
self.state = ROAD_EMPTY
self.distance= 0
end
function Road:add(t,Red,Green,Blue,Alpha)
if self.state == ROAD_EMPTY or self.state== ROAD_DRAW then
self.touches = {}
table.insert(self.touches, {t})
table.insert(self.colours, color(Red,Green,Blue,Alpha))
self.state = ROAD_DRAW
end
end
function Road:expand(t)
if self.state == ROAD_DRAW then
table.insert(self.touches[#self.touches],t)
end
end
function Road:draw()
if self.state == ROAD_RUN then
self:guide_person_to_house()
end
if self.state ~= ROAD_DRAW then return end
-- default seems to be 0
strokeWidth(8)
lineCapMode(ROUND)
local last
if #self.touches > 0 then
for i,v in ipairs(self.touches) do
last = v[1]
stroke(self.colours[i])
for j,w in ipairs(v) do
line(last.x,last.y,w.x,w.y)
last = w
end
end
end
end
function Road:guide_person_to_house()
if self.time == 0 then
self.time = 6
end
self.time = self.time - (1.6*(#self.touches[1]))
if self.time>0 then
-- print(self.time)
return
else
self.time = 6
end
for i,v in ipairs(self.touches) do
for j,w in ipairs(v) do
--if self.index == j then
self.person.position.x = w.x
self.person.position.y = w.y
table.remove(v,j)
if #v == 0 then
sound(DATA, "ZgBARwA+QSJAVCNeJe5KP9dSvj6N+mK/UQAif2BAQERFVSsw")
self.state = ROAD_END
else
sound(DATA, "ZgBAOQA/CnI3AzlERinpPDHybz5oQIU+TQBYVj9zQgpbLQo3")
end
return
--end
end
end
end
function Road:length()
local total = 0
for i,v in ipairs(self.touches) do
total = total + #v
end
return total
end
Star = class()
-- Star class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
function Star:init(pos, vel, angl)
self.position = pos
self.velocity = vel
self.angle = angl
end
function Star:update()
self.position.x = self.position.x - self.velocity
end
function Star:draw()
p = self.position
pushMatrix()
if self.angle ~= 0 then
translate(WIDTH/3,-HEIGHT/3)
end
rotate(self.angle)
line(p.x-self.velocity,p.y,p.x,p.y)
popMatrix()
end
function Star:shouldCull()
-- Check if off the left of the screen
if (self.position.x - self.velocity) < 0 then
return true
end
return false
end
----------------------------------------------
-- All stars
----------------------------------------------
Stars = class()
function Stars:init()
self.minSpeed = 6
self.speed = 23
self.spawnRate = 1
self.stars = {}
self.angle = 45
end
function Stars:updateAndCull()
toCull = {}
for i,star in ipairs(self.stars) do
if star:shouldCull() then
table.remove( self.stars, i )
else
star:update()
end
end
end
function Stars:update()
-- Create spawnRate lines per update
for i = 1,self.spawnRate do
-- Generate random spawn location
vel = math.random(self.minSpeed, self.speed)
spawn = vec2( WIDTH - vel, math.random(HEIGHT) )
table.insert(self.stars, Star(spawn, vel, self.angle))
end
-- Update and cull offscreen lines
self:updateAndCull()
end
function Stars:draw()
pushMatrix()
self:update()
pushStyle()
-- noSmooth()
-- stroke(179, 153, 180, 173)
--strokeWidth(2)
-- lineCapMode(SQUARE)
for i,star in ipairs(self.stars) do
star:draw()
end
popStyle()
popMatrix()
end
World = class()
-- Tiny World class
-- 20/4/2012 -- xixgames.com
-- Ludum Dare #23
-- Tiny World
-- in this magic tiny world people need others like you!
Red = math.random(255)
Green = math.random(255)
Blue = math.random(255)
WORLD_START = 1
WORLD_WAIT = 2
WORLD_BREAK = 3
WORLD_SAVED = 4
function World:init(maxpeople)
self.last_touch = nil
self.touch = nil
self.moving = false
self.people = {}
self.houses = {}
self.stars = Stars()
self.road = Road()
self.driver = math.random(maxpeople-1) -- random person is the driver of souls xD
self.destiny= 1 --math.random(math.floor(maxpeople/2)) -- random house
self.maxhouses = 1
self.maxpeople = maxpeople
self.state = WORLD_START
for i=1,maxpeople do
table.insert(self.people, Person(
vec2(math.random(WIDTH) , math.random(HEIGHT) )
))
end
for i=1,self.maxhouses do
table.insert(self.houses, House(
vec2( (WIDTH/2) - (math.random(-111,111)), (HEIGHT/2) + i*math.random(-111,111) )
) )
end
self.stars.angle = 0
self.fireworks = {}
self.aliens_touched = 0
self.asteroids_touched = 0
self.total_frames = 0
end
function World:travelStart()
sound(DATA, "ZgBAOQA/Syw4PwEr5tOFPL2cND/QyTU9TgASVExCPiUuNVJA")
self.state = WORLD_TRAVEL
--self.road = nil
self.aliens = {}
self.asteroids = {}
table.insert(self.aliens,Alien())
self.frame = 6
table.insert(self.asteroids,Asteroid(self.houses[self.destiny].position))
end
function World:updateTravel()
if self.frame<=0 then
if self.aliens_touched<7 then
table.insert(self.aliens,Alien())
end
table.insert(self.asteroids,Asteroid(self.houses[self.destiny].position))
self.frame = 7
elseif self.frame>0 then
self.frame = self.frame - 1/11
end
end
function World:update()
self.total_frames = (self.total_frames + 1)%128
if self.state == WORLD_TRAVEL then
self:updateTravel()
return
end
if self.state == WORLD_WAIT then return end
if self.road.state == ROAD_END then
table.insert(self.fireworks, Fireworks(
self.people[self.driver].position.x,
self.people[self.driver].position.y,
242,255,0
))
--length road penalization
score = score - (self.road.distance*100)
self.road:init()
for i,person in ipairs(self.people) do
if person.friend == nil then
table.insert(self.fireworks, Fireworks(
person.position.x,
person.position.y,
math.random(255),math.random(255),math.random(255)
))
score = score - math.random(600)
end
--person.with_us = false
--person.friend = nil
end
if score < 0 then
score = 0
end
self.people = {}
-- change instances states
for i,house in ipairs(self.houses) do
house.state = WORLD_WAIT
end
self.state = WORLD_WAIT
return
end
self.touch = vec2(CurrentTouch.x, CurrentTouch.y)
if CurrentTouch.state == ENDED then
-- to make sure we don't miss the touch began state
self.moving = false
local hit_house = self.houses[self.destiny].position:dist(self.touch) <66
if hit_house and self.road.person~= nil then
self.road.state = ROAD_RUN
-- add destiny
self.road:add(self.houses[self.destiny].position,R,G,B,255)
-- compute total distance
self.road.distance = self.road:length()
else -- clear not valid touches
self.road:init()
for i,p in ipairs(self.people) do
p.with_us = false
p.friend = nil
end
end
elseif CurrentTouch.state == BEGAN then
local hit_driver = self.people[self.driver].position:dist(self.touch) <33
if self.touch ~= self.last_touch and hit_driver then
self.last_touch = self.touch
self:new_move(self.touch,Red,Green,Blue)
end
elseif CurrentTouch.state == MOVING and self.road.person~=nil then
if self.touch ~= self.last_touch then
if self.moving then
sound(DATA, "ZgBAbQBDPxReASQqRoCBOv2Gtz5qbMS+QAAEUDhAMC8jUGFb")
self.road:expand(self.touch)
else
-- Did not detect the move
self:new_move(self.touch,Red,Green,Blue)
end
self.last_touch = self.touch
-- check for added chars
for i,person in ipairs(self.people) do
if person ~= self.people[self.driver] and not person.with_us then
local hitnew = person.position:dist(self.touch) <33
if hitnew then
person.with_us = true
table.insert(self.fireworks, Fireworks(
person.position.x,
person.position.y,
Red,Green,Blue
))
end
end
end
end
end
end
function World:new_move(touch,R,G,B)
self.moving = true
self.road:add(touch,R,G,B,255)
self.road.person = self.people[self.driver]
self.road.house = self.houses[self.destiny]
sound(DATA, "ZgBATABZHWRAO3oE2fqgvmtJlT4+uRg/XAAkbzxAXTFpC0gV")
end
function World:drawWin()
noTint()
background(183, 185, 189, 255)
self.frame = self.frame + 0.1
if self.frame>=33 then
score = score - self.total_frames/100
if highscore<score then
saveLocalData("highscore",score)
highscore = score
end
-- next level!
level = level + 1
self:init(4+(level*2))
return
end
pushStyle()
font("Futura-CondensedExtraBold")
fontSize(133)
pushMatrix()
fill(math.random(66,255),255,math.random(66,255),255)
translate(WIDTH/2+3,HEIGHT/2+66)
rotate(math.random(-6,6))
text("WINNER ")
fill(math.random(66,255),255,math.random(66,255),255)
popMatrix()
fontSize(33)
text("You totally save the tiny world inside us from crazy!. xixgames.com ",WIDTH/2,(HEIGHT/2)-66)
text("Score:"..(score),WIDTH/2,(HEIGHT/2) - 166)
if highscore<(score) then
text("Congrats!!, you did a highscore!!"..score,WIDTH/2,(HEIGHT/2) - 190)
saveLocalData("highscore",score)
end
text("Are you ready for level "..(level+1).."?",WIDTH/2,(HEIGHT/2) - 222)
popStyle()
end
function World:drawGameOver()
noTint()
background(Red,Green,Blue,math.random(255))
self.frame = self.frame + 0.1
if self.frame>=6 then
score = 0
level = 1
self:init(6)
end
pushStyle()
font("ArialRoundedMTBold")
fontSize(166)
pushMatrix()
fill(math.random(1,Red), math.random(Green), math.random(Blue),255)
translate(WIDTH/2,HEIGHT/2)
text("TRY AGAIN")
popMatrix()
popStyle()
end
function World:draw()
if self.state == WORLD_BREAK then
self:drawGameOver()
return
end
if self.state == WORLD_SAVED then
self:drawWin()
return
end
self:update()
background(0, 0, 0, 0)
self.stars:draw()
if self.state == WORLD_START or self.state==WORLD_WAIT then
self.road:draw()
for i,house in ipairs(self.houses) do
house:draw(i==self.destiny,Red,Green,Blue)
end
for i,person in ipairs(self.people) do
person:draw(i==self.driver,Red,Green,Blue)
if i==self.driver and self.road.state==ROAD_RUN then
for j,other in ipairs(self.people) do
if person ~= other and other.with_us and other.friend==nil then
local contact = person.position:dist(other.position) <33
if contact then
other.friend = person
table.insert(self.fireworks, Fireworks(
person.position.x,
person.position.y,
Red,Green,Blue
))
sound(DATA, "ZgFAQgBALV9APnoU48wkvAjhwj4ARU++TAB0fw1pOT84Tgpd")
score = score + math.random(666)
end
end
end
end
end
elseif self.state == WORLD_TRAVEL then
for i,house in ipairs(self.houses) do
house:draw(i==self.destiny,Red,Green,Blue)
end
for i,alien in ipairs(self.aliens) do
alien:draw()
if alien.state == ALIEN_DEAD then
table.insert(self.fireworks, Fireworks(
alien.position.x,
alien.position.y,
alien.R,alien.G,alien.B
))
table.remove(self.aliens,i)
end
end
for i,asteroid in ipairs(self.asteroids) do
asteroid:draw()
if asteroid.state == ASTEROID_DEAD then
table.insert(self.fireworks, Fireworks(
asteroid.position.x,
asteroid.position.y,
asteroid.R,asteroid.G,asteroid.B
))
table.remove(self.asteroids ,i)
end
end
end
for i,fw in ipairs(self.fireworks) do
fw:draw()
if not fw.active then
table.remove(self.fireworks, i)
end
end
-- draw tiny square
--noSmooth()
stroke(Red,Green,Blue,255)
fill(Red,Green,Blue,255)
rect(0, HEIGHT-33, 33, 33)
font("ArialRoundedMTBold")
fontSize(33)
if self.state==WORLD_START and score == 0 then
text("Guide the chosen one to the Tiny World", 444, HEIGHT-27)
fontSize(27)
text("Highscore: "..highscore, 444, HEIGHT-66)
else
text("Score: "..score, 333, HEIGHT-27)
end
if self.state == WORLD_TRAVEL then
fontSize(27)
text("Protect the Tiny World from Aliens and Asteroids", 444, HEIGHT-66)
end
end
function World:touched(touch)
if self.state ~= WORLD_TRAVEL then return end
for i,a in ipairs(self.aliens) do
if a:touched(touch) then
score = score + math.random(666)
self.aliens_touched = self.aliens_touched + 1
return
end
end
for i,a in ipairs(self.asteroids) do
if a:touched(touch) then
score = score + math.random(777)
self.asteroids_touched = self.asteroids_touched + 1
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment