Class: Pixiurge::Player

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • websocket (Websocket)

    A Websocket object for low-level network I/O

  • name (String)

    The name for the player's account, Demiurge item, Displayable and so on

  • displayable (Pixiurge::Displayable)

    The player's displayable presence

  • engine_connector (Pixiurge::EngineConnector)

    The EngineConnector coordinating this game

  • display_settings (Hash)

    JSON-serializable display settings to send to the browser during initial setup

Since:

  • 0.1.0



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_init_message(display_settings)
end

Instance Attribute Details

#displayableObject (readonly)

The Displayable object the player is identified with

Since:

  • 0.1.0



17
18
19
# File 'lib/pixiurge/player.rb', line 17

def displayable
  @displayable
end

#nameObject (readonly)

The Player's name, which should match its Displayable's name and it's Demiurge object's name

Since:

  • 0.1.0



13
14
15
# File 'lib/pixiurge/player.rb', line 13

def name
  @name
end

#websocketObject (readonly)

A websocket object for low-level network I/O

Since:

  • 0.1.0



15
16
17
# File 'lib/pixiurge/player.rb', line 15

def websocket
  @websocket
end

Instance Method Details

#destroy_all_displayablesvoid

This method returns an undefined value.

Destroy all displayables, including foregrounds and backdrops.

Since:

  • 0.1.0



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.message 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

Parameters:

Since:

  • 0.1.0



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

Parameters:

  • item_name (String)

    The name of the item to destroy

Since:

  • 0.1.0



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.

Parameters:

  • display_settings (Hash)

    The display settings to send, if unmodified

Since:

  • 0.1.0



57
58
59
60
# File 'lib/pixiurge/player.rb', line 57

def display_init_message(display_settings)
  message(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.

Parameters:

  • msg_name (String)

    The protocol message name, normally a constant from Pixiurge::Protocol

  • args (Array)

    A JSON-serializable list of arguments to pass with this message

Since:

  • 0.1.0



70
71
72
73
74
75
76
# File 'lib/pixiurge/player.rb', line 70

def message(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.

Parameters:

  • displayable (Pixiurge::Displayable)

    The currently-shown Displayable to be moved

  • old_position (String)

    The old position string within the same location

  • new_position (String)

    The new position string within the same location

  • options (Hash{String=>String}) (defaults to: {})

    JSON-serializable hash of options to pass to the Displayable

Since:

  • 0.1.0



144
145
146
147
148
149
150
# File 'lib/pixiurge/player.rb', line 144

def move_displayable(displayable, old_position, new_position, options = {})
  return unless @currently_shown[displayable.name]
  return if old_position == new_position

  displayable.move_for_player(self, old_position, new_position, options)
  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.

Parameters:

  • x (Integer)

    The new x block coordinate for the center of the display

  • y (Integer)

    The new y block coordinate for the center of the display

  • options (Hash) (defaults to: {})

    Options to send to the front end for handling this pan

Since:

  • 0.1.0



174
175
176
177
178
# File 'lib/pixiurge/player.rb', line 174

def pan_to_coordinates(x, y, options = {})
  loc = displayable.location_displayable
  send_instant_pan_to_pixel_offset(x * loc.block_width, y * loc.block_height, options)
  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.

Parameters:

  • x (Integer)

    The new x pixel coordinate for the center of the display

  • y (Integer)

    The new y pixel coordinate for the center of the display

  • options (Hash) (defaults to: {})

    Options to send to the front end for handling this pan

Since:

  • 0.1.0



159
160
161
162
163
164
165
# File 'lib/pixiurge/player.rb', line 159

def send_instant_pan_to_pixel_offset(x, y, options = {})
  return if x == @pan_center_x && y == @pan_center_y
  @pan_center_x = x
  @pan_center_y = y
  message Pixiurge::Protocol::Outgoing::DISPLAY_PAN_TO_PIXEL, x, y, options
  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.

Parameters:

Since:

  • 0.1.0



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