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.
-
#previous_screen_x ⇒ Integer
readonly
The entity’s previous x coordinate.
-
#previous_screen_y ⇒ Integer
readonly
The entity’s previous y coordinate.
-
#previous_x ⇒ Integer
readonly
The entity’s previous x coordinate.
-
#previous_y ⇒ Integer
readonly
The entity’s previous y coordinate.
-
#resource ⇒ Ruby2D::Image, ...
readonly
The entity’s resource.
Instance Method Summary collapse
- #contains?(x, y) ⇒ Boolean
-
#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
(also: #screen_x=)
Set the Entity x coordinate.
-
#y=(y_coordinate) ⇒ Object
(also: #screen_y=)
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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/or2d/entity.rb', line 28 def initialize(type, = {}) super(x: [:x] || 0, y: [:y] || 0, width: [:width].nil? ? 1 : [:width].scale, height: [:height].nil? ? 1 : [:height].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) @previous_screen_x = [:x] || 0 @previous_screen_y = [:y] || 0 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 |
#previous_screen_x ⇒ Integer (readonly)
Returns the entity’s previous x coordinate.
19 20 21 |
# File 'lib/or2d/entity.rb', line 19 def previous_screen_x @previous_screen_x end |
#previous_screen_y ⇒ Integer (readonly)
Returns the entity’s previous y coordinate.
23 24 25 |
# File 'lib/or2d/entity.rb', line 23 def previous_screen_y @previous_screen_y end |
#previous_x ⇒ Integer (readonly)
Returns the entity’s previous x coordinate.
19 |
# File 'lib/or2d/entity.rb', line 19 attr_reader :previous_screen_x |
#previous_y ⇒ Integer (readonly)
Returns the entity’s previous y coordinate.
23 |
# File 'lib/or2d/entity.rb', line 23 attr_reader :previous_screen_y |
#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
#contains?(x, y) ⇒ Boolean
71 72 73 |
# File 'lib/or2d/entity.rb', line 71 def contains?(x, y) (@x1..@x2).cover?(x) && (@y1..@y4).cover?(y) end |
#destroy ⇒ Object
Remove the entity from the game instance window, and from the game instance’s entity list.
88 89 90 91 92 |
# File 'lib/or2d/entity.rb', line 88 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.
82 83 84 85 |
# File 'lib/or2d/entity.rb', line 82 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.
76 77 78 79 |
# File 'lib/or2d/entity.rb', line 76 def show OR2D.game.window.add(self) OR2D.game.window.add(@resource) if @resource end |
#toggle_boundary ⇒ Object
Show the entity’s boundary.
63 64 65 66 67 68 69 |
# File 'lib/or2d/entity.rb', line 63 def toggle_boundary @color.opacity = if @color.opacity <= 0.0 0.35 else 0.0 end end |
#x=(x_coordinate) ⇒ Object Also known as: screen_x=
Set the Entity x coordinate.
48 49 50 51 52 |
# File 'lib/or2d/entity.rb', line 48 def x=(x_coordinate) @previous_screen_x = @x super(x_coordinate) @resource.x = x_coordinate if @resource.respond_to?(:x=) end |
#y=(y_coordinate) ⇒ Object Also known as: screen_y=
Set the Entity y coordinate.
56 57 58 59 60 |
# File 'lib/or2d/entity.rb', line 56 def y=(y_coordinate) @previous_screen_x = @y super(y_coordinate) @resource.y = y_coordinate if @resource.respond_to?(:y=) end |