Class: Reight::Chip
Instance Attribute Summary collapse
-
#h ⇒ Object
readonly
Returns the value of attribute h.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
-
#sensor ⇒ Object
writeonly
Sets the attribute sensor.
-
#shape ⇒ Object
Returns the value of attribute shape.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #clear_sprite ⇒ Object
- #cmp__(o) ⇒ Object
- #empty? ⇒ Boolean
- #frame ⇒ Object
-
#initialize(id, image, x, y, w, h, pos: nil, shape: nil, sensor: nil) ⇒ Chip
constructor
A new instance of Chip.
- #inspect ⇒ Object
- #sensor? ⇒ Boolean
- #sprite ⇒ Object
- #to_hash ⇒ Object
- #to_sprite ⇒ Object
- #with(**kwargs) ⇒ Object
Constructor Details
#initialize(id, image, x, y, w, h, pos: nil, shape: nil, sensor: nil) ⇒ Chip
Returns a new instance of Chip.
8 9 10 11 |
# File 'lib/reight/chip.rb', line 8 def initialize(id, image, x, y, w, h, pos: nil, shape: nil, sensor: nil) @id, @image, @x, @y, @w, @h, @pos, @shape, @sensor = id, image, x, y, w, h, pos, shape, (sensor || false) end |
Instance Attribute Details
#h ⇒ Object (readonly)
Returns the value of attribute h.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def h @h end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def id @id end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def image @image end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def pos @pos end |
#sensor=(value) ⇒ Object (writeonly)
Sets the attribute sensor
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def sensor=(value) @sensor = value end |
#shape ⇒ Object
Returns the value of attribute shape.
13 14 15 |
# File 'lib/reight/chip.rb', line 13 def shape @shape end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def w @w end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
17 18 19 |
# File 'lib/reight/chip.rb', line 17 def y @y end |
Class Method Details
.restore(hash, image) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/reight/chip.rb', line 91 def self.restore(hash, image) id, x, y, w, h, pos, shape, sensor = hash.values_at :id, :x, :y, :w, :h, :pos, :shape, :sensor #hash => {id:, x:, y:, w:, h:, pos:, shape:, sensor:} new( id, image, x, y, w, h, pos: pos&.then {create_vector(*_1)}, shape: shape&.to_sym, sensor: sensor || false) end |
Instance Method Details
#clear_sprite ⇒ Object
66 67 68 |
# File 'lib/reight/chip.rb', line 66 def clear_sprite() @sprite = nil end |
#cmp__(o) ⇒ Object
85 86 87 88 89 |
# File 'lib/reight/chip.rb', line 85 def cmp__(o) a = [@id, @image.object_id, @x, @y, @w, @h, @pos, @shape, @sensor] b = o.instance_eval {[@id, @image.object_id, @x, @y, @w, @h, @pos, @shape, @sensor]} a <=> b end |
#empty? ⇒ Boolean
23 24 25 |
# File 'lib/reight/chip.rb', line 23 def empty?() pixels.all? {red(_1) == 0 && green(_1) == 0 && blue(_1) == 0} end |
#frame ⇒ Object
19 |
# File 'lib/reight/chip.rb', line 19 def frame = [x, y, w, h] |
#inspect ⇒ Object
80 81 82 |
# File 'lib/reight/chip.rb', line 80 def inspect() "#<#{self.class.name}:0x#{object_id}>" end |
#sensor? ⇒ Boolean
21 |
# File 'lib/reight/chip.rb', line 21 def sensor? = @sensor |
#sprite ⇒ Object
62 63 64 |
# File 'lib/reight/chip.rb', line 62 def sprite() @sprite ||= to_sprite end |
#to_hash ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/reight/chip.rb', line 70 def to_hash() { id: id, x: x, y: y, w: w, h: h }.tap do |h| h[:pos] = pos.to_a(2) if pos h[:shape] = shape if shape h[:sensor] = true if sensor? end end |
#to_sprite ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/reight/chip.rb', line 43 def to_sprite() physics, shape = case @shape when :rect then [true, nil] when :circle then [true, RubySketch::Circle.new(0, 0, w)] else [false, nil] end Reight::Sprite.new( 0, 0, w, h, chip: self, image: image, offset: [x, y], shape: shape, physics: physics ).tap do |sp| sp.x, sp.y = pos.x, pos.y if pos if physics sp.sensor = true if sensor? sp.fix_angle end end end |
#with(**kwargs) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/reight/chip.rb', line 27 def with(**kwargs) id, image, x, y, w, h, pos, shape, sensor = kwargs.values_at :id, :image, :x, :y, :w, :h, :pos, :shape, :sensor #kwargs => {id:, image:, x:, y:, w:, h:, pos:, shape:, sensor:} self.class.new( id || @id, image || @image, x || @x, y || @y, w || @w, h || @h, pos: kwargs.key?(:pos) ? pos : @pos, shape: kwargs.key?(:shape) ? shape : @shape, sensor: kwargs.key?(:sensor) ? sensor : @sensor) end |