Birdskull

Help with tunnelling character

Recommended Posts

Birdskull    14

Hello.

I'm working on a character that is supposed to be able to tunnel. I'm getting nowhere. Here's the current iteration of my efforts, but it does nothing.

Modmain:

Spoiler

 

--Tunnelling
local TUNNELSTART = GLOBAL.Action()
TUNNELSTART.str = "TunnelStart"
TUNNELSTART.id = "TUNNELSTART"
TUNNELSTART.fn = function(act)
act.target.DynamicShadow:Enable(false)
act.target.Hide(true)
act.target.components.locomotor:SetExternalSpeedMultiplier(act, "demogorgon_speed_mod", 0.25)
act.target.AddTag("tunnelling")
end
AddAction(TUNNELSTART)

local TUNNELSTOP = GLOBAL.Action()
TUNNELSTOP.str = "TunnelStop"
TUNNELSTOP.id = "TUNNELSTOP"
TUNNELSTOP.fn = function(act)
act.target.DynamicShadow:Enable(true)
act.target.Hide(false)
act.target.components.locomotor:RemoveExternalSpeedMultiplier(act, "demogorgon_speed_mod")
act.target.RemoveTag("tunnelling")
end
AddAction(TUNNELSTOP)

 

Character:

Spoiler

local function OnKeyPressed(inst, data)
  if data.inst == ThePlayer then
    if data.key == KEY_LEFTBRACKET then -- You can change LEFTBRACKET to something like A or Z or B ect.
      if TheWorld.ismastersim then
      BufferedAction(inst, inst, ACTIONS.TUNNELSTART):Do()
      else
      SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.TUNNELSTART.code, inst, ACTIONS.TUNNELSTART.mod_name)
      end
      elseif data.key == KEY_RIGHTBRACKET then
      if TheWorld.ismastersim then
      BufferedAction(inst, inst, ACTIONS.TUNNELSTOP):Do()
      else
      SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.TUNNELSTOP.code, inst, ACTIONS.TUNNELSTOP.mod_name)
      end
    end
  end
end

 

local common_postinit = function(inst)
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "demogorgon.tex" )
    
    inst:AddTag("monster")
    inst:AddTag("demogorgon")
    
    inst:AddComponent("keyhandler")
    inst:ListenForEvent("keypressed", OnKeyPressed)
end

Component

Spoiler

local KeyHandler = Class(function(self, inst)
    self.inst = inst
    self.handler = TheInput:AddKeyHandler(function(key, down) self:OnRawKey(key, down) end )
 
end)
 
function KeyHandler:OnRawKey(key, down)
    local player = ThePlayer
      if (key and not down) and not IsPaused() then
          player:PushEvent("keypressed", {inst = self.inst, player = player, key = key})
    elseif key and down and not IsPaused() then
          player:PushEvent("keydown", {inst = self.inst, player = player, key = key})
      end
end
 
return KeyHandler

 

I haven't added the timer or effects yet, but my idea was to render the character sprite invisible at a key press and add an event that would create a trail of "mole_move_fx" following its movements until either the exit key was pressed or a timer ran out. Can anyone point out what I'm doing wrong, or suggest a better way of executing this?

Edited by Birdskull

Share this post


Link to post
Share on other sites
Thomas Die    886

Have you tried this on a cave or non -cave world ?
I think the buffered or SendRPCToServer may only work on non-cave.

AddModRPCHandler("mod_nane", "action_name", function(inst)
   --code
   --inst = person, maybe idk maybe maybe
end)

SendModRPCToServer(MOD_RPC["mod_nane"]["action_name"])

I think this may work better

Share this post


Link to post
Share on other sites
Birdskull    14

I have not tried it on a cave world yet, but I decided to try without SendRPCToServer and removed the aforementioned from my modmain entirely. Here's what I have in the character file, now. It's... almost working.

Problems:
- My speed modifiers aren't applying
- "jump" animation doesn't appear to play
- "inst:Hide()" is overridden any time the character walks under a tree.
- The "mole_move_fx" prefab only spawns consistently if the character is in constant motion at the time of and following keypress
- Sometimes the character just doesn't reappear

I'm attaching the full WIP to this post. If anyone has the time and inclination to take a look at it in action, I'd be much obliged. I think I'm in need of a fresh pair of eyes more than anything.

--Tunnelling
local function NotTunnelTime(inst, self)
    if  inst:HasTag("playerghost") then
        return
    end
    SpawnPrefab("dirt_puff").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.AnimState:PlayAnimation("jumpout")
    inst:RemoveTag("tunnelling")
    inst:AddTag("untunnelling")
    inst.DynamicShadow:Enable(true)
    inst:Show()
end

local function Dig(inst)
    if  inst:HasTag("playerghost") then
        return
    end
if inst:HasTag("tunnelling")
then
SpawnPrefab("mole_move_fx").Transform:SetPosition(inst.Transform:GetWorldPosition())
else return
end
end

local function TunnelTime(inst, self)
    if  inst:HasTag("playerghost") then
        return
    end
    SpawnPrefab("dirt_puff").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.AnimState:PlayAnimation("jump")
    inst:RemoveTag("untunnelling")
    inst:AddTag("tunnelling")
    inst.DynamicShadow:Enable(false)
    inst:Hide()
    inst:DoPeriodicTask(2, Dig)
end

local function OnKeyPressed(inst, data)    
  if data.inst == ThePlayer then
    if data.key == KEY_T and inst:HasTag("tunnelling") then
     inst.components.locomotor:SetExternalSpeedMultiplier(inst, "demogorgon_speed_mod", 1)
     NotTunnelTime(inst)
    elseif data.key == KEY_T and inst:HasTag("untunnelling") then
     inst.components.locomotor:SetExternalSpeedMultiplier(inst, "demogorgon_speed_mod", .25)
     TunnelTime(inst)
     end
     end
     end

 

local common_postinit = function(inst)
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "demogorgon.tex" )
    
    inst:AddTag("monster")
    inst:AddTag("demogorgon")
    inst:AddTag("untunnelling")
    
    inst:AddComponent("keyhandler")
    inst:ListenForEvent("keypressed", OnKeyPressed)
end

 

Demogorgon.zip

Share this post


Link to post
Share on other sites
Thomas Die    886

I don't think the jump animation is playing because you're switching to another when moving,
besides that do you have any other mods installed or something because everything is working normally?

Bt dubs you may want to save the peridic function as a variable so it doesn't duplicate itself everytime to tunnel
 

Share this post


Link to post
Share on other sites
Birdskull    14

No, I'm not using any other mods.


I switched to a periodic spawner and that works a lot better. Any idea why it's still duplicating itself, though? I've tried both  inst:RemoveComponent("periodicspawner") and inst.components.periodicspawner:Stop() but all I've managed to do is figure out several different ways to code the exact same failure. :???:

Still having issues with the jump animation not playing (though "jumpout" seems to work fine?). As you can see I tried StopMoving to pause and give the animation time to play, but nothing came of it so I think I'm doing something wrong.

The character still only reappears part of the time. Tree shadows still force a reappearance. I feel like I'm running in circles. :(

 

Current iteration:

 

--Tunnelling
local function Dig(inst)
    if  inst:HasTag("playerghost") then
        return
    end
    inst:AddComponent("periodicspawner")
    inst.components.periodicspawner:SetPrefab("mole_move_fx")
    inst.components.periodicspawner:SetRandomTimes(.05, .1)
    inst.components.periodicspawner:SetDensityInRange(.05, .1)
    inst.components.periodicspawner:Start()
end

local function StopDig(inst)
    if  inst:HasTag("playerghost") then
        return
    end
    inst:RemoveComponent("periodicspawner")
end

local function NotTunnelTime(inst, self)
    if  inst:HasTag("playerghost") then
        return
    end
    SpawnPrefab("dirt_puff").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.components.locomotor:StopMoving(2)
    inst.AnimState:PlayAnimation("jumpout")
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "demogorgon_speed_mod", 1)
    inst:RemoveTag("tunnelling")
    inst:AddTag("untunnelling")
    inst.DynamicShadow:Enable(true)
    inst:Show()
    
    ChangeToCharacterPhysics(inst)
end

local function TunnelTime(inst, self)
    if  inst:HasTag("playerghost") then
        return
    end
    SpawnPrefab("dirt_puff").Transform:SetPosition(inst.Transform:GetWorldPosition())
    inst.components.locomotor:StopMoving(2)
    inst.AnimState:PlayAnimation("jump")
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "demogorgon_speed_mod", .25)
    inst:RemoveTag("untunnelling")
    inst:AddTag("tunnelling")
    inst.DynamicShadow:Enable(false)
    inst:Hide()
    
    inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS)
    inst.Physics:ClearCollisionMask()
    inst.Physics:CollidesWith(COLLISION.WORLD)
    inst.Physics:CollidesWith(COLLISION.OBSTACLES)
end

local function OnKeyPressed(inst, data)    
  if data.inst == ThePlayer then
    if data.key == KEY_T and inst:HasTag("tunnelling") then
     NotTunnelTime(inst)
     StopDig(inst)
    elseif data.key == KEY_T and inst:HasTag("untunnelling") then
     TunnelTime(inst)
     Dig(inst)
     end
     end
     end

Share this post


Link to post
Share on other sites
Thomas Die    886

inst.tunneling = nil 

 

Local function Start_dig (inst)

  inst.tunneling = inst:DoPeriodicTask(2,function(inst) code end)

end

 

Local function End_dig(inst)

inst.tunneling = nil

end

 

Sorry this looks shod my phone is garbage rn

Share this post


Link to post
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