Class: PirateGame::MasterApp

Inherits:
Object
  • Object
show all
Defined in:
lib/pirate_game/master_app.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMasterApp

Returns a new instance of MasterApp.



8
9
10
# File 'lib/pirate_game/master_app.rb', line 8

def initialize
  @game_master = nil
end

Class Method Details

.runObject



4
5
6
# File 'lib/pirate_game/master_app.rb', line 4

def self.run
  new.run
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pirate_game/master_app.rb', line 12

def run
  Shoes.app width: 360, height: 360, resizeable: false, title: 'Game Master' do

    def launch_screen
      clear do
        background Boot::COLORS[:dark]
        stack margin: 20 do
          title "Start Game", stroke: Boot::COLORS[:light]
          edit_line text: 'Name' do |s|
            @name = s.text
          end
          button('launch') {
            @game_master = GameMaster.new(name: @name)
            display_screen
          }
        end
      end
    end

    def display_screen
      @game_master.update

      clear do
        background Boot::COLORS[:light]

        stack :margin => 20 do
          title "Game #{@game_master.name}", stroke: Boot::COLORS[:dark]

          @button_stack = stack
          @registrations = para stroke: Boot::COLORS[:dark]
          @stage_info = para stroke: Boot::COLORS[:dark]
          @game_info = para stroke: Boot::COLORS[:dark]
        end

        animate(5) {
          detect_state_change {
            update_button_stack
          }

          detect_stage_status_change {
            @game_master.send_stage_info_to_clients
          }

          @registrations.replace @game_master.registrations_text
          @stage_info.replace @game_master.stage_info
          @game_info.replace @game_master.game_info
        }
      end
    end

    ##
    # Responsible for updating the display of the START button

    def update_button_stack
      @button_stack.clear do

        case @state
        when :startable
          button('START') {
            @game_master.start
          }
        end
      end
    end

    ##
    # If the game_master.state has changed, yields to the block.

    def detect_state_change
      return if @state == @game_master.state

      @state = @game_master.state

      yield
    end

    ##
    # If the stage status has changed, yields to the block
    def detect_stage_status_change
      return unless @game_master.stage
      return if @stage_status == @game_master.stage.status

      @stage_status = @game_master.stage.status

      yield
    end

    launch_screen
  end
end