smalruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of smalruby might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e78acfab23aac318a7c3b7d1ce6b5a28f40eb75f
4
- data.tar.gz: ff6cf5103f4b72403318bcbc5ef1cb2a444645df
3
+ metadata.gz: fa28b7b90c3b367e69c381f63c600e9a51d0bcca
4
+ data.tar.gz: 2ecf984ee583d7876501a390b9094c3f511a15d1
5
5
  SHA512:
6
- metadata.gz: fb47053cbdbc54a55079475f2f4d288f6bb5451c331810a7928c1645ffb21dbf315b5542c8a7d9994a85255f4102cd51e1480de9e6ff3a4652606cd508481138
7
- data.tar.gz: 19433d235b9115fcaee73d1b55db2ba53f3426b5eaf29d3362818e8d409401d62e1edbbbbe972bff506a06bceb6d0468ef5e04d634e0d69e6703e6f6136d82b8
6
+ metadata.gz: 466edcdba9b9da5d83b708fcf35d0a228c1d8e22f8a31e2f7fde45abca91586e3d530f519cdc9aa6d7c941bfadd5e5fb8d949749190a4be6b032f886a1ff4445
7
+ data.tar.gz: 4c95e2202fe00353cd7d5d006f51cd7b8328e409adf1b4ac067ea0a2c4fa585e8a2cca620c3d4db1b93096db5c03d9dbcfb0b0bc20dd4ec83455feaa45165880
data/LEGAL CHANGED
@@ -8,4 +8,8 @@ mentioned below.
8
8
  assets/car1.png, assets/car2.png, assets/car3.png, assets/car4.png,
9
9
  assets/cat1.png, assets/cat2.png, assets/cat3.png, assets/frog1.png:
10
10
 
11
- This is downloaded from ICON HOIHOI http://iconhoihoi.oops.jp/.
11
+ These are downloaded from ICON HOIHOI http://iconhoihoi.oops.jp/.
12
+
13
+ assets/piano_*.wav
14
+
15
+ These are downloaded from 魔王魂 http://maoudamashii.jokersounds.com/.
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ end
36
36
 
37
37
  task :rubocop do
38
38
  files = `git ls-files | grep -e '.rb$' | grep -v '^samples/'`
39
- sh "rubocop #{files.split(/\s+/m).join(' ')}"
39
+ sh "rubocop -d #{files.split(/\s+/m).join(' ')}"
40
40
  end
41
41
 
42
42
  task :default => [:rubocop, :spec]
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,7 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'smalruby/version'
3
3
  require 'active_support/all'
4
- require 'active_support/concurrency/latch'
5
4
  require 'dxruby'
6
5
 
7
6
  require 'smalruby/world'
@@ -21,60 +20,9 @@ module Smalruby
21
20
  @started = true
22
21
  begin
23
22
  if world.objects.any? { |o| /console/i !~ o.class.name }
24
- # ウィンドウアプリケーション
25
- Window.caption = File.basename($PROGRAM_NAME)
26
- first = true
27
- Window.fps = 15
28
- Window.loop do
29
- lock do
30
- if first
31
- world.objects.each do |object|
32
- object.start
33
- end
34
- first = false
35
- end
36
- if Input.key_down?(K_ESCAPE)
37
- exit
38
- end
39
- if Input.mouse_push?(M_LBUTTON) || Input.mouse_push?(M_RBUTTON) ||
40
- Input.mouse_push?(M_MBUTTON)
41
- x, y = Input.mouse_pos_x, Input.mouse_pos_y
42
- s = Sprite.new(x, y)
43
- s.collision = [0, 0, 1, 1]
44
- buttons = []
45
- if Input.mouse_down?(M_LBUTTON)
46
- buttons << :left
47
- end
48
- if Input.mouse_down?(M_RBUTTON)
49
- buttons << :right
50
- end
51
- if Input.mouse_down?(M_MBUTTON)
52
- buttons << :center
53
- end
54
- s.check(world.objects).each do |o|
55
- if o.respond_to?(:click)
56
- o.click(buttons)
57
- end
58
- end
59
- end
60
- if (keys = Input.keys).length > 0
61
- key_down_and_push(keys)
62
- end
63
- world.objects.delete_if { |o|
64
- if !o.alive?
65
- o.join
66
- end
67
- o.vanished?
68
- }
69
- Sprite.draw(world.objects)
70
- end
71
- end
23
+ start_window_application
72
24
  else
73
- # コンソールアプリケーション
74
- world.objects.each do |object|
75
- object.start
76
- end
77
- world.objects.each(&:join)
25
+ start_console_application
78
26
  end
79
27
  rescue SystemExit
80
28
  end
@@ -101,9 +49,60 @@ module Smalruby
101
49
  @draw_cv = ConditionVariable.new
102
50
 
103
51
  class << self
104
-
105
52
  private
106
53
 
54
+ def init_window_application
55
+ Window.caption = File.basename($PROGRAM_NAME)
56
+ Window.fps = 15
57
+
58
+ # HACK: DXRubyのためのサウンド関係の初期化処理。こうしておかな
59
+ # いとDirectSoundの初期化でエラーが発生する
60
+ begin
61
+ Sound.new('')
62
+ rescue
63
+ end
64
+ end
65
+
66
+ def start_window_application
67
+ init_window_application
68
+
69
+ first = true
70
+ Window.loop do
71
+ lock do
72
+ if Input.key_down?(K_ESCAPE)
73
+ exit
74
+ end
75
+
76
+ if first
77
+ world.objects.each do |object|
78
+ object.start
79
+ end
80
+ first = false
81
+ end
82
+
83
+ mouse_down_and_push
84
+
85
+ key_down_and_push
86
+
87
+ world.objects.delete_if do |o|
88
+ if !o.alive?
89
+ o.join
90
+ end
91
+ o.vanished?
92
+ end
93
+
94
+ Sprite.draw(world.objects)
95
+ end
96
+ end
97
+ end
98
+
99
+ def start_console_application
100
+ world.objects.each do |object|
101
+ object.start
102
+ end
103
+ world.objects.each(&:join)
104
+ end
105
+
107
106
  def lock(&block)
108
107
  @draw_mutex.synchronize do
109
108
  yield
@@ -111,17 +110,42 @@ module Smalruby
111
110
  end
112
111
  end
113
112
 
114
- def key_down_and_push(keys)
115
- world.objects.each do |o|
116
- if o.respond_to?(:key_down)
117
- o.key_down(keys)
113
+ def mouse_down_and_push
114
+ clickable_objects = world.objects.select { |o| o.respond_to?(:click) }
115
+ if clickable_objects.length > 0 &&
116
+ (Input.mouse_push?(M_LBUTTON) || Input.mouse_push?(M_RBUTTON) ||
117
+ Input.mouse_push?(M_MBUTTON))
118
+ buttons = []
119
+ {
120
+ left: M_LBUTTON,
121
+ right: M_RBUTTON,
122
+ center: M_MBUTTON,
123
+ }.each do |sym, const|
124
+ if Input.mouse_down?(const)
125
+ buttons << sym
126
+ end
127
+ end
128
+ s = Sprite.new(Input.mouse_pos_x, Input.mouse_pos_y)
129
+ s.collision = [0, 0, 1, 1]
130
+ s.check(clickable_objects).each do |o|
131
+ o.click(buttons)
118
132
  end
119
133
  end
120
- pushed_keys = keys.select { |key| Input.key_push?(key) }
121
- if pushed_keys.length > 0
134
+ end
135
+
136
+ def key_down_and_push
137
+ if (keys = Input.keys).length > 0
122
138
  world.objects.each do |o|
123
- if o.respond_to?(:key_push)
124
- o.key_push(pushed_keys)
139
+ if o.respond_to?(:key_down)
140
+ o.key_down(keys)
141
+ end
142
+ end
143
+ pushed_keys = keys.select { |key| Input.key_push?(key) }
144
+ if pushed_keys.length > 0
145
+ world.objects.each do |o|
146
+ if o.respond_to?(:key_push)
147
+ o.key_push(pushed_keys)
148
+ end
125
149
  end
126
150
  end
127
151
  end
@@ -25,7 +25,7 @@ module Smalruby
25
25
  size: 32,
26
26
  color: "white",
27
27
  }.merge(option)
28
- image.draw_font(opt[:x], opt[:y], opt[:string], Font.new(opt[:size]),
28
+ image.draw_font(opt[:x], opt[:y], opt[:string], new_font(opt[:size]),
29
29
  Color.smalruby_to_dxruby(opt[:color]))
30
30
  end
31
31
 
@@ -1,11 +1,16 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  require 'forwardable'
3
+ require 'mutex_m'
3
4
 
4
5
  module Smalruby
5
6
  # キャラクターを表現するクラス
6
7
  class Character < Sprite
7
8
  extend Forwardable
8
9
 
10
+ cattr_accessor :font_cache
11
+ self.font_cache = {}
12
+ font_cache.extend(Mutex_m)
13
+
9
14
  attr_accessor :event_handlers
10
15
  attr_accessor :threads
11
16
 
@@ -72,7 +77,7 @@ module Smalruby
72
77
 
73
78
  def say(message)
74
79
  lines = message.to_s.lines.map { |l| l.scan(/.{1,10}/) }.flatten
75
- font = Font.new(16)
80
+ font = new_font(16)
76
81
  width = lines.map { |l| font.get_width(l) }.max
77
82
  height = lines.length * (font.size + 1)
78
83
  frame_size = 3
@@ -107,22 +112,19 @@ module Smalruby
107
112
 
108
113
  # @!endgroup
109
114
 
115
+ # @!group 音
116
+
117
+ def play(option = {})
118
+ @sound_cache ||= {}
119
+ (@sound_cache[option[:name]] ||= Sound.new(asset_path(option[:name])))
120
+ .play
121
+ end
122
+
123
+ # @!endgroup
124
+
110
125
  def draw
111
- if @balloon
112
- @balloon.x = self.x + image.width / 2
113
- if @balloon.x < 0
114
- @balloon.x = 0
115
- elsif @balloon.x + @balloon.image.width >= Window.width
116
- @balloon.x = Window.width - @balloon.image.width
117
- end
118
- @balloon.y = self.y - @balloon.image.height
119
- if @balloon.y < 0
120
- @balloon.y = 0
121
- elsif @balloon.y + @balloon.image.height >= Window.height
122
- @balloon.y = Window.height - @balloon.image.height
123
- end
124
- @balloon.draw
125
- end
126
+ draw_balloon
127
+
126
128
  if self.x < 0
127
129
  self.x = 0
128
130
  elsif self.x + image.width >= Window.width
@@ -200,5 +202,30 @@ module Smalruby
200
202
  def asset_path(name)
201
203
  return File.expand_path("../../../assets/#{name}", __FILE__)
202
204
  end
205
+
206
+ def new_font(size)
207
+ self.class.font_cache.synchronize do
208
+ self.class.font_cache[size] ||= Font.new(size)
209
+ end
210
+ return self.class.font_cache[size]
211
+ end
212
+
213
+ def draw_balloon
214
+ if @balloon
215
+ @balloon.x = self.x + image.width / 2
216
+ if @balloon.x < 0
217
+ @balloon.x = 0
218
+ elsif @balloon.x + @balloon.image.width >= Window.width
219
+ @balloon.x = Window.width - @balloon.image.width
220
+ end
221
+ @balloon.y = self.y - @balloon.image.height
222
+ if @balloon.y < 0
223
+ @balloon.y = 0
224
+ elsif @balloon.y + @balloon.image.height >= Window.height
225
+ @balloon.y = Window.height - @balloon.image.height
226
+ end
227
+ @balloon.draw
228
+ end
229
+ end
203
230
  end
204
231
  end
@@ -17,7 +17,7 @@ module Smalruby
17
17
  # start
18
18
  class Console < Character
19
19
  def initialize
20
- super(0, 0)
20
+ super
21
21
  end
22
22
 
23
23
  def loop(&block)
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Smalruby
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -0,0 +1,81 @@
1
+ # -*- coding: utf-8 -*-
2
+ # 電子ピアノをつくろう
3
+
4
+ require "smalruby"
5
+
6
+ # ドの鍵盤を作る
7
+ key_do = Canvas.new(x: 200, y: 200, width: 30, height: 100)
8
+
9
+ # (ドの鍵盤) プログラムがはじまった時
10
+ key_do.on(:start) do
11
+ # (0, 0)-(29, 99)、"white" 色、四角く塗りつぶす
12
+ box_fill(x1: 0, y1: 0,
13
+ x2: 29, y2: 99,
14
+ color: "white")
15
+
16
+ # (0, 0)-(29, 99)、"gray" 色、四角を描く
17
+ box(x1: 0, y1: 0,
18
+ x2: 29, y2: 99,
19
+ color: "gray")
20
+ end
21
+
22
+ # (ドの鍵盤) クリックしたとき
23
+ key_do.on(:click) do |x, y|
24
+ play(name: 'piano_do.wav')
25
+ end
26
+
27
+ # レの鍵盤を作る
28
+ key_re = Canvas.new(x: 230, y: 200, width: 30, height: 100)
29
+
30
+ # (レの鍵盤) プログラムがはじまった時
31
+ key_re.on(:start) do
32
+ # (0, 0)-(29, 99)、"white" 色、四角く塗りつぶす
33
+ box_fill(x1: 0, y1: 0,
34
+ x2: 29, y2: 99,
35
+ color: "white")
36
+
37
+ # (0, 0)-(29, 99)、"gray" 色、四角を描く
38
+ box(x1: 0, y1: 0,
39
+ x2: 29, y2: 99,
40
+ color: "gray")
41
+ end
42
+
43
+ # (レの鍵盤) クリックしたとき
44
+ key_re.on(:click) do |x, y|
45
+ play(name: 'piano_re.wav')
46
+ end
47
+
48
+ # ミの鍵盤を作る
49
+ key_mi = Canvas.new(x: 260, y: 200, width: 30, height: 100)
50
+
51
+ # (ミの鍵盤) プログラムがはじまった時
52
+ key_mi.on(:start) do
53
+ # (0, 0)-(29, 99)、"white" 色、四角く塗りつぶす
54
+ box_fill(x1: 0, y1: 0,
55
+ x2: 29, y2: 99,
56
+ color: "white")
57
+
58
+ # (0, 0)-(29, 99)、"gray" 色、四角を描く
59
+ box(x1: 0, y1: 0,
60
+ x2: 29, y2: 99,
61
+ color: "gray")
62
+ end
63
+
64
+ # (ミの鍵盤) クリックしたとき
65
+ key_mi.on(:click) do |x, y|
66
+ play(name: 'piano_mi.wav')
67
+ end
68
+
69
+ # ファ、ソ、ラ、シ、ドを一度に作成する
70
+ ['fa', 'so', 'ra', 'si', 'do_2'].each.with_index do |name, i|
71
+ key = Canvas.new(x: 290 + i * 30, y: 200, width: 30, height: 100)
72
+
73
+ key.on(:start) do
74
+ box_fill(x1: 0, y1: 0, x2: 29, y2: 99, color: "white")
75
+ box(x1: 0, y1: 0, x2: 29, y2: 99, color: "gray")
76
+ end
77
+
78
+ key.on(:click) do |x, y|
79
+ play(name: "piano_#{name}.wav")
80
+ end
81
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-16 00:00:00.000000000 Z
11
+ date: 2013-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -203,6 +203,14 @@ files:
203
203
  - assets/cat2.png
204
204
  - assets/cat3.png
205
205
  - assets/frog1.png
206
+ - assets/piano_do.wav
207
+ - assets/piano_do_2.wav
208
+ - assets/piano_fa.wav
209
+ - assets/piano_mi.wav
210
+ - assets/piano_ra.wav
211
+ - assets/piano_re.wav
212
+ - assets/piano_si.wav
213
+ - assets/piano_so.wav
206
214
  - lib/smalruby.rb
207
215
  - lib/smalruby/canvas.rb
208
216
  - lib/smalruby/character.rb
@@ -216,6 +224,7 @@ files:
216
224
  - samples/car.rb
217
225
  - samples/console.rb
218
226
  - samples/finding_cars.rb
227
+ - samples/piano.rb
219
228
  - smalruby.gemspec
220
229
  - spec/lib/smalruby/color_spec.rb
221
230
  - spec/lib/smalruby/world_spec.rb