Author Topic: Moving a dynamically-created entity  (Read 338 times)

Max

  • Full Member
  • ***
  • Posts: 174
    • View Profile
Moving a dynamically-created entity
« on: March 15, 2018, 12:38:18 am »
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
  1.         function enemy:shoot()
  2.     --first, check if the hero is in the same region
  3.           local map = enemy:get_map()
  4.           local hero = map:get_hero()
  5.           if not enemy:is_in_same_region(hero) then
  6.                 return true  -- Repeat the timer.
  7.           end
  8.  
  9.           local sprite = enemy:get_sprite()
  10.           local x, y, layer = enemy:get_position()
  11.           local direction = sprite:get_direction()
  12.  
  13.           sprite:set_animation("shooting")
  14.           enemy:stop_movement()
  15.  
  16.     --destroy an old bomb before creating a new one (mainly so we don't move the old bomb with our movement)
  17.     if map:has_entity("hinox_bomb") == true then
  18.       local bombx, bomby, bombl = map:get_entity("hinox_bomb"):get_position()
  19.       map:create_explosion({x = bombx, y = bomby, layer = bombl, })
  20.       sol.audio.play_sound("explosion")
  21.       map:get_entity("hinox_bomb"):remove()
  22.     end
  23.  
  24.     --now let's create a new bomb after we give the enemy a second to pull it out of his pocket
  25.           sol.timer.start(enemy, 400, function()
  26.       map:create_bomb({
  27.         name = "hinox_bomb", x = x, y = y, layer = layer,
  28.       })
  29.     --and throw the new bomb toward the player
  30.       local bomb_toss = sol.movement.create("jump")
  31.       local dir_to_hero = self:get_direction8_to(hero)
  32.       bomb_toss:set_direction8(dir_to_hero)
  33.       bomb_toss:set_distance(dist_hero + 16)
  34.       bomb_toss:set_speed(90)
  35.       bomb_toss:start(map:get_entity("hinox_bomb"))
  36.       sol.audio.play_sound("throw")
  37.           sprite:set_animation("walking")
  38.  
  39.       --now the enemy can go back to what it was doing before it threw the bomb
  40.       self:go_random()
  41.       self:check_hero()
  42.  
  43.           end)
  44.         end
  45.  


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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1026
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Moving a dynamically-created entity
« Reply #1 on: March 15, 2018, 05:19:15 am »
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.”