Jump to content

Need help with my mod


Recommended Posts

Im making a mod that adds an item that tells the direction of beefalo and the distance away and in a certain range around the player, and I can't find exactly what I'm looking for in tutorials or in the game or other mods code. I need some pointers or an explanation how you would do the above. 

Link to comment
Share on other sites

On 9/5/2016 at 4:59 PM, TheDerpyCat said:

Im making a mod that adds an item that tells the direction of beefalo and the distance away and in a certain range around the player, and I can't find exactly what I'm looking for in tutorials or in the game or other mods code. I need some pointers or an explanation how you would do the above. 

Here's my take on this.

specialhorn.lua:

local assets = {
	Asset("ANIM", "anim/horn.zip"),
	Asset("ANIM", "anim/reticule.zip"),
}

local HORN_RANGE = 100
local RETICLE_TIME = 10

-- Between 0 and 30, green
local CLOSE_DIST_SQ = 30 * 30
-- Between 30 and 50, blue
local MEDIUM_DIST_SQ = 50 * 50
-- Between 50 and 70, red
local FAR_DIST_SQ = 70 * 70
-- Between 70 and 100, black

local function SelfRemove(inst)
	inst:Remove()
end

local function HearHorn(listener, musician, instrument)
	if listener:HasTag("beefalo") then
		local muspos = musician:GetPosition()
		local lispos = listener:GetPosition()
		local muslis = (lispos - muspos):GetNormalized()
		local ret1 = SpawnPrefab("sh_reticule")
		ret1.Transform:SetPosition(muspos.x + muslis.x, muspos.y + muslis.y, muspos.z + muslis.z)
		ret1.components.colourtweener:StartTween({0, 0, 0, 1}, 0)
		local ret2 = SpawnPrefab("sh_reticule")
		ret2.Transform:SetPosition(muspos.x + muslis.x * 2, muspos.y + muslis.y * 2, muspos.z + muslis.z * 2)
		local color_code
		local distsq = musician:GetDistanceSqToInst(listener)
		color_code = (distsq < CLOSE_DIST_SQ) and {0, 1, 0, 1} or
				(distsq < MEDIUM_DIST_SQ) and {0, 0, 1, 1} or
				(distsq < FAR_DIST_SQ) and {1, 0, 0, 1} or
				{0, 0, 0, 1}
		ret2.components.colourtweener:StartTween(color_code, 0)
		ret1:DoTaskInTime(RETICLE_TIME, SelfRemove)
		ret2:DoTaskInTime(RETICLE_TIME, SelfRemove)
	end
end

local function fn()
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)

	inst:AddTag("horn")

	inst.AnimState:SetBank("horn")
	inst.AnimState:SetBuild("horn")
	inst.AnimState:PlayAnimation("idle")

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")

	inst:AddComponent("instrument")
	inst.components.instrument.range = HORN_RANGE
	inst.components.instrument:SetOnHeardFn(HearHorn)

	inst:AddComponent("tool")
	inst.components.tool:SetAction(ACTIONS.PLAY)

	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "horn"

	return inst
end

local function ret_fn()
	local inst = CreateEntity()
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddNetwork()

	inst.AnimState:SetBank("reticule")
	inst.AnimState:SetBuild("reticule")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
	inst.AnimState:SetLayer(LAYER_BACKGROUND)
	inst.AnimState:SetSortOrder(3)

	inst:AddTag("NOCLICK")

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end

	inst.persists = false

	inst:AddComponent("colourtweener")

	return inst
end

return Prefab("specialhorn", fn, assets), Prefab("sh_reticule", ret_fn)

When you blow on this horn, you are going to get two spheres that go from your character, to each beefalo it locates.

The first sphere is always black.

The second one, has a color depending on how far the beefalo is.

If you treat those two spheres like an arrow, you will reach the beefalo.

Those spheres disappear in 10 seconds, and everybody can see them.

For reference, one tile is 4 units. So 100 is 25 tiles of distance. Which is like 5 screens of distance.

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...