Class: BTetrisKp::NetJoinState
- Inherits:
-
Object
- Object
- BTetrisKp::NetJoinState
- Defined in:
- lib/btetris_kp/netjoin.rb
Overview
class representing a window for joining a game over network (CLIENT SIDE)
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
button handler.
-
#draw ⇒ Object
draws net join window.
-
#initialize(window) ⇒ NetJoinState
constructor
A new instance of NetJoinState.
-
#initialize_textfields ⇒ Object
initializes text fields for reading ip and port initializes positions for texts and textfields.
-
#initialize_title_image ⇒ Object
loads title image and calculates position, size variables.
-
#needs_cursor? ⇒ Boolean
shows default system cursor.
-
#try_connect ⇒ Object
tries to connect to server.
-
#update ⇒ Object
updates window state, tries to connect to server, starts game when ready.
Constructor Details
#initialize(window) ⇒ NetJoinState
Returns a new instance of NetJoinState.
9 10 11 12 13 14 15 16 |
# File 'lib/btetris_kp/netjoin.rb', line 9 def initialize(window) @window = window @font = Gosu::Font.new(@window, Gosu.default_font_name, Const::FONT_MED_SIZE) @connecting = false initialize_title_image initialize_textfields @socket = nil end |
Instance Method Details
#button_down(id) ⇒ Object
button handler
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/btetris_kp/netjoin.rb', line 87 def (id) case id when Gosu::KbEscape if @connecting @connecting = false else # windows text_input needs to be set on nil # otherwise gosu wont register some keys @window.text_input = nil @window.state = MenuState.new(@window) end when Gosu::KbReturn @connecting = true when Gosu::MsLeft @window.text_input = @text_fields.find do |tf| tf.mouse_over?(@window.mouse_x, @window.mouse_y) end unless @window.text_input.nil? @window.text_input.move_caret(@window.mouse_x) end end end |
#draw ⇒ Object
draws net join window
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/btetris_kp/netjoin.rb', line 75 def draw @text_fields.each { |t| t.draw } @font.draw(Const::IP_CAPTION, @ip_x, @text_y, 0) @font.draw(Const::PORT_CAPTION, @port_x, @text_y + @font.height + 2 * Const::FONT_GAP, 0) @font.draw(Const::CONNECTING, @text_x, @text_y + 3 * (@font.height + 2 * Const::FONT_GAP), 0) if @connecting @title_image.draw(@img_x, @img_y, 1, @img_size_factor, @img_size_factor) end |
#initialize_textfields ⇒ Object
initializes text fields for reading ip and port initializes positions for texts and textfields
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/btetris_kp/netjoin.rb', line 28 def initialize_textfields @text_x = @window.width / 3 + 2 * Const::FONT_GAP @text_y = @window.height / 3 @ip_x = @text_x - @font.text_width(Const::IP_CAPTION) - 3 * Const::FONT_GAP @port_x = @text_x - @font.text_width(Const::PORT_CAPTION) - 3 * Const::FONT_GAP @text_fields = [] @text_fields << TextField.new(@window, @font, @text_x, @text_y, Const::DEF_IP, /[^0-9.]/, @font.text_width('000.000.000.000'), 15) @text_fields << TextField.new(@window, @font, @text_x, @text_y + @font.height + 2 * Const::FONT_GAP, Const::DEF_PORT, /[^0-9]/, @font.text_width('00000'), 5) end |
#initialize_title_image ⇒ Object
loads title image and calculates position, size variables
19 20 21 22 23 24 |
# File 'lib/btetris_kp/netjoin.rb', line 19 def initialize_title_image @title_image = Gosu::Image.new(@window, Const::PATH_IMAGE_TITLE, false) @img_size_factor = (@window.width - Const::GAME_WIN_GAP).to_f / @title_image.width @img_x = (@window.width - @title_image.width * @img_size_factor) / 2 @img_y = 20 end |
#needs_cursor? ⇒ Boolean
shows default system cursor
111 112 113 |
# File 'lib/btetris_kp/netjoin.rb', line 111 def needs_cursor? true end |
#try_connect ⇒ Object
tries to connect to server
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/btetris_kp/netjoin.rb', line 44 def try_connect begin @socket = TCPSocket.new(@text_fields[0].text, @text_fields[1].text) @socket.sendmsg_nonblock(Const::MSG_WELCOME) msg = @socket.recv(1) unless msg == Const::MSG_WELCOME @socket.close @socket = nil end rescue # error while connecting end end |
#update ⇒ Object
updates window state, tries to connect to server, starts game when ready
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/btetris_kp/netjoin.rb', line 59 def update @text_fields.each { |t| t.update } if @connecting if @socket.nil? try_connect else # socket created and connected, start net game # windows text_input needs to be set on nil # otherwise gosu wont register some keys @window.text_input = nil @window.state = NetGameState.new(@window, @socket) end end end |