Modding Help 1H "Gun" with Alt Ability

Discussion in 'Starbound Modding' started by Omeruin, Sep 13, 2020.

  1. Omeruin

    Omeruin Void-Bound Voyager

    Heya!
    I'm trying to make a special type of weapon, an Orb weapon. It uses and acts similar to a gun, but the visuals are different.
    Anyways, I want this 1H weapon to have an alt ability. I'm wondering how to call for the secondary ability in the Lua file?
    Everything is major WIP:
    .activeitem
    Code:
    {
      "itemName" : "angelorb1",
      "level" : 6,
      "price" : 1500,
      "maxStack" : 1,
      "rarity" : "Legendary",
      "description" : "Test",
      "shortdescription" : "Test Orb",
      "tooltipKind" : "gun",
      "category" : "uniqueWeapon",
      "twoHanded" : false,
      "itemTags" : ["weapon","ranged","orb"],
    
      "inventoryIcon" : "angelorb1.png:idle.1",
    
      "animation" : "/items/active/weapons/ranged/gun.animation",
      "animationParts" : {
        "butt" : "",
        "middle" : "angelorb1.png",
        "barrel" : "",
        "muzzleFlash" : ""
      },
      "animationCustom" : {
        "animatedParts" : {
          "stateTypes" : {
            "eyeState" : {
              "default" : "idle",
              "states" : {
                "idle" : {
                  "frames" : 1,
                  "cycle" : 10,
                  "mode" : "transition",
                  "transition" : "fire"
                },
                "fire" : {
                  "frames" : 1,
                  "cycle" : 0.5,
                  "mode" : "transition",
                  "transition" : "idle"
                }
              }
            }
          },
          "parts" : {
            "middle" : {
              "properties" : {
                "image" : null
              },
    
              "partStates" : {
                "eyeState" : {
                  "idle" : {
                    "properties" : {
                      "image" : "<partImage>:idle.<frame><paletteSwaps>"
                    }
                  },
                  "fire" : {
                    "properties" : {
                      "image" : "<partImage>:fire.<frame><paletteSwaps>"
                    }
                  }
                }
              }
            }
          }
        },
        "sounds" : {
          "fire" : [ "/sfx/gun/evileye1.ogg", "/sfx/gun/evileye2.ogg", "/sfx/gun/evileye3.ogg", "/sfx/gun/evileye4.ogg", "/sfx/gun/evileye5.ogg", "/sfx/gun/evileye6.ogg" ]
        }
      },
      "baseOffset" : [0.2, 0.25],
      "muzzleOffset" : [1, 0.330],
    
      "scripts" : ["/items/active/weapons/ranged/gun.lua"],
    
      "elementalType" : "physical",
    
      "primaryAbility" : {
        "scripts" : ["/items/active/weapons/other/Orbs/angelorbgunfire.lua"],
        "class" : "GunFire",
    
        "fireTime" : 0.18,
        "baseDps" : 8,
        "energyUsage" : 24,
        "inaccuracy" : 0.02,
    
        "burstTime" : 0.075,
        "burstCount" : 2,
        "fireType" : "burst",
    
        "projectileCount" : 1,
    
        "projectileType" : "evileyeball",
        "projectileParameters" : {
          "knockback" : 5,
          "statusEffects" : [ ]
        },
    
        "stances" : {
          "idle" : {
            "armRotation" : 0,
            "weaponRotation" : 0,
            "twoHanded" : true,
    
            "allowRotate" : true,
            "allowFlip" : true
          },
          "fire" : {
            "duration" : 0,
            "armRotation" : 3,
            "weaponRotation" : 3,
            "twoHanded" : true,
    
            "animationStates" : {
              "eyeState" : "fire"
            },
    
            "allowRotate" : true,
            "allowFlip" : true
          },
          "cooldown" : {
            "duration" : 0.075,
            "armRotation" : 3,
            "weaponRotation" : 3,
            "twoHanded" : true,
    
            "allowRotate" : true,
            "allowFlip" : true
          }
        }
      },
    
      "altAbilityType" : "angelorb1",
    
      "builder" : "/items/buildscripts/buildunrandweapon.lua"
    }
    
    angelorbgunfire.lua
    Code:
    require "/scripts/util.lua"
    require "/scripts/interp.lua"
    require "/items/active/weapons/ranged/gun.lua"
    
    -- Base gun fire ability
    GunFire = WeaponAbility:new()
    
    function GunFire:init()
      self.weapon:setStance(self.stances.idle)
    
      self.cooldownTimer = self.fireTime
    
      self.weapon.onLeaveAbility = function()
        self.weapon:setStance(self.stances.idle)
      end
    end
    
      function GunFire:update(dt, fireMode, shiftHeld)
        if fireMode == "primary" and not shiftHeld then
        WeaponAbility.update(self, dt, fireMode, shiftHeld)
    
      self.cooldownTimer = math.max(0, self.cooldownTimer - self.dt)
    
      if animator.animationState("firing") ~= "fire" then
        animator.setLightActive("muzzleFlash", false)
            elseif fireMode == "primary" and shiftHeld then
            self.weapon.secondaryAbility
      end
        end
    
    
      if self.fireMode == (self.activatingFireMode or self.abilitySlot)
        and not self.weapon.currentAbility
        and self.cooldownTimer == 0
        and not status.resourceLocked("energy")
        and not world.lineTileCollision(mcontroller.position(), self:firePosition()) then
    
        if self.fireType == "auto" and status.overConsumeResource("energy", self:energyPerShot()) then
          self:setState(self.auto)
        elseif self.fireType == "burst" then
          self:setState(self.burst)
        end
      end
    end
    
    function GunFire:auto()
      self.weapon:setStance(self.stances.fire)
    
      self:fireProjectile()
      self:muzzleFlash()
    
      if self.stances.fire.duration then
        util.wait(self.stances.fire.duration)
      end
    
      self.cooldownTimer = self.fireTime
      self:setState(self.cooldown)
    end
    
    function GunFire:burst()
      self.weapon:setStance(self.stances.fire)
    
      local shots = self.burstCount
      while shots > 0 and status.overConsumeResource("energy", self:energyPerShot()) do
        self:fireProjectile()
        self:muzzleFlash()
        shots = shots - 1
    
        self.weapon.relativeWeaponRotation = util.toRadians(interp.linear(1 - shots / self.burstCount, 0, self.stances.fire.weaponRotation))
        self.weapon.relativeArmRotation = util.toRadians(interp.linear(1 - shots / self.burstCount, 0, self.stances.fire.armRotation))
    
        util.wait(self.burstTime)
      end
    
      self.cooldownTimer = (self.fireTime - self.burstTime) * self.burstCount
    end
    
    function GunFire:cooldown()
      self.weapon:setStance(self.stances.cooldown)
      self.weapon:updateAim()
    
      local progress = 0
      util.wait(self.stances.cooldown.duration, function()
        local from = self.stances.cooldown.weaponOffset or {0,0}
        local to = self.stances.idle.weaponOffset or {0,0}
        self.weapon.weaponOffset = {interp.linear(progress, from[1], to[1]), interp.linear(progress, from[2], to[2])}
    
        self.weapon.relativeWeaponRotation = util.toRadians(interp.linear(progress, self.stances.cooldown.weaponRotation, self.stances.idle.weaponRotation))
        self.weapon.relativeArmRotation = util.toRadians(interp.linear(progress, self.stances.cooldown.armRotation, self.stances.idle.armRotation))
    
        progress = math.min(1.0, progress + (self.dt / self.stances.cooldown.duration))
      end)
    end
    
    function GunFire:muzzleFlash()
      animator.setPartTag("muzzleFlash", "variant", math.random(1, self.muzzleFlashVariants or 3))
      animator.setAnimationState("firing", "fire")
      animator.burstParticleEmitter("muzzleFlash")
      animator.playSound("fire")
    
      animator.setLightActive("muzzleFlash", true)
    end
    
    function GunFire:fireProjectile(projectileType, projectileParams, inaccuracy, firePosition, projectileCount)
      local params = sb.jsonMerge(self.projectileParameters, projectileParams or {})
      params.power = self:damagePerShot()
      params.powerMultiplier = activeItem.ownerPowerMultiplier()
      params.speed = util.randomInRange(params.speed)
    
      if not projectileType then
        projectileType = self.projectileType
      end
      if type(projectileType) == "table" then
        projectileType = projectileType[math.random(#projectileType)]
      end
    
      local projectileId = 0
      for i = 1, (projectileCount or self.projectileCount) do
        if params.timeToLive then
          params.timeToLive = util.randomInRange(params.timeToLive)
        end
    
        projectileId = world.spawnProjectile(
            projectileType,
            firePosition or self:firePosition(),
            activeItem.ownerEntityId(),
            self:aimVector(inaccuracy or self.inaccuracy),
            false,
            params
          )
      end
      return projectileId
    end
    
    function GunFire:firePosition()
      return vec2.add(mcontroller.position(), activeItem.handPosition(self.weapon.muzzleOffset))
    end
    
    function GunFire:aimVector(inaccuracy)
      local aimVector = vec2.rotate({1, 0}, self.weapon.aimAngle + sb.nrand(inaccuracy, 0))
      aimVector[1] = aimVector[1] * mcontroller.facingDirection()
      return aimVector
    end
    
    function GunFire:energyPerShot()
      return self.energyUsage * self.fireTime * (self.energyUsageMultiplier or 1.0)
    end
    
    function GunFire:damagePerShot()
      return (self.baseDamage or (self.baseDps * self.fireTime)) * (self.baseDamageMultiplier or 1.0) * config.getParameter("damageLevelMultiplier") / self.projectileCount
    end
    
    function GunFire:uninit()
    end
    
    I'm trying to use the angelorbgunfire.lua to define the controls. Is this the right thing to do?
    If so, how do I call the Lua to use the alternate ability? Specifically at:
    Code:
      if animator.animationState("firing") ~= "fire" then
        animator.setLightActive("muzzleFlash", false)
            elseif fireMode == "primary" and shiftHeld then
            self.weapon.secondaryAbility
      end
        end
    
    self.weapon.secondaryAbility is right there at the moment, but that doesn't work.

    Any help would be appreciated! Thank you!
     
  2. projectmayhem

    projectmayhem Ketchup Robot

    I dont think you can have alt ability on a 1 handed weapon, since the secondary mouse button is used for the other hand. I might be mistaken though
     
    Omeruin likes this.
  3. Omeruin

    Omeruin Void-Bound Voyager

    Yeah, I'm starting to think so as well.
    I was told that perhaps coding the alt-ability to use a different button would work, but people I asked didn't have a clue. I think some people thought I was trying to make a 2H weapon with 3 abilities instead of a 1H with just 2.
    I figured out how to have the 1H weapon use the special alt ability as the main attack instead. :nuruconfused:
    It fits I guess so oh well.

    Thanks for replying!
     

Share This Page