Class: Asteroids::PlayState

Inherits:
GameState show all
Defined in:
lib/asteroids/states/play_state.rb

Instance Method Summary collapse

Methods inherited from GameState

#enter, #leave, #needs_redraw?, switch

Constructor Details

#initializePlayState

Returns a new instance of PlayState.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/asteroids/states/play_state.rb', line 4

def initialize
  @background = Gosu::Image.new($window,
    Utils.get_image_path('background.png'), false)
  @live_image = Gosu::Image.new($window,
    Utils.get_image_path('ship_small.png'), false)
  @object_pool = ObjectPool.new
  @ship = Ship.new(@object_pool)
  Utils.create_asteroids(@object_pool, 4)
        @font = Gosu::Font.new($window,
   Utils.get_font_path('victor-pixel.ttf'),
    34)
  @level = 1
  @level_compleate = false
end

Instance Method Details

#button_down(id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/asteroids/states/play_state.rb', line 48

def button_down(id)
  if id == Gosu::KbEscape
    Asteroids::GameState.switch(Asteroids::MenuState.instance)
  end
  if id == Gosu::KbS
    serialized_object = ::YAML::dump(@object_pool)
    File.open(Utils.saves_path + '/save.yaml', 'w+') do |file|
     file.write(serialized_object)
     end
  end
end

#check_level_compleateObject



44
45
46
# File 'lib/asteroids/states/play_state.rb', line 44

def check_level_compleate
  @level_compleate = !@object_pool.objects.any? { |object| object.is_a? Asteroids::Asteroid  }
end

#drawObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/asteroids/states/play_state.rb', line 19

def draw
  @background.draw(0, 0, 0)
  @object_pool.objects.map(&:draw)
  if !@ship.is_alive?
    @font.draw("Game Over", 210, 250, 50, 2.0, 2.0, 0xffffff00)
  else
    @font.draw("Score: ", 580, 10, 50, 1.0, 1.0, 0xffffff00)
    @font.draw(@ship.score, 700, 10, 50, 1.0, 1.0, 0xffffff00)
    @font.draw("Lives: ", 10, 10, 50, 1.0, 1.0, 0xffffff00)
    @ship.lives.times do |n|
      @live_image.draw(110 + n * 50, 10, 50)
    end
  end
end

#updateObject



34
35
36
37
38
39
40
41
42
# File 'lib/asteroids/states/play_state.rb', line 34

def update
  check_level_compleate
  if @level_compleate == true
    Utils.create_asteroids(@object_pool, 4 + @level)
    @level += 1
    @level_compleate = false
  end
  @object_pool.update_all
end