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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
1 local ATL = require("AdvTiledLoader")
0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2 local anim8 = require("anim8")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
3 local sprite = require("sprite")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
4
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
5 local sprites = {}
2
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
6 local shadows = {}
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
7 local map
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
8 local tx, ty = 0, 0
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
9 local DX = {0, 1, 0, -1}
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
10 local DY = {-1, 0, 1, 0}
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
11 local villain
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
12
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
13 function newVillain(x, y, image)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
14 local grid = anim8.newGrid(64, 64, 128, 128)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
15 local villain = sprite.newSprite(
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
16 x, y,
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
17 image,
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
18 anim8.newAnimation('loop', grid('1-2,1'), 0.25)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
19 )
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
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
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
29 end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
30 return villain
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
31 end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
32
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
33 function newCivilian(x, y, image)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
34 local grid = anim8.newGrid(64, 64, 128, 128)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
35 local civilian = sprite.newSprite(
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
36 x, y,
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
37 image,
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
38 anim8.newAnimation('loop', grid('1-2,1'), 0.25)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
39 )
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
40 return civilian
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
41 end
0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
42
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
43 function love.load()
2
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
44 local shadow_image = love.graphics.newImage('shadow.png')
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
45 local civilian_image = love.graphics.newImage('civilian.png')
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
46 local villain_image = love.graphics.newImage('villain.png')
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
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
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
60 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
61
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
62
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
63 function love.draw()
2
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
64 love.graphics.translate(math.floor(tx), math.floor(ty))
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
65 map:autoDrawRange(math.floor(tx), math.floor(ty), 1, 0)
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
66 map:draw()
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
67 for k, shadow in pairs(shadows) do
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
68 shadow:draw()
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
69 end
0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
70 for k, sprite in pairs(sprites) do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
71 sprite:draw()
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
72 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
73 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
74
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
75
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
76 function love.update(dt)
2
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
77 local scroll_speed = 250
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
78 local scroll_margin = 64
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
79 if love.keyboard.isDown("up") then villain:walk(dt, 1) end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
80 if love.keyboard.isDown("right") then villain:walk(dt, 2) end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
81 if love.keyboard.isDown("down") then villain:walk(dt, 3) end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
82 if love.keyboard.isDown("left") then villain:walk(dt, 4) end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
83 if villain.x + tx < scroll_margin then
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
84 tx = tx + dt * scroll_speed
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
85 else
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
86 if villain.x + tx > 800 - 64 - scroll_margin then
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
87 tx = tx - dt * scroll_speed
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
88 end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
89 end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
90 if villain.y + ty < scroll_margin then
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
91 ty = ty + dt * scroll_speed
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
92 else
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
93 if villain.y + ty > 600 - 64 - scroll_margin then
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
94 ty = ty - dt * scroll_speed
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
95 end
0c46edeabad6 scrolling map
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
96 end
0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
97 for k, sprite in pairs(sprites) do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
98 sprite:update(dt)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
99 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
100 table.sort(sprites, function (a, b) return a.y < b.y end)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
101 end