Class: Ray::Sprite

Inherits:
Drawable
  • Object
show all
Defined in:
lib/ray/sprite.rb,
ext/sprite.c

Instance Method Summary collapse

Constructor Details

#initialize(img = nil, opts = {}) ⇒ Sprite

Creates a sprite.

Parameters:

  • img (String, Ray::Image) (defaults to: nil)

    The image this object will use

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :at (Ray::Vecor2, #to_Vector2) — default: (0,0)

    Position of the sprite

  • :rect (Ray::Rect, Array<Integer>)

    Rect of the image which will be drawn.

  • :angle (Float) — default: 0

    The angle which will be used to draw the image in degrees. Defaults to 0.

  • :zoom (Ray::Vector2) — default: 1, 1

    The zoom level which will be used to draw the image.

  • :scale (Ray::Vector2)

    Alias for :zoom

  • :color (Ray::Color) — default: Ray::Color.white

    Color used, multiplying each pixel of the sprite.

  • :flip_x (true, false) — default: false

    Set to true to flip the sprite horizontally.

  • :flip_y (true, false) — default: false

    Set to true to flip the sprite vertically.

  • :origin (Ray::Vector2) — default: (0, 0)

    The origin of transformations

  • :shader (Ray::Shader) — default: nil

    Shader



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ray/sprite.rb', line 21

def initialize(img = nil, opts = {})
  self.image = img.is_a?(String) ? Ray::ImageSet[img] : img

  opts = {
    :at     => Ray::Vector2[0, 0],
    :angle  => 0,
    :zoom   => Ray::Vector2[1, 1],
    :color  => Ray::Color.white,
    :origin => Ray::Vector2[0, 0]
  }.merge(opts)

  self.pos      = opts[:at]
  self.sub_rect = opts[:rect] if opts[:rect]
  self.angle    = opts[:angle]
  self.scale    = opts[:scale] || opts[:zoom]
  self.color    = opts[:color]
  self.flip_x   = opts[:flip_x]
  self.flip_y   = opts[:flip_y]
  self.origin   = opts[:origin]
  self.shader   = opts[:shader]

  # @uses_sprite_sheet = false
  # @sprite_sheet_size = Ray::Vector2[1, 1]
  # @sprite_sheet_pos  = Ray::Vector2[0, 0]
end

Instance Method Details

#collide?(obj) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


118
119
120
# File 'lib/ray/sprite.rb', line 118

def collide?(obj)
  rect.collide?(obj.to_rect)
end

#colorRay::Color

Returns:

See Also:



100
101
102
103
# File 'ext/sprite.c', line 100

static
VALUE ray_sprite_color(VALUE self) {
  return ray_col2rb(say_sprite_get_color(ray_rb2sprite(self)));
}

#color=(col) ⇒ Object

Parameters:

  • col (Ray::Color)

    Color of the sprite. The color of each pixel will be multiplied by this color. The defaul is white (which means the color of the image isn’t changed).



58
59
60
61
62
# File 'ext/sprite.c', line 58

static
VALUE ray_sprite_set_color(VALUE self, VALUE color) {
  say_sprite_set_color(ray_rb2sprite(self), ray_rb2col(color));
  return color;
}

#disable_sprite_sheetObject

Disables usage of sprite sheet



198
199
200
201
202
# File 'ext/sprite.c', line 198

static
VALUE ray_sprite_disable_sprite_sheet(VALUE self) {
  say_sprite_disable_sprite_sheet(ray_rb2sprite(self));
  return self;
}

#flip_x=(val) ⇒ Object

Parameters:

  • val (true, false)

    True to flip the sprite horizontally.



68
69
70
71
72
# File 'ext/sprite.c', line 68

static
VALUE ray_sprite_set_flip_x(VALUE self, VALUE val) {
  say_sprite_flip_x(ray_rb2sprite(self), RTEST(val));
  return val;
}

#flip_y=(val) ⇒ Object

Parameters:

  • val (true, false)

    True to flip the sprite vertically.



78
79
80
81
82
# File 'ext/sprite.c', line 78

static
VALUE ray_sprite_set_flip_y(VALUE self, VALUE val) {
  say_sprite_flip_y(ray_rb2sprite(self), RTEST(val));
  return val;
}

#imageRay::Image?

Returns The image used by this sprite.

Returns:

  • (Ray::Image, nil)

    The image used by this sprite



85
86
87
88
# File 'ext/sprite.c', line 85

static
VALUE ray_sprite_image(VALUE self) {
  return rb_iv_get(self, "@image");
}

#image=(img) ⇒ Object

Parameters:

  • img (Ray::Image, nil)

    The image this sprite will use. No image means it will just use the default one.



33
34
35
36
37
38
39
# File 'ext/sprite.c', line 33

static
VALUE ray_sprite_set_image(VALUE self, VALUE img) {
  say_sprite_set_image(ray_rb2sprite(self),
                       NIL_P(img) ? NULL : ray_rb2image(img));
  rb_iv_set(self, "@image", img);
  return self;
}

#initialize_copy(orig) ⇒ Object



22
23
24
25
26
# File 'ext/sprite.c', line 22

VALUE ray_sprite_init_copy(VALUE self, VALUE orig) {
  rb_iv_set(self, "@image", rb_iv_get(orig, "@image"));
  say_sprite_copy(ray_rb2sprite(self), ray_rb2sprite(orig));
  return self;
}

#inside?(obj) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


123
124
125
# File 'lib/ray/sprite.rb', line 123

def inside?(obj)
  rect.inside?(obj.to_rect)
end

#outside?(obj) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


128
129
130
# File 'lib/ray/sprite.rb', line 128

def outside?(obj)
  rect.outside?(obj.to_rect)
end

#rectRay::Rect

Returns The rect where this sprite will be drawn, taking position and scale in account.

Returns:

  • (Ray::Rect)

    The rect where this sprite will be drawn, taking position and scale in account.



109
110
111
112
113
114
115
# File 'lib/ray/sprite.rb', line 109

def rect
  pos      = self.pos
  sub_rect = self.sub_rect
  scade    = self.scale

  Ray::Rect.new(pos.x, pos.y, sub_rect.w * scale.w, sub_rect.h * scale.h)
end

#sheet_posRay::Vector2?

Returns Position in the sprite sheet.

Returns:



168
169
170
171
172
173
174
175
# File 'ext/sprite.c', line 168

static
VALUE ray_sprite_sheet_pos(VALUE self) {
  say_sprite *sprite = ray_rb2sprite(self);
  if (say_sprite_uses_sprite_sheet(sprite))
    return ray_vector2_to_rb(say_sprite_get_sheet_pos(sprite));
  else
    return Qnil;
}

#sheet_pos=(pos) ⇒ Object

Sets which cell of the sprite sheet should be displayed. sprite.sheet_pos = [0, 1] # Uses the first cell of the second line.

pos.x and pos.y are rounded to floor. Passing a too high value will make the sprite use the previous cells.

sprite.sheet_size = [4, 4]

sprite.sheet_pos = [5, 5]
sprite.sheet_pos == [1, 1] # => true


155
156
157
158
159
160
161
162
163
164
165
# File 'ext/sprite.c', line 155

static
VALUE ray_sprite_set_sheet_pos(VALUE self, VALUE size) {
  say_sprite *sprite = ray_rb2sprite(self);

  if (say_sprite_uses_sprite_sheet(sprite))
    say_sprite_set_sheet_pos(ray_rb2sprite(self), ray_convert_to_vector2(size));
  else
    rb_raise(rb_eRuntimeError, "sprite sheet not enabled on this sprite");

  return size;
}

#sheet_sizeRay::Vector2?

Returns size of the sprite sheet.

Returns:



132
133
134
135
136
137
138
139
# File 'ext/sprite.c', line 132

static
VALUE ray_sprite_sheet_size(VALUE self) {
  say_sprite *sprite = ray_rb2sprite(self);
  if (say_sprite_uses_sprite_sheet(sprite))
    return ray_vector2_to_rb(say_sprite_get_sheet_size(sprite));
  else
    return Qnil;
}

#sheet_size=(size) ⇒ Object

Sets the size of the sprite sheet. For instance,

sprite.sheet_size = [3, 4]

would mean there are 4 rows and 3 columns in the sprite (and each cell has the same size).



125
126
127
128
129
# File 'ext/sprite.c', line 125

static
VALUE ray_sprite_set_sheet_size(VALUE self, VALUE size) {
  say_sprite_set_sheet_size(ray_rb2sprite(self), ray_convert_to_vector2(size));
  return size;
}

#sprite_heightFloat?

Returns height of a cell in the sprite she e.

Returns:

  • (Float, nil)

    height of a cell in the sprite she e



188
189
190
191
192
193
194
195
# File 'ext/sprite.c', line 188

static
VALUE ray_sprite_sprite_height(VALUE self) {
  say_sprite *sprite = ray_rb2sprite(self);
  if (say_sprite_uses_sprite_sheet(sprite))
    return rb_float_new(say_sprite_get_sprite_height(sprite));
  else
    return Qnil;
}

#sprite_widthFloat?

Returns width of a cell in the sprite sheet.

Returns:

  • (Float, nil)

    width of a cell in the sprite sheet



178
179
180
181
182
183
184
185
# File 'ext/sprite.c', line 178

static
VALUE ray_sprite_sprite_width(VALUE self) {
  say_sprite *sprite = ray_rb2sprite(self);
  if (say_sprite_uses_sprite_sheet(sprite))
    return rb_float_new(say_sprite_get_sprite_width(sprite));
  else
    return Qnil;
}

#sub_rectRay::Rect

Returns The part of the image shown by the sprite.

Returns:

  • (Ray::Rect)

    The part of the image shown by the sprite



91
92
93
94
# File 'ext/sprite.c', line 91

static
VALUE ray_sprite_sub_rect(VALUE self) {
  return ray_rect2rb(say_sprite_get_rect(ray_rb2sprite(self)));
}

#sub_rect=(rect) ⇒ Object

Parameters:

  • rect (Ray::Rect)

    Sets the part of the image the sprite will show. It is defaulted to the size of the image.



46
47
48
49
50
# File 'ext/sprite.c', line 46

static
VALUE ray_sprite_set_sub_rect(VALUE self, VALUE rect) {
  say_sprite_set_rect(ray_rb2sprite(self), ray_convert_to_rect(rect));
  return rect;
}

#to_rectObject



132
133
134
# File 'lib/ray/sprite.rb', line 132

def to_rect
  rect
end

#uses_sprite_sheet?true, false

Returns True when using sprite sheets.

Returns:

  • (true, false)

    True when using sprite sheets



205
206
207
208
209
# File 'ext/sprite.c', line 205

static
VALUE ray_sprite_uses_sprite_sheet(VALUE self) {
  return say_sprite_uses_sprite_sheet(ray_rb2sprite(self)) ?
    Qtrue : Qfalse;
}

#x_flipped?true, false

Returns True if the sprite is horizontally flipped.

Returns:

  • (true, false)

    True if the sprite is horizontally flipped



106
107
108
109
# File 'ext/sprite.c', line 106

static
VALUE ray_sprite_x_flipped(VALUE self) {
  return say_sprite_is_x_flipped(ray_rb2sprite(self)) ? Qtrue : Qfalse;
}

#y_flipped?true, false

Returns True if the sprite is vertically flipped.

Returns:

  • (true, false)

    True if the sprite is vertically flipped



112
113
114
115
# File 'ext/sprite.c', line 112

static
VALUE ray_sprite_y_flipped(VALUE self) {
  return say_sprite_is_y_flipped(ray_rb2sprite(self)) ? Qtrue : Qfalse;
}