Class: Pixiurge::Player
- Inherits:
-
Object
- Object
- Pixiurge::Player
- Defined in:
- lib/pixiurge/player.rb
Overview
A Player object handles the network connection to a single browser, via a protocol over Websockets. It also keeps track of what visual objects are currently visible in the browser, and handles panning (moving the window of visible display around.)
A Player is linked to a Displayable object on creation. The EngineConnector normally handles the movement of this object - the player just draws and updates what it is told to.
Instance Attribute Summary collapse
-
#displayable ⇒ Object
readonly
The Displayable object the player is identified with.
-
#name ⇒ Object
readonly
The Player's name, which should match its Displayable's name and it's Demiurge object's name.
-
#websocket ⇒ Object
readonly
A websocket object for low-level network I/O.
Instance Method Summary collapse
-
#destroy_all_displayables ⇒ void
Destroy all displayables, including foregrounds and backdrops.
-
#destroy_displayable(disp) ⇒ void
This destroys a given displayable for this player.
-
#destroy_displayable_name(item_name) ⇒ void
This destroys a given displayable for this player by name.
-
#display_init_message(display_settings) ⇒ void
Send the message to initialize the display.
-
#initialize(websocket:, name:, displayable:, engine_connector:, display_settings: {}) ⇒ void
constructor
Constructor.
-
#message(msg_name, *args) ⇒ void
Send a message to the connected browser for this player.
-
#move_displayable(displayable, old_position, new_position, options = {}) ⇒ void
Move the given Displayable from the old to the new position for the given player.
-
#pan_to_coordinates(x, y, options = {}) ⇒ void
Pan the display to a coordinate offset in the current backdrop.
-
#send_instant_pan_to_pixel_offset(x, y, options = {}) ⇒ void
Pan the display to a pixel offset in the current backdrop.
-
#show_displayable(displayable) ⇒ void
Show this Displayable to the player and track that it has been shown.
Constructor Details
#initialize(websocket:, name:, displayable:, engine_connector:, display_settings: {}) ⇒ void
Constructor. Set this player up with the appropriate network settings, Displayable object, engine connector and so on. This is normally called by the EngineConnector.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pixiurge/player.rb', line 30 def initialize(websocket:, name:, displayable:, engine_connector:, display_settings: {}) @websocket = websocket @name = name @displayable = displayable @engine_connector = engine_connector @currently_shown = {} # The player object expects to have a location and viewport set # pretty much immediately after creation, so we don't send a # message for it yet. The EngineConnector will cause the Player # viewpoint to begin following their Demiurge Agent, pretty much # immediately. @pan_center_x = 0 @pan_center_y = 0 (display_settings) end |
Instance Attribute Details
#displayable ⇒ Object (readonly)
The Displayable object the player is identified with
17 18 19 |
# File 'lib/pixiurge/player.rb', line 17 def displayable @displayable end |
#name ⇒ Object (readonly)
The Player's name, which should match its Displayable's name and it's Demiurge object's name
13 14 15 |
# File 'lib/pixiurge/player.rb', line 13 def name @name end |
#websocket ⇒ Object (readonly)
A websocket object for low-level network I/O
15 16 17 |
# File 'lib/pixiurge/player.rb', line 15 def websocket @websocket end |
Instance Method Details
#destroy_all_displayables ⇒ void
This method returns an undefined value.
Destroy all displayables, including foregrounds and backdrops.
122 123 124 125 126 127 128 129 130 |
# File 'lib/pixiurge/player.rb', line 122 def destroy_all_displayables #@currently_shown.each do |item_name, displayable| # displayable.destroy_for_player(self) #end # Let the front end know: hints, preloads and whatnot... Clear it all. self. Pixiurge::Protocol::Outgoing::DISPLAY_DESTROY_ALL @currently_shown = {} nil end |
#destroy_displayable(disp) ⇒ void
This method returns an undefined value.
This destroys a given displayable for this player
99 100 101 102 103 104 |
# File 'lib/pixiurge/player.rb', line 99 def destroy_displayable(disp) return unless @currently_shown[disp.name] disp.destroy_for_player(self) @currently_shown.delete(disp.name) nil end |
#destroy_displayable_name(item_name) ⇒ void
This method returns an undefined value.
This destroys a given displayable for this player by name
111 112 113 114 115 116 |
# File 'lib/pixiurge/player.rb', line 111 def destroy_displayable_name(item_name) return unless @currently_shown[item_name] @currently_shown[item_name].destroy_for_player(self) @currently_shown.delete(item_name) nil end |
#display_init_message(display_settings) ⇒ void
This method returns an undefined value.
Send the message to initialize the display. If you want to send additional settings or customize existing settings like display width and height per player, this is a pretty good place to do that. This is normally called by the Player constructor.
57 58 59 60 |
# File 'lib/pixiurge/player.rb', line 57 def (display_settings) (Pixiurge::Protocol::Outgoing::DISPLAY_INIT, display_settings) nil end |
#message(msg_name, *args) ⇒ void
This method returns an undefined value.
Send a message to the connected browser for this player. This will serialize to JSON. It respects the record_traffic setting for the Pixiurge app.
70 71 72 73 74 75 76 |
# File 'lib/pixiurge/player.rb', line 70 def (msg_name, *args) out_str = MultiJson.dump [ msg_name, *args ] record_traffic = @engine_connector.app.record_traffic File.open(@engine_connector.app.outgoing_traffic_logfile, "a") { |f| f.write out_str + "\n" } if record_traffic @websocket.send out_str nil end |
#move_displayable(displayable, old_position, new_position, options = {}) ⇒ void
This method returns an undefined value.
Move the given Displayable from the old to the new position for the given player. Options can be supplied to change the nature of the movement in Displayable-specific ways. For instance, there may be a difference between teleporting, swimming, walking and running, or it may be possible to supply a speed or a duration.
144 145 146 147 148 149 150 |
# File 'lib/pixiurge/player.rb', line 144 def move_displayable(displayable, old_position, new_position, = {}) return unless @currently_shown[displayable.name] return if old_position == new_position displayable.move_for_player(self, old_position, new_position, ) nil end |
#pan_to_coordinates(x, y, options = {}) ⇒ void
This method returns an undefined value.
Pan the display to a coordinate offset in the current backdrop.
174 175 176 177 178 |
# File 'lib/pixiurge/player.rb', line 174 def pan_to_coordinates(x, y, = {}) loc = displayable.location_displayable send_instant_pan_to_pixel_offset(x * loc.block_width, y * loc.block_height, ) nil end |
#send_instant_pan_to_pixel_offset(x, y, options = {}) ⇒ void
This method returns an undefined value.
Pan the display to a pixel offset in the current backdrop.
159 160 161 162 163 164 165 |
# File 'lib/pixiurge/player.rb', line 159 def send_instant_pan_to_pixel_offset(x, y, = {}) return if x == @pan_center_x && y == @pan_center_y @pan_center_x = x @pan_center_y = y Pixiurge::Protocol::Outgoing::DISPLAY_PAN_TO_PIXEL, x, y, nil end |
#show_displayable(displayable) ⇒ void
This method returns an undefined value.
Show this Displayable to the player and track that it has been shown. A later #destroy_displayable or #destroy_all_displayables call will normally be needed eventually to cause the displayable to disappear and be unloaded from the browser - this will usually happen automatically if the player changes locations in such a way as to lose previous visibility.
88 89 90 91 92 |
# File 'lib/pixiurge/player.rb', line 88 def show_displayable(displayable) displayable.show_to_player(self) @currently_shown[displayable.name] = displayable nil end |