Class: Ray::Sprite
- Inherits:
-
Drawable
- Object
- Drawable
- Ray::Sprite
- Defined in:
- lib/ray/sprite.rb,
ext/sprite.c
Instance Method Summary collapse
- #collide?(obj) ⇒ Boolean
- #color ⇒ Ray::Color
- #color=(col) ⇒ Object
-
#disable_sprite_sheet ⇒ Object
Disables usage of sprite sheet.
- #flip_x=(val) ⇒ Object
- #flip_y=(val) ⇒ Object
-
#image ⇒ Ray::Image?
The image used by this sprite.
- #image=(img) ⇒ Object
-
#initialize(img = nil, opts = {}) ⇒ Sprite
constructor
Creates a sprite.
- #initialize_copy(orig) ⇒ Object
- #inside?(obj) ⇒ Boolean
- #outside?(obj) ⇒ Boolean
-
#rect ⇒ Ray::Rect
The rect where this sprite will be drawn, taking position and scale in account.
-
#sheet_pos ⇒ Ray::Vector2?
Position in the sprite sheet.
-
#sheet_pos=(pos) ⇒ Object
Sets which cell of the sprite sheet should be displayed.
-
#sheet_size ⇒ Ray::Vector2?
Size of the sprite sheet.
-
#sheet_size=(size) ⇒ Object
Sets the size of the sprite sheet.
-
#sprite_height ⇒ Float?
Height of a cell in the sprite she e.
-
#sprite_width ⇒ Float?
Width of a cell in the sprite sheet.
-
#sub_rect ⇒ Ray::Rect
The part of the image shown by the sprite.
- #sub_rect=(rect) ⇒ Object
- #to_rect ⇒ Object
-
#uses_sprite_sheet? ⇒ true, false
True when using sprite sheets.
-
#x_flipped? ⇒ true, false
True if the sprite is horizontally flipped.
-
#y_flipped? ⇒ true, false
True if the sprite is vertically flipped.
Constructor Details
#initialize(img = nil, opts = {}) ⇒ Sprite
Creates a sprite.
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
118 119 120 |
# File 'lib/ray/sprite.rb', line 118 def collide?(obj) rect.collide?(obj.to_rect) end |
#color ⇒ Ray::Color
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
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_sheet ⇒ Object
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
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
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;
}
|
#image ⇒ Ray::Image?
Returns 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
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
123 124 125 |
# File 'lib/ray/sprite.rb', line 123 def inside?(obj) rect.inside?(obj.to_rect) end |
#outside?(obj) ⇒ Boolean
128 129 130 |
# File 'lib/ray/sprite.rb', line 128 def outside?(obj) rect.outside?(obj.to_rect) end |
#rect ⇒ Ray::Rect
Returns 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_pos ⇒ Ray::Vector2?
Returns Position in the sprite sheet.
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_size ⇒ Ray::Vector2?
Returns size of the sprite sheet.
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_height ⇒ Float?
Returns 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_width ⇒ Float?
Returns 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_rect ⇒ Ray::Rect
Returns 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
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_rect ⇒ Object
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.
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.
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.
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; } |