Class: Bestguigui::Tiled::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/bestguigui/tiled.rb

Overview

Un objet Tiled : position/dimensions en pixels + propriétés custom (accessibles via [], comme déclarées dans l'éditeur).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Object

Returns a new instance of Object.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bestguigui/tiled.rb', line 131

def initialize(data)
  @name = data["name"]
  @x = data["x"]
  @y = data["y"]
  @width = data["width"]
  @height = data["height"]
  @properties = (data["properties"] || []).each_with_object({}) do |property, hash|
    hash[property["name"].to_sym] = property["value"]
  end

  # Beaucoup de niveaux Tiled catégorisent leurs objets via une
  # propriété custom nommée "type" plutôt que le champ natif
  # type/class de l'objet (qui reste vide si tu n'utilises pas les
  # "Object Types" de l'éditeur) — priorité à la propriété custom.
  native_type = data["type"] || data["class"] # Tiled < 1.9 vs >= 1.9
  @type = @properties[:type] || (native_type.nil? || native_type.empty? ? nil : native_type)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def height
  @height
end

#nameObject (readonly)

Returns the value of attribute name.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def properties
  @properties
end

#typeObject (readonly)

Returns the value of attribute type.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def type
  @type
end

#widthObject (readonly)

Returns the value of attribute width.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



129
130
131
# File 'lib/bestguigui/tiled.rb', line 129

def y
  @y
end

Instance Method Details

#[](key) ⇒ Object



160
161
162
# File 'lib/bestguigui/tiled.rb', line 160

def [](key)
  @properties[key]
end

#tile_xObject

Position convertie en coordonnées de tuile — pratique pour les marqueurs ponctuels (spawn, sortie...) placés comme un objet de la taille d'une tuile dans Tiled.



152
153
154
# File 'lib/bestguigui/tiled.rb', line 152

def tile_x
  (x / width).floor
end

#tile_yObject



156
157
158
# File 'lib/bestguigui/tiled.rb', line 156

def tile_y
  (y / height).floor
end