Class: PirateGame::ClientApp

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

Class Method Summary collapse

Class Method Details

.runObject



3
4
5
6
7
8
9
10
11
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/pirate_game/client_app.rb', line 3

def self.run
  @my_app = Shoes.app width: 360, height: 360, resizeable: false, title: 'Pirate Game' do

    require 'pirate_game/shoes4_patch'

    def animate fps, &block
      opts = { framerate: fps }
      PirateGame::Animation.new @app, opts, block
    end

    ##
    # Creates the animation trigger for animating items.  An animatable item
    # must respond to #animate and accept a frame number.

    def animate_items
      @items_animation = animate(30) do |frame|
        @background.animate frame
      end
    end

    ##
    # Creates the waves and ship graphics that must be animated.

    def create_items
      @background = PirateGame::Background.new(self)
    end

    ##
    # Clears the app, draws the background, ship and waves, then yields to
    # draw UI items.

    def draw_items
      clear

      @background.draw do
        yield
      end
    end

    ##
    # Sets the application state

    def state= state
      @state = state
      @client.set_state @state
    end

    ##
    # The launch screen.

    def launch_screen
      @items_animation.start if @items_animation

      pirate_ship do
        title "What's your name", stroke: PirateGame::Boot::COLORS[:dark]

        edit_line text: 'Name' do |s|
          @name = s.text
        end

        button('launch') {
          @client = PirateGame::Client.new(name: @name)
        }
      end

      @state_watcher = animate(5) {
        watch_state
      }
    end

    ##
    # Draws a pirate ship background with waves yields to draw additional
    # UI.

    def pirate_ship
      draw_items do
        stack margin: 20 do
          yield
        end
      end
    end

    ##
    # Watches the state of the client to switch to a new screen.

    def watch_state
      return if @client.nil?
      return if @state == @client.state

      @state = @client.state

      case @state
      when :select_game
        select_game_screen
      when :pub
        pub_screen
      when :stage
        stage_screen
      when :end
        end_screen
      end
    end

    ##
    # Displays the UI for choosing a game to join

    def select_game_screen
      @items_animation.start if @items_animation

      motherships = @client.find_all_motherships

      title_text = if motherships.empty? then
                     "No Games Found"
                   else
                     "Choose Game"
                   end

      pirate_ship do
        title title_text, stroke: PirateGame::Boot::COLORS[:dark]

        for mothership in motherships
          draw_mothership_button mothership
        end

        button('rescan') {
          select_game_screen
        }
      end
    rescue DRb::DRbConnError
      state = :select_game
      select_game_screen
    end

    ##
    # The pirate pub screen where you wait to start a game

    def pub_screen
      @stage_animation.remove if @stage_animation
      @items_animation.stop if @items_animation

      @pub_tagline ||= "Welcome #{@client.name}"

      clear do
        background PirateGame::Boot::COLORS[:pub]
        stack :margin => 20 do
          title "Pirate Pub", stroke: PirateGame::Boot::COLORS[:light]
          tagline @pub_tagline, stroke: PirateGame::Boot::COLORS[:light]

          stack do @status = para '', stroke: PirateGame::Boot::COLORS[:light] end

          @registered = nil
          @updating_area = stack
          stack do
            @chat_title = tagline 'Pub Chat', stroke: PirateGame::Boot::COLORS[:light]
            @chat_input = flow
            @chat_messages = stack
          end
        end

        # checks for registration changes
        # updates chat messages
        @pub_animation = animate(5) {
          if @client

            # updates screen only when registration state changes
            update_on_registration_change

            # updates screen, runs every time
            update_chat_room
          end
        }
      end
    rescue DRb::DRbConnError
      state = :select_game
      select_game_screen
    end

    ##
    # The screen where the game occurs

    def stage_screen
      @pub_animation.remove if @pub_animation
      @items_animation.start if @items_animation

      # if we get back to pub then stage was success
      @pub_tagline = 'SUCCESS!'

      pirate_ship do
        title PirateCommand.exclaim!, stroke: PirateGame::Boot::COLORS[:dark]

        @instruction = caption stroke: PirateGame::Boot::COLORS[:dark]
        @progress = progress

        @client.bridge.items.each_slice(3).with_index do |items, row|
          items.each_with_index do |item, column|
            bridge_button =
              PirateGame::BridgeButton.new self, item, row, column do
                @client.clicked item
              end

            @background.add_extra_item bridge_button
          end
        end
      end

      @stage_animation = animate(10) {
        @client.issue_command unless @client.waiting?

        @instruction.replace @client.current_action

        progress_fraction = (@client.action_time_left.to_f / @client.completion_time.to_f)
        @progress.fraction = progress_fraction
      }
    rescue DRb::DRbConnError
      state = :select_game
      select_game_screen
    end

    ##
    # The end-game screen

    def end_screen
      @pub_animation.remove if @pub_animation
      @stage_animation.remove if @stage_animation
      @items_animation.stop if @items_animation

      clear do
        jolly_roger = File.expand_path '../../../imgs/jolly_roger_sm.png', __FILE__
        background PirateGame::Boot::COLORS[:dark]
        background jolly_roger, height: 256
        background rgb(0, 0, 0, 180)

        stack margin: 20 do

          title "END OF GAME", stroke: PirateGame::Boot::COLORS[:light]

          if @client.slop_bucket[:end_game]
            game_stats = @client.slop_bucket[:end_game]
            para "Game Stats", stroke: PirateGame::Boot::COLORS[:light]
            para "Total Stages Completed: #{game_stats[:total_stages]}", stroke: PirateGame::Boot::COLORS[:light]
            para "Total Actions: #{game_stats[:total_actions]}", stroke: PirateGame::Boot::COLORS[:light]
            para "My Contribution: #{game_stats[:player_breakdown][DRb.uri]}", stroke: PirateGame::Boot::COLORS[:light]
          end
        end
      end
    rescue DRb::DRbConnError
      state = :select_game
      select_game_screen
    end

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

    def detect_registration_change
      return if @client.registered? == @registered

      @registered = @client.registered?

      @status.replace "#{"Not " unless @registered}Registered"

      yield
    end

    ##
    # Updates the chat messages with new messages

    def update_chat_room
      if @registered
        @chat_messages.clear do
          for msg, name in @client.log_book
            para "#{name} said: #{msg}", stroke: PirateGame::Boot::COLORS[:light]
          end
        end
      end
    end

    ##
    # Updates the registration view in the pub screen

    def update_on_registration_change
      detect_registration_change do
        @updating_area.clear do
          button("Register") { register } unless @registered
        end

        # chat input box only appears when registered
        @chat_input.clear do
          if @registered
            el = edit_line

            button("Send") {
              unless el.text.empty?
                @client.broadcast(el.text)
                el.text = ''
              end
            }
          end
        end
      end
    end

    ##
    # Draws a button for joining a game playing on +mothership+

    def draw_mothership_button mothership
      button(mothership[:name]) {|b|
        begin
          @client.initiate_communication_with_mothership(b.text)
          @client.register
        rescue
          select_game_screen
        end
      }
    end

    ##
    # Registers the client with a game master

    def register
      @client.register if @client
    end

    ##
    # Unregisters a game with a game master

    def unregister
      @client.unregister if @client
    end

    @client = nil
    create_items
    animate_items
    launch_screen
  end
ensure
  @my_app.unregister if @my_app
end