summary refs log tree commit diff stats
path: root/res/scripts/common.lua
blob: 2023e63d329f778220bc319e073aec19a22d2ec5 (plain) (blame)
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
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
}

function StartCutscene()
  local playerId = getPlayerSprite()
  local playerSprite = getSprite(playerId)
  playerSprite.controllable = false
  character():halt(playerId)
end

function DisplayMessage(msg, name, type)
  message():displayMessage(msg, name, type)
end

function ShowChoice(one, two)
  message():showChoice(one, two)
end

function GetChoiceSelection()
  return message():getChoiceSelection()
end

function WaitForEndOfMessage()
  while (message().isMessageActive) do
    coroutine.yield()
  end
end

function HideCutsceneBars()
  WaitForEndOfMessage()
  message():hideCutsceneBars()

  local playerId = getPlayerSprite()
  local playerSprite = getSprite(playerId)
  playerSprite.controllable = true
end

function SetAnimation(spriteName, animName)
  local spriteId = getSpriteByAlias(spriteName)
  animation():setSpriteAnimation(spriteId, animName)
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)
  local progress = 0.0
  while progress < length do
    progress = progress + coroutine.yield()
    setFadeoutProgress(progress / length)
  end
  setFadeoutProgress(1.0)
end

function RemoveFadeout(length)
  local progress = length
  while progress > 0 do
    progress = progress - coroutine.yield()
    setFadeoutProgress(progress / length)
  end
  setFadeoutProgress(0.0)
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
  local oldState = playerSprite.characterState

  playerSprite.controllable = false
  FadeToBlack(150)
  loadMap("../res/maps/" .. map .. ".tmx", warp, direction)

  local newPlayerId = getPlayerSprite()
  local newPlayerSprite = getSprite(newPlayerId)
  if oldState == CharacterState.RUNNING then
    character():startRunning(newPlayerId)
  end

  coroutine.yield()
  RemoveFadeout(150)
  newPlayerSprite.controllable = true
end