-
Notifications
You must be signed in to change notification settings - Fork 0
/
hero.lua
76 lines (56 loc) · 1.77 KB
/
hero.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
local hero = {}
hero.images = {}
hero.images[1] = love.graphics.newImage("images/player_1.png")
hero.images[2] = love.graphics.newImage("images/player_2.png")
hero.images[3] = love.graphics.newImage("images/player_3.png")
hero.images[4] = love.graphics.newImage("images/player_4.png")
hero.imgCurrent = 1
hero.line = 1
hero.column = 1
hero.keyPressed = false
function hero.animateSprite(dt)
hero.imgCurrent = hero.imgCurrent + (5 * dt)
if math.floor(hero.imgCurrent) > #hero.images then
hero.imgCurrent = 1
end
end
function hero.move(pMap)
if love.keyboard.isDown("up","right","down","left") then
if hero.keyPressed == false then
local old_column = hero.column
local old_line = hero.line
if love.keyboard.isDown("up") and hero.line > 1 then
hero.line = hero.line - 1
end
if love.keyboard.isDown("right") and hero.column < pMap.MAP_WIDTH then
hero.column = hero.column + 1
end
if love.keyboard.isDown("down") and hero.line < pMap.MAP_HEIGHT then
hero.line = hero.line + 1
end
if love.keyboard.isDown("left") and hero.column > 1 then
hero.column = hero.column - 1
end
local id = pMap.Grid[hero.line][hero.column]
if pMap.isSolid(id) then
print("collision with a solid tile")
hero.column = old_column
hero.line = old_line
end
hero.keyPressed = true
end
else
pMap.ClearFog(hero.line, hero.column)
hero.keyPressed = false
end
end
function hero.Update(pMap, dt)
hero.animateSprite(dt)
hero.move(pMap)
end
function hero.Draw(pMap)
local x = (hero.column -1) * pMap.TILE_WIDTH
local y = (hero.line -1) * pMap.TILE_HEIGHT
love.graphics.draw(hero.images[math.floor(hero.imgCurrent)], x, y, 0, 2, 2)
end
return hero