-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacter inface.lua
147 lines (116 loc) · 3.58 KB
/
Character inface.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
--[[
- Depso
]]
local module = {}
--// Services
local KeyframeSequenceProvider = game:GetService("KeyframeSequenceProvider")
local RunService = game:GetService("RunService")
local IsStudio = RunService:IsStudio()
--// This function only applies to Roblox studio
local TrackCache = {}
local function CreateAnimation(KeyframeSequence): Animation
if not IsStudio then
return error("Cannot create animation track outside of Studio!")
end
--// Check cache for existing animation
local Cached = TrackCache[KeyframeSequence]
if Cached then
return Cached
end
--// Create animation from keyframes
local CreatedId = KeyframeSequenceProvider:RegisterKeyframeSequence(KeyframeSequence)
local Animation = Instance.new("Animation")
Animation.AnimationId = CreatedId
TrackCache[KeyframeSequence] = Animation
return Animation
end
local function GetRaycastParams(Filter): RaycastParams
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = Filter
return Params
end
local Character = {}
Character.__index = Character
function Character:CharacterAdded(Character)
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local Root = Character:WaitForChild("HumanoidRootPart")
--// Asign keys
self.Character = Character
self.Humanoid = Humanoid
self.Animator = Animator
self.Root = Root
end
function Character:GetLimb(Name: string)
local Character = self.Character
return Character:WaitForChild(Name)
end
function Character:IsObstructed(Offset: number, Vector: string?)
local Vector = Vector or "UpVector"
local Offset = Offset or 0
local Base = 2
local Character = self.Character
local Head = self:GetLimb("Head")
local Params = GetRaycastParams({
Character
})
--// Fire raycast
local From = Head.Position
local Direction = Head.CFrame[Vector] * (Base+Offset)
return workspace:Raycast(From, Direction, Params)
end
function Character:IsOnGround(): boolean
local Humanoid = self.Humanoid
return Humanoid.FloorMaterial ~= Enum.Material.Air
end
function Character:IsMoving(): boolean
local Humanoid = self.Humanoid
return Humanoid.MoveDirection.Magnitude >= 0.1
end
function Character:SetStateEnabled(State: Enum.HumanoidStateType, Enabled: boolean)
local Humanoid = self.Humanoid
Humanoid:SetStateEnabled(State, Enabled)
end
function Character:SetJumpEnabled(Enabled: boolean)
local JumpState = Enum.HumanoidStateType.Jumping
self:SetStateEnabled(JumpState, Enabled)
end
function Character:LoadAnimation(Animation: (KeyframeSequence | Animation), Priority): AnimationTrack
local Animator = self.Animator
if not Animator then return end
--// If track creation is required
if not Animation:IsA("Animation") then
Animation = CreateAnimation(Animation)
end
local Track = Animator:LoadAnimation(Animation)
Track.Priority = Priority or Enum.AnimationPriority.Action
return Track
end
function Character:PlayAnimation(Animation: (KeyframeSequence | Animation), Priority): AnimationTrack
local Track = self:LoadAnimation(Animation, Priority)
Track:Play()
return Track
end
function module:GetCharacter(Player: Player)
local Real = Player.Character
local Interface = setmetatable({}, Character)
--// Connect events
if Real then
Interface:CharacterAdded(Real)
end
Player.CharacterAdded:Connect(function(New: Model)
Interface:CharacterAdded(New)
end)
return setmetatable({},{
__index = function(self, Key)
--// Check if interface contains the key
local Method = Interface[Key]
if Method then
return Method
end
--// Grab part from character
return Interface:GetLimb(Key)
end,
})
end
return module