forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example11_animation.rb
83 lines (67 loc) · 2.14 KB
/
example11_animation.rb
1
2
3
4
5
6
7
8
9
10
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
41
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
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu
#
# Animation / retrofy example
#
class Game < Chingu::Window
def initialize
super
self.factor = 6
self.input = { :escape => :exit }
self.caption = "Chingu::Animation / retrofy example. Move with arrows!"
retrofy
Droid.create(:x => $window.width/2, :y => $window.height/2)
end
end
class Droid < Chingu::GameObject
traits :timer
def initialize(options = {})
super
#
# This shows up the shorten versio of input-maps, where each key calls a method of the very same name.
# Use this by giving an array of symbols to self.input
#
self.input = [:holding_left, :holding_right, :holding_up, :holding_down]
# Load the full animation from tile-file media/droid.bmp
@animation = Chingu::Animation.new(:file => "droid_11x15.bmp")
@animation.frame_names = { :scan => 0..5, :up => 6..7, :down => 8..9, :left => 10..11, :right => 12..13 }
# Start out by animation frames 0-5 (contained by @animation[:scan])
@frame_name = :scan
@last_x, @last_y = @x, @y
update
end
def holding_left
@x -= 2
@frame_name = :left
end
def holding_right
@x += 2
@frame_name = :right
end
def holding_up
@y -= 2
@frame_name = :up
end
def holding_down
@y += 2
@frame_name = :down
end
# We don't need to call super() in update().
# By default GameObject#update is empty since it doesn't contain any gamelogic to speak of.
def update
# Move the animation forward by fetching the next frame and putting it into @image
# @image is drawn by default by GameObject#draw
@image = @animation[@frame_name].next
#
# If droid stands still, use the scanning animation
#
@frame_name = :scan if @x == @last_x && @y == @last_y
@x, @y = @last_x, @last_y if outside_window? # return to previous coordinates if outside window
@last_x, @last_y = @x, @y # save current coordinates for possible use next time
end
end
Game.new.show