Advertisement
MegaLoler

Love Game Engine Demo

Apr 9th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.94 KB | None | 0 0
  1. require("class")
  2. require("engine")
  3. require("gui")
  4.  
  5. Game = newClass(engine.Engine)
  6.  
  7. function Game:setup()
  8.     self:setScene(Intro)
  9.     self:setTitle("Game Test")
  10.     self.fade = 1
  11. end
  12.  
  13. Intro = newClass(engine.Entity)
  14.  
  15. function Intro:setup()
  16.     self:setBackgroundColor({255, 255, 255})
  17.     self.hasBackground = true
  18.     local title = engine.Label:new(self.engine, self)
  19.     self:addChild(title, "Game Test")
  20.     title:setFont(love.graphics.newFont(50))
  21.     local sub = engine.Label:new(self.engine, self)
  22.     self:addChild(sub, "By MegaLoler")
  23.     sub:setFont(love.graphics.newFont(20))
  24.     sub:setPosition(title.x, title.y + title.height)
  25. end
  26.  
  27. function Intro:update(dt)
  28.     if self.time > 3 then
  29.         self:next()
  30.     end
  31. end
  32.  
  33. function Intro:mousepressed(x, y, button)
  34.     self:next()
  35. end
  36.  
  37. function Intro:next()
  38.     self.engine:fadeToScene(Menu)
  39. end
  40.  
  41. Menu = newClass(engine.Entity)
  42.  
  43. function Menu:setup()
  44.     self:setBackgroundColor({31, 31, 31})
  45.     self.clearCanvas = false
  46.     self.backgroundAlpha = 63
  47.     self.hasBackground = true
  48.     self.staticDrawing = false
  49.    
  50.     self.camX = 0
  51.     self.camY = 0
  52.     self.targetCamX = 0
  53.     self.targetCamY = 0
  54.     self.camSpeed = 0.2
  55.    
  56.     self.container = engine.Container:new(self.engine, self)
  57.     self:addChild(self.container, self.width * 2, self.height * 2)
  58.     self.container.staticDrawing = false
  59.     self.container.hasBackground = false
  60.     self.container.hasBorder = false
  61.     self.container:setOrigin(0, 0)
  62.     self.container:setPosition(0, 0)
  63.     self.container.preDraw = self.drawBackground
  64.    
  65.     self.button = engine.Button:new(self.engine, self.container)
  66.     self.container:addChild(self.button, "Start")
  67.     self.button:setFont(love.graphics.newFont(30))
  68.     self.button:setTextColor({20, 20, 20})
  69.     self.button:setPosition(self.width / 2, self.height / 2 - 20)
  70.    
  71.     self.button2 = engine.Button:new(self.engine, self.container)
  72.     self.container:addChild(self.button2, "Help", function() self.container.container:goToScreen(1, 0) end)
  73.     self.button2:setFont(love.graphics.newFont(30))
  74.     self.button2:setTextColor({20, 20, 20})
  75.     self.button2:setPosition(self.width / 2, self.button.y + self.button.height + 10)
  76.    
  77.     self.button3 = engine.Button:new(self.engine, self.container)
  78.     self.container:addChild(self.button3, "Credits", function() self.container.container:goToScreen(0, 1) end)
  79.     self.button3:setFont(love.graphics.newFont(30))
  80.     self.button3:setTextColor({20, 20, 20})
  81.     self.button3:setPosition(self.width / 2, self.button2.y + self.button2.height + 10)
  82.    
  83.     self.button4 = engine.Button:new(self.engine, self.container)
  84.     self.container:addChild(self.button4, "Quit", love.event.quit)
  85.     self.button4:setFont(love.graphics.newFont(30))
  86.     self.button4:setTextColor({20, 20, 20})
  87.     self.button4:setPosition(self.width / 2, self.button3.y + self.button3.height + 10)
  88.    
  89.     self.button:setText("Start")
  90.     self.button2:setText("Help")
  91.     self.button3:setText("Credits")
  92.     self.button4:setText("Exit")
  93.    
  94.     self.title = engine.Label:new(self.engine, self.container)
  95.     self.container:addChild(self.title, "Menu")
  96.     self.title:setFont(love.graphics.newFont(40))
  97.     self.title.hasShadow = true
  98.     self.title.shadowOffsetX = -5
  99.     self.title.shadowOffsetY = 5
  100.     self.title:setOrigin(0, 0)
  101.     self.title:setTextColor({240, 240, 240})
  102.     self.title:setPosition(50, 50)
  103.    
  104.     self.title2 = engine.Label:new(self.engine, self.container)
  105.     self.container:addChild(self.title2, "Help")
  106.     self.title2:setFont(love.graphics.newFont(40))
  107.     self.title2.hasShadow = true
  108.     self.title2.shadowOffsetX = -5
  109.     self.title2.shadowOffsetY = 5
  110.     self.title2:setOrigin(0, 0)
  111.     self.title2:setTextColor({240, 240, 240})
  112.     self.title2:setPosition(50 + self.width, 50)
  113.    
  114.     self.title3 = engine.Label:new(self.engine, self.container)
  115.     self.container:addChild(self.title3, "Credits")
  116.     self.title3:setFont(love.graphics.newFont(40))
  117.     self.title3.hasShadow = true
  118.     self.title3.shadowOffsetX = -5
  119.     self.title3.shadowOffsetY = 5
  120.     self.title3:setOrigin(0, 0)
  121.     self.title3:setTextColor({240, 240, 240})
  122.     self.title3:setPosition(50, 50 + self.height)
  123.    
  124.     self.title4 = engine.Label:new(self.engine, self.container)
  125.     self.container:addChild(self.title4, "Stuff")
  126.     self.title4:setFont(love.graphics.newFont(40))
  127.     self.title4.hasShadow = true
  128.     self.title4.shadowOffsetX = -5
  129.     self.title4.shadowOffsetY = 5
  130.     self.title4:setOrigin(0, 0)
  131.     self.title4:setTextColor({240, 240, 240})
  132.     self.title4:setPosition(50 + self.width, 50 + self.height)
  133.    
  134.     self.screenX = 0
  135.     self.screenY = 0
  136. end
  137.  
  138. function Menu:update(dt)
  139.     self.button:move(math.cos(self.time * 2) / 3, math.sin(self.time * 2) / 3)
  140.     self.button2:move(math.cos(self.time * 2 - 0.2) / 3, math.sin(self.time * 2 - 0.2) / 3)
  141.     self.button3:move(math.cos(self.time * 2 - 0.4) / 3, math.sin(self.time * 2 - 0.4) / 3)
  142.     self.button4:move(math.cos(self.time * 2 - 0.6) / 3, math.sin(self.time * 2 - 0.6) / 3)
  143.     self.title:move(0, math.sin(self.time * 5))
  144.     self.title2:move(0, math.sin(self.time * 5))
  145.     self.title3:move(0, math.sin(self.time * 5))
  146.     self.title4:move(0, math.sin(self.time * 5))
  147.     self.camX = self.camX + (self.targetCamX - self.camX) * self.camSpeed
  148.     self.camY = self.camY + (self.targetCamY - self.camY) * self.camSpeed
  149.     self.container:move((-self.camX - self.container.x) * self.camSpeed, (-self.camY - self.container.y) * self.camSpeed)
  150. end
  151.  
  152. function Menu:goToScreen(x, y)
  153.     if x then self.screenX = x end
  154.     if y then self.screenY = y end
  155.     self.targetCamX = (x or self.screenX) * self.width
  156.     self.targetCamY = (y or self.screenY) * self.height
  157. end
  158.  
  159. function Menu:drawBackground()
  160.     local ox = math.sin(self.time * 2.2) * 200
  161.     local oy = math.cos(self.time * 2.9) * 200
  162.     for x = -1, self.width / 128 do
  163.         for y = -1, self.height / 128 do
  164.             local xp = x * 200 + ox % 200
  165.             local yp = y * 200 + oy % 200
  166.             love.graphics.setColor(31 + xp / 8, 31 + yp / 8, 63)
  167.             love.graphics.polygon("fill", xp, yp, xp + 100, yp, xp + 100, yp + 100, xp, yp + 100)
  168.             xp = xp + 100
  169.             yp = yp + 100
  170.             love.graphics.setColor(31 + xp / 8, 31 + yp / 8, 63)
  171.             love.graphics.polygon("fill", xp, yp, xp + 100, yp, xp + 100, yp + 100, xp, yp + 100)
  172.         end
  173.     end
  174.     for x = -1, self.width / 128 do
  175.         for y = -1, self.height / 128 do
  176.             local xp = x * 200 + 5 + ox % 200
  177.             local yp = y * 200 - 5 + oy % 200
  178.             love.graphics.setColor(31 + xp / 8, 31 + yp / 8, 127)
  179.             love.graphics.polygon("fill", xp, yp, xp + 100, yp, xp + 100, yp + 100, xp, yp + 100)
  180.             xp = xp + 100
  181.             yp = yp + 100
  182.             love.graphics.setColor(31 + xp / 8, 31 + yp / 8, 127)
  183.             love.graphics.polygon("fill", xp, yp, xp + 100, yp, xp + 100, yp + 100, xp, yp + 100)
  184.         end
  185.     end
  186. end
  187.  
  188. function Menu:keypressed(key, unicode)
  189.     if key == "left" then
  190.         self.screenX = self.screenX - 1
  191.     elseif key == "right" then
  192.         self.screenX = self.screenX + 1
  193.     elseif key == "down" then
  194.         self.screenY = self.screenY + 1
  195.     elseif key == "up" then
  196.         self.screenY = self.screenY - 1
  197.     end
  198.     self.screenX = math.min(self.screenX, 1)
  199.     self.screenY = math.min(self.screenY, 1)
  200.     self.screenX = math.max(self.screenX, 0)
  201.     self.screenY = math.max(self.screenY, 0)
  202.     self:goToScreen()
  203. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement