-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.lua
137 lines (116 loc) · 3.65 KB
/
entity.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
local pd <const> = playdate
local gfx <const> = pd.graphics
class('Entity').extends(gfx.sprite)
local tileSize = 32
function Entity:init(imagePath)
Entity.super.init(self)
self.entityImage = gfx.image.new(imagePath)
self:setImage(self.entityImage)
self:setCenter(0, 0)
self.flipped = false
self.moveAnimator = nil
self.map = nil
self.gridX = 1
self.gridY = 1
self.bonusAttack = 0
self.hitSound = pd.sound.sample.new("sound/hit")
end
function Entity:getTruePosition()
local trueX = (self.gridX - 1) * tileSize + 8
local trueY = (self.gridY - 1) * tileSize + 32
return trueX, trueY
end
function Entity:damage(amount)
self.health -= amount
end
function Entity:update()
Entity.super.update(self)
if self.moveAnimator then
local animationX = self.moveAnimator:currentValue().x
local animationY = self.moveAnimator:currentValue().y
self:moveTo(animationX, animationY)
if self.moveAnimator:ended() then
self.takeInput = true
end
end
end
function Entity:moveEntityBy(gridX, gridY)
if self.flipped then
self:setImage(self.entityImage, gfx.kImageFlippedX)
else
self:setImage(self.entityImage)
end
local curX, curY = self:getTruePosition()
local curPos = pd.geometry.point.new(curX, curY)
local moveTime = 100
local moveEasingFunction = pd.easingFunctions.inOutQuad
local validSpace = self:checkValidSpace(gridX, gridY)
local targetX = self.gridX + gridX
local targetY = self.gridY + gridY
local entityAtTarget = self.map:getEntity(targetX, targetY)
if not validSpace or entityAtTarget then
local outX = curX + gridX * tileSize / 2
local outY = curY + gridY * tileSize / 2
local outLine = pd.geometry.lineSegment.new(curX, curY, outX, outY)
self.moveAnimator = gfx.animator.new(moveTime, outLine, moveEasingFunction)
self.map:destruct(targetX, targetY)
if self:isa(Player) then
if not validSpace then
screenShake(1)
else
self.hitSound:play()
screenShake(3)
end
end
if entityAtTarget then
entityAtTarget:damage(self.attack + self.bonusAttack)
self.attackedThisTurn = true
self.bonusAttack = 0
end
return false
end
self.map.entityMatrix[self.gridX][self.gridY] = nil
self.gridX += gridX
self.gridY += gridY
self.map.entityMatrix[self.gridX][self.gridY] = self
local x = gridX * tileSize
local y = gridY * tileSize
local newPos = pd.geometry.point.new(curX + x, curY + y)
self.moveAnimator = gfx.animator.new(moveTime, curPos, newPos, moveEasingFunction)
return true
end
function Entity:setMap(map)
self.map = map
self.map.entityMatrix[self.gridX][self.gridY] = self
end
function Entity:checkOutOfBounds(gridX, gridY)
local targetX = self.gridX + gridX
local targetY = self.gridY + gridY
if targetX <= 0 then
return true
elseif targetX > self.map.maxX then
return true
elseif targetY <= 0 then
return true
elseif targetY > self.map.maxY then
return true
end
return false
end
function Entity:checkValidSpace(gridX, gridY)
local targetX = self.gridX + gridX
local targetY = self.gridY + gridY
if targetX <= 0 then
return false
elseif targetX > self.map.maxX then
return false
elseif targetY <= 0 then
return false
elseif targetY > self.map.maxY then
return false
end
if self.map:isDestructible(targetX, targetY) then
return false
end
return true
end