Class: OrangeZest::Entity
- Defined in:
- lib/orange_zest/entity.rb
Overview
A subclass of ‘Component` with associated size, position, and animation information.
Instance Attribute Summary collapse
-
#animations ⇒ {Object => Animation}
The animations which this object can perform.
-
#mirror_x ⇒ Boolean
Whether this object should be mirrored along its X axis.
-
#opacity ⇒ Integer
The opacity of this entity.
-
#position ⇒ Point
The position of this object.
-
#rotation ⇒ Numeric
The rotation of this entity.
-
#scaling ⇒ Numeric
The scaling of this entity.
Attributes inherited from Component
Instance Method Summary collapse
-
#animation=(anim_name) ⇒ Object
Begin playing the animation with the given name, from the beginning.
-
#bounding_box ⇒ Object
Returns a bounding box for this entity, starting at its position and spanning the size of its current frame.
- #draw ⇒ Object
-
#image ⇒ Object
Retrieve the current frame of the currently-playing animation.
-
#initialize(position: nil, animations: nil, scaling: nil) ⇒ Entity
constructor
A new instance of Entity.
- #update ⇒ Object
Methods inherited from Component
Constructor Details
#initialize(position: nil, animations: nil, scaling: nil) ⇒ Entity
Returns a new instance of Entity.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/orange_zest/entity.rb', line 33 def initialize(position: nil, animations: nil, scaling: nil) @position = position || Point.new(0, 0) @animations = animations || {} @scaling = scaling || 1 @rotation = 0 @opacity = 255 if animations.any? @current_animation_name, @current_animation = animations.first end @mirror_x = false end |
Instance Attribute Details
#animations ⇒ {Object => Animation}
The animations which this object can perform.
14 15 16 |
# File 'lib/orange_zest/entity.rb', line 14 def animations @animations end |
#mirror_x ⇒ Boolean
Whether this object should be mirrored along its X axis. Useful for flipping animations when creating a game with a 2D character.
23 24 25 |
# File 'lib/orange_zest/entity.rb', line 23 def mirror_x @mirror_x end |
#opacity ⇒ Integer
The opacity of this entity. 255 is fully opaque, and 0 is fully transparent.
31 32 33 |
# File 'lib/orange_zest/entity.rb', line 31 def opacity @opacity end |
#position ⇒ Point
The position of this object.
10 11 12 |
# File 'lib/orange_zest/entity.rb', line 10 def position @position end |
#rotation ⇒ Numeric
The rotation of this entity.
27 28 29 |
# File 'lib/orange_zest/entity.rb', line 27 def rotation @rotation end |
#scaling ⇒ Numeric
The scaling of this entity.
18 19 20 |
# File 'lib/orange_zest/entity.rb', line 18 def scaling @scaling end |
Instance Method Details
#animation=(anim_name) ⇒ Object
Begin playing the animation with the given name, from the beginning. Raises an exception if an animation with that name does not exist in ‘#animations`.
56 57 58 59 60 61 62 63 64 |
# File 'lib/orange_zest/entity.rb', line 56 def animation=(anim_name) return if @current_animation_name == anim_name @current_animation = @animations[anim_name] @current_animation_name = anim_name raise "no animation named #{anim_name}" if !@current_animation @current_animation.reset end |
#bounding_box ⇒ Object
Returns a bounding box for this entity, starting at its position and spanning the size of its current frame.
84 85 86 |
# File 'lib/orange_zest/entity.rb', line 84 def bounding_box Box.new(position, image.width * scaling, image.height * scaling) end |
#draw ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/orange_zest/entity.rb', line 70 def draw return unless image image.draw_rot( position.x + (mirror_x ? image.width * scaling : 0), position.y, position.z, rotation, 0, 0, scaling * (mirror_x ? -1 : 1), scaling, Gosu::Color.new(opacity * 255, 255, 255, 255), ) end |
#image ⇒ Object
Retrieve the current frame of the currently-playing animation.
48 49 50 |
# File 'lib/orange_zest/entity.rb', line 48 def image @current_animation&.image end |
#update ⇒ Object
66 67 68 |
# File 'lib/orange_zest/entity.rb', line 66 def update @current_animation&.update end |