Class: Bestguigui::Particle
- Inherits:
-
Object
- Object
- Bestguigui::Particle
- Defined in:
- lib/bestguigui/particle.rb
Overview
Une particule qui se déplace en ligne droite (angle + vitesse) et meurt soit par fondu (fade_speed, en alpha perdu par seconde), soit selon une condition externe (dead_when), soit les deux.
Bestguigui::Particle.new(x: 100, y: 100, angle: Gosu.random(0, 360),
speed: Gosu.random(10, 200), size: 3,
fade_speed: 800)
Bestguigui::Particle.new(x: x, y: -100, angle: 180, speed: 150, size: 3,
color: Gosu::Color.new(128, 255, 255, 255),
dead_when: ->(p) { p.y >= 600 })
Utilisée seule ou via Particles pour gérer une collection.
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #dead? ⇒ Boolean
- #draw ⇒ Object
-
#initialize(x:, y:, angle:, speed:, size:, color: Gosu::Color::WHITE, fade_speed: nil, z: 0, dead_when: nil) ⇒ Particle
constructor
A new instance of Particle.
- #update(dt) ⇒ Object
Constructor Details
#initialize(x:, y:, angle:, speed:, size:, color: Gosu::Color::WHITE, fade_speed: nil, z: 0, dead_when: nil) ⇒ Particle
Returns a new instance of Particle.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bestguigui/particle.rb', line 18 def initialize(x:, y:, angle:, speed:, size:, color: Gosu::Color::WHITE, fade_speed: nil, z: 0, dead_when: nil) @x = x @y = y @angle = angle @speed = speed @size = size @color = color @fade_speed = fade_speed @z = z @dead_when = dead_when end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
16 17 18 |
# File 'lib/bestguigui/particle.rb', line 16 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
16 17 18 |
# File 'lib/bestguigui/particle.rb', line 16 def y @y end |
Instance Method Details
#dead? ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/bestguigui/particle.rb', line 41 def dead? return true if @fade_speed && @color.alpha <= 0 return true if @dead_when&.call(self) false end |
#draw ⇒ Object
48 49 50 |
# File 'lib/bestguigui/particle.rb', line 48 def draw Gosu.draw_rect(@x - @size / 2, @y - @size / 2, @size, @size, @color, @z) end |
#update(dt) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/bestguigui/particle.rb', line 31 def update(dt) @x += Gosu.offset_x(@angle, @speed * dt) @y += Gosu.offset_y(@angle, @speed * dt) return unless @fade_speed alpha = [@color.alpha - (@fade_speed * dt).floor, 0].max @color = Gosu::Color.new(alpha, @color.red, @color.green, @color.blue) end |