Skip to content
floere edited this page Sep 14, 2010 · 2 revisions

Lives is about a thing having lives. If it has lives, it can be killed. If it has been killed more times than it has lives, it will be destroyed.

Including Lives

The Lives trait can be applied as usual:

class Cat < Thing
  it_has Lives

or

class Cat < Thing
  it_has Lives do
    # …
  end

Properties

Lives has just one property: lives

It defines a number of lives. Default is 3.

class Cat < Thing
  lives 9

Methods

Turnable installs four methods, lives, lives=, killed!, and kill!.

The lives and lives= method are the reader and the writer. If you used the lives class method, the reader will return the amount you set.

The killed! method is empty, and is there for you to override:

class Cat < Thing
  # …
  def killed!
    puts "Meowrrrrgh"
  end
end

The killed! method is called when the kill! method is called and the amount of lives is larger than zero.

The kill! method can be used for example after a collision:

add_collision_func :projectile, :player do |projectile_shape, player_shape|
  window.remove projectile_shape
    window.moveables.each do |possible_player|
      if possible_player.shape == player_shape
        possible_player.kill!
        possible_player.draw_ui
      end
    end
  end

The kill! method checks whether a thing has already been killed too many times and will destroy. If not, it will call killed!.