Module: OR2D::Resource

Extended by:
Resource
Included in:
Resource
Defined in:
lib/or2d/resource.rb

Overview

The Resource module contains helper methods for creating resources.

Author:

  • Patrick W.

Since:

  • 2023-04-20

Instance Method Summary collapse

Instance Method Details

#create(type, options) ⇒ Object

Construct a new resource of the given type with the passed options.

Parameters:

  • type (Symbol)

    the type of resource to create

  • options (Hash)

    the options to pass to the resource

Since:

  • 2023-04-20



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/or2d/resource.rb', line 9

def create(type, options)
  case type
  when :image then OR2D::Resource.create_image(options)
  when :sprite then OR2D::Resource.create_sprite(options)
  when :text then OR2D::Resource.create_text(options)
  when :tileset then OR2D::Resource.create_tileset(options)
  when :triangle then OR2D::Resource.create_triangle(options)
  when :quad then OR2D::Resource.create_quad(options)
  when :circle then OR2D::Resource.create_circle(options)
  when :square then OR2D::Resource.create_square(options)
  when :line then OR2D::Resource.create_line(options)
  when :rectangle then OR2D::Resource.create_rectangle(options)
  else raise "Unknown resource type: #{type}!"
  end
end

#create_circle(options) ⇒ Ruby2D::Circle

Create a Ruby2D::Circle object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Circle

Returns:

  • (Ruby2D::Circle)

    the created Circle

Since:

  • 2023-04-20



28
29
30
31
32
33
34
35
36
# File 'lib/or2d/resource.rb', line 28

def create_circle(options)
  Ruby2D::Circle.new(x: options[:x] || 0,
                     y: options[:y] || 0,
                     z: options[:z] || 0,
                     radius: options[:radius] || 50,
                     sectors: options[:sectors] || 30,
                     color: options[:color] || 'yellow',
                     opacity: options[:opacity] || 1.0)
end

#create_image(options) ⇒ Ruby2D::Image

Create a Ruby2D::Image object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Image

Returns:

  • (Ruby2D::Image)

    the created Image

Since:

  • 2023-04-20



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/or2d/resource.rb', line 41

def create_image(options)
  Ruby2D::Image.new(options[:path],
                    atlas: options[:atlas],
                    width: options[:width].scale || 4,
                    height: options[:height].scale || 4,
                    opacity: options[:opacity] || 1.0,
                    rotate: options[:rotate] || 0,
                    x: options[:x] || 0,
                    y: options[:y] || 0,
                    z: options[:z] || 0)
end

#create_line(options) ⇒ Ruby2D::Line

Create a Ruby2D::Line object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Line

Returns:

  • (Ruby2D::Line)

    the created Line

Since:

  • 2023-04-20



56
57
58
59
60
61
62
63
64
65
# File 'lib/or2d/resource.rb', line 56

def create_line(options)
  Ruby2D::Line.new(x1: options[:x1],
                   y1: options[:y1],
                   x2: options[:x2],
                   y2: options[:y2],
                   z: options[:z] || 0,
                   width: options[:width].scale || 2,
                   color: options[:color] || 'yellow',
                   opacity: options[:opacity] || 1.0)
end

#create_quad(options) ⇒ Ruby2D::Quad

Create a Ruby2D::Quad object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Quad

Returns:

  • (Ruby2D::Quad)

    the created Quad

Since:

  • 2023-04-20



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/or2d/resource.rb', line 165

def create_quad(options)
  Ruby2D::Quad.new(x1: options[:x1],
                   y1: options[:y1],
                   x2: options[:x2],
                   y2: options[:y2],
                   x3: options[:x3],
                   y3: options[:y3],
                   x4: options[:x4],
                   y4: options[:y4],
                   z: options[:z],
                   color: options[:color] || 'yellow',
                   opacity: options[:opacity] || 1.0)
end

#create_rectangle(options) ⇒ Ruby2D::Rectangle

Create a Ruby2D::Rectangle object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Rectangle

Returns:

  • (Ruby2D::Rectangle)

    the created Rectangle

Since:

  • 2023-04-20



70
71
72
73
74
75
76
77
78
# File 'lib/or2d/resource.rb', line 70

def create_rectangle(options)
  Ruby2D::Rectangle.new(x: options[:x] || 0,
                        y: options[:y] || 0,
                        z: options[:z] || 0,
                        width: options[:width].scale,
                        height: options[:height].scale,
                        color: options[:color] || 'yellow',
                        opacity: options[:opacity] || 1.0)
end

#create_sprite(options) ⇒ Ruby2D::Sprite

Create a Ruby2D::Sprite object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Sprite

Returns:

Since:

  • 2023-04-20



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/or2d/resource.rb', line 83

def create_sprite(options)
  Ruby2D::Sprite.new(options[:path],
                     atlas: options[:atlas],
                     width: options[:width] ? options[:width].scale : nil,
                     height: options[:height] ? options[:height].scale : nil,
                     rotate: options[:rotate] || 0,
                     opacity: options[:opacity] || 1.0,
                     loop: options[:loop] || false,
                     time: options[:time] || 100,
                     animations: options[:animations] || {},
                     default: options[:default] || 0,
                     clip_height: options[:clip_height],
                     clip_width: options[:clip_width],
                     clip_x: options[:clip_x] || 0,
                     clip_y: options[:clip_y] || 0,
                     x: options[:x] || 0,
                     y: options[:y] || 0,
                     z: options[:z] || 0)
end

#create_square(options) ⇒ Ruby2D::Square

Create a Ruby2D::Square object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Square

Returns:

  • (Ruby2D::Square)

    the created Square

Since:

  • 2023-04-20



106
107
108
109
110
111
112
113
# File 'lib/or2d/resource.rb', line 106

def create_square(options)
  Ruby2D::Square.new(x: options[:x] || 0,
                     y: options[:y] || 0,
                     z: options[:z] || 0,
                     size: options[:size],
                     color: options[:color] || 'yellow',
                     opacity: options[:opacity] || 1.0)
end

#create_text(options) ⇒ Ruby2D::Text

Create a Ruby2D::Text object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Text

Returns:

  • (Ruby2D::Text)

    the created Text

Since:

  • 2023-04-20



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/or2d/resource.rb', line 118

def create_text(options)
  Ruby2D::Text.new(options[:text],
                   x: options[:x] || 0,
                   y: options[:y] || 0,
                   z: options[:z] || 0,
                   size: (options[:size] || 16).scale,
                   style: options[:style],
                   font: options[:font] || Ruby2D::Font.default,
                   color: options[:color] || 'yellow',
                   rotate: options[:rotate] || 0,
                   opacity: options[:opacity] || 1.0)
end

#create_tileset(options) ⇒ Ruby2D::Tileset

Create a Ruby2D::Tileset object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Tileset

Returns:

  • (Ruby2D::Tileset)

    the created Tileset

Since:

  • 2023-04-20



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/or2d/resource.rb', line 134

def create_tileset(options)
  Ruby2D::Tileset.new(options[:path],
                      tile_width: options[:tile_width] || 16,
                      tile_height: options[:tile_height] || 16,
                      width: options[:width],
                      height: options[:height],
                      z: options[:z] || 100,
                      padding: options[:padding] || 0,
                      spacing: options[:spacing] || 0,
                      scale: options[:scale] || 1,
                      show: options[:show] || false)
end

#create_triangle(options) ⇒ Ruby2D::Triangle

Create a Ruby2D::Triangle object from the given options.

Parameters:

  • options (Hash)

    the options to pass to the Triangle

Returns:

  • (Ruby2D::Triangle)

    the created Triangle

Since:

  • 2023-04-20



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/or2d/resource.rb', line 150

def create_triangle(options)
  Ruby2D::Triangle.new(x1: options[:x1],
                       y1: options[:y1],
                       x2: options[:x2],
                       y2: options[:y2],
                       x3: options[:x3],
                       y3: options[:y3],
                       z: options[:z],
                       color: options[:color] || 'yellow',
                       opacity: options[:opacity] || 1.0)
end