-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.lua
More file actions
231 lines (189 loc) · 4.71 KB
/
Copy pathtile.lua
File metadata and controls
231 lines (189 loc) · 4.71 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
--[[
This class is an outline for Tiles in the game
Tile is an outline that is meant to be extended
in other parts of the game
]]
Tile = table.copy(Base)
Tile.__index = Tile
Tile.x = 0
Tile.y = 0
Tile.texture = nil
Tile.map = nil
Tile.solidReal = false
Tile.solidGhost = false
Tile.isGhost = false
-- Move animation
Tile.moveDir = nil
Tile.faceDir = nil
Tile.moveX = 0
Tile.moveY = 0
--[[
Initializes a new Tile
params:
x - grid x-coordinate
y - grid y-coordinate
texture - the texture to draw on the specified tile
map - the map that the tile belongs to
isGhost - whether or not the Tile belongs to the GhostWorld
]]
function Tile:init(x, y, texture, map, isGhost)
self.x = x
self.y = y
self.texture = texture
self.map = map
self.isGhost = isGhost or false
self.faceDir = {x=1, y=0}
end
--[[
draws the Tile on the screen by invoking the draw function of the texture
component of the Tile
]]
function Tile:draw()
if self.texture then
self.texture:draw(self.x * TILEW + self.moveX, self.y * TILEH + self.moveY)
end
end
--[[
updates the location of the Tile
params:
x - x-location to update the tile to
y - y-location to update the tile to
]]
function Tile:setLocation(x, y)
self.x = x
self.y = y
return self
end
--[[
The following functions return various components of the Tile for use
elsewhere in the game
]]
function Tile:getX()
return self.x
end
function Tile:getY()
return self.y
end
function Tile:getMap()
return self.map
end
--[[
Sets whether the Tile is solid or not in the real world and ghost world
params:
realSolid - a value of "true" or "false" that tells you whether or not
the Tile is solid in the real world
ghostSolid - the same as realSolid but for the ghost world
]]
function Tile:setSolid(realSolid, ghostSolid)
self.solidReal = realSolid
self.solidGhost = ghostSolid
return self
end
--[[
returns "true" or "false" depending on whether or not the Tile is solid
in the real world
]]
function Tile:isRealSolid()
return self.solidReal
end
--[[
returns "true" or "false" depending on whether or not the Tile is solid
in the ghost world
]]
function Tile:isGhostSolid()
return self.solidGhost
end
--[[
params:
delta - the time passed between previous call to update
and this one
this makes calls to texture:draw depending on whether or not
the tile should be moving and stops its movement after it has
moved one tile
]]
function Tile:update(delta)
if self.moveDir then
self.moveX = self.moveX + self.moveDir.x * MOVESCALE * delta
self.moveY = self.moveY + self.moveDir.y * MOVESCALE * delta
if self.moveX * self.moveDir.x > 0 or self.moveY * self.moveDir.y > 0 then
self.moveX = 0
self.moveY = 0
self.moveDir = nil
removeUpdateTile(self)
end
end
end
--[[
Causes a Tile to move in a certain direction by one tile
params:
direction - 0 is right, 90 is up, 180 is left, 270 is down
All values passed in will be rounded to the nearest of the four
directions up
Ex: 135 will round up to 180 rather than down to 90
]]
function Tile:move(direction, notInMap)
if direction <= 360 then
direction = math.floor(direction / 90 + 0.5)
addUpdateTile(self)
local moveDir = {x=0, y=0}
if direction == 0 then
moveDir.x = 1
elseif direction == 1 then
moveDir.y = -1
elseif direction == 2 then
moveDir.x = -1
else
moveDir.y = 1
end
self.faceDir = moveDir
local newX = self.x + moveDir.x
local newY = self.y + moveDir.y
-- Player can't leave the map
if not self.map:tileAt(newX, newY, self.isGhost) then return false end
-- Solid tile is in the way
local solidTile = self.map:solidAt(newX, newY, self.isGhost)
if solidTile then
return false, solidTile
end
-- If the object is part of the map we will want to remove and replace the tile
-- spot it belongs to in the original map.
-- TODO: Find another way to detect objects that belong in the map.
if not notInMap then
self.map:removeTile(self.x, self.y, 2)
self.map:setTile(newX, newY, 2, self)
end
self.x = newX
self.y = newY
self.moveX = -moveDir.x*TILEW
self.moveY = -moveDir.y*TILEH
self.moveDir = moveDir
return true
else
self.moveDir = nil
removeUpdateTile(self)
return false
end
end
--[[
On player activate event.
player can be _G.player or _G.ghost
]]
function Tile:activate(player)
end
--[[
Event triggered when a player is over the tile.
player can be _G.player or _G.ghost
]]
function Tile:playerOn(player)
end
--[[
Event triggered when the player attempts to move into the object.
player can be _G.player or _G.ghost
return true if the player can push, false otherwise.
]]
function Tile:playerPush(player)
return false
end
include("tiles/player.lua")
include("tiles/boulder.lua")
include("tiles/toggle.lua")