Class: OR2D::Entity
- Inherits:
-
Ruby2D::Rectangle
- Object
- Ruby2D::Rectangle
- OR2D::Entity
- Includes:
- Animations::EntityAnimations
- Defined in:
- lib/or2d/entity.rb
Overview
A Entity object represents a single entity in an OR2D instance. All entities have an ID, and a natural boundary that is provided by it’s parent class (i.e. Ruby2D::Rectangle). It may also have a resource that can be rendered to the screen. It is recommended to use inheritance to create new entities.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The entity’s ID.
-
#resource ⇒ Ruby2D::Image, ...
readonly
The entity’s resource.
Instance Method Summary collapse
-
#destroy ⇒ Object
Remove the entity from the game instance window, and from the game instance’s entity list.
-
#hide ⇒ Object
Hide the entity from the game instance window.
-
#initialize(type, options = {}) ⇒ Entity
constructor
Constructs a new Entity object.
-
#show ⇒ Object
Show the entity on the game instance window.
-
#toggle_boundary ⇒ Object
Show the entity’s boundary.
-
#x=(x_coordinate) ⇒ Object
Set the Entity x coordinate.
-
#y=(y_coordinate) ⇒ Object
Set the Entity y coordinate.
Methods included from Animations::EntityAnimations
#fade, #fade_in, #fade_out, #grow, #grow_entity, #grow_text, #rotation, #shake, #shrink, #shrink_entity, #shrink_text
Constructor Details
#initialize(type, options = {}) ⇒ Entity
Constructs a new Entity object.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/or2d/entity.rb', line 20 def initialize(type, = {}) super(x: [:x] || 0, y: [:y] || 0, width: [:width].nil? ? 1 : [:width] * OR2D.scale, height: [:height].nil? ? 1 : [:height] * OR2D.scale, opacity: [:show_boundary] ? 0.55 : 0.0, color: 'green') @id = [:id] || SecureRandom.uuid @resource = [:resource] || OR2D::Resource.create(type, ) @width = @resource.width if @resource.respond_to?(:width) @height = @resource.height if @resource.respond_to?(:height) show if [:show] ensure OR2D.game.add_entity(self) end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns the entity’s ID.
11 12 13 |
# File 'lib/or2d/entity.rb', line 11 def id @id end |
#resource ⇒ Ruby2D::Image, ... (readonly)
Returns the entity’s resource.
15 16 17 |
# File 'lib/or2d/entity.rb', line 15 def resource @resource end |
Instance Method Details
#destroy ⇒ Object
Remove the entity from the game instance window, and from the game instance’s entity list.
72 73 74 75 76 |
# File 'lib/or2d/entity.rb', line 72 def destroy OR2D.game.window.remove(self) OR2D.game.window.remove(@resource) if @resource OR2D.game.remove_entity(@id) end |
#hide ⇒ Object
Hide the entity from the game instance window.
66 67 68 69 |
# File 'lib/or2d/entity.rb', line 66 def hide OR2D.game.window.remove(self) OR2D.game.window.remove(@resource) if @resource end |
#show ⇒ Object
Show the entity on the game instance window.
60 61 62 63 |
# File 'lib/or2d/entity.rb', line 60 def show OR2D.game.window.add(self) OR2D.game.window.add(@resource) if @resource end |
#toggle_boundary ⇒ Object
Show the entity’s boundary.
51 52 53 54 55 56 57 |
# File 'lib/or2d/entity.rb', line 51 def toggle_boundary @color.opacity = if @color.opacity <= 0.0 0.35 else 0.0 end end |
#x=(x_coordinate) ⇒ Object
Set the Entity x coordinate.
38 39 40 41 |
# File 'lib/or2d/entity.rb', line 38 def x=(x_coordinate) super(x_coordinate) @resource.x = x_coordinate if @resource.respond_to?(:x=) end |
#y=(y_coordinate) ⇒ Object
Set the Entity y coordinate.
45 46 47 48 |
# File 'lib/or2d/entity.rb', line 45 def y=(y_coordinate) super(y_coordinate) @resource.y = y_coordinate if @resource.respond_to?(:y=) end |