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)

64
65
66
# File 'lib/ray/sprite.rb', line 64

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

#colorRay::Color

Returns:

See Also:


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

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).


65
66
67
68
69
# File 'ext/sprite.c', line 65

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


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

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.


75
76
77
78
79
# File 'ext/sprite.c', line 75

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.


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

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


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

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.


40
41
42
43
44
45
46
# File 'ext/sprite.c', line 40

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


28
29
30
31
32
33
# File 'ext/sprite.c', line 28

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

#inside?(obj) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

69
70
71
# File 'lib/ray/sprite.rb', line 69

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

#outside?(obj) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

74
75
76
# File 'lib/ray/sprite.rb', line 74

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

#pretty_print(q, other_attr = []) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ray/sprite.rb', line 82

def pretty_print(q, other_attr = [])
  attr = [
          "image",
          "color",
          "x_flipped?", "y_flipped?",
          "sub_rect", "rect"
         ]

  if uses_sprite_sheet?
    attr.concat ["sheet_size", "sheet_pos", "sprite_width", "sprite_height"]
  end

  super q, attr + other_attr
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.


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ray/sprite.rb', line 49

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

  top_left = (-origin * scale) + pos

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

#sheet_posRay::Vector2?

Returns Position in the sprite sheet.

Returns:


175
176
177
178
179
180
181
182
# File 'ext/sprite.c', line 175

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

162
163
164
165
166
167
168
169
170
171
172
# File 'ext/sprite.c', line 162

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:


139
140
141
142
143
144
145
146
# File 'ext/sprite.c', line 139

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).


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

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


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

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


185
186
187
188
189
190
191
192
# File 'ext/sprite.c', line 185

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


98
99
100
101
# File 'ext/sprite.c', line 98

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.


53
54
55
56
57
# File 'ext/sprite.c', line 53

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


78
79
80
# File 'lib/ray/sprite.rb', line 78

def to_rect
  rect
end

#uses_sprite_sheet?true, false

Returns True when using sprite sheets.

Returns:

  • (true, false)

    True when using sprite sheets


212
213
214
215
216
# File 'ext/sprite.c', line 212

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


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

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


119
120
121
122
# File 'ext/sprite.c', line 119

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