Class: OrangeZest::Entity

Inherits:
Component show all
Defined in:
lib/orange_zest/entity.rb

Overview

A subclass of ‘Component` with associated size, position, and animation information.

Instance Attribute Summary collapse

Attributes inherited from Component

#group

Instance Method Summary collapse

Methods inherited from Component

anon, #register, #unregister

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.

Returns:



14
15
16
# File 'lib/orange_zest/entity.rb', line 14

def animations
  @animations
end

#mirror_xBoolean

Whether this object should be mirrored along its X axis. Useful for flipping animations when creating a game with a 2D character.

Returns:

  • (Boolean)


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

def mirror_x
  @mirror_x
end

#opacityInteger

The opacity of this entity. 255 is fully opaque, and 0 is fully transparent.

Returns:

  • (Integer)


31
32
33
# File 'lib/orange_zest/entity.rb', line 31

def opacity
  @opacity
end

#positionPoint

The position of this object.

Returns:



10
11
12
# File 'lib/orange_zest/entity.rb', line 10

def position
  @position
end

#rotationNumeric

The rotation of this entity.

Returns:

  • (Numeric)


27
28
29
# File 'lib/orange_zest/entity.rb', line 27

def rotation
  @rotation
end

#scalingNumeric

The scaling of this entity.

Returns:

  • (Numeric)


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`.

Parameters:

  • anim_name (Object)

    The name of the animation to begin, which should be a key 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_boxObject

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

#drawObject



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

#imageObject

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

#updateObject



66
67
68
# File 'lib/orange_zest/entity.rb', line 66

def update
  @current_animation&.update
end