Class: OR2D::Entity

Inherits:
Ruby2D::Rectangle
  • Object
show all
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.

Since:

  • 2023-04-26

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • type (Symbol)

    the type of entity to create

  • options (Hash) (defaults to: {})

    the options to create the entity with

Since:

  • 2023-04-26



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, options = {})
  super(x: options[:x] || 0,
        y: options[:y] || 0,
        width: options[:width].nil? ? 1 : options[:width] * OR2D.scale,
        height: options[:height].nil? ? 1 : options[:height] * OR2D.scale,
        opacity: options[:show_boundary] ? 0.55 : 0.0,
        color: 'green')
  @id = options[:id] || SecureRandom.uuid
  @resource = options[:resource] || OR2D::Resource.create(type, options)
  @width = @resource.width if @resource.respond_to?(:width)
  @height = @resource.height if @resource.respond_to?(:height)
  show if options[:show]
ensure
  OR2D.game.add_entity(self)
end

Instance Attribute Details

#idString (readonly)

Returns the entity’s ID.

Returns:

  • (String)

    the entity’s ID

Since:

  • 2023-04-26



11
12
13
# File 'lib/or2d/entity.rb', line 11

def id
  @id
end

#resourceRuby2D::Image, ... (readonly)

Returns the entity’s resource.

Returns:

  • (Ruby2D::Image, Ruby2D::Sprite, Ruby2D::Text, Ruby2D::Triangle, Ruby2D::Quad, Ruby2D::Circle, Ruby2D::Square, Ruby2D::Line, Ruby2D::Rectangle)

    the entity’s resource

Since:

  • 2023-04-26



15
16
17
# File 'lib/or2d/entity.rb', line 15

def resource
  @resource
end

Instance Method Details

#destroyObject

Remove the entity from the game instance window, and from the game instance’s entity list.

Since:

  • 2023-04-26



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

#hideObject

Hide the entity from the game instance window.

Since:

  • 2023-04-26



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

#showObject

Show the entity on the game instance window.

Since:

  • 2023-04-26



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_boundaryObject

Show the entity’s boundary.

Since:

  • 2023-04-26



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.

Parameters:

  • x_coordinate (Integer)

    the x coordinate of the Entity on the screen

Since:

  • 2023-04-26



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.

Parameters:

  • y_coordinate (Integer)

    the y coordinate of the Entity on the screen

Since:

  • 2023-04-26



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