Class: BTetrisKp::NetSetupState
- Inherits:
-
Object
- Object
- BTetrisKp::NetSetupState
- Defined in:
- lib/btetris_kp/netsetup.rb
Overview
class representing a window for creating a game over network (SERVER SIDE)
Instance Method Summary collapse
-
#button_down(id) ⇒ Object
button handler.
-
#draw ⇒ Object
draws net setup window.
-
#initialize(window) ⇒ NetSetupState
constructor
A new instance of NetSetupState.
-
#initialize_title_image ⇒ Object
loads title image and calculates position, size variables.
-
#needs_cursor? ⇒ Boolean
shows default system cursor.
-
#setup_server ⇒ Object
setups a server with random port.
-
#try_accept ⇒ Object
tries to accept client connection, sets @connected true when accepted.
-
#update ⇒ Object
updates window state, checks for incomming connections starts game when ready.
Constructor Details
#initialize(window) ⇒ NetSetupState
Returns a new instance of NetSetupState.
9 10 11 12 13 14 15 |
# File 'lib/btetris_kp/netsetup.rb', line 9 def initialize(window) @window = window @font = Gosu::Font.new(@window, Gosu.default_font_name, Const::FONT_MED_SIZE) initialize_title_image setup_server @connected = false end |
Instance Method Details
#button_down(id) ⇒ Object
button handler
70 71 72 |
# File 'lib/btetris_kp/netsetup.rb', line 70 def (id) @window.state = MenuState.new(@window) if id == Gosu::KbEscape end |
#draw ⇒ Object
draws net setup window
61 62 63 64 65 66 67 |
# File 'lib/btetris_kp/netsetup.rb', line 61 def draw @font.draw(Const::SERVER_WAIT, @window.width / 5, @window.height / 2 - Const::GAME_WIN_GAP, 0) @font.draw("#{Const::SERVER_PORT}#{@port}", @window.width / 3.5, @window.height / 2, 0) @title_image.draw(@img_x, @img_y, 1, @img_size_factor, @img_size_factor) end |
#initialize_title_image ⇒ Object
loads title image and calculates position, size variables
29 30 31 32 33 34 |
# File 'lib/btetris_kp/netsetup.rb', line 29 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
75 76 77 |
# File 'lib/btetris_kp/netsetup.rb', line 75 def needs_cursor? true end |
#setup_server ⇒ Object
setups a server with random port
18 19 20 21 22 23 24 25 26 |
# File 'lib/btetris_kp/netsetup.rb', line 18 def setup_server @port = 0 begin @port = rand(40_000) @server = TCPServer.new(@port) rescue retry end end |
#try_accept ⇒ Object
tries to accept client connection, sets @connected true when accepted
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/btetris_kp/netsetup.rb', line 37 def try_accept begin # tries to accept client connection @socket = @server.accept_nonblock @socket.sendmsg_nonblock(Const::MSG_WELCOME) # if welcome msg is recvd, start game, else close socket msg = @socket.recv(1) msg == Const::MSG_WELCOME ? @connected = true : @socket.close rescue # no one connected, try again next time end end |
#update ⇒ Object
updates window state, checks for incomming connections starts game when ready
52 53 54 55 56 57 58 |
# File 'lib/btetris_kp/netsetup.rb', line 52 def update if @connected @window.state = NetGameState.new(@window, @socket) else try_accept end end |