Module: Demiurge
- Defined in:
- lib/demiurge.rb,
lib/demiurge.rb,
lib/demiurge/tmx.rb,
lib/demiurge/tmx.rb,
lib/demiurge/util.rb,
lib/demiurge/zone.rb,
lib/demiurge/agent.rb,
lib/demiurge/version.rb,
lib/demiurge/location.rb,
lib/demiurge/container.rb,
lib/demiurge/exception.rb,
lib/demiurge/intention.rb,
lib/demiurge/action_item.rb,
lib/demiurge/inert_state_item.rb,
lib/demiurge/notification_names.rb
Overview
A Container may contain other items. Common examples include Zones and Locations.
Defined Under Namespace
Modules: ActionItemInternal, AgentInternal, DSL, Errors, Notifications, Util Classes: ActionItem, Agent, Container, Engine, InertStateItem, Intention, Location, RoomZone, StateItem, TileZone, TmxLocation, TmxZone, WanderingAgent, Zone
Constant Summary collapse
- VERSION =
Current version of Demiurge
"0.2.0"
Class Method Summary collapse
-
.animations_from_tilesets(tilesets) ⇒ Object
Find the animations included in the TMX file.
-
.sprites_from_manasource_tmx(filename) ⇒ Object
Load a TMX file and calculate the objects inside including the Spritesheet and Spritestack.
-
.sprites_from_tmx(filename) ⇒ Object
Load a TMX file and calculate the objects inside including the Spritesheet and Spritestack.
Class Method Details
.animations_from_tilesets(tilesets) ⇒ Object
Find the animations included in the TMX file
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/demiurge/tmx.rb', line 415 def self.animations_from_tilesets tilesets tilesets.flat_map do |tileset| (tileset.tiles || []).map do |tile| p = tile["properties"] if p && p["animation-frame0"] section = 0 anim = [] while p["animation-frame#{section}"] section_hash = { frame: p["animation-frame#{section}"].to_i + tileset[:firstgid], duration: p["animation-delay#{section}"].to_i } anim.push section_hash section += 1 end { "tile_anim_#{tile["id"].to_i + tileset[:firstgid]}".to_sym => anim } else nil end end.compact end.inject({}, &:merge) end |
.sprites_from_manasource_tmx(filename) ⇒ Object
Load a TMX file and calculate the objects inside including the Spritesheet and Spritestack. Assume this TMX file obeys ManaSource conventions such as fields for exits and names for layers.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/demiurge/tmx.rb', line 327 def self.sprites_from_manasource_tmx(filename) objs = sprites_from_tmx filename stack = objs[:spritestack] stack_layers = stack[:layers] # Remove the collision layer, add as separate collision top-level entry collision_index = stack_layers.index { |l| l[:name].downcase == "collision" } collision_layer = stack_layers.delete_at collision_index if collision_index # Some games make this true/false, others have separate visibility # or swimmability in it. In general, we'll just expose the data. objs[:collision] = collision_layer[:data] if collision_layer # Remove the heights layer, add as separate heights top-level entry heights_index = stack_layers.index { |l| ["height", "heights"].include?(l[:name].downcase) } heights_layer = stack_layers.delete_at heights_index if heights_index objs[:heights] = heights_layer fringe_index = stack_layers.index { |l| l[:name].downcase == "fringe" } raise ::Demiurge::Errors::TmxFormatError.new("No Fringe layer found in ManaSource TMX File #{filename.inspect}!", "filename" => filename) unless fringe_index stack_layers.each_with_index do |layer, index| # Assign a Z value based on layer depth, with fringe = 0 as a special case layer["z"] = (index - fringe_index) * 10.0 end objs end |
.sprites_from_tmx(filename) ⇒ Object
Load a TMX file and calculate the objects inside including the Spritesheet and Spritestack. Do not assume this TMX file obeys any particular additional conventions beyond basic TMX format.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/demiurge/tmx.rb', line 359 def self.sprites_from_tmx(filename) spritesheet = {} spritestack = {} # This recursively loads things like tileset .tsx files tiles = Tmx.load filename spritestack[:name] = tiles.name || File.basename(filename).split(".")[0] spritestack[:width] = tiles.width spritestack[:height] = tiles.height spritestack[:properties] = tiles.properties spritesheet[:tilewidth] = tiles.tilewidth spritesheet[:tileheight] = tiles.tileheight spritesheet[:images] = tiles.tilesets.map do |tileset| { firstgid: tileset.firstgid, tileset_name: tileset.name, image: tileset.image, imagewidth: tileset.imagewidth, imageheight: tileset.imageheight, tilewidth: tileset.tilewidth, tileheight: tileset.tileheight, oversize: tileset.tilewidth != tiles.tilewidth || tileset.tileheight != tiles.tileheight, spacing: tileset.spacing, margin: tileset.margin, imagetrans: tileset.imagetrans, # Currently unused, color to treat as transparent properties: tileset.properties, } end spritesheet[:cyclic_animations] = animations_from_tilesets tiles.tilesets spritesheet[:properties] = spritesheet[:images].map { |i| i[:properties] }.inject({}, &:merge) spritesheet[:name] = spritesheet[:images].map { |i| i[:tileset_name] }.join("/") spritestack[:spritesheet] = spritesheet[:name] spritestack[:layers] = tiles.layers.map do |layer| data = layer.data.each_slice(layer.width).to_a { name: layer.name, data: data, visible: layer.visible, opacity: layer.opacity, offsetx: layer.offsetx, # Currently unused offsety: layer.offsety, # Currently unused properties: layer.properties, } end objects = tiles.object_groups.flat_map { |og| og.objects.to_a }.map(&:to_h) { filename: filename, tmx_name: File.basename(filename).split(".")[0], spritesheet: spritesheet, spritestack: spritestack, objects: objects } end |