Advertisement
theblackshibe

Untitled

Jan 9th, 2021
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. ---@class character_controller
  2. local character = {}
  3.  
  4. local Players = game:GetService("Players")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local RunService = game:GetService("RunService")
  7. local UserInputService = game:GetService("UserInputService")
  8.  
  9. local smooth_value = require(ReplicatedStorage.common.classes.smooth_value)
  10. local inputs = require(ReplicatedStorage.common.modules.inputs)
  11. local util = require(ReplicatedStorage.common.util)
  12. local spring: spring = require(ReplicatedStorage.common.classes.spring)
  13.  
  14. local local_player = Players.LocalPlayer
  15.  
  16. local default_character_cframe = CFrame.Angles(0, 0, 0)
  17. local move_directions = {
  18.     { "W"; Vector3.new(0, 0, -1) };
  19.     { "A"; Vector3.new(-1, 0, 0) };
  20.     { "S"; Vector3.new(0, 0, 1) };
  21.     { "D"; Vector3.new(1, 0, 0) }
  22. }
  23.  
  24. --[[
  25.     disable_mouse_input: to disable mouse input
  26.     interaction_disabled: to disable WASD, flashlight, others
  27. --]]
  28.  
  29. function character.new()
  30.     local self = {}
  31.  
  32.     self.connections = {}
  33.     self.pickupSamples = {}
  34.     self.inventory = {}
  35.  
  36.     self.player = local_player
  37.     self.camera = workspace.Camera
  38.  
  39.     self.input_group = inputs:addGroup("game")
  40.  
  41.     self.character_cframe = default_character_cframe
  42.     self.move_vector = Vector3.new()
  43.     self.target_move_vector = Vector3.new()
  44.  
  45.     self.seconds_without_movement = 0
  46.     self.glitch_magnitude = 0
  47.  
  48.     self.moving = {}
  49.  
  50.     return setmetatable(self, { __index = character })
  51. end
  52.  
  53. function character:init()
  54.  
  55.     self.character = self.player.Character or self.player.CharacterAdded:Wait()
  56.  
  57.     for _, movement in pairs(move_directions) do
  58.         self.input_group:bindActionBegan(movement[1].."-begin", nil, movement[1], false, function()
  59.             self.moving[movement[2]] = true
  60.         end)
  61.  
  62.         self.input_group:bindActionEnded(movement[1].."-end", nil, movement[1], false, function()
  63.             self.moving[movement[2]] = false
  64.         end)
  65.     end
  66.  
  67. end
  68.  
  69. function character:add_connection(connection)
  70.     self.connections[#self.connections+1] = connection
  71. end
  72.  
  73. function character:render_stepped(delta_time)
  74.  
  75.     local user_settings = UserSettings():GetService("UserGameSettings")
  76.     local user_input = UserInputService:GetMouseDelta()*0.01*user_settings.MouseSensitivity
  77.  
  78.     UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
  79.     UserInputService.MouseIconEnabled = false
  80.  
  81.     -- # update camera
  82.     self.character_cframe *= CFrame.new(-user_input.y, -user_input.x, 0)
  83.     -- # clamp axis
  84.     local clamp_x = math.clamp(self.character_cframe.x, -1.3, 1.3) - self.character_cframe.x
  85.     self.character_cframe *= CFrame.new(clamp_x, 0, 0)
  86.  
  87. end
  88.  
  89. function character:heartbeat(delta_time)
  90.  
  91.     local speed = 8/(self.character.Humanoid.WalkSpeed/16)
  92.     local lerp = math.min(delta_time*25, 1)
  93.  
  94.     local target_move_vector = Vector3.new()
  95.     for i, v in pairs(self.moving) do
  96.         if v then target_move_vector += i end
  97.     end
  98.  
  99.     self.target_move_vector = target_move_vector
  100.     if self.target_move_vector == Vector3.new() then
  101.         speed *= 3
  102.     end
  103.  
  104.     if self.interaction_disabled then
  105.         self.target_move_vector = Vector3.new()
  106.     end
  107.  
  108.     -- # move vector
  109.     self.move_vector = self.move_vector:Lerp(self.target_move_vector, delta_time*speed)
  110.     self.player:Move(self.camera.CFrame:VectorToWorldSpace(self.target_move_vector), false)
  111.     self.lerp = lerp
  112.  
  113.     self.glitch_magnitude = math.max(0, self.glitch_magnitude)
  114. end
  115.  
  116. return character
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement