Class: Reight::Chip

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.



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

#hObject (readonly)

Returns the value of attribute h.



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

def h
  @h
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image.



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

def image
  @image
end

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

Parameters:

  • value

    the value to set the attribute sensor to.



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

def sensor=(value)
  @sensor = value
end

#shapeObject

Returns the value of attribute shape.



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

def shape
  @shape
end

#wObject (readonly)

Returns the value of attribute w.



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

def w
  @w
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

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



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

Returns:

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

#frameObject



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

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

#inspectObject



80
81
82
# File 'lib/reight/chip.rb', line 80

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

#sensor?Boolean

Returns:

  • (Boolean)


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

def sensor? = @sensor

#spriteObject



62
63
64
# File 'lib/reight/chip.rb', line 62

def sprite()
  @sprite ||= to_sprite
end

#to_hashObject



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_spriteObject



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