Jump to content

Moisture to Speed ratio help. (perk)


Recommended Posts

I am currently making a character, and one of the attributes that I want is so that they slow down gradually with increased wetness. Here are some scripts that I have tried but don't seem to work. (I am looking for something that I can easily insert into the code)

	--local function GetSpeed(inst) --Moves slower the higher is wet, and slightly slower when raining.
	--local speed = (inst.wetness > 79 and -0.5) or
	--	   (inst.wetness > 39 and -0.5) or
	--	   (inst.wetness > 19 and -0.5) or
	--	   (inst.wetness > 4 and -0.5) or
	--	   0
	--	   
    --return inst.currentRain and speed - 0.5 or speed
--end

--local dapperness_onrain = -50 -- You can also use (-TUNING.SANITY_SMALL), or a custom value
--	local function rain_start()
--	inst.components.sanity.dapperness = dapperness_onrain
--	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 0.1)
--end
--	GetWorld():ListenForEvent("rainstart", rain_start)
--	local function rain_stop()
--	inst.components.sanity.dapperness = 0
--	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 1)	
--end
--	GetWorld():ListenForEvent("rainstop", rain_stop)

The code as a whole: 


local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = {"featherpencil"
}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when reviving from ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "character_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "character.tex" )
end

-- Code Below, I added
local function runner(inst)
	if TheWorld.state.phase == "day" then
		inst.components.combat.damagemultiplier = 1
		inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.2)
	    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.3)
		inst.Light:Enable(false)
	elseif TheWorld.state.phase == "dusk" then
		inst.components.combat.damagemultiplier = 1.2
		inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.5)
	    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.6)
		inst.Light:Enable(false)
	elseif TheWorld.state.phase == "night" then
		inst.components.combat.damagemultiplier = 1.1
		inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.4)
	    inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.5)
		inst.Light:Enable(true)
		inst.Light:SetRadius(6)
		inst.Light:SetFalloff(0.75)
		inst.Light:SetIntensity(.5)
		inst.Light:SetColour(70/255,230/255,14/170)
		end
	end
	
	--local function GetSpeed(inst) --Moves slower the higher is wet, and slightly slower when raining.
	--local speed = (inst.wetness > 79 and -0.5) or
	--	   (inst.wetness > 39 and -0.5) or
	--	   (inst.wetness > 19 and -0.5) or
	--	   (inst.wetness > 4 and -0.5) or
	--	   0
	--	   
    --return inst.currentRain and speed - 0.5 or speed
--end
	
	function SanityToWetness(inst)
	return (inst.wetness > 79 and -4 ) or -- -4 sanity per second while at 80 wetness or higher.
			(inst.wetness < 6 and -0.5) or -- -.5 sanity per second when at 5 wetness or below
			(inst.wetness < 21 and -1) or -- -1 sanity per second when at 20 wetness or below (but still above 5)
			 -2 -- -2 sanity per second when between 20 and 80 wetness.
end


--local dapperness_onrain = -50 -- You can also use (-TUNING.SANITY_SMALL), or a custom value
--	local function rain_start()
--	inst.components.sanity.dapperness = dapperness_onrain
--	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 0.1)
--end
--	GetWorld():ListenForEvent("rainstart", rain_start)
--	local function rain_stop()
--	inst.components.sanity.dapperness = 0
--	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "character_speed_mod", 1)	
--end
--	GetWorld():ListenForEvent("rainstop", rain_stop)

-- Code Above, I added

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)

-- Code Below, I added
	inst.components.sanity.night_drain_mult = -10
    inst.components.temperature.mintemp = 20
	
	inst.currentRain = false
	inst.wetness = inst.components.moisture:GetMoisture()
-- Code Above, I added

	-- choose which sounds this character will play
	inst.soundsname = "willow"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(120)
	inst.components.hunger:SetMax(112)
	inst.components.sanity:SetMax(200)
	inst.components.locomotor.walkspeed = (TUNING.WILSON_WALK_SPEED * 1.4)
	inst.components.locomotor.runspeed = (TUNING.WILSON_RUN_SPEED * 1.5)
	inst.components.temperature.inherentinsulation = -50
	
	inst.components.sanity.custom_rate_fn = SanityToWetness
	
	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1.1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 0.75 * TUNING.WILSON_HUNGER_RATE
	
	-- Code Below, I added
		local refreshTime = 1/5
	inst:DoPeriodicTask(refreshTime, function() runner(inst, refreshTime)
	end)
	-- Code Above, I added
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	
end

return MakePlayerCharacter("character", prefabs, assets, common_postinit, master_postinit, start_inv)

Also thanks for taking your time to reply

Edited by AudioFox
Added Full code to character, Include (perk in title) (to help people that might want to help me) (added a lil comment at end)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...