/
character.lua
145 lines (125 loc) · 3.16 KB
/
character.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local map = require 'map'
local utils = require 'utils'
local M = {}
function M.loadAssets()
M.sprites = {}
M.sprites.shadow = love.graphics.newImage('images/character-shadow.png')
M.sprites.target = love.graphics.newImage('images/target.png')
end
function M.init(map)
M.map = map
end
function M.create(x, y, dir)
return {
pixelPos = {
x = x * M.map.options.tile.width,
y = y * M.map.options.tile.height,
},
pos = {
x = x,
y = y,
},
nextTile = {
x = x,
y = y,
},
dir = dir,
speed = 4,
moving = false,
}
end
function M.turn(character, dx, dy)
if dx > 0 then
character.dir = 'right'
end
if dx < 0 then
character.dir = 'left'
end
if dy > 0 then
character.dir = 'down'
end
if dy < 0 then
character.dir = 'up'
end
end
function M.move(character, dx, dy)
if not character.moving then
local nextX = character.pos.x + dx + 1
local nextY = character.pos.y + dy + 1
local tile = map.getTile(M.map, nextX, nextY)
character.nextTile = tile
M.turn(character, dx, dy)
if tile.walkable then
character.moving = true
end
end
end
local function moving(character, dt)
if character.moving and character.nextTile then
local tw = M.map.options.tile.width
local th = M.map.options.tile.height
local differenceX = character.nextTile.x * tw - character.pixelPos.x
local differenceY = character.nextTile.y * th - character.pixelPos.y
local movementX = 0
local movementY = 0
if differenceX > 0 then
movementX = 1
end
if differenceX < 0 then
movementX = -1
end
if differenceY > 0 then
movementY = 1
end
if differenceY < 0 then
movementY = -1
end
if not character.nextTile.walkable then
character.moving = false
return
end
if math.abs(differenceX) < 3 and math.abs(differenceY) < 3 then
character.pos.x = character.nextTile.x
character.pos.y = character.nextTile.y
character.pixelPos.x = character.nextTile.x * tw
character.pixelPos.y = character.nextTile.y * th
character.moving = false
return
end
character.pixelPos.x = character.pixelPos.x + movementX * dt * 10 * character.speed
character.pixelPos.y = character.pixelPos.y + movementY * dt * 10 * character.speed
end
end
function M.update(character, dt)
moving(character, dt)
end
local function draw(image, x, y, rot, w, h)
local rot = utils.deg2rad(rot)
local imgW,imgH = image:getDimensions()
local sx = w / imgW
local sy = h / imgH
local ox = imgW / 2
local oy = imgH / 2
love.graphics.draw(image, x+w/2 , y+h/2, rot, sx, sy, ox, oy)
end
function M.drawShadow(character)
local tw = M.map.options.tile.width
local th = M.map.options.tile.height
local x = character.pixelPos.x
local y = character.pixelPos.y
draw(M.sprites.shadow, x, y, 0, tw, th)
local tx = character.nextTile.x * tw
local ty = character.nextTile.y * th
love.graphics.setColor(255, 255, 255, 50)
draw(M.sprites.target, tx, ty, 0, tw, th)
love.graphics.setColor(255, 255, 255, 255)
end
function M.draw(character, image)
local tw = M.map.options.tile.width
local th = M.map.options.tile.height
local x = character.pixelPos.x
local y = character.pixelPos.y
local rot = utils.dir2rot(character.dir)
draw(image, x, y, rot, tw, th)
end
return M