Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@class character_controller
- local character = {}
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local RunService = game:GetService("RunService")
- local UserInputService = game:GetService("UserInputService")
- local smooth_value = require(ReplicatedStorage.common.classes.smooth_value)
- local inputs = require(ReplicatedStorage.common.modules.inputs)
- local util = require(ReplicatedStorage.common.util)
- local spring: spring = require(ReplicatedStorage.common.classes.spring)
- local local_player = Players.LocalPlayer
- local default_character_cframe = CFrame.Angles(0, 0, 0)
- local move_directions = {
- { "W"; Vector3.new(0, 0, -1) };
- { "A"; Vector3.new(-1, 0, 0) };
- { "S"; Vector3.new(0, 0, 1) };
- { "D"; Vector3.new(1, 0, 0) }
- }
- --[[
- disable_mouse_input: to disable mouse input
- interaction_disabled: to disable WASD, flashlight, others
- --]]
- function character.new()
- local self = {}
- self.connections = {}
- self.pickupSamples = {}
- self.inventory = {}
- self.player = local_player
- self.camera = workspace.Camera
- self.input_group = inputs:addGroup("game")
- self.character_cframe = default_character_cframe
- self.move_vector = Vector3.new()
- self.target_move_vector = Vector3.new()
- self.seconds_without_movement = 0
- self.glitch_magnitude = 0
- self.moving = {}
- return setmetatable(self, { __index = character })
- end
- function character:init()
- self.character = self.player.Character or self.player.CharacterAdded:Wait()
- for _, movement in pairs(move_directions) do
- self.input_group:bindActionBegan(movement[1].."-begin", nil, movement[1], false, function()
- self.moving[movement[2]] = true
- end)
- self.input_group:bindActionEnded(movement[1].."-end", nil, movement[1], false, function()
- self.moving[movement[2]] = false
- end)
- end
- end
- function character:add_connection(connection)
- self.connections[#self.connections+1] = connection
- end
- function character:render_stepped(delta_time)
- local user_settings = UserSettings():GetService("UserGameSettings")
- local user_input = UserInputService:GetMouseDelta()*0.01*user_settings.MouseSensitivity
- UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
- UserInputService.MouseIconEnabled = false
- -- # update camera
- self.character_cframe *= CFrame.new(-user_input.y, -user_input.x, 0)
- -- # clamp axis
- local clamp_x = math.clamp(self.character_cframe.x, -1.3, 1.3) - self.character_cframe.x
- self.character_cframe *= CFrame.new(clamp_x, 0, 0)
- end
- function character:heartbeat(delta_time)
- local speed = 8/(self.character.Humanoid.WalkSpeed/16)
- local lerp = math.min(delta_time*25, 1)
- local target_move_vector = Vector3.new()
- for i, v in pairs(self.moving) do
- if v then target_move_vector += i end
- end
- self.target_move_vector = target_move_vector
- if self.target_move_vector == Vector3.new() then
- speed *= 3
- end
- if self.interaction_disabled then
- self.target_move_vector = Vector3.new()
- end
- -- # move vector
- self.move_vector = self.move_vector:Lerp(self.target_move_vector, delta_time*speed)
- self.player:Move(self.camera.CFrame:VectorToWorldSpace(self.target_move_vector), false)
- self.lerp = lerp
- self.glitch_magnitude = math.max(0, self.glitch_magnitude)
- end
- return character
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement