Jump to content

Custom Badge Implementation


Recommended Posts

Hello! I recently got into Don't Starve Together and am in the process of making a custom character mod. I want to give her a dark energy badge whose level will increase/decrease when she spawns(crafts) a follower.

I have been able to get the badge to show up, but haven't been able to get the animations or the number/value to work.

Any help is very appreciated!! :D Code is as follows:

 

modmain: 

--Dark Energy Badge
AddReplicableComponent("darkenergy")

local function Makedarkenergybadge(self, badge_name, owner_tag, bank, build, background_build, show_progress, show_remaining, position_normal, position_modded)
	
	if self.owner:HasTag("wisteria") then
		local darkenergybadge = require "badges/darkenergybadge"
		self.darkenergybadge = self:AddChild(darkenergybadge("darkenergybadge", self.owner))
		
		if GLOBAL.KnownModIndex:IsModEnabled("workshop-376333686") then
		
		self.darkenergybadge:SetPosition(-62, -52, 0)
		
		else 
		
		self.darkenergybadge:SetPosition(-80, -40, 0)
		end
		
	local function darkenergydelta(self, data)
		self.darkenergybadge:SetPercent(data.newpercent, data.currentdarkenergy, data.maxdarkenergy)
	end
	
	local function OnSetPlayerMode( self )
		if self.ondarkenergydelta == nil then
			self.ondarkenergydelta = function( owner, data ) darkenergydelta( data ) end
			self.inst:ListenForEvent( "darkenergydelta", self.ondarkenergydelta, self.owner )
				if self.owner.darkenergy ~= nil then 			
				darkenergydelta({newpercent = self.owner.darkenergy:GetPercent(),
				currentdarkenergy = self.owner.darkenergy:GetPercent(),
				maxdarkenergy = self.owner.darkenergy:GetMax() })
					
					--darkenergydelta({ newpercent = self.owner.darkenergyCurrent / self.owner.darkenergyMax,
						--currentdarkenergy = inst.components.darkenergyCurrent,
						--maxdarkenergy = self.owner.darkenergyMax })
				end
		end
	end


	local function OnSetGhostMode(self)
		if self.ondarkenergydelta ~= nil then
			self.inst:RemoveEventCallback("darkenergydelta", self.ondarkenergydelta, self.owner)
			self.ondarkenergydelta = nil
		end
	end

	local _SetGhostMode = self.SetGhostMode
	
	function self:SetGhostMode(ghostmode)
		_SetGhostMode(self, ghostmode)
			if ghostmode then
				self.darkenergybadge:Hide()
				OnSetGhostMode(self)
			else
				self.darkenergybadge:Show()
				OnSetPlayerMode(self)
			end
		end
		
		OnSetPlayerMode(self)
		
	end
		
	return self
	
end
		
		
local function StatusDisplaysPostInit(self)
	Makedarkenergybadge(self, "darkenergybadge", "wisteria", "darkenergybadge", "darkenergybadge", "darkenergybadge", true, false, {x=40,y=-55,z=0}, {x=-62,y=-52,z=0})
end

AddClassPostConstruct("widgets/statusdisplays", StatusDisplaysPostInit)

 

character:

 

local function onspawnpet(inst, food)
	if food.components.edible and food.components.edible.footype == "MEAT" and inst.components.darkenergy ~= nil then --test with food eaten: later i want to covert this into when spawning/crafting the pet
		inst.components.darkenergy:GainAmount(-10)
		inst.SoundEmitter:PlaySound("dontstarve/HUD/sanity_down")
		inst.HUD.controls.status.darkenergybadge:PulseRed()
	end

	if inst.darkenergyCurrent == inst.darkenergyMin then
		inst.components.health:DoDelta(-40)
		end
	inst:PushEvent("darkenergydelta", {newpercent = inst.darkenergyCurrent / inst.darkenergyMax, currentdarkenergy = inst.darkenergyCurrent, maxdarkenergy = inst.darkenergyMax})
end	

 

darkenergybadge: 

local Badge = require "widgets/badge"
local UIAnim = require "widgets/uianim"
local Text = require "widgets/text"
local Widget = require "widgets/widget"

local function OnGhostDeactivated(inst)
	if inst.AnimState:isCurrentAnimation("anim") then
	inst.widget:Hide()
	end
	end


local darkenergybadge = Class(Badge, function(self, owner, bank, build, background_build, show_progress, show_remaining) Badge._ctor(self, "darkenergybadge", owner)

self.owner = owner

 self.num = self:AddChild(Text(BODYTEXTFONT, 33))
    self.num:SetHAlign(ANCHOR_MIDDLE)
    self.num:SetPosition(-80, -40, 0)
	self.num:SetClickable(false)
    self.num:Hide()

--Icon shown
 self.sanityarrow = self.underNumber:AddChild(UIAnim())
 self.sanityarrow:GetAnimState():SetBank(bank)
 self.sanityarrow:GetAnimState():SetBuild(build)
 self.sanityarrow:GetAnimState():PlayAnimation("anim")
 self.sanityarrow:SetClickable(false)	

 --Hide the original frame since it is now overlapped by the topperanim
 
 self.anim:GetAnimState():Hide("frame")
 self.ghostanim = self.underNumber:AddChild(UIAnim())
 self.ghostanim:GetAnimState():SetBank(bank)
 self.ghostanim:GetAnimState():SetBuild(build)
 self.ghostanim:GetAnimState():PlayAnimation("anim")
 self.ghostanim:Hide()
 self.ghostanim:SetClickable(false)
 self.ghostanim.inst:ListenForEvent("animover", OnGhostDeactivated)
 

 self:StartUpdating()
 
	function self:OverrideIcon(bank, build)
		self.overlay:GetAnimState():SetBank("sanity_arrow")
		self.overlay:GetAnimState():SetBuild("darkenergybadge")
		self.overlay:GetAnimState():PlayAnimation("anim")
 
	end

function self:SetPercent(pct, val, max)
	self.val = val or self.percent
	self.max = max or 100
	self.percent = pct or (self.val/self.max) or 1
	
	--change percentage bar
		self.anim:GetAnimState():SetPercent("anim", 1 - self.percent)
		self.overlay:GetAnimState():SetPercent("anim", 1 - ((val or 1) / (max or 100)))
		self.num:SetString(tostring(math.ceil(self.percent * self.max)))
		
		if KnownModIndex:IsModEnabled("workshop-376333686") then
			if self.show_progress then
				if self.show_remaining then
					self.maxnum:SetString(tostring(math.floor((1 - val) * 100)) .. "%")
				else
					self.maxnum:SetString(tostring(math.floor(val * 100)) .. "%")
				end
			else
		    		self.maxnum:SetString(tostring(max or 100))
			end
		end 
		self.percent = val/self
	end
		
 function self:OnUpdate(dt)
	if self.owner.darkenergy ~= nil then
	self:SetPercent(
		self.owner.darkenergy:GetPercent(),
		self.owner.darkenergy:GetCurrent(),
		self.owner.darkenergy:GetMax())
	end
	
end

end)
 
 
return darkenergybadge

Sorry the code is kind of a mess! I tried to piece the code together from a multitude of examples as best I could. If you need any other code, etc. I can provide it.

Thank you <3

~theDarkFey

Edited by theDarkFey
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...