Author Topic: Multiple enemies not acting the same  (Read 130 times)

Starlock

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Multiple enemies not acting the same
« on: August 06, 2018, 06:18:11 am »
I have an enemy that creates small spheres that circle around the enemy and are thrown at the hero. However, when I have 2 enemies, only one of them will do the proper attack.

Code: Lua
  1. local enemy = ...
  2. local game = enemy:get_game()
  3.  
  4. local can_shoot = true
  5.  
  6.  
  7. local sprite = nil
  8. local nb_bats_created = 0
  9. local nb_to_create = 0
  10. local raintimer = nil
  11.  
  12. function enemy:on_created()
  13.   -- Set properties.
  14.   self:set_life(4); self:set_damage(6); self:set_hurt_style("normal")
  15.   self:set_pushed_back_when_hurt(true); self:set_push_hero_on_sword(false)
  16.   self:set_traversable(true)
  17.   -- Create sprite if necessary, etc.
  18.   sprite = self:get_sprite()
  19.   if not sprite then sprite = self:create_sprite("enemies/more/dark/rainbow") end
  20.   function sprite:on_animation_finished(animation)
  21.         if animation == "looking" then sprite:set_animation("stopped") end --***
  22.   end
  23.   -- Start enemy.
  24.   self:restart()
  25. end
  26.  
  27.  
  28.  
  29. local function go_hero()
  30.  
  31.   local map = enemy:get_map()
  32.   local hero = map:get_hero()
  33.  
  34.   if enemy:get_distance(hero) < 120 then
  35.  
  36.   local sprite = enemy:get_sprite()
  37.   sprite:set_animation("walking")
  38.   local movement = sol.movement.create("target")
  39.   movement:set_speed(64)
  40.   movement:start(enemy)
  41.  
  42.   else
  43.  
  44.   local sprite = enemy:get_sprite()
  45.   sprite:set_animation("walking")
  46.   local movement = sol.movement.create("random")
  47.   movement:set_speed(64)
  48.   movement:start(enemy)
  49.   end
  50. end
  51.  
  52.  
  53.  
  54.  
  55. function enemy:attack()
  56.  
  57.  
  58.  
  59.   if jumping or attacking then
  60.     return
  61.   end
  62.  
  63.   if cancel_next_attack then
  64.     cancel_next_attack = false
  65.     return
  66.   end
  67.  
  68.   attacking = true
  69.   sprite:set_animation("shooting")
  70.  
  71.   local prefix = "bat" .. "_bat_"
  72.   nb_to_create = 6
  73.  
  74.   function repeat_throw_bat()
  75.  
  76.     sol.audio.play_sound("lamp")
  77.     nb_bats_created = nb_bats_created + 1
  78.     local son_name = prefix .. nb_bats_created
  79.     local son = enemy:create_enemy{
  80.       name = son_name,
  81.       breed = "more/dark/rainattack",
  82.       x = 0,
  83.       y = 0,
  84.       layer = 0,
  85.     }
  86.     if math.random(6) == 1 then
  87.       son:set_treasure("magic_flask", 1)
  88.     end
  89.     son:go_circle(enemy)
  90.     local go_hero_delay = 2000 + (nb_to_create * 150)
  91.     sol.timer.start(son, go_hero_delay, function() son:go_hero() end)
  92.  
  93.     nb_to_create = nb_to_create - 1
  94.     if nb_to_create > 0 then
  95.       raintimer = sol.timer.start(enemy:get_map(), 350, repeat_throw_bat)
  96.     else
  97.       attacking = false
  98.       attack_scheduled = false
  99.       if not vulnerable then
  100.   sol.timer.start(2000, function()
  101.         go_hero()
  102.   end)
  103.       else
  104.         cancel_next_attack = false
  105.       end
  106.     end
  107.   end
  108.   enemy:stop_movement()
  109.   enemy:get_sprite():set_direction(0)
  110.   repeat_throw_bat()
  111. end
  112.  
  113. function enemy:on_hurt()
  114.   nb_to_create = 0
  115.   if raintimer ~= nil then
  116.   raintimer:stop()
  117.   end
  118.   enemy:get_map():remove_entities("bat" .. "_bat_")
  119. end
  120.  
  121. function enemy:on_dying()
  122.   nb_to_create = 0
  123.   if raintimer ~= nil then
  124.   raintimer:stop()
  125.   end
  126.   enemy:get_map():remove_entities("bat" .. "_bat_")
  127. end
  128.  
  129. function enemy:on_restarted()
  130.  
  131.   local map = enemy:get_map()
  132.   local hero = map:get_hero()
  133.  
  134.   go_hero()
  135.  
  136.   can_shoot = true
  137.  
  138.   sol.timer.start(enemy, 100, function()
  139.  
  140.     local hero_x, hero_y = hero:get_position()
  141.     local x, y = enemy:get_center_position()
  142.  
  143.     if can_shoot then
  144. if enemy:get_distance(hero) < 150 then
  145.         attack()
  146.         can_shoot = false
  147.         sol.timer.start(enemy, 6666, function()
  148.           can_shoot = true
  149.         end)
  150.       end
  151.     end
  152.     return true  -- Repeat the timer.
  153.   end)
  154. end
  155.  
  156. function enemy:on_movement_changed(movement)
  157.  
  158.   local direction4 = movement:get_direction4()
  159.   local sprite = self:get_sprite()
  160.   sprite:set_direction(direction4)
  161. end
  162.  
  163.  

Diarandor

  • Hero Member
  • *****
  • Posts: 1020
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Multiple enemies not acting the same
« Reply #1 on: August 06, 2018, 08:23:15 am »
Some of your state variables, like 'attacking', are not local.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”