Class: PlayState

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

Direct Known Subclasses

DemoState

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GameState

#needs_redraw?, switch

Constructor Details

#initializePlayState

Returns a new instance of PlayState.



4
5
6
7
8
9
10
11
12
13
# File 'lib/game_states/play_state.rb', line 4

def initialize
  # http://www.paulandstorm.com/wha/clown-names/
  @names = Names.new(
    Utils.media_path('names.txt'))
  @object_pool = ObjectPool.new(Map.bounding_box)
  @map = Map.new(@object_pool)
  @camera = Camera.new
  @object_pool.camera = @camera
  create_tanks(7)
end

Instance Attribute Details

#object_poolObject

Returns the value of attribute object_pool.



2
3
4
# File 'lib/game_states/play_state.rb', line 2

def object_pool
  @object_pool
end

#tankObject

Returns the value of attribute tank.



2
3
4
# File 'lib/game_states/play_state.rb', line 2

def tank
  @tank
end

#update_intervalObject

Returns the value of attribute update_interval.



2
3
4
# File 'lib/game_states/play_state.rb', line 2

def update_interval
  @update_interval
end

Instance Method Details

#button_down(id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/game_states/play_state.rb', line 45

def button_down(id)
  if id == Gosu::KbEscape
    pause = PauseState.instance
    pause.play_state = self
    GameState.switch(pause)
  end
  if id == Gosu::KbT
    t = Tank.new(@object_pool,
      AiInput.new(@names.random, @object_pool))
    t.move(*@camera.mouse_coords)
  end
  if id == Gosu::KbF1
    $debug = !$debug
  end
  if id == Gosu::KbF2
    toggle_profiling
  end
  if id == Gosu::KbR
    @tank.mark_for_removal
    @tank = Tank.new(@object_pool,
      PlayerInput.new('Player', @camera, @object_pool))
    @camera.target = @tank
    @hud.player = @tank
  end
end

#drawObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/game_states/play_state.rb', line 23

def draw
  cam_x = @camera.x
  cam_y = @camera.y
  off_x =  $window.width / 2 - cam_x
  off_y =  $window.height / 2 - cam_y
  viewport = @camera.viewport
  x1, x2, y1, y2 = viewport
  box = AxisAlignedBoundingBox.new(
    [x1 + (x2 - x1) / 2, y1 + (y2 - y1) / 2],
    [x1 - Map::TILE_SIZE, y1 - Map::TILE_SIZE])
  $window.translate(off_x, off_y) do
    zoom = @camera.zoom
    $window.scale(zoom, zoom, cam_x, cam_y) do
      @map.draw(viewport)
      @object_pool.query_range(box).map do |o|
        o.draw(viewport)
      end
    end
  end
  @hud.draw
end

#enterObject



79
80
81
# File 'lib/game_states/play_state.rb', line 79

def enter
  @hud.active = true
end

#leaveObject



71
72
73
74
75
76
77
# File 'lib/game_states/play_state.rb', line 71

def leave
  StereoSample.stop_all
  if @profiling_now
    toggle_profiling
  end
  @hud.active = false
end

#updateObject



15
16
17
18
19
20
21
# File 'lib/game_states/play_state.rb', line 15

def update
  StereoSample.cleanup
  @object_pool.update_all
  @camera.update
  @hud.update
  update_caption
end