Module: OR2D::Animations::EntityAnimations

Included in:
Entity
Defined in:
lib/or2d/animations/entity_animations.rb

Overview

A module that contains methods for animating entities.

Since:

  • 2023-04-26

Instance Method Summary collapse

Instance Method Details

#fade(speed: 0.1, entity: self) ⇒ Object

Fades an Animatable object in and out.

Parameters:

  • speed (Float) (defaults to: 0.1)

    the speed at which to fade the object in and out

  • entity (OR2D::Entity) (defaults to: self)

    the entity to fade in and out

Since:

  • 2023-04-26



7
8
9
10
11
12
13
# File 'lib/or2d/animations/entity_animations.rb', line 7

def fade(speed: 0.1, entity: self)
  if entity.resource.color.opacity.zero? || entity.resource.color.opacity.negative?
    fade_in(entity: entity, speed: speed)
  else
    fade_out(entity: entity, speed: speed)
  end
end

#fade_in(speed: 0.1, entity: self) ⇒ Object

Fades an Animatable object in.

Parameters:

  • speed (Float) (defaults to: 0.1)

    the speed at which to fade the object in

  • entity (OR2D::Entity) (defaults to: self)

    the entity to fade in

Since:

  • 2023-04-26



31
32
33
34
35
36
37
38
39
# File 'lib/or2d/animations/entity_animations.rb', line 31

def fade_in(speed: 0.1, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    if entity.resource.color.opacity >= 1.0
      animation.complete
    else
      entity.resource.color.opacity += speed
    end
  end
end

#fade_out(speed: 0.1, entity: self) ⇒ Object

Fades an Animatable object out.

Parameters:

  • speed (Float) (defaults to: 0.1)

    the speed at which to fade the object out

  • entity (OR2D::Entity) (defaults to: self)

    the entity to fade out

Since:

  • 2023-04-26



18
19
20
21
22
23
24
25
26
# File 'lib/or2d/animations/entity_animations.rb', line 18

def fade_out(speed: 0.1, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    if entity.resource.color.opacity.zero? || entity.resource.color.opacity.negative?
      animation.complete
    else
      entity.resource.color.opacity -= speed
    end
  end
end

#grow(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘shrink` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Grows an Animatable object.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to grow the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to grow

  • delay (Integer) (defaults to: 100)

    the delay at which to grow the object

Since:

  • 2023-04-26



67
68
69
70
71
72
73
# File 'lib/or2d/animations/entity_animations.rb', line 67

def grow(factor: 1.0, delay: 100, entity: self)
  if entity.resource.is_a?(Ruby2D::Text)
    grow_text(factor: factor, delay: delay, entity: entity)
  else
    grow_entity(factor: factor, delay: delay, entity: entity)
  end
end

#grow_entity(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘shrink` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Grows an Animatable Entity object. This is accomplished by changing the width and height of the Entity.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to grow the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to grow

  • delay (Integer) (defaults to: 100)

    the delay at which to grow the object

Since:

  • 2023-04-26



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/or2d/animations/entity_animations.rb', line 80

def grow_entity(factor: 1.0, delay: 100, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    animation.cache[:delay] ||= delay
    animation.cache[:end_width] ||= entity.resource.width + (entity.resource.width * factor)
    animation.cache[:end_height] ||= entity.resource.height + (entity.resource.height * factor)
    animation.cache[:increment_width] ||= ((animation.cache[:end_width] - entity.resource.width) / animation.cache[:delay]).round(3)
    animation.cache[:increment_height] ||= ((animation.cache[:end_height] - entity.resource.height) / animation.cache[:delay]).round(3)

    if animation.cache[:delay].positive?
      if entity.resource.width >= animation.cache[:end_width] &&
        entity.resource.height >= animation.cache[:end_height]
        animation.complete
      else
        entity.resource.width += animation.cache[:increment_width] if entity.resource.width != animation.cache[:end_width]
        entity.resource.height += animation.cache[:increment_height] if entity.resource.height != animation.cache[:end_height]
      end
    else
      entity.resource.width = animation.cache[:end_width]
      entity.resource.height = animation.cache[:end_height]
      animation.complete
    end
  end
end

#grow_text(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘shrink_text` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Grows an Animatable Text object. This is accomplished by changing the size of the Text.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to grow the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to grow

  • delay (Integer) (defaults to: 100)

    the delay at which to grow the object

Since:

  • 2023-04-26



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/or2d/animations/entity_animations.rb', line 109

def grow_text(factor: 1.0, delay: 100, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    animation.cache[:end_size] ||= entity.resource.size + (entity.resource.size * factor)
    animation.cache[:delay] ||= delay
    animation.cache[:increment] ||= ((animation.cache[:end_size] - entity.resource.size) / animation.cache[:delay]).round(3)


    if animation.cache[:delay].positive?
      if entity.resource.size >= animation.cache[:end_size]
        animation.complete
      else
        entity.resource.size += animation.cache[:increment]
      end
    else
      entity.resource.size = animation.cache[:end_size]
      animation.complete
    end
  end
end

#rotation(speed: 1.0, angle: 360, clockwise: true, entity: self) ⇒ Object

Rotates an Animatable object.

Parameters:

  • speed (Float) (defaults to: 1.0)

    the speed at which to rotate the object

  • angle (Integer) (defaults to: 360)

    the angle at which to stop rotating the object

  • clockwise (Boolean) (defaults to: true)

    whether or not to rotate the object clockwise

  • entity (OR2D::Entity) (defaults to: self)

    the entity to rotate

Since:

  • 2023-04-26



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/or2d/animations/entity_animations.rb', line 46

def rotation(speed: 1.0, angle: 360, clockwise: true, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    if clockwise
      entity.resource.rotate += speed
    else
      entity.resource.rotate -= speed
    end

    case angle
    when Range then animation.complete if angle.include?(entity.resource.rotate)
    when Integer then animation.complete if (entity.resource.rotate % angle).zero?
    else rotation(speed: speed, clockwise: clockwise, entity: entity)
    end
  end
end

#shake(duration: 100, magnitude: 10, entity: self) ⇒ Object

Shakes an Animatable object. This is accomplished by changing the x and y coordinates of the object.

Parameters:

  • duration (Integer) (defaults to: 100)

    the duration of the shake

  • magnitude (Integer) (defaults to: 10)

    the magnitude of the shake

  • entity (OR2D::Entity) (defaults to: self)

    the entity to shake

Since:

  • 2023-04-26



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/or2d/animations/entity_animations.rb', line 200

def shake(duration: 100, magnitude: 10, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    animation.cache[:duration] ||= duration
    animation.cache[:magnitude] ||= magnitude
    animation.cache[:x] ||= entity.x
    animation.cache[:y] ||= entity.y
    animation.cache[:increment] ||= ((animation.cache[:magnitude] / animation.cache[:duration]) * 0.001).round(3)
    jitter = rand(-animation.cache[:magnitude]..animation.cache[:magnitude])
    if animation.cache[:duration].positive?
      entity.x = animation.cache[:x] + animation.cache[:magnitude] + jitter
      entity.y = animation.cache[:y] + animation.cache[:magnitude] + jitter
      animation.cache[:duration] -= 1
      animation.cache[:magnitude] -= animation.cache[:increment]
    else
      entity.x = animation.cache[:x]
      entity.y = animation.cache[:y]
      animation.complete
    end
  end
end

#shrink(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘grow` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Shrinks an Animatable object.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to shrink the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to shrink

  • delay (Integer) (defaults to: 100)

    the delay at which to shrink the object

Since:

  • 2023-04-26



134
135
136
137
138
139
140
# File 'lib/or2d/animations/entity_animations.rb', line 134

def shrink(factor: 1.0, delay: 100, entity: self)
  if entity.resource.is_a?(Ruby2D::Text)
    shrink_text(factor: factor, delay: delay, entity: entity)
  else
    shrink_entity(factor: factor, delay: delay, entity: entity)
  end
end

#shrink_entity(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘grow_entity` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Shrinks an Animatable Entity object. This is accomplished by changing the width and height of the Entity.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to shrink the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to shrink

Since:

  • 2023-04-26



146
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/or2d/animations/entity_animations.rb', line 146

def shrink_entity(factor: 1.0, delay: 100, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    animation.cache[:delay] ||= delay
    animation.cache[:end_width] ||= entity.resource.width - (entity.resource.width * factor)
    animation.cache[:end_height] ||= entity.resource.height - (entity.resource.height * factor)
    animation.cache[:increment_width] ||= ((entity.resource.width - animation.cache[:end_width]) / delay).round(3)
    animation.cache[:increment_height] ||= ((entity.resource.height - animation.cache[:end_height]) / delay).round(3)


    if animation.cache[:delay].positive?
      if entity.resource.width <= animation.cache[:end_width] &&
        entity.resource.height <= animation.cache[:end_height]
        animation.complete
      else
        entity.resource.width -= animation.cache[:increment_width] if entity.resource.width != animation.cache[:end_width]
        entity.resource.height -= animation.cache[:increment_height] if entity.resource.height != animation.cache[:end_height]
      end
    else
      entity.resource.width = animation.cache[:end_width]
      entity.resource.height = animation.cache[:end_height]
      animation.complete
    end
  end
end

#shrink_text(factor: 1.0, delay: 100, entity: self) ⇒ Object

Note:

Using this animation multiple times in conjunction with multiple ‘grow_text` animations can end up preventing animations from completing, leaving the target Entity in a state where it is not animatable via growth or shrinkage.

Shrinks an Animatable shape object. This is accomplished by changing the size of the text.

Parameters:

  • factor (Float) (defaults to: 1.0)

    the factor at which to shrink the object

  • entity (OR2D::Entity) (defaults to: self)

    the entity to shrink

Since:

  • 2023-04-26



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/or2d/animations/entity_animations.rb', line 176

def shrink_text(factor: 1.0, delay: 100, entity: self)
  OR2D.game.animate(entity.id) do |animation|
    animation.cache[:end_size] ||= entity.resource.size - (entity.resource.size * factor)
    animation.cache[:delay] ||= delay
    animation.cache[:increment] ||= ((entity.resource.size - animation.cache[:end_size]) / animation.cache[:delay]).round(3)

    if animation.cache[:delay].positive?
      if entity.resource.size <= animation.cache[:end_size]
        animation.complete
      else
        entity.resource.size -= animation.cache[:increment]
        animation.cache[:delay] -= 1
      end
    else
      entity.resource.size = animation.cache[:end_size]
      animation.complete
    end
  end
end