Here is a new script.
I added a feature to choose if the enemy will reappear or not. If you want to reset the enemy every time you enter the region, just add "reappear" in the enemy name. If the enemy is dead, it will be recreated. If it is alive, the life counter will be reset.
For a weird reason, when I paste the code, the indentations are not correct. Sorry for this.
manager_separator.lua
-- This script manages enemies when there are separators in a map.
-- Enemies that are prefixed by "auto_enemy" are automatically
-- reset at their initial position when taking separators prefixed by "auto_separator".
local separator_manager = {}
function separator_manager:manage_map(map)
local enemy_places = {}
-- Store the position and properties of enemies.
for enemy in map:get_entities("auto_enemy") do
local x, y, layer = enemy:get_position()
local spriteTemp = enemy:get_sprite()
local reappear = 0
if( string.find(enemy:get_name(), "reappear") ~= nil ) then
reappear = 1
else
reappear = 0
end
enemy_places[#enemy_places + 1] = {
x = x,
y = y,
layer = layer,
--direction = enemy:get_sprite():get_direction(),
enemy = enemy,
-- Get breed to recreate it if it has to reappear
breed = enemy:get_breed(),
name = enemy:get_name(),
life = enemy:get_life(),
--Set if enemy should reappear or not
reappear = reappear,
}
enemy:set_enabled(false)
end
-- Function called when a separator was just taken.
local function separator_on_activating(separator)
local hero = map:get_hero()
for _, enemy_place in ipairs(enemy_places) do
local enemy = enemy_place.enemy
--Check if enemy is tagged to reappear unpon entering the screen
if( enemy_place.reappear == 1 ) then
-- if enemy is destroyed or about to be
if( enemy:exists() == false ) or ( enemy:get_life() <= 0 ) then
-- Create enemy
enemy_place.enemy = map:create_enemy{
name = enemy_place.name,
breed = enemy_place.breed,
x = enemy_place.x,
y = enemy_place.y,
layer = enemy_place.layer,
direction = 0,
}
enemy = enemy_place.enemy
else
-- If not destroyed or about to then reset life counter
enemy:set_life(enemy_place.life)
end
end
if enemy:exists() then
if enemy:get_life() > 0 then
if enemy:is_in_same_region(hero) then
enemy:set_position(enemy_place.x, enemy_place.y, enemy_place.layer)
--enemy:get_sprite():set_direction(enemy_place.direction)
enemy:set_enabled(false)
end
end
end
end
end
-- Function called after a separator was just taken.
local function separator_on_activated(separator)
local hero = map:get_hero()
for _, enemy_place in ipairs(enemy_places) do
local enemy = enemy_place.enemy
if enemy:exists() then
if enemy:get_life() > 0 then
if enemy:is_in_same_region(hero) then
enemy:set_enabled(true)
enemy:restart()
end
end
end
end
end
for separator in map:get_entities("auto_separator") do
separator.on_activating = separator_on_activating
separator.on_activated = separator_on_activated
end
end
return separator_manager