Class: Reight::Chip
- Inherits:
-
Object
- Object
- Reight::Chip
- Defined in:
- lib/reight/chip.rb
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.
6 7 8 9 |
# File 'lib/reight/chip.rb', line 6 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.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def h @h end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def id @id end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def image @image end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def pos @pos end |
#sensor=(value) ⇒ Object (writeonly)
Sets the attribute sensor
13 14 15 |
# File 'lib/reight/chip.rb', line 13 def sensor=(value) @sensor = value end |
#shape ⇒ Object
Returns the value of attribute shape.
11 12 13 |
# File 'lib/reight/chip.rb', line 11 def shape @shape end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def w @w end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
15 16 17 |
# File 'lib/reight/chip.rb', line 15 def y @y end |
Class Method Details
.restore(hash, image) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/reight/chip.rb', line 89 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
64 65 66 |
# File 'lib/reight/chip.rb', line 64 def clear_sprite() @sprite = nil end |
#cmp__(o) ⇒ Object
83 84 85 86 87 |
# File 'lib/reight/chip.rb', line 83 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
21 22 23 |
# File 'lib/reight/chip.rb', line 21 def empty?() pixels.all? {red(_1) == 0 && green(_1) == 0 && blue(_1) == 0} end |
#frame ⇒ Object
17 |
# File 'lib/reight/chip.rb', line 17 def frame = [x, y, w, h] |
#inspect ⇒ Object
78 79 80 |
# File 'lib/reight/chip.rb', line 78 def inspect() "#<#{self.class.name}:0x#{object_id}>" end |
#sensor? ⇒ Boolean
19 |
# File 'lib/reight/chip.rb', line 19 def sensor? = @sensor |
#sprite ⇒ Object
60 61 62 |
# File 'lib/reight/chip.rb', line 60 def sprite() @sprite ||= to_sprite end |
#to_hash ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/reight/chip.rb', line 68 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/reight/chip.rb', line 41 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/reight/chip.rb', line 25 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 |