Modding Help Getting vehicle to despawn

Discussion in 'Starbound Modding' started by projectmayhem, Mar 23, 2019.

  1. projectmayhem

    projectmayhem Spaceman Spiff

    Trying to work on a mod that adds combat mounts. I've got them to spawn in with the Controllers, but when i click to despawn them back into the controllers, they stay. However, the controller recognizing them as back inside and ready to place again. This results in being able to put down a numerous amount of a single mount. Can anyone help me figure out what I am doing wrong? Here is the lua im using for the mounts.


    Code:
    require "/scripts/vec2.lua"
    
    function init()
     
      self.fireTimer = 0
      animator.rotateGroup("guns", 0, true)
      self.level = config.getParameter("mechLevel", 6)
      self.groundFrames = 1
     
       self.protection = config.getParameter("protection")
      self.maxHealth = config.getParameter("maxHealth")
        --this comes in from the controller.
    
      self.ownerKey = config.getParameter("ownerKey")
      vehicle.setPersistent(self.ownerKey)
     
       --assume maxhealth
      if (storage.health) then
        animator.setAnimationState("movement", "idle")
      else
        local startHealthFactor = config.getParameter("startHealthFactor")
    
        if (startHealthFactor == nil) then
            storage.health = self.maxHealth
        else
           storage.health = math.min(startHealthFactor * self.maxHealth, self.maxHealth)
        end
        animator.setAnimationState("movement", "warpInPart1")
      end
    
      --setup the store functionality
      message.setHandler("store",
          function(_, _, ownerKey)
            if (self.ownerKey and self.ownerKey == ownerKey and self.driver == nil and animator.animationState("movement") == "idle") then
              animator.setAnimationState("movement", "warpOutPart1")
               return {storable = true, healthFactor = storage.health / self.maxHealth}
            else
              return {storable = false, healthFactor = storage.health / self.maxHealth}
            end
          end)
    
     
    end
    
    function update()
       
      if mcontroller.atWorldLimit() then
        vehicle.destroy()
        return
      end
     
      if (animator.animationState("movement") == "invisible") then
        vehicle.destroy()
       
        local healthFactor = storage.health / self.maxHealth
           
      end
    
      local mechAimLimit = config.getParameter("mechAimLimit") * math.pi / 180
      local mechHorizontalMovement = config.getParameter("mechHorizontalMovement")
      local mechJumpVelocity = config.getParameter("mechJumpVelocity")
      local mechFireCycle = config.getParameter("mechFireCycle")
      local mechProjectile = config.getParameter("mechProjectile")
      local mechProjectileConfig = config.getParameter("mechProjectileConfig")
      local offGroundFrames = config.getParameter("offGroundFrames")
    
      local mechCollisionPoly = mcontroller.collisionPoly()
      local position = mcontroller.position()
    
      if mechProjectileConfig.power then
        mechProjectileConfig.power = root.evalFunction("weaponDamageLevelMultiplier", self.level) * mechProjectileConfig.power
      end
    
      local entityInSeat = vehicle.entityLoungingIn("seat")
      if entityInSeat then
        vehicle.setDamageTeam(world.entityDamageTeam(entityInSeat))
      else
        vehicle.setDamageTeam({type = "passive"})
      end
    
      local diff = world.distance(vehicle.aimPosition("seat"), mcontroller.position())
      local aimAngle = math.atan(diff[2], diff[1])
      local facingDirection = (aimAngle > math.pi / 2 or aimAngle < -math.pi / 2) and -1 or 1
    
      if facingDirection < 0 then
        animator.setFlipped(true)
    
        if aimAngle > 0 then
          aimAngle = math.max(aimAngle, math.pi - mechAimLimit)
        else
          aimAngle = math.min(aimAngle, -math.pi + mechAimLimit)
        end
    
        animator.rotateGroup("guns", math.pi - aimAngle)
      else
        animator.setFlipped(false)
    
        if aimAngle > 0 then
          aimAngle = math.min(aimAngle, mechAimLimit)
        else
          aimAngle = math.max(aimAngle, -mechAimLimit)
        end
    
        animator.rotateGroup("guns", aimAngle)
      end
    
      local onGround = mcontroller.onGround()
      local movingDirection = 0
    
      if vehicle.controlHeld("seat", "left") and onGround then
        mcontroller.setXVelocity(-mechHorizontalMovement)
        movingDirection = -1
      end
    
      if vehicle.controlHeld("seat", "right") and onGround then
        mcontroller.setXVelocity(mechHorizontalMovement)
        movingDirection = 1
      end
    
      if onGround then
        self.groundFrames = offGroundFrames
      else
        self.groundFrames = self.groundFrames - 1
      end
    
      if vehicle.controlHeld("seat", "jump") and onGround then
        mcontroller.setXVelocity(mechJumpVelocity[1] * movingDirection)
        mcontroller.setYVelocity(mechJumpVelocity[2])
        animator.setAnimationState("movement", "jump")
        self.groundFrames = 0
      end
    
      if self.groundFrames <= 0 then
        if mcontroller.velocity()[2] > 0 then
          animator.setAnimationState("movement", "jump")
        else
          animator.setAnimationState("movement", "fall")
        end
      elseif movingDirection ~= 0 then
        if facingDirection ~= movingDirection then
          animator.setAnimationState("movement", "backWalk")
        else
          animator.setAnimationState("movement", "walk")
        end
      elseif onGround then
        animator.setAnimationState("movement", "idle")
      end
    
      if vehicle.controlHeld("seat", "primaryFire") then
        if self.fireTimer <= 0 then
          world.spawnProjectile(mechProjectile, vec2.add(mcontroller.position(), animator.partPoint("frontGun", "firePoint")), entity.id(), {math.cos(aimAngle), math.sin(aimAngle)}, false, mechProjectileConfig)
          self.fireTimer = self.fireTimer + mechFireCycle
          animator.setAnimationState("frontFiring", "fire")
        else
          local oldFireTimer = self.fireTimer
          self.fireTimer = self.fireTimer - script.updateDt()
          if oldFireTimer > mechFireCycle / 2 and self.fireTimer <= mechFireCycle / 2 then
            world.spawnProjectile(mechProjectile, vec2.add(mcontroller.position(), animator.partPoint("backGun", "firePoint")), entity.id(), {math.cos(aimAngle), math.sin(aimAngle)}, false, mechProjectileConfig)
            animator.setAnimationState("backFiring", "fire")
          end
        end
      end
    end
    
    function applyDamage(damageRequest)
      local damage = 0
      if damageRequest.damageType == "Damage" then
        damage = damage + root.evalFunction2("protection", damageRequest.damage, self.protection)
      elseif damageRequest.damageType == "IgnoresDef" then
        damage = damage + damageRequest.damage
      else
        return {}
      end
    
      setDamageEmotes()
    
      updateVisualEffects(storage.health, damage)
    
      local healthLost = math.min(damage, storage.health)
      storage.health = storage.health - healthLost
    
      return {{
        sourceEntityId = damageRequest.sourceEntityId,
        targetEntityId = entity.id(),
        position = mcontroller.position(),
        damageDealt = damage,
        healthLost = healthLost,
        hitType = "Hit",
        damageSourceKind = damageRequest.damageSourceKind,
        targetMaterialKind = self.materialKind,
        killed = storage.health <= 0
      }}
    end


    and here is the animation file

    Code:
    {
      "animatedParts" : {
        "stateTypes" : {
          "movement" : {
            "default" : "idle",
            "states" : {
            "warpInPart1" : {
                "frames" : 4, 
                "cycle" : 0.33,
                "mode" : "transition",
                "transition" : "warpInPart2"
                },
    
              "warpInPart2" : {
                "frames" : 4, 
                "cycle" : 0.33,
                "mode" : "transition",
                "transition" : "idle"
                },
                 "warpOutPart1" : {
                "frames" : 4, 
                "cycle" : 0.33,
                "mode" : "transition",
                "transition" : "warpOutPart2"
                },
    
              "warpOutPart2" : {
                "frames" : 4, 
                "cycle" : 0.33,
                "mode" : "transition",
                "transition" : "invisible"
                },
                "invisible" : {},
               
              "idle" : { },
    
              "jump" : {
                "frames" : 1,
                "cycle" : 0.5,
                "mode" : "end"
              },
    
              "fall" : {
                "frames" : 1,
                "cycle" : 0.5,
                "mode" : "end"
              },
    
              "walk" : {
                "frames" : 8,
                "cycle" : 1.0,
                "mode" : "loop"
              },
    
              "backWalk" : {
                "frames" : 8,
                "cycle" : 1.0,
                "mode" : "loop"
              }
            }
          },
    
          "frontFiring" : {
            "default" : "off",
    
            "states" : {
              "fire" : {
                "frames" : 3,
                "cycle" : 0.15,
                "mode" : "transition",
                "transition" : "off",
                "properties" : {
                  "immediateSound" : "/sfx/organic-texture-whoosh1.wav"
                }
              },
    
              "off" : { }
            }
          },
    
          "backFiring" : {
            "default" : "off",
    
            "states" : {
              "fire" : {
                "frames" : 3,
                "cycle" : 0.15,
                "mode" : "transition",
                "transition" : "off",
                "properties" : {
                  "immediateSound" : ""
                }
              },
    
              "off" : { }
            }
          }
        },
    
        "parts" : {
          "body" : {
            "properties" : {
              "centered" : true,
              "zLevel" : -1,
              "sitPosition" : [-0.75, 1.0]
            },
    
            "partStates" : {
              "movement" : {
              "warpInPart1" : {
                  "properties" : {
                    "image" : ""
                  }
                },
                "warpInPart2" : {
                  "properties" : {
                    "image" : "falcon<colour>foreground.png:move.<damageState>"
                  }
                },
                 "warpOutPart1" : {
                  "properties" : {
                    "image" : "falcon<colour>foreground.png:move.<damageState>"
                  }
                },
    
                "warpOutPart2" : {
                  "properties" : {}
                },
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:idle",
                    "immediateSound" : ""
                  },
                  "frameProperties" : {
                    "offset" : [ [0, 0.5], [0, 0.5], [0, 0.375], [0, 0.375] ]
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  },
                  "frameProperties" : {
                    "offset" : [ [0, 0.25], [0, 0.25], [0, 0.125], [0, 0] ]
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  },
                  "frameProperties" : {
                  "immediateSound" : [ "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "" ],
                    "offset" : [ [0, 0.375], [0, 0.125], [0, 0], [0, 0.125], [0, 0.25], [0, 0.375], [0, 0.125], [0, 0], [0, 0.125], [0, 0.25] ]
                  }
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  },
                  "frameProperties" : {
                  "immediateSound" : [ "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "", "/sfx/mount-footstep.wav", "" ],
                    "offset" : [ [0, 0.375], [0, 0.125], [0, 0], [0, 0.125], [0, 0.25], [0, 0.375], [0, 0.125], [0, 0], [0, 0.125], [0, 0.25] ]
                  }
                }
              }
            }
          },
         
          "warp" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 3,
              "fullbright" : true,
              "transformationGroups" : ["flip", "rotation"]
            },
    
            "partStates" : {
    
              "movement" : {
    
                "warpInPart1" : {
                  "properties" : {
                    "image" : "mount2warp.png:warpInPart1.<frame>"
                  }
                },
                "warpInPart2" : {
                  "properties" : {
                    "image" : "mount2warp.png:warpInPart2.<frame>"
                  }
                },
    
    
                "idle" : {
                  "properties" : {}
                },
    
    
                "warpOutPart1" : {
                  "properties" : {
                    "image" : "mount2warp.png:warpOutPart1.<frame>"
                  }
                },
                "warpOutPart2" : {
                  "properties" : {
                    "image" : "mount2warp.png:warpOutPart2.<frame>"
                  }
                },
    
    
                "invisible" : {
                  "properties" : {}
                }
    
              }
            }
          },
    
          "backLeg" : {
            "properties" : {
              "centered" : true,
              "zLevel" : -2,
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "movement" : {
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:jump.<frame>"
                  },
                  "frameProperties" : {
                    "offset" : [ [0, -0.5], [0, -0.5], [0, -0.375], [0, -0.375] ]
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:fall.<frame>"
                  },
                  "frameProperties" : {
                   "offset" : [ [0, -0.5], [0, -0.5], [0, -0.375], [0, -0.375] ]
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:move.<frame>"
                  }
                 
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:bmove.<frame>"
                  }
                 
                }
              }
            }
          },
    
          "background" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 1,
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "movement" : {
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                }
              }
            }
          },
    
          "backGunFire" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 1.5,
              "offset" : [6, -0.35],
              "rotationGroup" : "guns",
              "rotationCenter" : [2.4, 0],
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "backFiring" : {
                "fire" : {
                  "properties" : {
                    "image" : "/vehicles/mech/blankmuzzle.png:<frameIndex>"
                  }
                }
              }
            }
          },
    
          "backGun" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 2,
              "rotationGroup" : "guns",
              "offset" : [2, 0],
              "rotationCenter" : [2.4, 0],
              "firePoint" : [2.5, -0.35],
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "movement" : {
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                }
              }
            }
          },
    
          "frontGunFire" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 3.5,
              "offset" : [2, -0.35],
              "rotationGroup" : "guns",
              "rotationCenter" : [0.4, -0.25],
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "frontFiring" : {
                "fire" : {
                  "properties" : {
                    "image" : "/vehicles/mech/blankmuzzle.png:<frameIndex>"
                  }
                }
              }
            }
          },
    
          "frontGun" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 4,
              "rotationGroup" : "guns",
              "rotationCenter" : [0.4, -0.25],
              "firePoint" : [2.0, -0.35],
              "anchorPart" : "body"
            },
    
            "partStates" : {
              "movement" : {
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:rotation"
                  }
                }
              }
            }
          },
    
          "frontLeg" : {
            "properties" : {
              "centered" : true,
              "zLevel" : 0,
              "anchorPart" : "background"
            },
    
            "partStates" : {
              "movement" : {
                "idle" : {
                  "properties" : {
                    "image" : "<partImage>:idle"
                  }
                },
                "jump" : {
                  "properties" : {
                    "image" : "<partImage>:jump.<frame>"
                  },
                  "frameProperties" : {
                     "offset" : [ [0, -0.5], [0, -0.5], [0, -0.375], [0, -0.375] ]
                  }
                },
                "fall" : {
                  "properties" : {
                    "image" : "<partImage>:fall.<frame>"
                  },
                  "frameProperties" : {
                    "offset" : [ [0, -0.5], [0, -0.5], [0, -0.375], [0, -0.375] ]
                  }
                },
                "walk" : {
                  "properties" : {
                    "image" : "<partImage>:move.<frame>"
                  }
                 
                },
                "backWalk" : {
                  "properties" : {
                    "image" : "<partImage>:bmove.<frame>"
                  }
                 
                }
              }
            }
          }
        }
      },
    
      "rotationGroups" : {
        "guns" : {
          "angularVelocity" : 10.0
        }
      }
    }
    



    And last.... the vehicle file.

    Code:
    {
      "name" : "mount2",
      "script" : "mech.lua",
      "boundBox" : [-5, -5, 5, 5],
      "slaveControlTimeout" : 2.0,
      "slaveControlHeartbeat" : 1.0,
      "canBeHit" : true,
      "animation" : "mount2.animation",
    
    "animationPartTags" : {
        "backLeg" : {
          "partImage" : "/vehicles/mount2/mount2bleg.png"
        },
        "background" : {
          "partImage" : "/vehicles/mount2/mount2background.png"
        },
        "backGun" : {
          "partImage" : "/vehicles/mount2/invisgun.png"
        },
        "body" : {
          "partImage" : "/vehicles/mount2/mount2foreground.png"
        },
        "frontGun" : {
          "partImage" : "/vehicles/mount2/invisgun.png"
        },
        "frontLeg" : {
          "partImage" : "/vehicles/mount2/mount2fleg.png"
        }
      },
    
      "movementSettings" : {
        "collisionPoly" : [ [-3.0, 0.0], [-2, -2.0], [2, -2.0], [3.0, 0.0], [3.0, 1], [2, 1.5], [-2, 1.5], [-3.0, 1] ],
        "mass" : 8,
        "airFriction" : 0.5,
        "groundFriction" : 20.0,
        "ignorePlatformCollision" : true,
       
      },
    
      "loungePositions" : {
        "seat" : {
          "part" : "body",
          "partAnchor" : "sitPosition",
          "orientation" : "sit"
        }
      },
     
     
      "protection" : 75,
      "maxHealth" : 2000,
     
     
      "mechAimLimit" : 40,
      "mechFireCycle" : 5.0,
      "mechProjectile" : "mount2aoe",
      "mechProjectileConfig" : {
        "power" : 0
      },
      "mechLevel" : 6
    }
    
     

Share This Page