Class: Smalruby3::Sprite

Direct Known Subclasses

Stage

Constant Summary collapse

ROTATION_STYLE =
{
  all_around: "all around",
  left_right: "left-right",
  none: "don't rotate",
}

Constants included from Smalruby3::SpriteMethod::Control

Smalruby3::SpriteMethod::Control::STOP_OPTION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Smalruby3::SpriteMethod::Variables

#hide_list, #hide_variable, #show_list, #show_variable

Methods included from Smalruby3::SpriteMethod::Sensing

#touching?

Methods included from Smalruby3::SpriteMethod::Control

#create_clone, #delete_this_clone, #forever, #repeat, #stop, #wait

Methods included from Smalruby3::SpriteMethod::Events

#broadcast, #broadcast_and_wait, #when

Methods included from Smalruby3::SpriteMethod::Looks

#say

Methods included from Smalruby3::SpriteMethod::Motion

#direction=, #go_to, #move, #point_towards, #position=, #turn_left, #turn_right, #x=, #y=

Constructor Details

#initialize(name, options = {}, &block) ⇒ Sprite

Returns a new instance of Sprite.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smalruby3/sprite.rb', line 45

def initialize(name, options = {}, &block)
  @name = name
  @x = 0
  @y = 0
  @costumes = []
  @current_costume = 0

  @vector = { x: 1, y: 0 }
  @event_handlers = {}
  @threads = []
  @name_to_costume = {}

  @dxruby_sprite = DXRuby::Sprite.new(0, 0)

  defaults = {
    x: 0,
    y: 0,
    direction: 90,
    visible: true,
    size: 100,
    current_costume: 0,
    costumes: [],
    rotation_style: ROTATION_STYLE[:all_around],
    variables: [],
    lists: [],
  }
  opts = Util.process_options(options, defaults)
  opts.each do |k, v|
    send("#{k}=", v)
  end

  World.instance.add_target(self)

  if block_given?
    instance_eval(&block)
  end
end

Instance Attribute Details

#checking_hit_targetsObject

Returns the value of attribute checking_hit_targets.



40
41
42
# File 'lib/smalruby3/sprite.rb', line 40

def checking_hit_targets
  @checking_hit_targets
end

#costumesObject

Returns the value of attribute costumes.



32
33
34
# File 'lib/smalruby3/sprite.rb', line 32

def costumes
  @costumes
end

#current_costumeObject

Returns the value of attribute current_costume.



31
32
33
# File 'lib/smalruby3/sprite.rb', line 31

def current_costume
  @current_costume
end

#directionObject (readonly)

Returns the value of attribute direction.



28
29
30
# File 'lib/smalruby3/sprite.rb', line 28

def direction
  @direction
end

#enable_penObject (readonly)

Returns the value of attribute enable_pen.



41
42
43
# File 'lib/smalruby3/sprite.rb', line 41

def enable_pen
  @enable_pen
end

#event_handlersObject

Returns the value of attribute event_handlers.



37
38
39
# File 'lib/smalruby3/sprite.rb', line 37

def event_handlers
  @event_handlers
end

#listsObject

Returns the value of attribute lists.



35
36
37
# File 'lib/smalruby3/sprite.rb', line 35

def lists
  @lists
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/smalruby3/sprite.rb', line 25

def name
  @name
end

#pen_colorObject

Returns the value of attribute pen_color.



42
43
44
# File 'lib/smalruby3/sprite.rb', line 42

def pen_color
  @pen_color
end

#rotation_styleObject

Returns the value of attribute rotation_style.



33
34
35
# File 'lib/smalruby3/sprite.rb', line 33

def rotation_style
  @rotation_style
end

#sizeObject

Returns the value of attribute size.



30
31
32
# File 'lib/smalruby3/sprite.rb', line 30

def size
  @size
end

#threadsObject

Returns the value of attribute threads.



38
39
40
# File 'lib/smalruby3/sprite.rb', line 38

def threads
  @threads
end

#variablesObject

Returns the value of attribute variables.



34
35
36
# File 'lib/smalruby3/sprite.rb', line 34

def variables
  @variables
end

#visibleObject

Returns the value of attribute visible.



29
30
31
# File 'lib/smalruby3/sprite.rb', line 29

def visible
  @visible
end

#volumeObject

Returns the value of attribute volume.



43
44
45
# File 'lib/smalruby3/sprite.rb', line 43

def volume
  @volume
end

#xObject (readonly)

Returns the value of attribute x.



26
27
28
# File 'lib/smalruby3/sprite.rb', line 26

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



27
28
29
# File 'lib/smalruby3/sprite.rb', line 27

def y
  @y
end

Instance Method Details

#costumeObject



110
111
112
# File 'lib/smalruby3/sprite.rb', line 110

def costume
  @costumes[@current_costume]
end

#drawObject



131
132
133
134
135
# File 'lib/smalruby3/sprite.rb', line 131

def draw
  if @visible
    @dxruby_sprite.draw
  end
end

#fire(event, *options) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/smalruby3/sprite.rb', line 137

def fire(event, *options)
  if (events = @event_handlers[event])
    events.each do |e|
      if e.options == options
        @threads << e.call
      end
    end
  end
end

#join_threads(wait = false) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/smalruby3/sprite.rb', line 147

def join_threads(wait = false)
  @threads.compact!
  error = false
  @threads.delete_if { |t|
    if t.alive?
      false
    else
      begin
        t.join
      rescue => e
        Util.print_exception(e)
        error = true
      end
      true
    end
  }
  if error
    exit(1)
  end
  if wait
    @threads.each(&:join)
  end
end

#positionObject



87
88
89
# File 'lib/smalruby3/sprite.rb', line 87

def position
  [x, y]
end

#stage?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/smalruby3/sprite.rb', line 83

def stage?
  false
end