Object you can throw over and over

Started by Max, March 15, 2019, 04:41:51 PM

Previous topic - Next topic
March 15, 2019, 04:41:51 PM Last Edit: March 15, 2019, 07:12:28 PM by Max
Hey, so I coded this custom entity that's a lot like the iron ball from A Link to the Past's Eagle Tower- it's an object the hero can pick up and throw, and then pick up again and keep on throwing. You can even carry it to other maps and keep smashing it into enemies faces. I can it "toss_ball", haha.

You can set the properties "weight" and "damage" on the custom entity, which by default are 1 and 6. When this entity hits an enemy, it'll try to call enemy:hit_by_toss_ball() , so if you want certain enemies to exhibit different behavior on being hit by this than being damaged, define that function for those enemies (if, for example, you wanted an enemy to have its shield destroyed, or become vulnerable when hit with this or something.). Note: hit_by_toss_ball() will be called whether the hero throws the ball into the enemy, but also if the enemies runs into the entity while it's on the ground.


local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local DEFAULT_DAMAGE = 6


function entity:on_created()
  entity:set_traversable_by(false)
  entity:set_drawn_in_y_order(true)
  entity:set_follow_streams(true)
  entity:set_traversable_by("enemy", true)
  entity:set_weight(1)
  if entity:get_property("weight") then
    entity:set_weight(entity:get_property("weight"))
  end
end

local enemies_touched = {}
entity:add_collision_test("sprite", function(entity, other)
  if other:get_type() == "enemy" then
    local enemy = other
    if not enemies_touched[enemy] and enemy:hit_by_toss_ball() then
      enemy:hit_by_toss_ball()
    end
    enemies_touched[enemy] = enemy
    sol.timer.start(map, 1000, function() enemies_touched[enemy] = false end)
  end
end)

function entity:on_lifting(carrier, carried_object)
  carried_object:set_damage_on_enemies(game:get_value(DEFAULT_DAMAGE)
  if entity:get_property("damage") then
    entity:set_weight(entity:get_property("damage"))
  end
  carried_object:set_destruction_sound("running_obstacle")

  --landing, and therefore needing to create a new toss_ball
  function carried_object:on_breaking()
    local x, y, layer = carried_object:get_position()
    local width, height = carried_object:get_size()
    local sprite = carried_object:get_sprite()
    local direction = sprite:get_direction()

    if carried_object:get_ground_below() == "wall" then y = y + 34 end
    carried_object:get_map():create_custom_entity({
      width = width, height = height, x = x, y = y, layer = layer,
      direction = direction, model = "toss_ball", sprite = sprite:get_animation_set()
    })


  end

end



If you see any errors or mistakes I've made, please let me know so I can fix them! Have fun using this, modifying it, whatever you want.