Advertisement
Hachem16

Hamster Wheels !

Dec 23rd, 2015
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.62 KB | None | 0 0
  1. default = {width = 50, height = 50}
  2. prot = {}
  3. prot[12] = {
  4.     type = 12,
  5.     width = default.width,
  6.     height = default.height,
  7.     color = 0x5F58F2,
  8.     foreground = false,
  9.     friction = 0.3,
  10.     restitution = 0.2,
  11.     angle = 0,
  12.     miceCollision = true,
  13.     groundCollision = true,
  14.     dynamic = false,
  15.     fixedRotation = false,
  16.     mass = 0,
  17.     linearDamping = 0,
  18.     angularDamping = 0,
  19. }
  20. prot[13] = {
  21.     type = 13,
  22.     width = default.width,
  23.     color = 0x0FF0F0,
  24.     foreground = false,
  25.     friction = 0.3,
  26.     restitution = 0.2,
  27.     angle = 0,
  28.     miceCollision = true,
  29.     groundCollision = true,
  30.     dynamic = false,
  31.     fixedRotation = false,
  32.     mass = 0,
  33.     linearDamping = 0,
  34.     angularDamping = 0,
  35. }
  36. joint = {
  37.     type=0,
  38.     point1 = nil,
  39.     point2 = nil,
  40.     point3 = nil,
  41.     point4 = nil,
  42.     frequency=nil,
  43.     damping=nil,
  44.     line=nil,
  45.     color= 0xFF6600,
  46.     ratio = nil,
  47.     speedMotor = nil,
  48.     forceMotor = nil,
  49.     limit1 = nil,
  50.     limit2 = nil,
  51.     axis  = nil,
  52.     alpha = nil,
  53.     foreground=false,
  54. }
  55. g_id = 0
  56. j_id = 0
  57. spinners = {}
  58. missiles = {}
  59. wheel_delay = 10000
  60. local add_ground = tfm.exec.addPhysicObject
  61. local add_joint = tfm.exec.addJoint
  62. function tfm.exec.addPhysicObject(_id, x, y, ground)
  63.     if g_id >= 9000 then
  64.         return
  65.     end
  66.     if _id == nil then
  67.         g_id = g_id + 1
  68.         add_ground(g_id, x, y, ground)
  69.     else
  70.         add_ground(_id, x, y, ground)
  71.     end
  72. end
  73. function tfm.exec.addJoint(_id, g1, g2, jointt)
  74.     if _id == nil then
  75.         j_id = j_id + 1
  76.         add_joint(j_id, g1, g2, jointt)
  77.     else
  78.         add_joint(_id, g1, g2, jointt)
  79.     end
  80. end
  81. function spaw_spinner(x, y, dyn, color, direction)
  82.     local d = {left = -1, right = 1}
  83.     local d = d[direction] or -1
  84.     local color = color or 0x272727
  85.     local x = x
  86.     local y = y
  87.     local num = 25
  88.     local rad = math.random(70,90)
  89.     local jnts = {}
  90.     local grnds = {}
  91.     local _joint = {
  92.     type = 3,
  93.     forceMotor = d*8735,
  94.     speedMotor = d*7531,
  95.     ratio = 5,
  96.     }
  97.     local dyn = dyn or false
  98.  
  99.     local circle = prot[13]
  100.     circle.dynamic = dyn
  101.     circle.width = 20
  102.     circle.color = color + 10
  103.     circle.mass = 5
  104.  
  105.     local rect = prot[12]
  106.     rect.dynamic = true
  107.     rect.height = 40
  108.     rect.width = 20
  109.     rect.color = color
  110.     rect.mass = 5
  111.     rect.friction = 0
  112.  
  113.     tfm.exec.addPhysicObject(nil, x, y, circle)
  114.     local circle_id = g_id
  115.     grnds[#grnds+1] = circle_id
  116.     for i=1,num do
  117.         local lx = rad * math.cos(math.rad(i*360/num)) +x
  118.         local ly = rad * math.sin(math.rad(i*360/num)) +y
  119.         rect.angle = i*360/num + 90
  120.         tfm.exec.addPhysicObject(nil, lx, ly, rect)
  121.         local p_id = g_id
  122.         grnds[#grnds+1] = p_id
  123.         tfm.exec.addJoint(nil, circle_id, p_id, _joint)
  124.         jnts[#jnts+1] = j_id
  125.     end
  126.     return circle_id, jnts, grnds
  127. end
  128. function slide_ground(id, direction)
  129.     directions = {right = "400,-50", left = "-400,-50", up="0,-1", down = "0,1"}
  130.     speed = {right = 2, left = -2, up = -50, down = 0.5}
  131.     force = {right = 100, left = 100, up = 9999999, down = 100}
  132.     limit = {right = 5, left = 5, up = 0, down = 5}
  133.     tfm.exec.addPhysicObject(nil, -200,-200, {})
  134.     local slider_id = g_id
  135.     local _joint = {
  136.         type=1,
  137.         axis=directions[direction] or "0,0",
  138.         forceMotor=force[direction] or nil,
  139.         limit2 = limit[direction] or nil,
  140.         speedMotor=speed[direction] or 2,
  141.     }
  142.     tfm.exec.addJoint(nil, slider_id, id, _joint)
  143.     return j_id, g_id
  144. end
  145. function hang_ground(id, x, y)
  146.     tfm.exec.addPhysicObject(nil, x, y-200, {type=14, miceCollision=false,groundCollision=false})
  147.     tfm.exec.addJoint(nil, g_id, id, {type=0,frequency=5, damping=0.5})
  148.     return j_id, g_id
  149. end
  150. players = {}
  151. function eventNewPlayer(name)
  152.     system.bindKeyboard(name, 9, false, true) -- Tab
  153.     system.bindKeyboard(name, 32, false, true) -- Space
  154.     system.bindKeyboard(name, 85, false, true) -- U
  155.     system.bindKeyboard(name, 72, false, true) -- H
  156.     system.bindKeyboard(name, 75, false, true) -- J
  157.     system.bindKeyboard(name, 74, false, true) -- K
  158.     system.bindKeyboard(name, 76, false, true) -- L
  159.  
  160.     players[name] = players[name] or {time_start = -wheel_delay, color = math.random( 0x000000, 0xFFFFFF)}
  161. end
  162. function eventKeyboard(name, key, down, x, y)
  163.     if key == 32 then
  164.         if players[name].wheeled then
  165.             for l = players[name].l_id,players[name].h_id do
  166.                 tfm.exec.removeJoint(l)
  167.             end
  168.             players[name].h_id = 0
  169.             for k,v in pairs(players[name].grounds) do
  170.                 tfm.exec.removePhysicObject(v)
  171.             end
  172.             tfm.exec.removePhysicObject(players[name].ground_joint)
  173.             players[name].grounds = {}
  174.             players[name].wheeled = false
  175.         end
  176.     elseif key == 9 then
  177.         if (not players[name].wheeled) and (os.time()>=wheel_delay+players[name].time_start) then
  178.             local x = tfm.get.room.playerList[name].x
  179.             local y = tfm.get.room.playerList[name].y - 75
  180.             tfm.exec.movePlayer(name, x, y)
  181.             local p , stop, grounds = spaw_spinner(x,y, true, players[name].color, right)
  182.             players[name].grounds = grounds
  183.             local _id, _gr = hang_ground(p, x,y)
  184.             players[name].l_id, players[name].h_id = stop[1], _id
  185.             players[name].circle = p
  186.             players[name].circle_joint = _id
  187.             players[name].ground_joint = _gr
  188.             players[name].wheeled = true
  189.             players[name].time_start = os.time()
  190.         end
  191.     elseif key == 85 then
  192.         tfm.exec.removeJoint(players[name].circle_joint)
  193.         tfm.exec.removePhysicObject(players[name].ground_joint)
  194.         players[name].circle_joint, players[name].ground_joint = slide_ground(players[name].circle, "up")
  195.     elseif key == 75 then
  196.         tfm.exec.removeJoint(players[name].circle_joint)
  197.         tfm.exec.removePhysicObject(players[name].ground_joint)
  198.         players[name].circle_joint, players[name].ground_joint = slide_ground(players[name].circle, "down")
  199.     elseif key == 72 then
  200.         tfm.exec.removeJoint(players[name].circle_joint)
  201.         tfm.exec.removePhysicObject(players[name].ground_joint)
  202.         players[name].circle_joint, players[name].ground_joint = slide_ground(players[name].circle, "left")
  203.     elseif key == 74 then
  204.         tfm.exec.removeJoint(players[name].circle_joint)
  205.         tfm.exec.removePhysicObject(players[name].ground_joint)
  206.         players[name].circle_joint, players[name].ground_joint = slide_ground(players[name].circle, "right")
  207.     elseif key == 76 then
  208.         tfm.exec.removeJoint(players[name].circle_joint)
  209.         tfm.exec.removePhysicObject(players[name].ground_joint)
  210.         local x = tfm.get.room.playerList[name].x
  211.         local y = tfm.get.room.playerList[name].y
  212.         players[name].circle_joint, players[name].ground_joint = hang_ground(players[name].circle, x, y)
  213.     end
  214. end
  215. function eventChatCommand(name, cmd)
  216.     local args = {}
  217.     for arg in cmd:gmatch("[^%s]+") do
  218.         args[#args+1] = arg
  219.     end
  220.     if args[1] == "color" or args[1] == "colour" then
  221.         ui.showColorPicker(1, name, players[name].color, "Choose a color")
  222.     end
  223. end
  224. function eventColorPicked(id , name, color)
  225.     if id == 1 then
  226.         players[name].color = color
  227.     end
  228. end
  229. for k,v in pairs(tfm.get.room.playerList) do eventNewPlayer(k) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement