Class: Reight::Chip

Inherits:
Object
  • Object
show all
Defined in:
lib/reight/chip.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hObject (readonly)

Returns the value of attribute h.



15
16
17
# File 'lib/reight/chip.rb', line 15

def h
  @h
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/reight/chip.rb', line 15

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



15
16
17
# File 'lib/reight/chip.rb', line 15

def image
  @image
end

#posObject (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

Parameters:

  • value

    the value to set the attribute sensor to.



13
14
15
# File 'lib/reight/chip.rb', line 13

def sensor=(value)
  @sensor = value
end

#shapeObject

Returns the value of attribute shape.



11
12
13
# File 'lib/reight/chip.rb', line 11

def shape
  @shape
end

#wObject (readonly)

Returns the value of attribute w.



15
16
17
# File 'lib/reight/chip.rb', line 15

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



15
16
17
# File 'lib/reight/chip.rb', line 15

def x
  @x
end

#yObject (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_spriteObject



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

Returns:

  • (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

#frameObject



17
# File 'lib/reight/chip.rb', line 17

def frame   = [x, y, w, h]

#inspectObject



78
79
80
# File 'lib/reight/chip.rb', line 78

def inspect()
  "#<#{self.class.name}:0x#{object_id}>"
end

#sensor?Boolean

Returns:

  • (Boolean)


19
# File 'lib/reight/chip.rb', line 19

def sensor? = @sensor

#spriteObject



60
61
62
# File 'lib/reight/chip.rb', line 60

def sprite()
  @sprite ||= to_sprite
end

#to_hashObject



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_spriteObject



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