Class: GGLib::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/image.rb

Direct Known Subclasses

MapImage

Constant Summary collapse

@@animations =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, src, speed, width, height, loop = true) ⇒ Animation

Returns a new instance of Animation.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/image.rb', line 6

def initialize(name,src,speed,width,height,loop=true)
  @animObj=Gosu::Image::load_tiles($window,src,width,height,false)
  @src=src
  @name=name
  @width=width
  @height=height
  @speed=speed
  @loop=loop
  @started=false
  @done=false
  @@animations[@name]=self
end

Instance Attribute Details

#animObjObject (readonly)

Returns the value of attribute animObj.



5
6
7
# File 'lib/image.rb', line 5

def animObj
  @animObj
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/image.rb', line 5

def height
  @height
end

#loopObject (readonly)

Returns the value of attribute loop.



5
6
7
# File 'lib/image.rb', line 5

def loop
  @loop
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/image.rb', line 5

def name
  @name
end

#speedObject (readonly)

Returns the value of attribute speed.



5
6
7
# File 'lib/image.rb', line 5

def speed
  @speed
end

#srcObject (readonly)

Returns the value of attribute src.



5
6
7
# File 'lib/image.rb', line 5

def src
  @src
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/image.rb', line 5

def width
  @width
end

Class Method Details

.deleteAllAnimationsObject



74
75
76
# File 'lib/image.rb', line 74

def Animation.deleteAllAnimations
  @@animations={}
end

.get(at) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/image.rb', line 21

def Animation.get(at)
  if @@animations[at] !=nil
    return @@animations[at]
  else
    puts caller
    STDIN.gets
    raise "Animation resource '#{at}' does not exist."
  end
end

Instance Method Details

#dataObject



30
31
32
# File 'lib/image.rb', line 30

def data
  return @animObj
end

#delObject



18
19
20
# File 'lib/image.rb', line 18

def del
  @@animations.delete_at(@name)
end

#doneObject



33
34
35
36
37
# File 'lib/image.rb', line 33

def done
  return true if @loop and (Gosu::milliseconds / @speed % @animObj.size)==@animObj.size-1
  return false if @loop
  return nil
end

#draw(x, y, z) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/image.rb', line 42

def draw(x,y,z)
  if @loop
    @animObj[(Gosu::milliseconds / @speed % @animObj.size)].draw(x,y,z)  #looping mode
    return true #signifies that the animation is still playing
  else  
    if not @started
      start=(Gosu::milliseconds / @speed % @animObj.size)
      @started=true
      @map=[]
      i=0
      start.upto(@animObj.size-1) { |index|
        @map[index]=i
        i+=1
      }
      if start-1>=0
        0.upto(start-1) { |index|
          @map[index]=i
          i+=1
        }
      end
    end
    index=@map[(Gosu::milliseconds / @speed % @animObj.size)]
    if not @done and index < @animObj.size - 1 #single play mode
      @animObj[index].draw(x,y,z)
      return true #singifies that the animation is still playing
    else
      @done=true
      @animObj[@animObj.size - 1].draw(x,y,z)
      return false #signifies that the animation is done. The last frame will be played continueuosly. The caller can take further action 
    end
  end
end

#restartObject



38
39
40
41
# File 'lib/image.rb', line 38

def restart
  @started=false
  @done=false
end