-
Notifications
You must be signed in to change notification settings - Fork 2
/
character.lua
49 lines (42 loc) · 1.31 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
local Character = {}
local reparentCharacter = function(character, grid)
if character.displayObject then
character.displayObject.parent:remove(character.displayObject)
character.displayObject = nil
end
character.displayObject = display.newImageRect(grid.displayGroup,
character.imageName, grid.squareSize, grid.squareSize)
end
local characterFunctions = {
enter = function(character, gridSquare)
if character.gridSquare == nil or character.gridSquare.grid ~= gridSquare.grid then
reparentCharacter(character, gridSquare.grid)
end
if type(character.canEnter) == "function" and not character:canEnter(gridSquare) then
return
end
character.gridSquare = gridSquare
character.displayObject.x = gridSquare.displayObject.x
character.displayObject.y = gridSquare.displayObject.y
return character
end,
show = function(character)
if character.displayObject then
character.displayObject.isVisible = true
end
end,
hide = function(character)
if character.displayObject then
character.displayObject.isVisible = false
end
end,
}
Character.newCharacter = function(imageName)
local character = {}
character.imageName = imageName
character.enter = characterFunctions.enter
character.show = characterFunctions.show
character.hide = characterFunctions.hide
return character
end
return Character