Class: RubySketch::Context
- Inherits:
-
Processing::Context
- Object
- Processing::Context
- RubySketch::Context
- Defined in:
- lib/rubysketch/context.rb
Constant Summary collapse
- Sprite =
RubySketch::Sprite
Instance Method Summary collapse
-
#addSprite(sprite) ⇒ Sprite
Adds the sprite to the physics engine.
-
#createSprite(*args, **kwargs) ⇒ Object
Creates a new sprite and add it to physics engine.
-
#gravity(*args) ⇒ Object
Sets gravity for the physics engine.
-
#removeSprite(sprite) ⇒ Sprite
Removes the sprite from the physics engine.
-
#sprite(*sprites) ⇒ nil
(also: #drawSprite)
Draws one or more sprites.
Instance Method Details
#addSprite(sprite) ⇒ Sprite
Adds the sprite to the physics engine.
52 53 54 55 |
# File 'lib/rubysketch/context.rb', line 52 def addSprite(sprite) @layer__.add sprite.getInternal__ if sprite sprite end |
#createSprite(image: img) ⇒ Object #createSprite(x, y, image: img) ⇒ Object #createSprite(x, y, w, h) ⇒ Object #createSprite(x, y, w, h, image: img, offset: off) ⇒ Object
Creates a new sprite and add it to physics engine.
42 43 44 |
# File 'lib/rubysketch/context.rb', line 42 def createSprite(*args, **kwargs) addSprite Sprite.new(*args, **kwargs, context: self) end |
#gravity(vec) ⇒ Object #gravity(ary) ⇒ Object #gravity(x, y) ⇒ Object
Sets gravity for the physics engine.
126 127 128 129 130 131 132 133 134 |
# File 'lib/rubysketch/context.rb', line 126 def gravity(*args) x, y = case arg = args.first when Vector then arg.array when Array then arg else args end @layer__.gravity x, y end |
#removeSprite(sprite) ⇒ Sprite
Removes the sprite from the physics engine.
63 64 65 66 |
# File 'lib/rubysketch/context.rb', line 63 def removeSprite(sprite) @layer__.remove sprite.getInternal__ if sprite sprite end |
#sprite(*sprites) ⇒ nil Also known as: drawSprite
Draws one or more sprites.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rubysketch/context.rb', line 74 def sprite(*sprites) sprites.flatten! if sprites.first&.is_a? Array sprites.each do |sp| view, draw = sp.getInternal__, sp.instance_variable_get(:@drawBlock__) f, degrees, pivot = view.frame, view.angle, view.pivot if draw push do translate f.x + pivot.x * f.w, f.y + pivot.y * f.h rotate fromDegrees__ degrees translate (-pivot.x) * f.w, (-pivot.y) * f.h draw.call {drawSprite__ sp, 0, 0, f.w, f.h} end elsif degrees == 0 drawSprite__ sp, f.x, f.y, f.w, f.h else pushMatrix do translate f.x + pivot.x * f.w, f.y + pivot.y * f.h rotate fromDegrees__ degrees translate (-pivot.x) * f.w, (-pivot.y) * f.h drawSprite__ sp, 0, 0, f.w, f.h end end end nil end |