75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
import { Players } from "@rbxts/services";
|
|
import { GameplayServerEvents } from "./networking";
|
|
import { OnStart, Service } from "@flamework/core";
|
|
import { getRagdollEvent, makeRagdoll, prepareRagdoll, stopRagdoll, waitMotionLess } from "shared/ragdoll";
|
|
|
|
/**
|
|
* Service related to Gameplay.
|
|
* - Contains Script related to Sprinting, Ragdoll, etc.
|
|
*/
|
|
@Service()
|
|
class GameplayService implements OnStart {
|
|
// Sprint config.
|
|
private base_sprint_speed = 16;
|
|
private sprint_speed = 1.5;
|
|
private is_running = new Map<number, boolean>();
|
|
|
|
onStart(): void {
|
|
// Handle Animation.
|
|
const RunAnim = new Animation();
|
|
RunAnim.AnimationId = "rbxassetid://88297455683117";
|
|
|
|
Players.PlayerAdded.Connect((player) => {
|
|
let running_connection: RBXScriptConnection;
|
|
|
|
player.CharacterAdded.Connect((character) => {
|
|
prepareRagdoll(character);
|
|
getRagdollEvent(character).Connect(() => {
|
|
makeRagdoll(character);
|
|
waitMotionLess(character, 0.1).Wait();
|
|
stopRagdoll(character);
|
|
});
|
|
|
|
const humanoid = character.FindFirstChildWhichIsA("Humanoid");
|
|
if (humanoid) {
|
|
// Setup the animation.
|
|
const animator = humanoid.WaitForChild("Animator") as Animator;
|
|
const animation_track = animator.LoadAnimation(RunAnim);
|
|
animation_track.Priority = Enum.AnimationPriority.Action;
|
|
|
|
// Change the animation when the running speed change.
|
|
running_connection = humanoid.Running.Connect((speed) => {
|
|
if (this.is_running.get(player.UserId)) {
|
|
if (speed > 2) {
|
|
if (!animation_track.IsPlaying) animation_track.Play(0.2);
|
|
} else {
|
|
if (animation_track.IsPlaying) animation_track?.Stop(0.2);
|
|
}
|
|
} else if (animation_track?.IsPlaying) {
|
|
animation_track?.Stop(1);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
// Clean up the connection when the character died.
|
|
player.CharacterRemoving.Once(() => running_connection.Disconnect());
|
|
});
|
|
|
|
// Change the sprint state with networking
|
|
GameplayServerEvents.sprint.connect((player, enabled) => {
|
|
const character = player.Character;
|
|
this.is_running.set(player.UserId, enabled);
|
|
if (character) {
|
|
const humanoid = character.FindFirstChildOfClass("Humanoid");
|
|
if (humanoid) {
|
|
if (enabled) {
|
|
humanoid.WalkSpeed = this.base_sprint_speed * this.sprint_speed;
|
|
} else {
|
|
humanoid.WalkSpeed = this.base_sprint_speed;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|