-
Notifications
You must be signed in to change notification settings - Fork 354
/
entity.lua
370 lines (328 loc) · 12.7 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
--[[ Copyright (c) 2009 Peter "Corsix" Cawley
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
--! Abstraction for visible gameplay things which sit somewhere on the map.
class "Entity"
---@type Entity
local Entity = _G["Entity"]
local TH = require("TH")
function Entity:Entity(animation)
self.th = animation
self.layers = {}
animation:setHitTestResult(self)
self.ticks = true
self.playing_sounds_in_random_sequence = false
self.waiting_for_sound_effects_to_be_turned_on = false
self.random_sound_sequence_parameters = nil
self.dynamic_info = nil
end
--[[
This plays a sound "at" the entity, meaning the sound will not be played
if the entity is off-screen, and the volume will be quieter the further
the entity is from the center of the screen. If this is not what you want
then use UI:playSound instead.
!param name (string, integer) The filename or ordinal of the sound to play.
!param played_callback (function) a optional parameter.
!param played_callback_delay (integer) a optional milliseconds parameter.
--]]
function Entity:playSound(name, played_callback, played_callback_delay)
if TheApp.config.play_sounds then
TheApp.audio:playSound(name, self, false, played_callback, played_callback_delay)
end
end
function Entity:setWaitingForSoundEffectsToBeTurnedOn()
self.waiting_for_sound_effects_to_be_turned_on = true
end
--[[
Plays a sequence of related sounds at an entity while entity.playing_sounds_in_random_sequence = true.
The silences between these sounds can either have randomly generated lengths between the min and max length parameters or
they can all be a specified length by providing min and max tables with one value for the desired pause duration.
!param name_pattern (String) example: LAVA00*.WAV
!param min_silence_lengths (table) the desired minimum silences length for the different tick rates, [3] = Normal
!param max_silence_lengths (table) the desired maximum silences length for the different tick rates, [3] = Normal
!param num_silences (integer) how many different silence lengths should be used, this can be a nil parameter.
-]]
function Entity:playEntitySounds(name_pattern, min_silence_lengths, max_silence_lengths, num_silences)
self.playing_sounds_in_random_sequence = true
self.random_sound_sequence_parameters = {}
self.random_sound_sequence_parameters["namePattern"] = name_pattern
self.random_sound_sequence_parameters["minSilence"] = min_silence_lengths
self.random_sound_sequence_parameters["maxSilence"] = max_silence_lengths
self.random_sound_sequence_parameters["numSilences"] = num_silences
TheApp.audio:playEntitySounds(name_pattern, self,
min_silence_lengths, max_silence_lengths, num_silences)
end
--[[ Set which animation is used to give the entity a visual appearance.
! Until an entity is given an animation, it is invisible to the player. Note
that some "animations" consist of a single frame, and hence the term animation
is used both to mean things which are animated and things which are static.
!param animation (integer) The ordinal into the main animation set
!param flags (integer) A combination of zero or more drawing flags to control
the use of alternative palettes, transparency, and other similar settings. See
`THDF_` values in `th_gfx.h` for the possible bit values.
]]
function Entity:setAnimation(animation, flags)
flags = flags or 0
if self.permanent_flags then
flags = flags + self.permanent_flags
end
if animation ~= self.animation_idx or flags ~= self.animation_flags then
self.animation_idx = animation
self.animation_flags = flags
self.th:setAnimation(self.world.anims, animation, flags)
end
return self
end
--[[ Set the map tile which the entity is on.
!param x (integer) The 1-based X coordinate of the tile.
!param y (integer) The 1-based Y coordinate of the tile.
]]
function Entity:setTile(x, y)
if self.user_of then
print("Warning: Entity tile changed while marked as using an object")
end
local entity_map = self.world.entity_map
-- Remove the reference to the entity at it's previous coordinates
if entity_map then
entity_map:removeEntity(self.tile_x,self.tile_y, self)
end
self.tile_x = x
self.tile_y = y
self.th:setDrawingLayer(self:getDrawingLayer())
-- NB: (x, y) can be nil, in which case th:setTile expects all nil arguments
self.th:setTile(x and self.world.map.th, x, y)
if self.mood_info then
self.mood_info:setParent(self.th)
end
-- Update the entity map for the new position
if entity_map then
entity_map:addEntity(x,y,self)
end
return self
end
function Entity:getRoom()
if self.tile_x and self.tile_y then
return self.world:getRoom(self.tile_x, self.tile_y)
else
return nil
end
end
--[[ Set the pixel position of the entity within the current tile.
!param x (integer) The 0-based X pixel offset from the default position.
!param y (integer) The 0-based Y pixel offset from the default position.
]]
function Entity:setPosition(x, y)
self.th:setPosition(x, y)
return self
end
--[[ Set the rate at which the entity pixel position changes
!param x (integer) The X component of the speed in pixels per tick.
!param y (integer) The Y component of the speed in pixels per tick.
]]
function Entity:setSpeed(x, y)
self.th:setSpeed(x, y)
return self
end
--[[ Combined form of `setTile`, `setPosition`, and `setSpeed`
!param tx (integer) The 1-based X coordinate of the tile.
!param ty (integer) The 1-based Y coordinate of the tile.
!param px (integer) The 0-based X pixel offset from the default position.
!param py (integer) The 0-based Y pixel offset from the default position.
!param sx (integer) The X component of the speed in pixels per tick.
!param sy (integer) The Y component of the speed in pixels per tick.
]]
function Entity:setTilePositionSpeed(tx, ty, px, py, sx, sy)
self:setTile(tx, ty)
self:setPosition(px or 0, py or 0)
self:setSpeed(sx or 0, sy or 0)
return self
end
-- Inner tick function that will skip every other tick when
-- slow_animation is set.
function Entity:_tick()
if self.slow_animation then
if not self.skip_next_tick then
self.th:tick()
end
self.skip_next_tick = not self.skip_next_tick
else
self.th:tick()
end
end
-- Function which is called once every tick, where a tick is the smallest unit
-- of time in the game engine. Typically used to advance animations and similar
-- recurring or long-duration tasks.
function Entity:tick()
if self.num_animation_ticks then
for _ = 1, self.num_animation_ticks do
self:_tick()
end
if self.num_animation_ticks == 1 then
self.num_animation_ticks = nil
end
else
self:_tick()
end
-- Tick any mood animation
if self.mood_info then
self.mood_info:tick()
end
local timer = self.timer_time
if timer then
timer = timer - 1
if timer == 0 then
self.timer_time = nil
local timer_function = self.timer_function
self.timer_function = nil
timer_function(self)
else
self.timer_time = timer
end
end
end
--[[ Set which parts of the animation to be displayed.
! Each animation is made up of 13 layers, and generally has several different
options for each layer. In the animation viewer tool, multiple options can be
toggled on for each layer, however here only one option may be selected per
layer.
!param layer (integer) The layer to set (1 through 13).
!param id (integer) The option to display for the given layer.
]]
function Entity:setLayer(layer, id)
self.th:setLayer(layer, id)
self.layers[layer] = id
return self
end
--[[ Register a function (related to the entity) to be called at a later time.
! Each `Entity` can have a single timer associated with it, and due to this
limit of one, it is almost always the case that the currently active humanoid
action is the only thing which calls `setTimer`.
If self.slow_animation is set then all timers will be doubled as animation
length will be doubled.
!param tick_count (integer) If 0, then `f` will be called during the entity's
next `tick`. If 1, then `f` will be called one tick after that, and so on.
!param f (function) Function which takes a single argument (the entity).
]]
function Entity:setTimer(tick_count, f)
self.timer_time = tick_count
self.timer_function = f
if self.slow_animation and tick_count then
self.skip_next_tick = true
self.timer_time = tick_count * 2
end
end
-- Used to set a mood icon over the entity.
function Entity:setMoodInfo(new_mood)
if new_mood then
if not self.mood_info then
self.mood_info = TH.animation()
self.mood_info:setPosition(-1, -96)
self.mood_info:setParent(self.th)
end
self.mood_info:setAnimation(self.world.anims, new_mood.icon)
else
if self.mood_info then
self.mood_info:setTile(nil)
end
self.mood_info = false
end
end
-- Function which is called when the entity is to be permanently removed from
-- the world.
function Entity:onDestroy()
-- Clear any mood
self:setMoodInfo()
self:setTile(nil)
self.world.dispatcher:dropFromQueue(self)
-- Debug aid to check that there are no hanging references after the entity
-- has been destroyed:
--[[
self.gc_dummy = newproxy(true) -- undocumented Lua library function
getmetatable(self.gc_dummy).__gc = function()
print("Entity " .. tostring(self) .. " has been garbage collected.")
end --]]
if self.waiting_for_sound_effects_to_be_turned_on then
TheApp.audio:entityNoLongerWaitingForSoundsToBeTurnedOn(self)
end
end
-- Function which is called at the end of each ingame day. Should be used to
-- implement behaviours which happen regularly, but not as frequently as to
-- need them in `tick`.
function Entity:tickDay()
end
--! Notify the entity of a new object.
--!param id Name of the new object.
-- TODO Currently only used for benches placed nearby.
-- TODO Maybe also pass the object tile coordinates?
function Entity:notifyNewObject(id)
end
--! Notify the entity that a new room was built.
--!param room (Room) The new room.
function Entity:notifyNewRoom(room)
end
function Entity:setMood(mood_name, activate)
end
-- Returns a table of hover info about an object.
function Entity:getDynamicInfo()
return self.dynamic_info
end
-- Sets a piece of Dynamic_info.
-- type could be 'text', 'progress' or 'dividers'
function Entity:setDynamicInfo(type, value)
if not self.dynamic_info then
self.dynamic_info = {
text = nil,
progress = nil,
dividers = nil,
}
end
self.dynamic_info[type] = value
end
-- Completely clears the dynamic info.
function Entity:clearDynamicInfo()
self.dynamic_info = nil
end
--! Stub to be extended in subclasses, if needed.
function Entity:afterLoad(old, new)
end
function Entity:playAfterLoadSound()
if self.random_sound_sequence_parameters then
self.playing_sounds_in_random_sequence = true
self:playEntitySounds(self.random_sound_sequence_parameters["namePattern"],
self.random_sound_sequence_parameters["minSilence"],
self.random_sound_sequence_parameters["maxSilence"],
self.random_sound_sequence_parameters["numSilences"])
end
end
--! Stub to be extended in subclasses, if needed.
function Entity:eraseObject()
-- Give entity the chance to clear itself from the map before resetAnimation gets called.
end
function Entity:resetAnimation()
self.th:setDrawingLayer(self:getDrawingLayer())
local x, y = self.tile_x, self.tile_y
self.th:setTile(x and self.world.map.th, x, y)
end
--[[
Returns the drawing layer for this particular entity. Should be overridden in derived classes. The drawing layer
specifies the order in which object are drawn in a tile (the object with the smallest layer is drawn first).
Litter should have layer 0, side objects to the north layer 1, side objects to the west layer 2,
normal objects should have layers between 3 and 7, east side object should have layer 8 and south side
objects layer 9.
]]
function Entity:getDrawingLayer()
return 4
end