Skip to content

Instantly share code, notes, and snippets.

@Patosito
Last active September 12, 2016 18:16
Show Gist options
  • Save Patosito/477cdcd1d7f493b4f993c6e451a62f47 to your computer and use it in GitHub Desktop.
Save Patosito/477cdcd1d7f493b4f993c6e451a62f47 to your computer and use it in GitHub Desktop.
Sistema de vidas
-- Ejemplo de sistema de vidas
-- Lives system example
_imgHearts = {"14d787f74b0","14d787f8754","14d787fa0e9"}
local playersAlive = 0
local playersInRoom = 0
local mice = {}
local isAlive
local idLives = {}
function main()
tfm.exec.disableAutoNewGame()
tfm.exec.disableAutoTimeLeft()
tfm.exec.disableAutoShaman()
tfm.exec.disableAfkDeath()
for name in pairs(tfm.get.room.playerList) do
eventNewPlayer(name)
end
startGame()
end
function startGame()
tfm.exec.newGame("#0")
end
function eventNewGame()
playersAlive = playersInRoom
isAlive = {}
for name,mouse in pairs(mice) do
isAlive[name] = true
mouse.lives = 3
updateLives(name, 3)
end
end
function endGame()
startGame()
end
function eventNewPlayer(name)
playersInRoom = playersInRoom + 1
mice[name] = {
lives = 0
}
end
function eventPlayerLeft(name)
playersInRoom = playersInRoom - 1
mice[name] = nil
-- A player can leave while still having lives remaining
if isAlive[name] then
setDeath(name)
end
end
function eventPlayerDied(name)
-- If their death is already set (e.g. by leaving the room), do nothing
if not isAlive[name] then return end
local mouse = mice[name]
mouse.lives = mouse.lives - 1
if mouse.lives == 0 then
setDeath(name)
else
tfm.exec.respawnPlayer(name)
end
end
function setDeath(name)
playersAlive = playersAlive - 1
isAlive[name] = nil
checkPlayers()
end
function eventPlayerRespawn(name)
local mouse = mice[name]
updateLives(name, mouse.lives)
end
function checkPlayers()
if playersAlive <= 1 then
endGame()
end
end
function updateLives(name, lives)
local id = idLives[name]
if id then tfm.exec.removeImage(id) end
idLives[name] = tfm.exec.addImage(_imgHearts[lives]..".png", "$"..name, -28, -40, name)
end
function eventLoop(t, tr)
if tr <= 0 then
endGame()
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment