Class: Chobo::Entity
- Inherits:
-
GameObject
- Object
- GameObject
- Chobo::Entity
- Defined in:
- lib/chobo/entity.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#behaviors ⇒ Object
Returns the value of attribute behaviors.
-
#color ⇒ Object
Returns the value of attribute color.
-
#flipped ⇒ Object
Returns the value of attribute flipped.
-
#height ⇒ Object
Returns the value of attribute height.
-
#image ⇒ Object
Returns the value of attribute image.
-
#time ⇒ Object
Returns the value of attribute time.
-
#width ⇒ Object
Returns the value of attribute width.
Attributes inherited from GameObject
#origin, #position, #rotation, #scale, #velocity
Instance Method Summary collapse
- #add_behavior(behavior) ⇒ Object
- #collides?(other) ⇒ Boolean
- #draw ⇒ Object
- #get_behavior(type) ⇒ Object
-
#initialize(image) ⇒ Entity
constructor
A new instance of Entity.
- #remove_behavior(behavior) ⇒ Object
- #set_attribute(attribute, value) ⇒ Object
- #set_color(r = 255, g = 255, b = 255, a = 255) ⇒ Object
- #update(dt) ⇒ Object
Methods inherited from GameObject
#set_position, #set_rotation, #set_scale, #set_velocity
Constructor Details
#initialize(image) ⇒ Entity
Returns a new instance of Entity.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/chobo/entity.rb', line 6 def initialize(image) super() @image = image @width = image.width @height = image.height @color = Chobo.color() @attributes = {} @time = 0.0 @behaviors = [] @flipped = false end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def attributes @attributes end |
#behaviors ⇒ Object
Returns the value of attribute behaviors.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def behaviors @behaviors end |
#color ⇒ Object
Returns the value of attribute color.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def color @color end |
#flipped ⇒ Object
Returns the value of attribute flipped.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def flipped @flipped end |
#height ⇒ Object
Returns the value of attribute height.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def height @height end |
#image ⇒ Object
Returns the value of attribute image.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def image @image end |
#time ⇒ Object
Returns the value of attribute time.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def time @time end |
#width ⇒ Object
Returns the value of attribute width.
4 5 6 |
# File 'lib/chobo/entity.rb', line 4 def width @width end |
Instance Method Details
#add_behavior(behavior) ⇒ Object
47 48 49 50 51 |
# File 'lib/chobo/entity.rb', line 47 def add_behavior behavior @behaviors.push behavior behavior.on_added self self end |
#collides?(other) ⇒ Boolean
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/chobo/entity.rb', line 18 def collides? other if @position.x - (@width / 2.0) > other.position.x + (other.width / 2.0) return false end if @position.x + (@width / 2.0) < other.position.x - (other.width / 2.0) return false end if @position.y - (@width / 2.0) > other.position.y + (other.height / 2.0) return false end if @position.y + (@width / 2.0) < other.position.y - (other.height / 2.0) return false end return true end |
#draw ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/chobo/entity.rb', line 73 def draw if @flipped @image.draw_rot(@position.x.to_i, @position.y.to_i, 0, @rotation.to_degrees - 180, @origin.x, @origin.y, @scale.x * -1.0, @scale.y, @color) else @image.draw_rot(@position.x.to_i, @position.y.to_i, 0, @rotation.to_degrees, @origin.x, @origin.y, @scale.x, @scale.y, @color) end end |
#get_behavior(type) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/chobo/entity.rb', line 59 def get_behavior type @behaviors.each do |b| if b.is_a? type return b end end nil end |
#remove_behavior(behavior) ⇒ Object
53 54 55 56 57 |
# File 'lib/chobo/entity.rb', line 53 def remove_behavior behavior @behaviors.delete behavior behavior.on_removed self self end |
#set_attribute(attribute, value) ⇒ Object
42 43 44 45 |
# File 'lib/chobo/entity.rb', line 42 def set_attribute attribute, value @attributes[attribute] = value self end |
#set_color(r = 255, g = 255, b = 255, a = 255) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/chobo/entity.rb', line 34 def set_color r = 255, g = 255, b = 255, a = 255 @color.red = r @color.green = g @color.blue = b @color.alpha = a self end |
#update(dt) ⇒ Object
68 69 70 71 |
# File 'lib/chobo/entity.rb', line 68 def update dt @time += dt @behaviors.each{|b| b.update dt, self } end |