Class: PlayState

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

Overview

Game state of main gameplay

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GameState

switch!

Instance Attribute Details

#desired_new_map=(value) ⇒ Object (writeonly)

Sets the attribute desired_new_map

Parameters:

  • value

    the value to set the attribute desired_new_map to.



30
31
32
# File 'lib/lib/game_states/play_state.rb', line 30

def desired_new_map=(value)
  @desired_new_map = value
end

#mapObject (readonly)

Returns the value of attribute map.



29
30
31
# File 'lib/lib/game_states/play_state.rb', line 29

def map
  @map
end

Instance Method Details

#after_startObject

Load new scenario or resume game



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lib/game_states/play_state.rb', line 33

def after_start
  if @desired_new_map
    WinState.instance.unset_victory!
    load_new_map!
    help # show help after each loading of a new map
    @desired_new_map = nil

    next_faction_turn! # so FAC1 will be the first active faction, with announcement
  else
    # Make sure everything is set up
    unless @infopane and @map and @cursor
      abort("PlayState.after_start(): Map parts are not properly loaded
        (#{@infopane}, #{@map}, #{@cursor})")
    end

    @cursor.info_stopped = false

    # Remind player the current status
    if @cursor.freeroam
      @infopane.text = 'freeroam is enabled'
      puts 'freeroam is enabled'
    else
      @cursor.info
    end
  end
end

#before_endObject

What to do just before state gets deactivated



61
62
63
# File 'lib/lib/game_states/play_state.rb', line 61

def before_end
  @cursor.info_stopped = true
end

#drawObject

Draw all parts of main window



84
85
86
87
88
# File 'lib/lib/game_states/play_state.rb', line 84

def draw
  @map.draw_tiles
  @cursor.draw
  @infopane.draw
end

#helpObject

Printout the controls



141
142
143
144
145
146
147
148
# File 'lib/lib/game_states/play_state.rb', line 141

def help
  puts "-----------\n" \
       "H: help, Esc: menu, Enter: info, " \
       ".: end your turn, J: switch freeroam\n" \
       "QWEADZXC or arrow keys: movement, K: previous unit, L: next unit\n" \
       "functions: S sentry, B build, N none\n" \
       "-----------\n"
end

#load_new_map!Object

Load the desired map



91
92
93
94
95
96
97
98
# File 'lib/lib/game_states/play_state.rb', line 91

def load_new_map!
  puts("=============\n" \
       "=============\n" \
       "Loading save: #{@desired_new_map}")
  @infopane = Infopane.new
  @map = Map.new(@desired_new_map, @infopane)
  @cursor = Cursor.new(0, 0, @map, @infopane)
end

#next_faction_turn!Object

End turn of the active faction



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lib/game_states/play_state.rb', line 101

def next_faction_turn!
  @infopane.next_faction!

  # Have all factions played this turn so we are now back at the first one?
  if @infopane.faction == 1
    puts "=============\n" \
         "End of turn\n" \
         "=============\n"
    @infopane.next_turn!
  end

  # Announce game state at the start of turn of the next faction
  # TODO add change-of-active-faction screen
  puts "-----------\n" + @infopane.turnscore

  # Activate functions and reset moves for units of the next faction
  functionable_faction_units = @map.all_units.select { |uu|
    uu.faction == @infopane.faction and
    uu.function != FUNCNONE
  }
  functionable_faction_units.each { |uu| uu.function! }

  all_faction_units = @map.all_units.select { |uu|
    uu.faction == @infopane.faction
  }
  all_faction_units.each { |uu| uu.reset_moves!}

  # Win check: current faction has lost all units
  if !WinState.instance.faction and all_faction_units.empty?
    WinState.instance.set_victory!(
      3 - @infopane.faction, # TODO more factions?
      'Annihilation', @infopane.turn, @infopane.score
    )
  end

  # Lock the cursor to the first waiting unit
  @cursor.reset!
end

#update(button) ⇒ Object

Process given button or send it to cursor



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lib/game_states/play_state.rb', line 66

def update(button)
  case(button)
  when Gosu::KbEscape then
    unless WinState.instance.faction
      GameState.switch!(WelcomeState.instance)
    else
      GameState.switch!(WinState.instance)
    end
  when Gosu::KbPeriod then
    next_faction_turn!
  when Gosu::KbH then
    help
  else
    @cursor.update(button)
  end
end