Skip to content

Instantly share code, notes, and snippets.

@rroa
Created May 14, 2014 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rroa/b0d7ba7b6463ed97d6f1 to your computer and use it in GitHub Desktop.
Save rroa/b0d7ba7b6463ed97d6f1 to your computer and use it in GitHub Desktop.
--[[
Author: Raúl Roa
Date : 4/16/2013
]]
--[[ GLOBALS ]]
--[[ Time Management ]]
timeSteps = 0
--[[ Filesystem Settings ]]
dialogsDir = "dialogs/"
--[[ UI Options ]]
--[[ Style ]]
backgroundImage = nil
backgroundImageOffsetX = 0
backgroundImageOffsetY = 0
rgbR = 1
rgbG = 1
rgbB = 1
--[[ Primary Dialog ]]
dialogText = nil
useTextFile = false
textFileName = nil
--[[ Alternate Dialog ]]
alternateDialogText = nil
useAlternateTextFile = false
altTextFileName = nil
--[[ Settings ]]
--General
dialogTime = 3
-- Style
fontType = "arial"
fontSize = 18
-- Multiple contacts
displayMax = 1
displayAfterNContacts = 0
totalDisplays = 0
displayOnFirstContact = false
ui = {
displayMax = { order = 1, type = "number", label = "Max Amount Of Displays" },
dialogTime = { order = 2, type = "number", label = "Time (secs) to be displayed" },
dialogText = { order = 3, type = "string", label = "Dialog Text" },
useTextFile = { order = 4, type = "bool", label = "Use Text File?" },
textFileName = { order = 5, type = "string", label = "Dialog File Name" },
displayAfterNContacts = { order = 6, type = "number", label = "Display Alternate Text after # contacts" },
alternateDialogText = { order = 7, type = "string", label = "Alternate Dialog Text" },
useAlternateTextFile = { order = 8, type = "bool", label = "Use Alternate Text File?" },
altTextFileName = { order = 9, type = "string", label = "Alternate Dialog File Name" },
displayOnFirstContact = { order = 10, type = "bool", label = "Display On First Contact?" },
fontType = { order = 11, type = "string", label = "Text Font Type" },
fontSize = { order = 12, type = "number", label = "Text Font Size" },
backgroundImage = { order = 13, type = "string", label = "Background Image" },
backgroundImageOffsetX = { order = 14, type = "number", label = "Background Offset X" },
backgroundImageOffsetY = { order = 15, type = "number", label = "Background Offset Y" },
rgbR = { order = 16, type = "number", label = "Red" },
rgbG = { order = 17, type = "number", label = "Green" },
rgbB = { order = 18, type = "number", label = "Blue" },
}
--[[ LOCALS ]]
local thisFont = nil
local active = false
local destroy = false
local playedOnce = false
local dialogContacts = 0
local lastHitTime = 0
local renderText = true
-- #
function init()
-- RR: Initializing the font
if( fontType == nil ) then
fontType = "arial"
end
thisFont = Font( fontType, fontSize )
--RR: Loading the main text from the file
if( useTextFile ) then
if( textFileName ~= nil and textFileName ~= "" ) then
dialogText = loadText( textFileName )
end
end
--RR: Loading the alt text from the file
if( useAlternateTextFile ) then
if( altTextFileName ~= nil and altTextFileName ~= "" ) then
alternateDialogText = loadText( altTextFileName )
end
end
--RR: Loading background image
if( ( backgroundImage ~= nil ) and ( backgroundImage ~= "" ) ) then
thisBackground = Image( backgroundImage )
end
--RR: Setting the dialog time
timeSteps = dialogTime
end
function beginContact( collidedWith )
if( collidedWith:isType( "Player" ) and not destroy ) then
if( g.activeDialog ~= this.id ) then
--RR: Only account for hits if there's certain time difference between them. 2 secs will be ok
if( g.os.clock() - lastHitTime > 2 ) then
lastHitTime = g.os.clock()
dialogContacts = dialogContacts + 1
end
--RR: Setting the alternate text
if( ( displayAfterNContacts > 0 ) and ( dialogContacts > 0 ) and ( dialogContacts % displayAfterNContacts == 0 ) ) then
renderText = true
if( alternateDialogText ~= nil and alternateDialogText ~= "" ) then
dialogText = alternateDialogText
end
elseif( ( displayAfterNContacts > 0 ) and ( displayOnFirstContact ) ) then
--RR: First contact now turn off. I don't want to come here again
displayOnFirstContact = false
renderText = true
dialogText = alternateDialogText
elseif ( displayAfterNContacts > 0 ) then
renderText = false
end
--RR: Can I render?
if( renderText ) then
--RR: Setup
g.activeDialog = this.id
playedOnce = true
timeSteps = dialogTime
active = true
totalDisplays = totalDisplays + 1
end
end
end
end
-- #1
function update( dt )
if( ( g.activeDialog ~= this.id ) or ( g.paused ) ) then
timeSteps = 0
active = false
end
--RR:Symphony of Destruction!
if( destroy ) then
this:destroy()
end
end
-- #2
function render( dt )
if( active ) then
local x = ( width() / 2 ) - ( g.player.iconW )
local y = ( height() / 2 ) - ( g.player.iconH )
--RR: if there's a background image then render it
if( thisBackground ~= nil ) then
thisBackground:draw( x - backgroundImageOffsetX, y - backgroundImageOffsetY, 0, Image_COORDS_SCREEN_TOPLEFT )
end
--RR: This will set the text color
drawLine( 0, 0, 0, 0, rgbR, rgbG, rgbB )
thisFont:draw( x, y, dialogText )
end
end
-- #3
function postUpdate( dt )
if( timeSteps <= 0 ) then
active = false
-- RR: Globally saying this dialog is no longer active
if( g.activeDialog == this.id ) then
g.activeDialog = nil
end
--RR: The lifecycle of this actor is over. Remove it.
if( ( playedOnce ) and ( displayMax > 0 ) and ( totalDisplays >= displayMax ) ) then
destroy = true
end
end
--RR: Only reduce the time when active
if( active ) then
timeSteps = timeSteps - dt
end
end
--EXTRA TOOLS
function loadText( path )
if( path ~= nil ) and ( dialogsDir ~= nil ) then
local filepath = dialogsDir .. path
--RR: Using custom function to load file contents
return openTextfile( filepath )
end
return nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment