Jump to content

Throwing an item with a keybind


Recommended Posts

I'm trying to make a hat that allows you to throw explosives with a keybind, since the mouse buttons are reserved for the hand slot. I currently have the code for an item that is a simple explosive you throw, but since it's a hand slot item it uses the normal mouse buttons. How can I make it throw projectiles with a keybind, like the R button?

local kidpotion_assets = {
    Asset("ANIM", "anim/kidpotion.zip"),
    Asset("ANIM", "anim/swap_kidpotion.zip"),
	
    Asset("ATLAS", "images/inventoryimages/kidpotion.xml"),
    Asset("IMAGE", "images/inventoryimages/kidpotion.tex"),
}
 

local kidpotion_prefabs = {
    "explode_small",
} 
local function DoExplode(self)
	print("DoExplode")
    local explosiverange = 5
 
    local stacksize = self.inst.components.stackable ~= nil and self.inst.components.stackable:StackSize() or 1
    local totaldamage = self.explosivedamage * stacksize
 
    local x, y, z = self.inst.Transform:GetWorldPosition()
    -- Players are off limits now
    local ents = TheSim:FindEntities(x, y, z, explosiverange, nil, { "INLIMBO", "player" })
 
    for i, v in ipairs(ents) do
        if v ~= self.inst and v:IsValid() and not v:IsInLimbo() then
            if v.components.workable ~= nil and v.components.workable:CanBeWorked() then
                v.components.workable:WorkedBy(self.inst, self.buildingdamage)
            end
 
            --Find a way to get rid of this stuff, it's freaking useless since the potion doesn't set stuff on fire
            if v:IsValid() and not v:IsInLimbo() then
                if self.lightonexplode and
                    v.components.fueled == nil and
                    v.components.burnable ~= nil and
                    not v.components.burnable:IsBurning() and
                    not v:HasTag("burnt") then
                    --v.components.burnable:Ignite()
                end
 
                if v.components.combat ~= nil then
                    v.components.combat:GetAttacked(self.inst, totaldamage, nil)
                end
 
                v:PushEvent("explosion", { explosive = self.inst })
            end
        end
    end
end
 
local function OnExplodeFn(inst)
	print("OnExplode")
    SpawnPrefab("explode_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
    DoExplode(inst.components.explosive)
end
 
local function OnHitWater(inst, attacker, target)
	print("OnHitWater")
    inst.components.explosive:OnBurnt()
end

local function common_fn(bank, build, anim, tag, isinventoryitem)
	print("common_fn")
    local inst = CreateEntity()
 
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
 
    if isinventoryitem then
        MakeInventoryPhysics(inst)
    else
        inst.entity:AddPhysics()
        inst.Physics:SetMass(1)
        inst.Physics:SetCapsule(0.2, 0.2)
        inst.Physics:SetFriction(0)
        inst.Physics:SetDamping(0)
        inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
        inst.Physics:ClearCollisionMask()
        inst.Physics:CollidesWith(COLLISION.WORLD)
    end
 
    if tag ~= nil then
        inst:AddTag(tag)
    end
 
    inst.AnimState:SetBank(bank)
    inst.AnimState:SetBuild(build)
    inst.AnimState:PlayAnimation(anim, true)
 
    inst.entity:SetPristine()
 
    if not TheWorld.ismastersim then
        return inst
    end
 
    inst:AddComponent("locomotor")
 
    inst:AddComponent("complexprojectile")
 
    return inst
end
 
local function onequip(inst, owner)
	print("OnEquip")
    owner.AnimState:OverrideSymbol("swap_object", "swap_kidpotion", "swap_kidpotion")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
end
 
local function onunequip(inst, owner)
	print("OnUnequip")
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
end
 
local function onthrown(inst)
	print("OnThrown")
    inst:AddTag("NOCLICK")
    inst.persists = false
 
    inst.AnimState:PlayAnimation("spin_loop", true)
 
    inst.Physics:SetMass(1)
    inst.Physics:SetCapsule(0.2, 0.2)
    inst.Physics:SetFriction(0)
    inst.Physics:SetDamping(0)
    inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.WORLD)
    inst.Physics:CollidesWith(COLLISION.OBSTACLES)
    inst.Physics:CollidesWith(COLLISION.ITEMS)
end
 
local function ReticuleTargetFn()
	print("ReticuleTargetFn")
    local player = ThePlayer
    local ground = TheWorld.Map
    local x, y, z
    --Attack range is 8, leave room for error
    --Min range was chosen to not hit yourself (2 is the hit range)
    for r = 6.5, 3.5, -.25 do
        x, y, z = player.entity:LocalToWorldSpace(r, 0, 0)
        if ground:IsPassableAtPoint(x, y, z) then
            return Vector3(x, y, z)
        end
    end
    return Vector3(x, y, z)
end


local function kidpotion_fn()
	print("kidpotion_fn")
    local inst = common_fn("kidpotion", "kidpotion", "idle", "projectile", true)
 
    inst:AddComponent("reticule")
    inst.components.reticule.targetfn = ReticuleTargetFn
    inst.components.reticule.ease = true
 
    if not TheWorld.ismastersim then
        return inst
    end
 
    inst:AddComponent("explosive")
    inst.components.explosive:SetOnExplodeFn(OnExplodeFn)
    inst.components.explosive.explosivedamage = 100
    -- So explosion doesn't affect anything, including players
    inst.components.explosive.explosiverange = 0
 
    inst.components.complexprojectile:SetHorizontalSpeed(15)
    inst.components.complexprojectile:SetGravity(-40)
    inst.components.complexprojectile:SetLaunchOffset(Vector3(.25, 1, 0))
    inst.components.complexprojectile:SetOnLaunch(onthrown)
    inst.components.complexprojectile:SetOnHit(OnHitWater)
 
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetRange(8, 10)
 
    inst:AddComponent("inspectable")
 
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "kidpotion"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/kidpotion.xml"
 
    inst:AddComponent("stackable")
 
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable.equipstack = true
 
    return inst
end

local testkey = "r"
TheInput:AddKeyUpHandler(
	testkey:lower():byte(), 
	function()
		print("R Button Test") 
	end)

 
STRINGS.NAMES.KIDPOTION = "Kid's Potion"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.KIDPOTION = "It might explode!"
 
return Prefab("kidpotion", kidpotion_fn, kidpotion_assets, kidpotion_prefabs)

 

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