Mercurial > vanillian
annotate main.lua @ 4:e4263ea87780
Place the sprites on the map
| author | Radomir Dopieralski <sheep-devel@sheep.art.pl> |
|---|---|
| date | Sat, 15 Dec 2012 13:54:13 +0100 |
| parents | f5de6c75c4d0 |
| children | 98ab870293bc |
| rev | line source |
|---|---|
| 2 | 1 local ATL = require("AdvTiledLoader") |
| 0 | 2 local anim8 = require("anim8") |
| 3 local sprite = require("sprite") | |
| 4 | |
| 5 local sprites = {} | |
| 2 | 6 local shadows = {} |
| 7 local map | |
| 8 local tx, ty = 0, 0 | |
| 9 local DX = {0, 1, 0, -1} | |
| 10 local DY = {-1, 0, 1, 0} | |
| 11 local villain | |
| 12 | |
| 13 function newVillain(x, y, image) | |
| 14 local grid = anim8.newGrid(64, 64, 128, 128) | |
| 15 local villain = sprite.newSprite( | |
| 16 x, y, | |
| 17 image, | |
| 18 anim8.newAnimation('loop', grid('1-2,1'), 0.25) | |
| 19 ) | |
| 20 function villain:walk(dt, dir) | |
|
3
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
21 local x = self.x + DX[dir] * dt * 250 |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
22 local y = self.y + DY[dir] * dt * 250 |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
23 local tx, ty = math.floor((x+32)/48), math.floor((y+60)/48) |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
24 local cell = map.layers['control']:get(tx, ty) |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
25 if cell and cell.properties and cell.properties.block == 0 then |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
26 self.x = x |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
27 self.y = y |
|
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
28 end |
| 2 | 29 end |
| 30 return villain | |
| 31 end | |
| 32 | |
| 33 function newCivilian(x, y, image) | |
| 34 local grid = anim8.newGrid(64, 64, 128, 128) | |
| 35 local civilian = sprite.newSprite( | |
| 36 x, y, | |
| 37 image, | |
| 38 anim8.newAnimation('loop', grid('1-2,1'), 0.25) | |
| 39 ) | |
| 40 return civilian | |
| 41 end | |
| 0 | 42 |
| 43 function love.load() | |
| 2 | 44 local shadow_image = love.graphics.newImage('shadow.png') |
| 45 local civilian_image = love.graphics.newImage('civilian.png') | |
| 46 local villain_image = love.graphics.newImage('villain.png') | |
| 47 map = ATL.Loader.load('level01.tmx') | |
|
3
f5de6c75c4d0
make the walls block
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
2
diff
changeset
|
48 map.drawObjects = false |
|
4
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
49 for i, mob in pairs(map.layers['mobs'].objects) do |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
50 if mob.name == 'villain' then |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
51 villain = newVillain(mob.x - 32, mob.y - 60, villain_image) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
52 table.insert(sprites, villain) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
53 table.insert(shadows, sprite.newShadow(shadow_image, villain)) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
54 else |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
55 local civilian = newCivilian(mob.x - 32, mob.y - 60, civilian_image) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
56 table.insert(sprites, civilian) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
57 table.insert(shadows, sprite.newShadow(shadow_image, civilian)) |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
58 end |
|
e4263ea87780
Place the sprites on the map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
3
diff
changeset
|
59 end |
| 0 | 60 end |
| 61 | |
| 62 | |
| 63 function love.draw() | |
| 2 | 64 love.graphics.translate(math.floor(tx), math.floor(ty)) |
| 65 map:autoDrawRange(math.floor(tx), math.floor(ty), 1, 0) | |
| 66 map:draw() | |
| 67 for k, shadow in pairs(shadows) do | |
| 68 shadow:draw() | |
| 69 end | |
| 0 | 70 for k, sprite in pairs(sprites) do |
| 71 sprite:draw() | |
| 72 end | |
| 73 end | |
| 74 | |
| 75 | |
| 76 function love.update(dt) | |
| 2 | 77 local scroll_speed = 250 |
| 78 local scroll_margin = 64 | |
| 79 if love.keyboard.isDown("up") then villain:walk(dt, 1) end | |
| 80 if love.keyboard.isDown("right") then villain:walk(dt, 2) end | |
| 81 if love.keyboard.isDown("down") then villain:walk(dt, 3) end | |
| 82 if love.keyboard.isDown("left") then villain:walk(dt, 4) end | |
| 83 if villain.x + tx < scroll_margin then | |
| 84 tx = tx + dt * scroll_speed | |
| 85 else | |
| 86 if villain.x + tx > 800 - 64 - scroll_margin then | |
| 87 tx = tx - dt * scroll_speed | |
| 88 end | |
| 89 end | |
| 90 if villain.y + ty < scroll_margin then | |
| 91 ty = ty + dt * scroll_speed | |
| 92 else | |
| 93 if villain.y + ty > 600 - 64 - scroll_margin then | |
| 94 ty = ty - dt * scroll_speed | |
| 95 end | |
| 96 end | |
| 0 | 97 for k, sprite in pairs(sprites) do |
| 98 sprite:update(dt) | |
| 99 end | |
| 100 table.sort(sprites, function (a, b) return a.y < b.y end) | |
| 101 end |
