Mercurial > vanillian
view main.lua @ 39:4feeb8cb736e
music mute added under "M"
| author | Makdaam |
|---|---|
| date | Sun, 16 Dec 2012 14:21:51 +0100 |
| parents | 0d75bfc00f01 |
| children | 53ae6905cfff |
line wrap: on
line source
local ATL = require("AdvTiledLoader") local anim8 = require("anim8") local sprite = require("sprite") local SPRITES = {} local SHADOWS = {} local MAP local tx, ty = 0, 0 local DX = {0, 1, 0, -1} local DY = {-1, 0, 1, 0} local SHADOW_IMAGE local MOBS_IMAGE local GRID = anim8.newGrid(64, 64, 256, 256) local scroll_speed = 250 local scroll_margin = 64 local MUSIC local LAST_MUSIC local TEXT = "" local SOUNDS = {} function newMob(x, y, cells) local mob = {} mob.sprite = sprite.newSprite( x - 32, y - 60, MOBS_IMAGE, anim8.newAnimation('loop', GRID(cells), 0.25) ) mob.shadow = sprite.newShadow(SHADOW_IMAGE, mob.sprite) table.insert(SPRITES, mob.sprite) table.insert(SHADOWS, mob.shadow) return mob end function makeVillain(villain) villain.is_villain = true function villain.sprite:walk(dt, dir) local x = self.x + DX[dir] * 24 local y = self.y + DY[dir] * 16 local mx, my = math.floor((x+32)/48), math.floor((y+60)/48) local cell = MAP.layers['control']:get(mx, my) local bump = false for i, s in pairs(SPRITES) do if not s.is_villain then local d = (x - s.x) * (x - s.x) + ((y - s.y) * (y - s.y)) if d < 100 then bump = true if s.properties.text then TEXT = s.properties.text SOUNDS.talk:play() else if s.properties.exit then load_map(s.properties.exit) TEXT = s.properties.exit else SOUNDS.bump:play() TEXT = "" end end end end end if not bump and cell and cell.properties and not cell.properties.block then self.x = self.x + DX[dir] * dt * 250 self.y = self.y + DY[dir] * dt * 250 end end function villain.sprite:update(dt) local map_w = MAP.width * MAP.tileWidth local map_h = MAP.height * MAP.tileHeight if love.keyboard.isDown("up") then self:walk(dt, 1) end if love.keyboard.isDown("right") then self:walk(dt, 2) end if love.keyboard.isDown("down") then self:walk(dt, 3) end if love.keyboard.isDown("left") then self:walk(dt, 4) end if love.keyboard.isDown(" ") then TEXT = "" end if self.x + tx < scroll_margin then tx = math.min(tx + dt * scroll_speed, 0) else if self.x + tx > 800 - 64 - scroll_margin then tx = math.max(tx - dt * scroll_speed, 800 - map_w) end end if self.y + ty < scroll_margin then ty = math.min(ty + dt * scroll_speed, 0) else if self.y + ty > 600 - 64 - scroll_margin then ty = math.max(ty - dt * scroll_speed, 600 - map_h) end end sprite.Sprite.update(self, dt) end return villain end function love.load() love.graphics.setFont(love.graphics.newFont('pf_tempesta_seven_extended_bold.ttf')) SHADOW_IMAGE = love.graphics.newImage('shadow.png') MOBS_IMAGE = love.graphics.newImage('mobs.png') SOUNDS.bump = love.audio.newSource('bump.xm', 'static') SOUNDS.talk = love.audio.newSource('talk.xm', 'static') load_map('level01.tmx') end function load_map(filename) SPRITES = {} SHADOWS = {} MAP = ATL.Loader.load(filename) MAP.drawObjects = false MAP.layers['control'].visible = false if MAP.properties.music then if not LAST_MUSIC or MAP.properties.music ~= LAST_MUSIC then if MUSIC then MUSIC:stop() end MUSIC = love.audio.newSource(MAP.properties.music, 'static') MUSIC:setLooping(true) MUSIC:play() LAST_MUSIC = MAP.properties.music end end for i, data in pairs(MAP.layers['mobs'].objects) do if data.name == 'villain' then local mob = makeVillain(newMob(data.x, data.y, '1-2,1')) mob.sprite.properties = data.properties else local mob = newMob(data.x, data.y, data.properties.cells or '1-2,2') mob.sprite.properties = data.properties end end end function draw_text(text) text = text:gsub('[|]', '\n') love.graphics.setColor(0, 0, 0) love.graphics.print(text, 3, 3) love.graphics.print(text, 5, 3) love.graphics.print(text, 3, 5) love.graphics.print(text, 5, 5) love.graphics.setColor(255, 255, 255) love.graphics.print(text, 4, 4) end function love.draw() love.graphics.translate(math.floor(tx), math.floor(ty)) MAP:autoDrawRange(math.floor(tx), math.floor(ty), 1, 0) MAP:draw() for k, shadow in pairs(SHADOWS) do shadow:draw() end for k, sprite in pairs(SPRITES) do sprite:draw() end for i, layer in pairs(MAP.layerOrder) do if layer.properties.overlay then layer:draw() end end love.graphics.translate(-math.floor(tx), -math.floor(ty)) draw_text(TEXT) end function love.update(dt) for k, sprite in pairs(SPRITES) do sprite:update(dt) end if love.keyboard.isDown("m") then if MUSIC:isStopped() then MUSIC:play() else MUSIC:stop() end end table.sort(SPRITES, function (a, b) return a.y < b.y end) end
