Class: Lotu::Actor

Inherits:
GameEntity show all
Extended by:
Behavior
Includes:
Helpers::Util
Defined in:
lib/lotu/actor.rb

Direct Known Subclasses

Cursor, TextBox

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Behavior

behave_like, inherited

Methods included from Helpers::Util

#class_debug_info, #instance_debug_info, #parse_cli_options

Constructor Details

#initialize(opts = {}) ⇒ Actor

Returns a new instance of Actor.



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
41
42
43
44
45
46
47
# File 'lib/lotu/actor.rb', line 16

def initialize(opts={})
  # if debug is set, print out class info
  class_debug_info if $lotu.debug

  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]
  set_keys(opts[:keys]) unless opts[:keys].nil?

  # start behaving as
  init_behavior opts

  # if debug is set, print out instance info
  instance_debug_info if $lotu.debug
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def angle
  @angle
end

#center_xObject

Returns the value of attribute center_x.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def color
  @color
end

#factor_xObject

Returns the value of attribute factor_x.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def factor_y
  @factor_y
end

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def height
  @height
end

#imageObject

Returns the value of attribute image.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def image
  @image
end

#modeObject

Returns the value of attribute mode.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def mode
  @mode
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def parent
  @parent
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def width
  @width
end

#xObject

Returns the value of attribute x.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def x
  @x
end

#yObject

Returns the value of attribute y.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def y
  @y
end

#zObject

Returns the value of attribute z.



11
12
13
# File 'lib/lotu/actor.rb', line 11

def z
  @z
end

Instance Method Details

#adjust_width_and_height(opts) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lotu/actor.rb', line 107

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



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

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

#dieObject

Remove ourselves from the update queue



129
130
131
132
133
134
# File 'lib/lotu/actor.rb', line 129

def die
  # to call die on behaviors (that in turn wil call
  # die on systems, for example)
  super
  @parent.kill_me(self)
end

#drawObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/lotu/actor.rb', line 142

def draw
  # to call draw on behaviors (that in turn will call
  # draw on systems, for example)
  super

  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



158
159
160
161
162
163
# File 'lib/lotu/actor.rb', line 158

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



153
154
155
156
# File 'lib/lotu/actor.rb', line 153

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



50
51
52
# File 'lib/lotu/actor.rb', line 50

def dt
  $lotu.dt
end

#factor=(factor) ⇒ Object



71
72
73
# File 'lib/lotu/actor.rb', line 71

def factor= factor
  @factor_x = @factor_y = factor
end

#parse_options(opts) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lotu/actor.rb', line 54

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
  @width = opts[:width]
  @color = opts[:color] || @color
  if @color.kind_of?(Integer)
    @color = Gosu::Color.new(opts[:color])
  end
  @mode = opts[:mode] || @mode
end

#rand_colorObject



75
76
77
# File 'lib/lotu/actor.rb', line 75

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

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



90
91
92
93
94
95
# File 'lib/lotu/actor.rb', line 90

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

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



79
80
81
82
83
84
85
86
87
88
# File 'lib/lotu/actor.rb', line 79

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



136
137
138
139
140
# File 'lib/lotu/actor.rb', line 136

def update
  # to call update on behaviors (that in turn wil call
  # update on systems, for example)
  super
end