Class: Swarm::Game
- Inherits:
-
Object
- Object
- Swarm::Game
- Defined in:
- lib/swarm/game.rb
Constant Summary collapse
- REFRESH_RATE =
This seems to keep the laptop under 750°.
0.05
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
private
A new instance of Game.
- #listen_for_keypress ⇒ Object private
-
#over? ⇒ Boolean
A game is “over” when any of these conditions are met.
- #pause ⇒ Object
- #play! ⇒ Object
- #setup!(level) ⇒ Object
- #show! ⇒ Object
-
#spawn_player ⇒ Object
private
def find_player @player = @level.find_player end.
-
#start ⇒ true
Set the “start game” flag.
- #statistics ⇒ Hash private
-
#stop ⇒ true
Set the “stop game” flag.
- #stopped? ⇒ Boolean
- #to_s ⇒ String
Constructor Details
#initialize ⇒ Game (private)
Returns a new instance of Game.
167 168 169 170 171 |
# File 'lib/swarm/game.rb', line 167 def initialize @players = 5 @rate = 2.0 end |
Instance Method Details
#listen_for_keypress ⇒ Object (private)
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/swarm/game.rb', line 153 def listen_for_keypress Console.keypress do |command| case command when Console.up; @popup || @level.move_player(:north) when Console.down; @popup || @level.move_player(:south) when Console.left; @popup || @level.move_player(:west) when Console.right; @popup || @level.move_player(:east) when Console.pause; pause { 'PAUSED' } when Console.quit; stop end STDIN.iflush # prevent command queueing end end |
#over? ⇒ Boolean
A game is “over” when any of these conditions are met
-
the game has been stopped via #stop
-
all of the player’s lives are gone
-
all of the invaders are dead
59 60 61 |
# File 'lib/swarm/game.rb', line 59 def over? stopped? || @players.zero? end |
#pause ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/swarm/game.rb', line 79 def pause if @popup @popup.close @popup = false @level.update! else @popup = Console::PopUp.new(yield) @popup.refresh end end |
#play! ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/swarm/game.rb', line 7 def play! turn = Time.now.to_f @player = @level.find_player @player ||= spawn_player listen_for_keypress unless @popup if (turn - @turn) >= @rate @level.play @turn = turn end @level.update end sleep REFRESH_RATE end |
#setup!(level) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/swarm/game.rb', line 91 def setup!(level) @level = level @level.define_singleton_method(:pause, &method(:pause)) @popup = false @status = true @turn = Time.now.to_f @level.setup end |
#show! ⇒ Object
103 104 105 106 107 |
# File 'lib/swarm/game.rb', line 103 def show! @level.update # update the level and refresh the console before displaying anything @level.show(@players) end |
#spawn_player ⇒ Object (private)
def find_player
@player = @level.find_player
end
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/swarm/game.rb', line 137 def spawn_player @players -= 1 pause do <<-POPUP.gsub(/[ ]{10}/, '') % Console.key_info YOU HAVE BEEN KILLED! ---------------------------------------------- Press %<pause>s to continue. (#@players lives remaining) POPUP end @player = @level.spawn_player end |
#start ⇒ true
Set the “start game” flag.
32 33 34 35 36 |
# File 'lib/swarm/game.rb', line 32 def start @status = true true end |
#statistics ⇒ Hash (private)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/swarm/game.rb', line 112 def statistics Hash.new(0).tap do |stats| stats[:eggs] += Catalog.fetch(:destroyed, :egg, 0) stats[:workers] += Catalog.fetch(:destroyed, :worker, 0) stats[:soldiers] += Catalog.fetch(:destroyed, :soldier, 0) stats[:queens] += Catalog.fetch(:destroyed, :queen, 0) stats[:deaths] += Catalog.fetch(:destroyed, :player, 0) stats[:points] += (stats[:eggs] * 20) + (stats[:workers] * 20) + (stats[:soldiers] * 50) + (stats[:queens] * 50) - (stats[:deaths] * 100) end end |
#stop ⇒ true
Set the “stop game” flag.
41 42 43 44 45 |
# File 'lib/swarm/game.rb', line 41 def stop @status = false true end |
#stopped? ⇒ Boolean
48 49 50 |
# File 'lib/swarm/game.rb', line 48 def stopped? !@status end |
#to_s ⇒ String
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/swarm/game.rb', line 64 def to_s <<-TO_S.gsub(/^[ ]{8}/, '') % statistics GAME OVER! + %<eggs>4i x oo (eggs) squished (x20) + %<workers>4i x ├┤ (workers) killed (x20) + %<soldiers>4i x ╟╢ (soldiers) crushed (x50) + %<queens>4i x ╬╬ (queens) defeated (x50) - %<deaths>4i x ◄▶ (players) destroyed (x100) ------------------------------- %<points>6i points TO_S end |