Jump to content

A code detail about crabking SpawnCannonTower


Piggy_
  • Pending

I tried to take advantage of the cannontower spawned by crabking and give them additional abilities, but I can't get their entity data.

-- crabking.lua
local function SpawnCannonTower(inst, i, pt, numcannons)
    pt = pt or inst:FindCannonPositions(numcannons, i)

    if pt == nil then
        inst.cannontowers[i] = false

        return
    end

    local tower = SpawnPrefab("crabking_cannontower")

    tower.Transform:SetPosition(pt.x, 0, pt.z)

    local health = TUNING.CRABKING_CANNONTOWER_HEALTH
    if inst.gemcount.yellow > 4 then health = health + TUNING.CRABKING_CANNONTOWER_HEALTH end
    if inst.gemcount.yellow > 7 then health = health + TUNING.CRABKING_CANNONTOWER_HEALTH end

    if inst.gemcount.yellow >= 11 then
       health = health + TUNING.CRABKING_CANNONTOWER_HEALTH
    end

    tower.components.health:SetMaxHealth(health)
    tower.components.health:SetPercent(1) -- For pushing events?
    tower.redgemcount = inst.gemcount.red -- Saved in prefab.
    tower.yellowgemcount = inst.gemcount.yellow -- Saved in prefab.

    inst:ListenForEvent("onremove", inst.oncannontowerremoved, tower)

    inst.cannontowers[i] = tower

    local platform = tower:GetBoatIntersectingPhysics()
    local max_dist = platform ~= nil and math.max(0, platform:GetSafePhysicsRadius() - 2.5) or nil -- Maximum distance from platform origin.

    -- Too close to the platform border.
    if max_dist ~= nil and not tower:IsNear(platform, max_dist) then
        pt = tower:GetPositionAdjacentTo(platform, max_dist)

        -- Moves the tower closer to the platform origin and get the platform again for safety.
        tower.Transform:SetPosition(pt.x, 0, pt.z)
        platform = tower:GetBoatIntersectingPhysics()
    end

    if platform == nil or platform.components.health == nil then
        tower:PushEvent("ck_spawn") -- Open ocean, just spawn.

        return
    end

    -- Platform is leak proof, destroy it instantly.
    if platform ~= nil and platform.components.hullhealth.leakproof then
        platform:InstantlyBreakBoat()

        tower:PushEvent("ck_spawn")

        return
    end

    local destruction_radius = tower.Physics:GetRadius() + 0.8
    local leaks = TheSim:FindEntities(pt.x, 0, pt.z, destruction_radius, LEAK_MUST_TAGS)

    for i, leak in pairs(leaks) do
        leak:Remove()
    end

    platform.components.health:DoDelta(-TUNING.CRABKING_CANNONTOWER_HULL_SMASH_DAMAGE)
    platform.components.boatphysics:AddEmergencyBrakeSource("crabking_cannontower"..tower.GUID)

    if platform.leak_build_override ~= nil then
        tower.AnimState:AddOverrideBuild(platform.leak_build_override)
    end

    tower:PushEvent("ck_breach")

    tower:ListenForEvent("onremove", function()
        if platform:IsValid() then
            platform.components.boatphysics:RemoveEmergencyBrakeSource("crabking_cannontower"..tower.GUID)
        end
    end)

    return tower -- Mods.
end

It should be returned after the entity is spawned

if platform == nil or platform.components.health == nil then
  tower:PushEvent("ck_spawn") -- Open ocean, just spawn.

  return tower -- should return tower
end

-- Platform is leak proof, destroy it instantly.
if platform ~= nil and platform.components.hullhealth.leakproof then
  platform:InstantlyBreakBoat()

  tower:PushEvent("ck_spawn")

  return tower -- should return tower
end

 


Steps to Reproduce

As Above




User Feedback


There are no comments to display.



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

×
  • Create New...