summaryrefslogtreecommitdiffhomepage
path: root/test/sprite.rb
blob: e62488bf800aa184f06e1850e3187d6db1c4f691 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'ruby2d'

if RUBY_ENGINE == 'opal'
  media = "../test/media"
else
  media = "media"
end

set title: "Ruby 2D — Sprite", width: 400, height: 300

coin1 = Sprite.new(
  "#{media}/coin.png",
  clip_width: 84,
  time: 300,
  loop: true
)

coin1.play

coin2 = Sprite.new(
  "#{media}/coin.png",
  y: 90,
  width: 42,
  height: 42,
  clip_width: 84,
  time: 300,
  loop: true
)

coin2.play

boom = Sprite.new(
  "#{media}/boom.png",
  x: 109,
  clip_width: 127,
  time: 75
)

hero = Sprite.new(
  "#{media}/hero.png",
  x: 261,
  width: 78,
  height: 99,
  clip_width: 78,
  time: 250,
  animations: {
    walk: 1..2,
    climb: 3..4,
    cheer: 5..6
  }
)

atlas = Sprite.new(
  "#{media}/texture_atlas.png",
  x: 50, y: 90,
  animations: {
    count: [
      {
        x: 0, y: 0,
        width: 35, height: 41,
        time: 300
      },
      {
        x: 26, y: 46,
        width: 35, height: 38,
        time: 400
      },
      {
        x: 65, y: 10,
        width: 32, height: 41,
        time: 500
      },
      {
        x: 10, y: 99,
        width: 32, height: 38,
        time: 600
      },
      {
        x: 74, y: 80,
        width: 32, height: 38,
        time: 700
      }
    ]
  }
)

atlas.play :count, :loop


on :key_down do |e|
  close if e.key == 'escape'

  case e.key
  when 'p'
    coin1.play
    coin2.play
    boom.play
    atlas.play :count
  when 'b'
    boom.play nil, nil, nil do
      puts "Boom animation finished!"
    end
  when 's'
    coin1.stop
    coin2.stop
    hero.stop
    atlas.stop
  when 'left'
    hero.play :walk, :loop, :flip_h
  when 'right'
    hero.play :walk, :loop
  when 'up'
    hero.play :climb, :loop
  when 'down'
    hero.play :climb, :loop, :flip_v
  when 'h'
    hero.play :climb, :loop, :flip_hv
  when 'c'
    hero.play :cheer
  end
end

on :key_held do |e|
  case e.key
  when 'a'
    hero.play :walk, :loop, :flip_h
    hero.x -= 1
  when 'd'
    hero.play :walk, :loop
    hero.x += 1
  when 'w'
    hero.play :climb, :loop
    hero.y -= 1
  when 's'
    hero.play :climb, :loop, :flip_v
    hero.y += 1
  when 'z'
    hero.width  = get(:mouse_x)
    hero.height = get(:mouse_y)
  end
end

on :key_up do |e|
  if ['w', 'a', 's', 'd'].include? e.key
    hero.stop
  end
end

show