-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharacter.lua
More file actions
234 lines (199 loc) · 6.55 KB
/
Copy pathcharacter.lua
File metadata and controls
234 lines (199 loc) · 6.55 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require "assets"
local sodapop = require "libs/sodapop/sodapop"
local Object = require "libs/classic/classic"
local ActionMenu = require "actionmenu"
local ActionMap = require "actionmap"
local Attack = require "attack"
Character = Object:extend()
function Character.loadCharFromScript(charName, map, x, y)
local character = Character(map, x, y)
local fileName = "assets/scripts/chars/"..charName:lower()..".char"
local content, _ = love.filesystem.read(fileName)
local lines = content:split('\n')
for i, line in ipairs(lines) do
local parts = line:split(':')
local key = parts[1]:lower()
local value = parts[2]:trim()
if key=='name' then character.name = value
elseif key=='hp' then character.HP = tonumber(value); character.originalHP = tonumber(value)
elseif key=='mp' then character.MP = tonumber(value); character.originalMP = tonumber(value)
elseif key=='speed' then character.speed = tonumber(value)
elseif key=='movement' then character.movement = tonumber(value)
elseif key=='attack' then character.attack = tonumber(value)
elseif key=='damage' then character.damage = tonumber(value)
elseif key=='portrait' then character.portrait = love.graphics.newImage(value)
elseif key=='avatar' then character.avatar = love.graphics.newImage(value)
elseif key:starts('animation') and map ~= nil then
local subparts = key:split('.')
local func = assert(loadstring("return " .. value))
character.sprite:addAnimation(subparts[2], func())
end
end
if map then
character:createMaps()
end
return character
end
function Character:new(map, x, y)
self.highlighted = false
self.selected = false
self.moving = false -- show move map
self.attacking = false -- show attack map
self.moved = false
self.attacked = false
if map ~= nil then
self:setMap(map, x, y)
end
end
function Character:setMap(map, x, y)
self.map = map
self.tileX = math.floor(x / self.map.tilewidth)
self.tileY = math.floor(y / self.map.tileheight)
self.x = self.tileX * self.map.tilewidth
self.y = self.tileY * self.map.tilewidth
self.sprite = sodapop.newAnimatedSprite(self.x + map.tilewidth / 2, self.y + map.tileheight / 2)
self.actionMenu = ActionMenu(self, 4, 4, self.map.tilewidth, self.map.tileheight)
end
function Character:createMaps()
self.moveMap = ActionMap(self.movement, self.tileX, self.tileY, self.map, {0, 255, 255, 64}, {0, 255, 255, 100})
self.attackMap = ActionMap(self.attack, self.tileX, self.tileY, self.map, {255, 0, 0, 64}, {255, 0, 0, 100})
end
function Character:update(dt)
if self.sprite == nil then
return
end
self.sprite:update(dt)
if self.attackAnimation ~= nil then
self.attackAnimation:update(dt)
end
end
function Character:draw(ox, oy)
if self.sprite == nil then
return
end
if not self:dead() then
if self.highlighted then
self:drawHighlight(ox, oy)
end
if self.moving then
self.moveMap:draw(ox, oy)
elseif self.attacking then
self.attackMap:draw(ox, oy)
end
self.sprite:draw(ox, oy)
end
if self.attackAnimation ~= nil then
self.attackAnimation:draw(ox, oy)
end
end
function Character:moveTo(tileX, tileY)
self.tileX = tileX
self.tileY = tileY
self.x = tileX * self.map.tilewidth
self.y = tileY * self.map.tileheight
self.sprite.x = self.x + self.map.tilewidth / 2
self.sprite.y = self.y + self.map.tileheight / 2
self:resetUI()
self.actionMenu:move(self.x, self.y)
self.actionMenu:select(2)
end
function Character:drawHighlight(ox, oy)
local r, g, b, a = love.graphics.getColor()
love.graphics.setColor(255, 0, 0, 64)
love.graphics.rectangle('fill', self.x - ox, self.y - oy,
self.map.tilewidth, self.map.tileheight,
self.map.tilewidth / 4, self.map.tileheight / 4)
love.graphics.setColor(255, 0, 0, 100)
love.graphics.rectangle('line', self.x - ox, self.y - oy,
self.map.tilewidth, self.map.tileheight,
self.map.tilewidth / 4, self.map.tileheight / 4)
love.graphics.setColor(r, g, b, a)
end
-- TODO: move this function somewhere else
function Character:charHit(x, y)
for entity, v in pairs(manager:getEntities()) do
if x == entity.tileX and y == entity.tileY then
return entity
end
end
return nil
end
function Character:moveToTarget()
if self:actionMap().target.x == self.tileX and self:actionMap().target.y == self.tileY then
return
end
local hit = self:charHit(self:actionMap().target.x, self:actionMap().target.y)
if hit and not hit:dead() then
return
end
self:moveTo(self:actionMap().target.x, self:actionMap().target.y)
self.moving = false
self.moved = true
end
function Character:attackTarget()
if self:actionMap().target.x == self.tileX and self:actionMap().target.y == self.tileY then
return
end
local hit = self:charHit(self:actionMap().target.x, self:actionMap().target.y)
if not hit then
return
end
self.attacking = false
self.attacked = true
self.actionMenu:select(1)
hit.HP = hit.HP - self.damage
hit:newAttackAnimation()
end
function Character:newAttackAnimation()
self.attackAnimation = Attack(self.x + 16, self.y + 16)
end
function Character:dead()
return self.HP <= 0
end
function Character:turnDone()
return (self.moved and self.attacked) or self:dead()
end
function Character:skip()
self.highlighted = false
self.moving = false
self.attacking = false
self.moved = true
self.attacked = true
self.selected = false
end
function Character:action()
return self.actionMenu.line
end
function Character:acting()
return self.moving or self.attacking
end
function Character:unselect()
self.selected = false
self.attacking = false
self.moving = false
self.moveMap:setTarget(self.tileX, self.tileY)
self.attackMap:setTarget(self.tileX, self.tileY)
end
function Character:actionMap()
if self.moving then
return self.moveMap
elseif self.attacking then
return self.attackMap
end
end
function Character:reset()
self.selected = false
self.moving = false
self.attacking = false
self.moved = false
self.attacked = false
self:resetUI()
end
function Character:resetUI()
self.actionMenu:select(1)
self.moveMap:move(self.tileX, self.tileY)
self.attackMap:move(self.tileX, self.tileY)
self.moveMap:setTarget(self.tileX, self.tileY)
self.attackMap:setTarget(self.tileX, self.tileY)
end
return Character