Class: Pixiurge::Displayable

Inherits:
Object
  • Object
show all
Defined in:
lib/pixiurge/displayable.rb

Overview

A Pixiurge Displayable handles server-side display of in-world simulated items. This doesn't directly mess with Javascript or Pixi.js code, but it sets up data for display primitives like sprites, TMX areas, particles, animations and so on.

The Player knows what objects are being displayed, while the Displayables know how to send messages to display themselves.

More complex visual groupings like tilemap areas and humanoid bodies inherit from this class.

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, engine_connector:) ⇒ Displayable

Constructor

Parameters:

  • name (String)

    The Demiurge item name for this Displayable

  • engine_connector (Pixiurge::EngineConnector)

    The Pixiurge EngineConnector this Displayable belongs to

Since:

  • 0.1.0



61
62
63
64
65
66
67
68
# File 'lib/pixiurge/displayable.rb', line 61

def initialize name:, engine_connector:
  @name = name
  @engine_connector = engine_connector

  # Most child classes should override this
  @block_width = 1
  @block_height = 1
end

Instance Attribute Details

#block_heightObject (readonly)

Since:

  • 0.1.0



49
50
51
# File 'lib/pixiurge/displayable.rb', line 49

def block_height
  @block_height
end

#block_widthObject (readonly)

For a tiled-type area, #block_width and #block_height are the tile height and width of this Displayable in pixels. This isn't the tile size of this object's location - it's the tile size of this object itself for any items that might move inside of it or on top of it.

Since:

  • 0.1.0



48
49
50
# File 'lib/pixiurge/displayable.rb', line 48

def block_width
  @block_width
end

#displayable_typeObject (readonly)

You can't just be a "Displayable", you need a concrete child class. Each child class gets a string to represent it in the network protocol.

Since:

  • 0.1.0



28
29
30
# File 'lib/pixiurge/displayable.rb', line 28

def displayable_type
  @displayable_type
end

#location_displayableObject (readonly)

Since:

  • 0.1.0



41
42
43
# File 'lib/pixiurge/displayable.rb', line 41

def location_displayable
  @location_displayable
end

#location_nameObject (readonly)

Since:

  • 0.1.0



40
41
42
# File 'lib/pixiurge/displayable.rb', line 40

def location_name
  @location_name
end

#nameObject (readonly)

Name, which should be the same as the Demiurge item name if there is one.

Since:

  • 0.1.0



23
24
25
# File 'lib/pixiurge/displayable.rb', line 23

def name
  @name
end

#positionObject

This is the most recently-displayed position of this Displayable. When it changes, fields like #x and #location_name get updated automatically.

Since:

  • 0.1.0



54
55
56
# File 'lib/pixiurge/displayable.rb', line 54

def position
  @position
end

#xObject (readonly)

Most recently-displayed coordinate and location. This can vary significantly from the Demiurge item's location during a long series of movement notifications - the Demiurge item may already be at the final location, while the notifications go one at a time through the places in between. In general the EngineConnector will assign these coordinates and tell the Displayable to play a movement animation, but the Displayable can't easily know exactly what happens when.

Since:

  • 0.1.0



38
39
40
# File 'lib/pixiurge/displayable.rb', line 38

def x
  @x
end

#yObject (readonly)

Since:

  • 0.1.0



39
40
41
# File 'lib/pixiurge/displayable.rb', line 39

def y
  @y
end

Instance Method Details

#destroy_for_player(player) ⇒ void

This method returns an undefined value.

Hide this Displayable from a player. The default method assumes this Displayable uses a SpriteStack and has set the spritesheet and spritestack names already. For other display methods, override this method in a subclass.

Parameters:

Since:

  • 0.1.0



138
139
140
# File 'lib/pixiurge/displayable.rb', line 138

def destroy_for_player(player)
  player.message Pixiurge::Protocol::Outgoing::DISPLAY_DESTROY_DISPLAYABLE, self.name
end

#messages_to_show_player(player) ⇒ Array

Return zero or more messages for #show_to_player. An empty array of messages mean "don't show" or "no messages are required to show." The primary reason for this having an independent existence is for containers and other Displayables that can contain other Displayables - this is a way "hide" this Displayable inside another one.

The specific #messages_to_show_player superclass method is for parent-class information. A child class will either replace this information or supplement it, depending. Calling "super" in a child implementation isn't mandatory but may be useful.

Parameters:

Returns:

  • (Array)

    A JSON-serializable Array of messages which are used to show the Displayable to this player

Since:

  • 0.1.0



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pixiurge/displayable.rb', line 117

def messages_to_show_player(player)
  [{
      "type" => @displayable_type,
      "displayable" => {
        "x" => @x,
        "y" => @y,
        "location_block_width" => @location_displayable ? @location_displayable.block_width : nil,
        "location_block_height" => @location_displayable ? @location_displayable.block_height : nil,
        "position" => @position,
      }
  }]
end

#move_for_player(player, old_position, new_position, options = {}) ⇒ void

This method returns an undefined value.

Animate the motion of this Displayable from an old location to a new location for the stated player. This will be handled by the EngineConnector, usually as part of showing various things to multiple players.

When doing an animation or other transition, it's important to specify the older and newer coordinates. This can't easily use state in the item itself for both old and new positions - the same transition may need to be made for many viewing players, so setting the state as "part of the transition" doesn't work well. Further complicating things, different players aren't guaranteed to be seeing this Displayable object in the same state at the start of the transition, since they could have just arrived and missed an animation, for example.

Parameters:

  • player (Pixiurge::Player)

    The player to show

  • old_position (String)

    The old/beginning position string

  • new_position (String)

    The new/ending position string

Since:

  • 0.1.0



162
163
164
# File 'lib/pixiurge/displayable.rb', line 162

def move_for_player(player, old_position, new_position, options = {})
  player.message( Pixiurge::Protocol::Outgoing::DISPLAY_MOVE_DISPLAYABLE, @name, { "old_position" => old_position, "position" => new_position, "options" => options } )
end

#show_to_player(player) ⇒ void

This method returns an undefined value.

Show this Displayable to a player by sending a DISPLAY_SHOW_DISPLAYABLE message. Be very careful overriding this method - a Displayable that uses something other than a single DISPLAY_SHOW_DISPLAYABLE may not work properly if put inside a Container or other Displayable that contains other Displayables.

Parameters:

Since:

  • 0.1.0



95
96
97
98
99
# File 'lib/pixiurge/displayable.rb', line 95

def show_to_player(player)
  msgs = messages_to_show_player(player)
  return if msgs.nil? || msgs.empty?
  player.message(Pixiurge::Protocol::Outgoing::DISPLAY_SHOW_DISPLAYABLE, @name, *msgs)
end