Advertisement
Guest User

Teverse: basic camera with axel

a guest
Apr 8th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. --Title: Basic Camera with Axel
  2. --By: utrain
  3. --Description: Allows for basic camera movements
  4. --It uses bindKeyToPressed from this repo: https://github.com/u-train/utilityKeyBindfunctions-Teverse/blob/master/bindUtility.lua
  5. --However, it has been pasted since require isn't available for scripts on the machine on verison 0.19.0.
  6. --Intended to work on a Teverse environment on 0.19.0
  7.  
  8. local function bindKeyToPressed(keyName, functionOnPressed, functionOnRelease)
  9.     --Make sure the function doesn't error because of missing table field; give best feedback.
  10.     assert(keyName, "keyName argument is nil.")
  11.     local enumKey = enums.key[keyName]
  12.    
  13.     assert(enumKey, "keyName is not a vaild argument.")
  14.     assert(type(functionOnPressed) == "function", "functionOnPressed is not a vaild argument.")
  15.  
  16.     --functionOnRelease is an optional argument
  17.     if functionOnRelease then
  18.         assert(type(functionOnRelease) == "function", "functionOnRelease is not a vaild argument.")
  19.      end
  20.  
  21.     local debounce = false
  22.  
  23.     local onKeyPressConnection = engine.input:on("keyPressed", function(inputObject)
  24.         if debounce then return end
  25.         if inputObject.key == enumKey then
  26.             debounce = true
  27.  
  28.             functionOnPressed(inputObject)
  29.          end
  30.     end)
  31.  
  32.     local onKeyReleaseConnection = engine.input:on("keyReleased", function(inputObject)
  33.         if not debounce then return end
  34.         if inputObject.key == enumKey then
  35.             debounce = false
  36.  
  37.             if functionOnRelease then
  38.                 functionOnRelease(inputObject)
  39.             end
  40.         end
  41.     end)
  42.  
  43.     --listOfDeconstructor
  44.     return function()
  45.         onKeyPressConnection:disconnect()
  46.         onKeyPressConnection = nil
  47.    
  48.         onKeyReleaseConnection:disconnect()
  49.         onKeyReleaseConnection = nil
  50.     end
  51. end
  52.  
  53. --Objects
  54. --The axel of life.
  55. local Center;
  56. local Blocks = {};
  57. do
  58.     Blocks[1] = engine.construct("block", engine.workspace, {
  59.         position = vector3(0,0,0);
  60.         size = vector3(2,0.1,0.1);
  61.         colour = colour(1,0,0);
  62.     })
  63.  
  64.     Blocks[2] = engine.construct("block", engine.workspace, {
  65.         position = vector3(0,0,0);
  66.         size = vector3(0.1,2,0.1);
  67.         colour = colour(0,1,0);
  68.     })
  69.  
  70.     Blocks[3]  = engine.construct("block", engine.workspace, {
  71.         position = vector3(0,0,0);
  72.         size = vector3(0.1,0.1,2);
  73.         colour = colour(0,0,1);
  74.     })
  75.  
  76.     Center = engine.construct("block", engine.workspace, {
  77.         position = vector3(0,0,0);
  78.         size = vector3(0.1,0.1,0.1);
  79.         opacity = 0;
  80.     })
  81. end
  82.  
  83. --Textbox informing player of coordinates
  84. local CoordinateBox = engine.construct("guiTextBox", engine.interface, {
  85.     size = guiCoord(1, 0, 0, 25);
  86.     textColour = colour(0, 0, 0);
  87. })
  88.  
  89. local camera = engine.workspace.camera
  90.  
  91. --Stores the deconstructors for the binds.
  92. local listOfDeconstructor = {}
  93.  
  94. --Whenever camera positions moves, it will run this.
  95. local function Update()
  96.     camera:lookAt(Center.position)
  97.     CoordinateBox.text = ("Position: %s, %s, %s; press 'C' to clear everything."):format(camera.position.x, camera.position.y, camera.position.z)
  98. end
  99.  
  100. --Start it.
  101. camera.position = vector3(2, 2, 2)
  102. Update()
  103.  
  104. --All of the hotkeys.
  105. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("d", function()
  106.     camera.position = vector3(camera.position.x + 0.5, camera.position.y, camera.position.z)
  107.     Update()
  108. end)
  109. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("a", function()
  110.     camera.position = vector3(camera.position.x - 0.5, camera.position.y, camera.position.z)
  111.     Update()
  112. end)
  113. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("w", function()
  114.     camera.position = vector3(camera.position.x, camera.position.y, camera.position.z + 0.5)
  115.     Update()
  116. end)
  117. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("s", function()
  118.     camera.position = vector3(camera.position.x, camera.position.y, camera.position.z - 0.5)
  119.     Update()
  120. end)
  121. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("q", function()
  122.     camera.position = vector3(camera.position.x, camera.position.y + 0.5, camera.position.z )
  123.     Update()
  124. end)
  125. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("e", function()
  126.     camera.position = vector3(camera.position.x, camera.position.y - 0.5, camera.position.z)
  127.     Update()
  128. end)
  129.  
  130. --To clear everything afterwards
  131. listOfDeconstructor[#listOfDeconstructor + 1] = bindKeyToPressed("c", function()
  132.     for _, deconstructors in next, listOfDeconstructor do
  133.         deconstructors()
  134.     end
  135.     for _, block in next, Blocks do
  136.         block:destroy()
  137.     end
  138.     Center:destroy()
  139.  
  140.     CoordinateBox:destroy()
  141.  
  142.     --Don't need to run lookAt because it is already facing 0, 0, 0
  143.     camera.position = vector3(0, 0, 0)
  144. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement