Sha256: 16efa16149ef4f7b469c8462318adb7088865432aedc1fa9fa7bac1cba4c26b8

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

module Dedalus
  module Elements
    class SpriteField < Dedalus::Organism
      attr_accessor :grid, :sprite_map, :scale, :camera_location
      # TODO tiles path and width/height as attrs
      
      def show
        layers
      end

      def camera_offset
        if camera_location
          cx,cy = *camera_location
          [-cx * 64, -cy * 64]
        else
          [0,0]
        end
      end

      def layers
        layer_stack = Dedalus::LayerStack.new
        layer_stack.push(Dedalus::Layer.new(background_image))
        layer_stack.push(Dedalus::Layer.new(image_grid))

        layer_stack.push(canvas_layer)
        layer_stack
      end

      def canvas_layer
        Dedalus::Layer.new(sprites, freeform: true)
      end

      def sprites
        sprite_map.flat_map do |location, sprite_list|
          sprite_list.map do |sprite|
            position = to_screen_coordinates(location: location)
            sprite.position = position
            sprite
          end
        end
      end

      def image_grid
        ImageGrid.new(
          tiles_path: 'media/images/tiles.png',
          grid: grid,
          tile_width: 64,
          tile_height: 64,
          offset: camera_offset
        )
      end

      def to_screen_coordinates(location:)
        x,y = *location
        cx,cy = *camera_offset
        [(x * image_grid.tile_width + cx), (y * image_grid.tile_height + cy)]
      end

      def background_image
        Image.new(path: "media/images/cosmos.jpg", z_order: -1, scale: self.scale)
      end

      def self.description
        'sprites overlaid on an image grid'
      end

      def self.example_data
        {
          grid: [[0,2,0,0,0],
                 [0,0,0,0,0],
                 [1,1,3,1,1],
                 [1,1,1,1,1]],
          scale: 0.3,
          camera_location: [1.2,2.4],
          sprite_map: { [1.2,2.4] => [ Sprite.new(Sprite.example_data) ] }
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dedalus-0.2.13 lib/dedalus/elements/sprite_field.rb