Advertisement
ezak

Tile engine Codea

Feb 7th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | None | 0 0
  1. --# Main
  2.  
  3.  
  4. -- Use this function to perform your initial setup
  5. displayMode(FULLSCREEN)
  6. function setup()
  7.     stick = Stick()
  8.  
  9.     pylon = Wall("Cargo Bot:Crate Red 2")
  10.     wall = Wall("Cargo Bot:Crate Yellow 2")
  11.     floor = Floor("Cargo Bot:Crate Green 2")
  12.  
  13.     world = World()
  14.     hero = Hero(3,3)
  15.  
  16.     TO_DEG = 180/math.pi
  17. end
  18.  
  19. -- This function gets called once every frame
  20. function draw()
  21.     background(0)
  22.     local TO_DEG = TO_DEG
  23.     local hero = hero
  24.  
  25.     perspective(60)
  26.     camera(hero.x, 3, 1 + hero.z, hero.x, 0, hero.z, 0, 1, 0)
  27.  
  28. -- Draw world
  29.     pushMatrix()
  30.     world:draw()
  31.     popMatrix()
  32.  
  33. -- Draw hero
  34.     translate(hero.x, hero.y, hero.z)
  35.     rotate(stick.direction*TO_DEG, 0, 1, 0)
  36.  
  37.     -- roll animation
  38.     if stick.active then
  39.         rotate(-ElapsedTime*10*TO_DEG, 0, 0, 1)
  40.     end
  41.  
  42.     scale(.25, .25, .25)
  43.     hero:draw()
  44.  
  45. -- Restore orthographic projection
  46.     ortho()
  47.     viewMatrix(matrix())
  48.     resetMatrix()
  49.  
  50.     -- fade out overlay
  51.     sprite("Cargo Bot:Background Fade", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
  52.  
  53.     if stick.active then
  54.         local ceil = math.ceil
  55.         stick:draw()
  56.  
  57.         -- move hero based on stick direction
  58.         local mvtx = math.cos(stick.direction)/50*stick.dist
  59.         local mvtz = -math.sin(stick.direction)/50*stick.dist
  60.         hero.x = hero.x + mvtx
  61.         hero.z = hero.z + mvtz
  62.  
  63.         -- convert to table coordinates
  64.         hero.px = ceil(hero.x - .5)
  65.         hero.py = ceil(hero.z - .5)
  66.  
  67.         -- lazy colision check
  68.         if world.data[hero.py][hero.px] ~= 0 then
  69.             hero.x = hero.x - mvtx
  70.             hero.z = hero.z - mvtz
  71.             hero.px = ceil(hero.x - .5)
  72.             hero.py = ceil(hero.z - .5)
  73.         end
  74.     end
  75.  
  76. end
  77.  
  78. function touched(touch)
  79.     stick:touched(touch)
  80. end
  81.  
  82. --# World
  83. World = class()
  84.  
  85. function World:init()
  86.  
  87.     -- define the world
  88.     self.data =
  89.     {
  90.         {1, 1, 1, 1, 1, 1, 1, 1},
  91.         {1, 2, 0, 0, 0, 0, 2, 1},
  92.         {1, 0, 0, 0, 0, 0, 0, 1},
  93.         {1, 0, 0, 1, 2, 0, 0, 1},
  94.         {1, 0, 0, 2, 1, 0, 0, 1},
  95.         {1, 0, 0, 0, 0, 0, 0, 1},
  96.         {1, 2, 0, 0, 0, 0, 2, 1},
  97.         {1, 1, 1, 1, 1, 1, 1, 1}
  98.     }
  99. end
  100.  
  101. function World:draw()
  102.     local floor, wall, pylon = floor, wall, pylon
  103.     local offSet = 3
  104.     local px, py = hero.px, hero.py
  105.  
  106.     translate(px - offSet, 0, py - offSet)
  107.     for y = py - offSet, py + offSet do
  108.         for x = px - offSet, px + offSet do
  109.             if self.data[y] then
  110.                 local val = self.data[y][x]
  111.                 if val == 0 then
  112.                     floor:draw()
  113.                 elseif val == 1 then
  114.                     wall:draw()
  115.                 elseif val == 2 then
  116.                     pylon:draw()
  117.                 end
  118.             end
  119.             translate(1,0,0)
  120.         end
  121.         translate(-(1 + 2 * offSet), 0, 1)
  122.     end
  123. end
  124.  
  125.  
  126. --# Wall
  127. Wall = class()
  128.  
  129.  
  130. function Wall:init(tex)
  131.     -- all the unique vertices that make up a cube
  132.     local vertices =
  133.     {
  134.         vec3(-0.5, -0.5,  0.5), -- Left  bottom front
  135.         vec3( 0.5, -0.5,  0.5), -- Right bottom front
  136.         vec3( 0.5,  0.5,  0.5), -- Right top    front
  137.         vec3(-0.5,  0.5,  0.5), -- Left  top    front
  138.         vec3(-0.5, -0.5, -0.5), -- Left  bottom back
  139.         vec3( 0.5, -0.5, -0.5), -- Right bottom back
  140.         vec3( 0.5,  0.5, -0.5), -- Right top    back
  141.         vec3(-0.5,  0.5, -0.5), -- Left  top    back
  142.     }
  143.  
  144.     -- now construct a cube out of the vertices above
  145.     local verts =
  146.     {
  147.         -- Front
  148.         vertices[1], vertices[2], vertices[3],
  149.         vertices[1], vertices[3], vertices[4],
  150.         -- Right
  151.         vertices[2], vertices[6], vertices[7],
  152.         vertices[2], vertices[7], vertices[3],
  153.         -- Back
  154.         vertices[6], vertices[5], vertices[8],
  155.         vertices[6], vertices[8], vertices[7],
  156.         -- Left
  157.         vertices[5], vertices[1], vertices[4],
  158.         vertices[5], vertices[4], vertices[8],
  159.         -- Top
  160.         vertices[4], vertices[3], vertices[7],
  161.         vertices[4], vertices[7], vertices[8],
  162.        -- Bottom
  163.         vertices[5], vertices[6], vertices[2],
  164.         vertices[5], vertices[2], vertices[1],
  165.     }
  166.  
  167.     -- all the unique texture positions needed
  168.     local texvertices =
  169.     {
  170.         vec2(0,0),
  171.         vec2(1,0),
  172.         vec2(0,1),
  173.         vec2(1,1)
  174.     }
  175.  
  176.     -- apply the texture coordinates to each triangle
  177.     local texCoords =
  178.     {
  179.         -- Front
  180.         texvertices[1], texvertices[2], texvertices[4],
  181.         texvertices[1], texvertices[4], texvertices[3],
  182.         -- Right
  183.         texvertices[1], texvertices[2], texvertices[4],
  184.         texvertices[1], texvertices[4], texvertices[3],
  185.         -- Back
  186.         texvertices[1], texvertices[2], texvertices[4],
  187.         texvertices[1], texvertices[4], texvertices[3],
  188.         -- Left
  189.         texvertices[1], texvertices[2], texvertices[4],
  190.         texvertices[1], texvertices[4], texvertices[3],
  191.         -- Top
  192.         texvertices[1], texvertices[2], texvertices[4],
  193.         texvertices[1], texvertices[4], texvertices[3],
  194.         -- Bottom
  195.         texvertices[1], texvertices[2], texvertices[4],
  196.         texvertices[1], texvertices[4], texvertices[3],
  197.     }
  198.  
  199.     self.model = mesh()
  200.     self.model.vertices = verts
  201.     self.model.texture = tex
  202.     self.model.texCoords = texCoords
  203.     self.model:setColors(255,255,255,255)
  204. end
  205.  
  206. function Wall:draw()
  207.     self.model:draw()
  208. end
  209. --# Floor
  210. Floor = class()
  211.  
  212. function Floor:init(tex)
  213.     -- all the unique vertices that make up a cube
  214.     local vertices =
  215.     {
  216.         vec3( 0.5,  -0.5,  0.5), -- Right top    front
  217.         vec3(-0.5,  -0.5,  0.5), -- Left  top    front
  218.         vec3( 0.5,  -0.5, -0.5), -- Right top    back
  219.         vec3(-0.5,  -0.5, -0.5), -- Left  top    back
  220.     }
  221.  
  222.  
  223.     -- now construct a cube out of the vertices above
  224.     local verts =
  225.     {
  226.         -- Bottom
  227.         vertices[3], vertices[4], vertices[2],
  228.         vertices[3], vertices[2], vertices[1],
  229.     }
  230.  
  231.     -- all the unique texture positions needed
  232.     local texvertices =
  233.     {
  234.         vec2(0,0),
  235.         vec2(1,0),
  236.         vec2(0,1),
  237.         vec2(1,1)
  238.     }
  239.  
  240.     -- apply the texture coordinates to each triangle
  241.     local texCoords =
  242.     {
  243.         -- Bottom
  244.         texvertices[1], texvertices[2], texvertices[4],
  245.         texvertices[1], texvertices[4], texvertices[3],
  246.     }
  247.  
  248.     self.model = mesh()
  249.     self.model.vertices = verts
  250.     self.model.texture = tex
  251.     self.model.texCoords = texCoords
  252.     self.model:setColors(255,255,255,255)
  253. end
  254.  
  255. function Floor:draw()
  256.     self.model:draw()
  257. end
  258. --# Hero
  259. Hero = class()
  260.  
  261. function Hero:init(x, z)
  262.     self.x, self.y, self.z = x,0,z
  263.     self.px, self.py = math.ceil(.5+x), math.ceil(.5+z)
  264.  
  265.     self.mdl = Wall("Cargo Bot:Crate Blue 2")
  266. end
  267.  
  268. function Hero:draw()
  269.     self.mdl:draw()
  270. end
  271.  
  272.  
  273. --# Stick
  274. Stick = class()
  275.  
  276. function Stick:init()
  277.     self.direction = 0
  278.     self.dist = 0
  279.  
  280.     self.active = false
  281.     self.origin = vec2(150, 150)
  282.     self.center = self.origin
  283.     self.pos = self.origin
  284.  
  285.     self.stick_bg = readImage("Space Art:Eclipse")
  286.     self.stick = readImage("Space Art:UFO")
  287.  
  288. end
  289.  
  290. function Stick:draw()
  291.     sprite(self.stick_bg, self.center.x, self.center.y)
  292.     sprite(self.stick, self.pos.x, self.pos.y)
  293. end
  294.  
  295. function Stick:touched(touch)
  296.     if touch.state == BEGAN then
  297.         self.center = vec2(touch.x, touch.y)
  298.         self.active = true
  299.     end
  300.  
  301.     self.pos = vec2(touch.x, touch.y)
  302.     self.direction = math.atan2(self.pos.y - self.center.y, self.pos.x - self.center.x)
  303.  
  304.     self.dist = math.min(2, self.pos:dist(self.center)/32)
  305.  
  306.     if touch.state == ENDED then
  307.         self.center = self.origin
  308.         self.pos = self.center
  309.         self.active = false
  310.     end
  311.  
  312.  
  313. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement