Class: Ninja
- Inherits:
-
Object
- Object
- Ninja
- Defined in:
- lib/escape_to_rubyconf/ninja.rb
Instance Attribute Summary collapse
-
#boundary ⇒ Object
Returns the value of attribute boundary.
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #current_angle ⇒ Object
- #direction_from_angle(a) ⇒ Object
- #draw ⇒ Object
- #draw_coordinates ⇒ Object
- #frame ⇒ Object
- #idle? ⇒ Boolean
-
#initialize(level, width = 128, height = 128, boundary = 80) ⇒ Ninja
constructor
A new instance of Ninja.
- #play_sneak ⇒ Object
- #state ⇒ Object
- #stop_sneak ⇒ Object
- #update ⇒ Object
- #walk? ⇒ Boolean
Constructor Details
#initialize(level, width = 128, height = 128, boundary = 80) ⇒ Ninja
Returns a new instance of Ninja.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 6 def initialize level, width=128, height=128, boundary=80 @level = level @width, @height = width, height # x and y are the center of the bounds @x, @y = 0, 0 @boundary = boundary @tiles = Gosu::Image.load_tiles @level.window, File.dirname(__FILE__) + '/assets/ninja.png', @width, @height, false @direction = :right @state = :idle @states = { :walk => { :frames => [1,2,3,4], :speed => 0.5, :movement => 5, :time => 0.0 }, :idle => { :frames => [0], :speed => 1.0, :movement => 0, :time => 0.0 } } @bounds = [ [ @boundary/2 - 0.49, @boundary/2 - 0.49 ], [ @level.window.width - @boundary/2 + 0.49, @level.window.height - @boundary/2 + 0.49] ] @sneak = Gosu::Sample.new @level.window, File.dirname(__FILE__) + '/assets/ninja-walk.wav' @sneak_impl = nil end |
Instance Attribute Details
#boundary ⇒ Object
Returns the value of attribute boundary.
5 6 7 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 5 def boundary @boundary end |
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 5 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 5 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 5 def y @y end |
Instance Method Details
#current_angle ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 74 def current_angle angle = nil if @level.window. Gosu::KbRight if @level.window. Gosu::KbUp 45 elsif @level.window. Gosu::KbDown angle = 135 else angle = 90 end elsif @level.window. Gosu::KbLeft if @level.window. Gosu::KbUp angle = 315 elsif @level.window. Gosu::KbDown angle = 225 else angle = 270 end elsif @level.window. Gosu::KbUp angle = 0 elsif @level.window. Gosu::KbDown angle = 180 end angle end |
#direction_from_angle(a) ⇒ Object
100 101 102 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 100 def direction_from_angle a (a >= 0 && a <= 180) ? :right : :left end |
#draw ⇒ Object
104 105 106 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 104 def draw @tiles[frame].draw *draw_coordinates end |
#draw_coordinates ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 108 def draw_coordinates x = Math::round(@x - @width/2) y = Math::round((@y - @boundary/2)/2+200 - @height + @boundary/2) z = 1 + (@y/@level.window.height) fx = 1.0 fy = 1.0 if @direction == :left fx = -1.0 x += @width end [x, y, z, fx, fy] end |
#frame ⇒ Object
41 42 43 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 41 def frame (((@level.window.time - state[:time]) / state[:speed] % 1) * state[:frames].size).to_i + state[:frames].first end |
#idle? ⇒ Boolean
33 34 35 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 33 def idle? @state == :idle end |
#play_sneak ⇒ Object
45 46 47 48 49 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 45 def play_sneak if @sneak_impl.nil? @sneak_impl = @sneak.play 1, 1, true end end |
#state ⇒ Object
29 30 31 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 29 def state @states[idle? ? :idle : :walk] end |
#stop_sneak ⇒ Object
51 52 53 54 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 51 def stop_sneak @sneak_impl.stop if @sneak_impl @sneak_impl = nil end |
#update ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 56 def update a = current_angle if a @state = :walk @direction = direction_from_angle a @x += Gosu::offset_x a, state[:movement] @y += Gosu::offset_y a, state[:movement] play_sneak else @state = :idle stop_sneak end @x = @bounds[0][0] if @x < @bounds[0][0] @x = @bounds[1][0] if @x > @bounds[1][0] @y = @bounds[0][1] if @y < @bounds[0][1] @y = @bounds[1][1] if @y > @bounds[1][1] end |
#walk? ⇒ Boolean
37 38 39 |
# File 'lib/escape_to_rubyconf/ninja.rb', line 37 def walk? !idle? end |