From d83ddf4e5326713ad9ed78634c0877cf24684132 Mon Sep 17 00:00:00 2001 From: reidlab Date: Thu, 20 Jul 2023 15:09:09 -0700 Subject: [PATCH] add character to clientstate and add sprint system --- src/ReplicatedStorage/ecs/state.ts | 4 +-- .../ecs/systems/client/sprint.ts | 34 +++++++++++++++++++ .../StarterPlayerScripts/main.client.ts | 5 +++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/ReplicatedStorage/ecs/systems/client/sprint.ts diff --git a/src/ReplicatedStorage/ecs/state.ts b/src/ReplicatedStorage/ecs/state.ts index 949c421..4d480e4 100644 --- a/src/ReplicatedStorage/ecs/state.ts +++ b/src/ReplicatedStorage/ecs/state.ts @@ -1,14 +1,14 @@ /* eslint-disable roblox-ts/no-private-identifier */ +import { CharacterRigR6 } from "@rbxts/character-promise" import { InputKind } from "ReplicatedStorage/inputKind" /** * The client ECS state. */ export class clientState { - [index: string]: unknown, + character?: CharacterRigR6 debugEnabled = false - isJumping = false isRunning = false lastProcessedCommand?: InputKind } diff --git a/src/ReplicatedStorage/ecs/systems/client/sprint.ts b/src/ReplicatedStorage/ecs/systems/client/sprint.ts new file mode 100644 index 0000000..14b1e39 --- /dev/null +++ b/src/ReplicatedStorage/ecs/systems/client/sprint.ts @@ -0,0 +1,34 @@ +import { World } from "@rbxts/matter" +import { match } from "@rbxts/variant" +import { clientState } from "ReplicatedStorage/ecs/state" + +function sprint(_: World, client: clientState): void { + if (client.lastProcessedCommand !== undefined) { + match(client.lastProcessedCommand, { + KeyDown: ({ key }) => { + if (key === Enum.KeyCode.LeftControl) { + client.isRunning = true + } + }, + KeyUp: ({ key }) => { + if (key === Enum.KeyCode.LeftControl) { + client.isRunning = false + } + }, + default: () => { + // do nothing + } + }) + } + + if (client.isRunning && client.character) { + client.character.Humanoid.WalkSpeed = 24 + return + } + + if (client.character) { + client.character.Humanoid.WalkSpeed = 16 + } +} + +export = sprint \ No newline at end of file diff --git a/src/StarterPlayer/StarterPlayerScripts/main.client.ts b/src/StarterPlayer/StarterPlayerScripts/main.client.ts index 686184e..a2f158f 100644 --- a/src/StarterPlayer/StarterPlayerScripts/main.client.ts +++ b/src/StarterPlayer/StarterPlayerScripts/main.client.ts @@ -1,3 +1,5 @@ +import { CharacterRigR6 } from "@rbxts/character-promise" +import { Players } from "@rbxts/services" import { start } from "ReplicatedStorage/ecs" import { clientState } from "ReplicatedStorage/ecs/state" import { Host } from "ReplicatedStorage/hosts" @@ -7,5 +9,8 @@ const HOST = Host.Client const ClientState = new clientState() +const player = Players.LocalPlayer +ClientState.character = (player.Character || player.CharacterAdded.Wait()[0]) as CharacterRigR6 + setEnvironment(HOST) start(HOST, ClientState) \ No newline at end of file