SHARE
TWEET

Untitled

a guest Jun 27th, 2017 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. m_CurrentText = nil
  2. m_sCurrentTurn = nil
  3.  
  4. function onPlayerTurnStart(player_color_start, player_color_previous)
  5.     m_sCurrentTurn = player_color_start
  6. end
  7.  
  8. function onSave()
  9.     local sGUID = nil
  10.     if m_CurrentText ~= nil then
  11.         sGUID = m_CurrentText.getGUID()
  12.     end
  13.     return JSON.encode({sGUIDtext = sGUID})
  14. end
  15. function onLoad(save_state)
  16.     --Create buttons
  17.     local button_parameters = {}
  18.     button_parameters.rotation = {0.0, 0.0, 0.0}
  19.     button_parameters.width = 900
  20.     button_parameters.height = 320
  21.     button_parameters.function_owner = self
  22.  
  23.     --Accept Offer button
  24.     button_parameters.position = {-3.06, 0.1, 0.48}
  25.     button_parameters.label = 'Accept Offer'
  26.     button_parameters.click_function = 'respAccept'
  27.     button_parameters.font_size = 150
  28.     self.createButton( button_parameters )
  29.  
  30.     local SavedData = JSON.decode(save_state)
  31.     if SavedData ~= nil then
  32.         if SavedData.sGUIDtext ~= nil then
  33.             m_CurrentText = getObjectFromGUID(SavedData.sGUIDtext)
  34.         end
  35.     end
  36. end
  37.  
  38. function respAccept(Board, sPlayer)       displayResponse('I Accept!', sPlayer)                end
  39.  
  40. function displayResponse(sText, sPlayer)
  41.     --clear old text
  42.     if m_CurrentText ~= nil then
  43.         m_CurrentText.destruct()
  44.     end
  45.  
  46.     --spawn new response text
  47.     local vPos = self.getPosition()
  48.     --orient the text according to the orientation of the board
  49.     local vRot = {90.0, 180 + self.getRotation().y, 0.0} --yeah, I think this is weird too
  50.     if vRot[2] > 360.0 then
  51.         vRot[2] = vRot[2] - 360.0
  52.     end
  53.  
  54.     --rotate the text towards the active player -> see if we have to flip it to make it better readable for the active player
  55.     local bFlip = false
  56.     if m_sCurrentTurn ~= nil then
  57.         local fRotationDiff = Player[m_sCurrentTurn].getPlayerHand().rot_y - self.getRotation()[2]
  58.         if fRotationDiff < 0 then
  59.             fRotationDiff = 360.0 + fRotationDiff
  60.         end
  61.         if fRotationDiff > 270.0 or fRotationDiff < 90.0 then
  62.             bFlip = true
  63.         end
  64.     end
  65.  
  66.     --position the text above the board
  67.     local fR = math.rad(vRot[2])
  68.     local fSpace = 3.5
  69.  
  70.     if bFlip then
  71.         vRot[2] = vRot[2] + 180.0
  72.         fSpace = 2.2
  73.     end
  74.  
  75.     vPos.x = vPos.x + (math.sin(fR) * fSpace)
  76.     vPos.y = vPos.y + 0.2 --lift the text so it will not be covered by anything on the board
  77.     vPos.z = vPos.z + (math.cos(fR) * fSpace)
  78.  
  79.     local params = {
  80.         type = '3DText',
  81.         position = vPos,
  82.         rotation = vRot, --neither this nor setRotation/setRotationSmooth works ... only .rotate?!
  83.         callback_owner = self,
  84.         callback = 'newTextCallback',
  85.         params = {  sText = sText,
  86.                     sPlayer = sPlayer,
  87.                     iFontSize = 110,
  88.                     vRotation = vRot
  89.                 }
  90.     }
  91.     m_CurrentText = spawnObject(params)
  92.  
  93. end
  94.  
  95. --[[ This actually is needed because rotating a text is super weird. It only works in the callback of the object with the text tool ]]--
  96. function newTextCallback(TextObj, p)
  97.     TextObj.TextTool.setValue(p.sText)
  98.     TextObj.TextTool.setFontColor(getColor(p.sPlayer))
  99.     TextObj.TextTool.setFontSize(p.iFontSize)
  100.     TextObj.setRotation(p.vRotation)
  101. end
  102.  
  103. function clearText()
  104.     if m_CurrentText ~= nil then
  105.         m_CurrentText.destruct()
  106.     end
  107. end
  108.  
  109. function getColor(sPlayer)
  110.     if sPlayer == 'Orange' then
  111.         return {['r']=1.0, ['g']=0.544700563, ['b']=0.188231677}
  112.     elseif sPlayer == 'Red' then
  113.         return {['r']=0.7921569, ['g']=0.0, ['b']=0.0}
  114.     elseif sPlayer == 'White' then
  115.         return {['r']=0.99649936, ['g']=1.0, ['b']=0.996506333}
  116.     elseif sPlayer == 'Purple' then
  117.         return {['r']=0.5528258, ['g']=0.164702222, ['b']=1.0}
  118.     elseif sPlayer == 'Blue' then
  119.         return {['r']=0.149015814, ['g']=0.419941843, ['b']=0.8235495}
  120.     elseif sPlayer == 'Green' then
  121.         return {['r']=0.201173037, ['g']=0.8549019, ['b']=0.188231409}
  122.     elseif sPlayer == 'Black' then
  123.         return {['r']=0.0, ['g']=0.0, ['b']=0.0}
  124.     else
  125.         return {['r']=1.0, ['g']=0.8, ['b']=0.0}
  126.     end
  127. end
  128.  
  129. --reset the response text when the turn is ended
  130. function onPlayerTurnEnd(player_color_end, player_color_next)
  131.     clearText()
  132. end
  133.  
  134. --reset the response when the board is moved
  135. function onPickUp(player_color)
  136.     clearText()
  137. end
  138.  
  139. --reset the response when the board is removed
  140. function onDestroy()
  141.     clearText()
  142. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top