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



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, options = {})
  super(x: options[:x] || 0,
        y: options[:y] || 0,
        width: options[:width].nil? ? 1 : options[:width].scale,
        height: options[:height].nil? ? 1 : options[:height].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)
  @previous_screen_x = options[:x] || 0
  @previous_screen_y = options[:y] || 0
  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

#previous_screen_xInteger (readonly)

Returns the entity’s previous x coordinate.

Returns:

  • (Integer)

    the entity’s previous x coordinate

Since:

  • 2023-04-26



19
20
21
# File 'lib/or2d/entity.rb', line 19

def previous_screen_x
  @previous_screen_x
end

#previous_screen_yInteger (readonly)

Returns the entity’s previous y coordinate.

Returns:

  • (Integer)

    the entity’s previous y coordinate

Since:

  • 2023-04-26



23
24
25
# File 'lib/or2d/entity.rb', line 23

def previous_screen_y
  @previous_screen_y
end

#previous_xInteger (readonly)

Returns the entity’s previous x coordinate.

Returns:

  • (Integer)

    the entity’s previous x coordinate



19
# File 'lib/or2d/entity.rb', line 19

attr_reader :previous_screen_x

#previous_yInteger (readonly)

Returns the entity’s previous y coordinate.

Returns:

  • (Integer)

    the entity’s previous y coordinate



23
# File 'lib/or2d/entity.rb', line 23

attr_reader :previous_screen_y

#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

#contains?(x, y) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 2023-04-26



71
72
73
# File 'lib/or2d/entity.rb', line 71

def contains?(x, y)
  (@x1..@x2).cover?(x) && (@y1..@y4).cover?(y)
end

#destroyObject

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

Since:

  • 2023-04-26



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

#hideObject

Hide the entity from the game instance window.

Since:

  • 2023-04-26



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

#showObject

Show the entity on the game instance window.

Since:

  • 2023-04-26



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_boundaryObject

Show the entity’s boundary.

Since:

  • 2023-04-26



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.

Parameters:

  • x_coordinate (Integer)

    the x coordinate of the Entity on the screen

Since:

  • 2023-04-26



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.

Parameters:

  • y_coordinate (Integer)

    the y coordinate of the Entity on the screen

Since:

  • 2023-04-26



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