-- Test for bug #1514: drowning and falling into a hole should trigger hero:on_taking_damage().
local map = ...
local game = map:get_game()
local hero = game:get_hero()

-- Phase 1: walk right into deep water (expects damage = 1).
-- Phase 2: walk left into a hole (expects damage = 2).
local phase = 1
local water_damage_called = false
local hole_damage_called = false

function hero:on_taking_damage(damage)
  if phase == 1 then
    assert_equal(damage, 1)
    water_damage_called = true
  elseif phase == 2 then
    assert_equal(damage, 2)
    hole_damage_called = true
  end
  return true  -- prevent life loss so the hero survives the test
end

function map:on_opening_transition_finished()
  game:simulate_command_pressed("right")
end

function hero:on_state_changed(state)
  if state == "back to solid ground" then
    if phase == 1 then
      game:simulate_command_released("right")
      assert(water_damage_called,
          "hero:on_taking_damage should be called when drowning in deep water")
      phase = 2
      game:simulate_command_pressed("left")
    elseif phase == 2 then
      game:simulate_command_released("left")
      assert(hole_damage_called,
          "hero:on_taking_damage should be called when falling into a hole")
      sol.main.exit()
    end
  end
end
