1-- Lua script of enemy slime_red_egg.
2local enemy = ...
3local map = enemy:get_map()
4
5local time_to_hatch = 5000
6local can_procreate = true -- Allow the new slime to create more eggs.
7local slime_model = "slime_red" -- Type of the new slime.
8
9-- Event called when the enemy is initialized.
10function enemy:on_created()
11  local sprite = enemy:create_sprite("enemies/slime_egg_blue")
12  sprite:set_animation("egg_hatching")
13  self:set_pushed_back_when_hurt(false)
14  enemy:set_life(1)
15  enemy:set_damage(1)
16end
17
18-- Event called when the enemy is restarted.
19function enemy:on_restarted()
20  -- Remove the egg after a delay and create a red slime.
21  sol.timer.start(self, time_to_hatch, function()
22    self:get_sprite():set_animation("egg_explode")
23    sol.audio.play_sound("explosion")
24    sol.timer.start(self, 200, function()
25      local x, y, layer = self:get_position()
26      local prop = {x = x, y = y, layer = layer, direction = 0, breed = slime_model}
27      local slime = map:create_enemy(prop)
28      slime:set_egg_enabled(false) -- Do not allow to procreate again.
29      slime:jump() -- Start jump when created.
30      self:remove()
31    end)
32  end)
33end
34
35-- Model for the slime to be born.
36function enemy:set_slime_model(enemy_id) slime_model = enemy_id end
37
38-- Falling animation from certain height.
39function enemy:fall(initial_height)
40  local height = initial_height or 0
41  sol.audio.play_sound("throw")
42  local fall_duration = 1000 -- Duration of the first part of the fall.
43  local max_height = 24
44  local sprite = self:get_sprite()
45  sprite:set_animation("egg")
46  -- Create shadow.
47  local shadow = self:create_sprite("shadows/shadow_small")
48  -- Shift sprite of egg.
49  local function f(t) -- Shifting function.
50    return math.floor(4 * max_height * (t / fall_duration - (t / fall_duration) ^ 2) + height)
51  end
52  local t = 0
53  local refreshing_time = 10
54  sol.timer.start(enemy, refreshing_time, function() -- Update shift each 10 milliseconds.
55    local shift = - f(t)
56    sprite:set_xy(0, shift)
57    t = t + refreshing_time
58    if shift >= 0 and t >= fall_duration then
59      sprite:set_xy(0, 0)
60      sol.audio.play_sound("bomb") -- Sound when fall on ground.
61      self:remove_sprite(shadow)
62      sprite:set_animation("egg_hatching")
63      return false
64    else return true end
65  end)
66end
67
68-- Determine if the new red slime can procreate or not.
69function enemy:set_can_procreate(bool) can_procreate = bool end
70