Class: Lotu::Actor

Inherits:
Object
  • Object
show all
Includes:
Controllable, SystemUser
Defined in:
lib/lotu/actor.rb

Direct Known Subclasses

Cursor, TextBox

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Controllable

#set_keys

Methods included from SystemUser

#use

Constructor Details

#initialize(opts = {}) ⇒ Actor

Returns a new instance of Actor.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lotu/actor.rb', line 11

def initialize(opts={})
  default_opts = {
    :x => 0,
    :y => 0,
    :z => 0,
    :angle => 0.0,
    :center_x => 0.5,
    :center_y => 0.5,
    :factor_x => 1.0,
    :factor_y => 1.0,
    :color => 0xffffffff,
    :mode => :default,
    :parent => $lotu
  }
  opts = default_opts.merge!(opts)
  @parent = opts[:parent]
  @parent.manage_me(self)
  set_image(opts[:image], opts) if opts[:image]
  parse_options(opts)
  @color = rand_color if opts[:rand_color]
  @systems = {}

  set_keys(opts[:keys]) unless opts[:keys].nil?

  # Add extra functionality
  self.extend Eventful
  self.extend Collidable
  use(AnimationSystem)
  use(TransformationSystem)
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def angle
  @angle
end

#center_xObject

Returns the value of attribute center_x.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def color
  @color
end

#factor_xObject

Returns the value of attribute factor_x.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def factor_y
  @factor_y
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def height
  @height
end

#imageObject

Returns the value of attribute image.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def image
  @image
end

#modeObject

Returns the value of attribute mode.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def mode
  @mode
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def parent
  @parent
end

#systemsObject

Returns the value of attribute systems.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def systems
  @systems
end

#widthObject

Returns the value of attribute width.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def width
  @width
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def y
  @y
end

#zObject

Returns the value of attribute z.



3
4
5
# File 'lib/lotu/actor.rb', line 3

def z
  @z
end

Instance Method Details

#adjust_width_and_height(opts) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/lotu/actor.rb', line 95

def adjust_width_and_height(opts)
  if(opts[:width] && opts[:height])
    @width = Float(opts[:width])
    @height = Float(opts[:height])
  elsif(opts[:width])
    @width = Float(opts[:width])
    @height = @width * @image.height / @image.width
  elsif(opts[:height])
    @height = Float(opts[:height])
    @width = @height * @image.width / @image.height
  else
    @width = Float(@image.width)
    @height = Float(@image.height)
  end
end

#calc_zoomObject



111
112
113
114
# File 'lib/lotu/actor.rb', line 111

def calc_zoom
  @zoom_x = Float(@width)/@image.width
  @zoom_y = Float(@height)/@image.height
end

#dieObject

Remove ourselves from the update queue



117
118
119
# File 'lib/lotu/actor.rb', line 117

def die
  @parent.kill_me(self)
end

#drawObject



127
128
129
130
131
132
# File 'lib/lotu/actor.rb', line 127

def draw
  unless @image.nil?
    @image.draw_rot(@x, @y, @z, @angle, @center_x, @center_y, @factor_x*@zoom_x, @factor_y*@zoom_y, @color, @mode)
    draw_debug if $lotu.debug?
  end
end

#draw_box(x, y, w, h, c = 0xffff0000) ⇒ Object



139
140
141
142
143
144
# File 'lib/lotu/actor.rb', line 139

def draw_box(x, y, w, h, c = 0xffff0000)
  $lotu.draw_line(x, y, c, x+w, y, c)
  $lotu.draw_line(x, y, c, x, y+h, c)
  $lotu.draw_line(x+w, y+h, c, x+w, y, c)
  $lotu.draw_line(x+w, y+h, c, x, y+h, c)
end

#draw_debugObject



134
135
136
137
# File 'lib/lotu/actor.rb', line 134

def draw_debug
  draw_box(@x-@image.width/2, @y-@image.height/2, @image.width, @image.height)
  draw_box(@x-@width/2, @y-@height/2, @width, @height, 0xff00ff00)
end

#dtObject

Easy access to delta-time



43
44
45
# File 'lib/lotu/actor.rb', line 43

def dt
  $lotu.dt
end

#parse_options(opts) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lotu/actor.rb', line 47

def parse_options(opts)
  @x = opts[:x] || @x
  @y = opts[:y] || @y
  @z = opts[:z] || @z
  @angle = opts[:angle] || @angle
  @center_x = opts[:center_x] || @center_x
  @center_y = opts[:center_y] || @center_y
  @factor_x = opts[:factor_x] || @factor_x
  @factor_y = opts[:factor_y] || @factor_y
  @color = opts[:color] || @color
  if @color.kind_of?(Integer)
    @color = Gosu::Color.new(opts[:color])
  end
  @mode = opts[:mode] || @mode
end

#rand_colorObject



63
64
65
# File 'lib/lotu/actor.rb', line 63

def rand_color
  Gosu::Color.from_hsv(rand(360), 1, 1)
end

#set_gosu_image(image, opts = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/lotu/actor.rb', line 78

def set_gosu_image(image, opts={})
  @image = image
  parse_options(opts)
  adjust_width_and_height(opts)
  calc_zoom
end

#set_image(image, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/lotu/actor.rb', line 67

def set_image(image, opts={})
  @image = @parent.image(image)
  if @image.nil?
    puts "Image \"#{image}\" not found".red
    return
  end
  parse_options(opts)
  adjust_width_and_height(opts)
  calc_zoom
end

#updateObject



121
122
123
124
125
# File 'lib/lotu/actor.rb', line 121

def update
  @systems.each_pair do |klass, system|
    system.update
  end
end