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
|
SpeakerType = {
NONE = 0,
MAN = 1,
WOMAN = 2,
BOY = 3,
GIRL = 4,
NONHUMAN = 5
}
Direction = {
UP = 0,
UP_RIGHT = 1,
RIGHT = 2,
DOWN_RIGHT = 3,
DOWN = 4,
DOWN_LEFT = 5,
LEFT = 6,
UP_LEFT = 7
}
CharacterState = {
STILL = 0,
WALKING = 1,
CROUCHING = 2,
RUNNING = 3
}
SpriteLayer = {
NORMAL = 0,
ABOVE = 1
}
gamestate = {}
--- Yields until the specified amount of time has passed.
-- @param time in milliseconds
function Delay(time)
while time > 0 do
time = time - coroutine.yield()
end
end
--- Starts a cutscene.
-- This takes care of showing the cutscene bars, as well as halting character
-- movement. It does not block.
function StartCutscene()
local playerId = getPlayerSprite()
local playerSprite = getSprite(playerId)
playerSprite.controllable = false
character():halt(playerId)
message():displayCutsceneBars()
end
--- Queues a message for display.
-- Non-blocking! If you want to block until the message has been fully
-- revealed, use WaitForEndOfMessage().
-- @param msg the text of the message
-- @param name the name of the character speaking (leave blank to not show a character name)
-- @param type the SpeakerType for the character, which determines which beep sound is played, if any
function DisplayMessage(msg, name, type)
message():displayMessage(msg, name, type)
end
--- Queues a choice prompt for the player.
-- This should be called after DisplayMessage(), as it is not valid for a choice
-- prompt to be the first line of the text box. This also does not block. You
-- should use WaitForEndOfMessage() after this.
-- @param one the text of the first option
-- @param two the text of the second option
function ShowChoice(one, two)
message():showChoice(one, two)
end
--- Gets the result of the last choice prompt.
-- @return 0 for the left choice, 1 for the right choice
function GetChoiceSelection()
return message():getChoiceSelection()
end
--- Yields until all queued messages / choice prompts have been dismissed.
-- This does not hide the cutscene bars, nor does it wait for them to hide.
function WaitForEndOfMessage()
while (message().isMessageActive) do
coroutine.yield()
end
end
--- Hides the cutscene bars.
-- This also re-enables player movement.
function HideCutsceneBars()
WaitForEndOfMessage()
message():hideCutsceneBars()
local playerId = getPlayerSprite()
local playerSprite = getSprite(playerId)
playerSprite.controllable = true
end
function GetPosition(spriteName)
local spriteId = getSpriteByAlias(spriteName)
local sprite = getSprite(spriteId)
return sprite.loc
end
function SetDirection(spriteName, dir)
local spriteId = getSpriteByAlias(spriteName)
animation():setSpriteDirection(spriteId, dir)
end
function SetAnimation(spriteName, animName)
local spriteId = getSpriteByAlias(spriteName)
animation():setSpriteAnimation(spriteId, animName)
end
function WaitForAnimation(spriteName)
local spriteId = getSpriteByAlias(spriteName)
local sprite = getSprite(spriteId)
repeat
coroutine.yield()
until sprite.animFinished
end
function PlaySound(filename)
mixer():playSound("../res/sfx/" .. filename)
end
function LoopSound(filename)
return mixer():loopSound("../res/sfx/" .. filename)
end
function StopSound(soundId)
mixer():stopChannel(soundId)
end
function FadeToBlack(length)
effect():fadeScreen(length, 1.0)
repeat
coroutine.yield()
until effect():isScreenFadeComplete()
end
function RemoveFadeout(length)
effect():fadeScreen(length, 0.0)
repeat
coroutine.yield()
until effect():isScreenFadeComplete()
end
function FadeMap(length, amount)
effect():fadeMap(length, amount)
end
function WaitForMapFade()
while not effect():isMapFadeComplete() do
coroutine.yield()
end
end
function ShakeCamera(period)
effect():shakeCamera(period)
end
function StopShakingCamera()
effect():stopShakingCamera()
end
function PanToSprite(spriteName, length)
local spriteId = getSpriteByAlias(spriteName)
camera():panToSprite(spriteId, length)
end
function WaitForPan()
while camera():isPanning() do
coroutine.yield()
end
end
function ReturnCamera(length)
local playerId = getPlayerSprite()
camera():panToSprite(playerId, length)
while camera():isPanning() do
coroutine.yield()
end
camera():setFollowingSprite(playerId)
camera():unlockCamera()
end
function SetPartyDirection(spriteId, direction)
animation():setSpriteDirection(spriteId, direction)
local sprite = getSprite(spriteId)
for i=1,#sprite.followers do
animation():setSpriteDirection(sprite.followers[i], direction)
end
end
function ChangeMap(map, warp)
local playerId = getPlayerSprite()
local playerSprite = getSprite(playerId)
local direction = playerSprite.dir
playerSprite.controllable = false
FadeToBlack(150)
loadMap(map)
character():transplantParty(playerId, getWarpPoint(warp), direction)
coroutine.yield()
RemoveFadeout(150)
playerSprite.controllable = true
end
function CreateAnimatedSpriteAtPosition(alias, character, x, y, animName, direction, layer)
local spriteId = emplaceSprite(alias)
transform():initSprite(spriteId, x, y, layer)
animation():initSprite(spriteId, "../res/sprites/" .. character .. "_anim.txt")
animation():setSpriteDirection(spriteId, direction)
animation():setSpriteAnimation(spriteId, animName)
end
function DestroyNamedSprite(alias)
local spriteId = getSpriteByAlias(alias)
destroySprite(spriteId)
end
function AliasForSpriteExpression(spriteName)
return "expression (" .. spriteName .. ")"
end
function ShowExpression(spriteName, expression)
local spriteId = getSpriteByAlias(spriteName)
local sprite = getSprite(spriteId)
local animFrame = sprite:getCurrentFrame()
local x = sprite.loc:x()
local y = sprite.loc:y() - animFrame.center:y()
CreateAnimatedSpriteAtPosition(AliasForSpriteExpression(spriteName), "expression", x, y, expression, Direction.DOWN, SpriteLayer.ABOVE)
end
function RemoveExpression(spriteName)
DestroyNamedSprite(AliasForSpriteExpression(spriteName))
end
|