Moving a dynamically-created entity

Started by Max, March 15, 2018, 12:38:18 AM

Previous topic - Next topic
So I'm designing basically a Hinox enemy, who will throw bombs at you if you are within range. Everything is going great, and I just have a small problem that I'd like to know if I can fix a different way than I did.

Basically, I use map:create_bomb(), and then create a jump movement and "throw" the bomb toward the player that way. However, if I end up creating a new bomb before the old one explodes, the code "throws" the first bomb again instead of the newly created one, since the newly created bomb isn't called hinox_bomb, it's called hinox_bomb_2 since the first one is still around.

I got around this by just detonating the first bomb if it's still lying around before throwing a second one, which works well. But is there a better way to do this that anyone can think of?



The code for when the enemy throws bombs is this:

Code (lua) Select

function enemy:shoot()
    --first, check if the hero is in the same region
  local map = enemy:get_map()
  local hero = map:get_hero()
  if not enemy:is_in_same_region(hero) then
  return true  -- Repeat the timer.
  end

  local sprite = enemy:get_sprite()
  local x, y, layer = enemy:get_position()
  local direction = sprite:get_direction()

  sprite:set_animation("shooting")
  enemy:stop_movement()

    --destroy an old bomb before creating a new one (mainly so we don't move the old bomb with our movement)
    if map:has_entity("hinox_bomb") == true then
      local bombx, bomby, bombl = map:get_entity("hinox_bomb"):get_position()
      map:create_explosion({x = bombx, y = bomby, layer = bombl, })
      sol.audio.play_sound("explosion")
      map:get_entity("hinox_bomb"):remove()
    end

    --now let's create a new bomb after we give the enemy a second to pull it out of his pocket
  sol.timer.start(enemy, 400, function()
      map:create_bomb({
        name = "hinox_bomb", x = x, y = y, layer = layer,
      })
    --and throw the new bomb toward the player
      local bomb_toss = sol.movement.create("jump")
      local dir_to_hero = self:get_direction8_to(hero)
      bomb_toss:set_direction8(dir_to_hero)
      bomb_toss:set_distance(dist_hero + 16)
      bomb_toss:set_speed(90)
      bomb_toss:start(map:get_entity("hinox_bomb"))
      sol.audio.play_sound("throw")
    sprite:set_animation("walking")

      --now the enemy can go back to what it was doing before it threw the bomb
      self:go_random()
      self:check_hero()

  end)
end



If anyone is interested in the whole code, I can post that for general use also if nobody's already made an enemy like this. It's based on the Solarus Team's scripts, and is actually pretty fun to battle because the best strategy is to pick up its bombs and throw them back. This could also be used for a "King Moblin" type enemy, which has appeared in a few Zelda games.

Do not give names to bombs: that is useless. Instead, use a variable to store the bomb carried by the boss, if any, and update the variable when necessary.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."