Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions example/customclient/Characters/NicerHumanoid.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
local module = {}


function module:Setup(simulation)

simulation.constants.maxSpeed = 16 --Units per second
simulation.constants.airSpeed = 16 --Units per second
simulation.constants.accel = 10 --Units per second per second
simulation.constants.airAccel = 10 --Uses a different function than ground accel!
simulation.constants.jumpPunch = 35 --Raw velocity, just barely enough to climb on a 7 unit tall block
simulation.constants.turnSpeedFrac = 10 --seems about right? Very fast.
simulation.constants.runFriction = 0.01 --friction applied after max speed
simulation.constants.brakeFriction = 0.03 --Lower is brake harder, dont use 0
simulation.constants.maxGroundSlope = 0.55 --about 45o
simulation.constants.jumpThrustPower = 300 --If you keep holding jump, how much extra vel per second is there? (turn this off for no variable height jumps)
simulation.constants.jumpThrustDecay = 0.25 --Smaller is faster
simulation.constants.maxSpeed = 16 --Units per second
simulation.constants.airSpeed = 16 --Units per second
simulation.constants.accel = 10 --Units per second per second
simulation.constants.airAccel = 10 --Uses a different function than ground accel!
simulation.constants.jumpPunch = 35 --Raw velocity, just barely enough to climb on a 7 unit tall block
simulation.constants.turnSpeedFrac = 10 --seems about right? Very fast.
simulation.constants.runFriction = 0.01 --friction applied after max speed
simulation.constants.brakeFriction = 0.03 --Lower is brake harder, dont use 0
simulation.constants.maxGroundSlope = 0.55 --about 45o
simulation.constants.jumpThrustPower = 300 --If you keep holding jump, how much extra vel per second is there? (turn this off for no variable height jumps)
simulation.constants.jumpThrustDecay = 0.25 --Smaller is faster

--Example on adding a flying movement type
local MoveTypeFlying = require(script.Parent.utils.MoveTypeFlying)
MoveTypeFlying:ModifySimulation(simulation)


local JumpPadDetection = require(script.Parent.utils.JumpPadDetection)
JumpPadDetection:ModifySimulation(simulation)
end

return module

40 changes: 40 additions & 0 deletions example/customclient/Characters/utils/JumpPadDetection.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local module = {}

local path = game.ReplicatedFirst.Packages.Chickynoid
local Enums = require(path.Enums)

local Simulation = game.ReplicatedFirst.Packages.Chickynoid.Simulation
local MoveTypeJumping = require(Simulation.MoveTypeJumping)
--Jump Pads

function module:ModifySimulation(simulation)
simulation:RegisterMoveState("JumpPadDetection", nil, module.AlwaysThink, nil, nil, nil)
end

function module.AlwaysThink(simulation, cmd)
local onGround = simulation:OnGround()

if onGround ~= nil then
--Check jumpPads
if onGround.hullRecord then
local instance = onGround.hullRecord.instance

if instance then
local vec3 = instance:GetAttribute("launch")
--Jump!
if vec3 then
simulation.state.vel = instance.CFrame:VectorToWorldSpace(vec3)

MoveTypeJumping.StartJump(simulation)
end

--For platform standing
if simulation.state.jump == 0 then
simulation.lastGround = onGround
end
end
end
end
end

return module
51 changes: 51 additions & 0 deletions src/Simulation/MoveTypeJumping.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local module = {}

local path = game.ReplicatedFirst.Packages.Chickynoid
local Enums = require(path.Enums)

function module.AlwaysThink(simulation, cmd)
--Check ground
local onGround = simulation:OnGround()

if simulation.state.jump > 0 then
simulation.state.jump -= cmd.deltaTime
if simulation.state.jump < 0 then
simulation.state.jump = 0
end
end

if onGround ~= nil then
if cmd.y > 0 and simulation.state.jump <= 0 then
simulation:SetMoveState("Jumping")
else
simulation:SetMoveState("Walking")
end
end
end

function module.ActiveThink(simulation, cmd)
--Check ground
local onGround = simulation:OnGround()

--Do jumping?
if onGround ~= nil then
--jump!
if cmd.y > 0 and simulation.state.jump <= 0 then
simulation.state.vel = Vector3.new(
simulation.state.vel.x,
simulation.constants.jumpPunch,
simulation.state.vel.z
)

module.StartJump(simulation)
end
end
end

function module.StartJump(simulation)
simulation.state.jump = 0.2 --jumping has a cooldown (think jumping up a staircase)
simulation.state.jumpThrust = simulation.constants.jumpThrustPower
simulation.characterData:PlayAnimation(Enums.Anims.Jump, true, 0.2)
end

return module
167 changes: 167 additions & 0 deletions src/Simulation/MoveTypeWalking.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
local module = {}

local path = game.ReplicatedFirst.Packages.Chickynoid
local Enums = require(path.Enums)
local MathUtils = require(script.Parent.MathUtils)

function module.AlwaysThink(simulation, cmd)
--Check ground
local onGround = simulation:OnGround()

--Mark if we were onground at the start of the frame
local startedOnGround = onGround

--In air?
if onGround == nil then
simulation.state.inAir += cmd.deltaTime
if simulation.state.inAir > 10 then
simulation.state.inAir = 10 --Capped just to keep the state var reasonable
end

--Jump thrust
if cmd.y > 0 then
if simulation.state.jumpThrust > 0 then
simulation.state.vel += Vector3.new(0, simulation.state.jumpThrust * cmd.deltaTime, 0)
simulation.state.jumpThrust = MathUtils:Friction(
simulation.state.jumpThrust,
simulation.constants.jumpThrustDecay,
cmd.deltaTime
)
end
if simulation.state.jumpThrust < 0.001 then
simulation.state.jumpThrust = 0
end
else
simulation.state.jumpThrust = 0
end

--gravity
simulation.state.vel += Vector3.new(0, simulation.constants.gravity * cmd.deltaTime, 0)

--Switch to falling if we've been off the ground for a bit
if simulation.state.vel.y <= 0.01 and simulation.state.inAir > 0.5 then
simulation.characterData:PlayAnimation(Enums.Anims.Fall, false)
end
else
simulation.state.inAir = 0
end

--Sweep the player through the world, once flat along the ground, and once "step up'd"
local stepUpResult = nil
local walkNewPos, walkNewVel, hitSomething = simulation:ProjectVelocity(
simulation.state.pos,
simulation.state.vel,
cmd.deltaTime
)

--Did we crashland
if onGround == nil and hitSomething == true then
--Land after jump
local groundCheck = simulation:DoGroundCheck(walkNewPos)

if groundCheck ~= nil then
--Crashland
walkNewVel = simulation:CrashLand(walkNewVel)
end
end

-- Do we attempt a stepup? (not jumping!)
if onGround ~= nil and hitSomething == true and simulation.state.jump == 0 then
stepUpResult = simulation:DoStepUp(simulation.state.pos, simulation.state.vel, cmd.deltaTime)
end

--Choose which one to use, either the original move or the stepup
if stepUpResult ~= nil then
simulation.state.stepUp += stepUpResult.stepUp
simulation.state.pos = stepUpResult.pos
simulation.state.vel = stepUpResult.vel
else
simulation.state.pos = walkNewPos
simulation.state.vel = walkNewVel
end

--Do stepDown
if true then
if startedOnGround ~= nil and simulation.state.jump == 0 and simulation.state.vel.y <= 0 then
local stepDownResult = simulation:DoStepDown(simulation.state.pos)
if stepDownResult ~= nil then
simulation.state.stepUp += stepDownResult.stepDown
simulation.state.pos = stepDownResult.pos
end
end
end
end

function module.ActiveThink(simulation, cmd)
--Check ground
local onGround = simulation:OnGround()

--Did the player have a movement request?
local wishDir = nil
if cmd.x ~= 0 or cmd.z ~= 0 then
wishDir = Vector3.new(cmd.x, 0, cmd.z).Unit
simulation.state.pushDir = Vector2.new(cmd.x, cmd.z)
else
simulation.state.pushDir = Vector2.new(0, 0)
end

--Create flat velocity to operate our input command on
--In theory this should be relative to the ground plane instead...
local flatVel = MathUtils:FlatVec(simulation.state.vel)

--Does the player have an input?
if wishDir ~= nil then
if onGround then
--Moving along the ground under player input

flatVel = MathUtils:GroundAccelerate(
wishDir,
simulation.constants.maxSpeed,
simulation.constants.accel,
flatVel,
cmd.deltaTime
)

--Good time to trigger our walk anim
if simulation.state.pushing > 0 then
simulation.characterData:PlayAnimation(Enums.Anims.Push, false)
else
simulation.characterData:PlayAnimation(Enums.Anims.Walk, false)
end
else
--Moving through the air under player control
flatVel = MathUtils:Accelerate(
wishDir,
simulation.constants.airSpeed,
simulation.constants.airAccel,
flatVel,
cmd.deltaTime
)
end
else
if onGround ~= nil then
--Just standing around
flatVel = MathUtils:VelocityFriction(flatVel, simulation.constants.brakeFriction, cmd.deltaTime)

--Enter idle
simulation.characterData:PlayAnimation(Enums.Anims.Idle, false)
-- else
--moving through the air with no input
end
end

--Turn out flatvel back into our vel
simulation.state.vel = Vector3.new(flatVel.x, simulation.state.vel.y, flatVel.z)

--Do angles
if wishDir ~= nil then
simulation.state.targetAngle = MathUtils:PlayerVecToAngle(wishDir)
simulation.state.angle = MathUtils:LerpAngle(
simulation.state.angle,
simulation.state.targetAngle,
simulation.constants.turnSpeedFrac * cmd.deltaTime
)
end
end

return module
Loading