Class: Smalruby::Character
- Inherits:
-
Sprite
- Object
- Sprite
- Smalruby::Character
- Extended by:
- Forwardable, Mutex_m
- Defined in:
- lib/smalruby/character.rb
Overview
キャラクターを表現するクラス
Instance Attribute Summary collapse
-
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
-
#threads ⇒ Object
Returns the value of attribute threads.
動き collapse
-
#move(val = 1) ⇒ Object
( )歩動かす.
-
#turn ⇒ Object
振り返る.
-
#turn_if_reach_wall ⇒ Object
もし端に着いたら、跳ね返る.
見た目 collapse
調べる collapse
音 collapse
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #click(buttons) ⇒ Object
- #draw ⇒ Object
-
#initialize(option = {}) ⇒ Character
constructor
A new instance of Character.
- #join ⇒ Object
- #key_down(keys) ⇒ Object
- #key_push(keys) ⇒ Object
- #loop(&block) ⇒ Object
- #on(event, *options, &block) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(option = {}) ⇒ Character
Returns a new instance of Character.
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 |
# File 'lib/smalruby/character.rb', line 17 def initialize(option = {}) opt = { x: 0, y: 0, costume: nil, visible: true }.merge(option) # TODO: コスチュームの配列に対応する if opt[:costume].is_a?(String) opt[:costume] = Image.load(asset_path(opt[:costume])) end super(opt[:x], opt[:y], opt[:costume]) @event_handlers = {} @threads = [] self.scale_x = 1.0 self.scale_y = 1.0 @vector = { x: 1, y: 0 } [:visible].each do |k| if opt.key?(k) send("#{k}=", opt[k]) end end World.instance.objects << self end |
Instance Attribute Details
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
14 15 16 |
# File 'lib/smalruby/character.rb', line 14 def event_handlers @event_handlers end |
#threads ⇒ Object
Returns the value of attribute threads.
15 16 17 |
# File 'lib/smalruby/character.rb', line 15 def threads @threads end |
Instance Method Details
#alive? ⇒ Boolean
185 186 187 |
# File 'lib/smalruby/character.rb', line 185 def alive? return @threads.any?(&:alive?) end |
#click(buttons) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/smalruby/character.rb', line 176 def click() @event_handlers[:click].try(:each) do |h| if h..length > 0 && !h..any? { |b| .include?(b) } next end @threads << h.call(Input.mouse_pos_x, Input.mouse_pos_y) end end |
#distance(x, y) ⇒ Object
107 108 109 110 111 |
# File 'lib/smalruby/character.rb', line 107 def distance(x, y) res = Math.sqrt((self.x + center_x - x).abs**2 + (self.y + center_y - y).abs**2).to_i return res end |
#draw ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/smalruby/character.rb', line 125 def draw draw_balloon if self.x < 0 self.x = 0 elsif self.x + image.width >= Window.width self.x = Window.width - image.width end if self.y < 0 self.y = 0 elsif self.y + image.height >= Window.height self.y = Window.height - image.height end super end |
#join ⇒ Object
189 190 191 |
# File 'lib/smalruby/character.rb', line 189 def join @threads.each(&:join) end |
#key_down(keys) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/smalruby/character.rb', line 158 def key_down(keys) @event_handlers[:key_down].try(:each) do |h| if h..length > 0 && !h..any? { |k| keys.include?(k) } next end @threads << h.call end end |
#key_push(keys) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/smalruby/character.rb', line 167 def key_push(keys) @event_handlers[:key_push].try(:each) do |h| if h..length > 0 && !h..any? { |k| keys.include?(k) } next end @threads << h.call end end |
#loop(&block) ⇒ Object
193 194 195 196 197 198 |
# File 'lib/smalruby/character.rb', line 193 def loop(&block) Kernel.loop do yield Smalruby.await end end |
#move(val = 1) ⇒ Object
( )歩動かす
50 51 52 53 |
# File 'lib/smalruby/character.rb', line 50 def move(val = 1) self.x += @vector[:x] * val self.y += @vector[:y] * val end |
#on(event, *options, &block) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/smalruby/character.rb', line 141 def on(event, *, &block) event = event.to_sym @event_handlers[event] ||= [] h = EventHandler.new(self, , &block) @event_handlers[event] << h if Smalruby.started? @threads << h.call end end |
#play(option = {}) ⇒ Object
117 118 119 120 121 |
# File 'lib/smalruby/character.rb', line 117 def play(option = {}) @sound_cache ||= {} (@sound_cache[option[:name]] ||= Sound.new(asset_path(option[:name]))) .play end |
#say(message) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/smalruby/character.rb', line 78 def say() lines = .to_s.lines.map { |l| l.scan(/.{1,10}/) }.flatten font = new_font(16) width = lines.map { |l| font.get_width(l) }.max height = lines.length * (font.size + 1) frame_size = 3 margin_size = 3 image = Image.new(width + (frame_size + margin_size) * 2, height + (frame_size + margin_size) * 2) image.box_fill(0, 0, width + (frame_size + margin_size) * 2 - 1, height + (frame_size + margin_size) * 2 - 1, [125, 125, 125]) image.box_fill(frame_size, frame_size, width + (frame_size + margin_size) + margin_size - 1, height + (frame_size + margin_size) + margin_size - 1, [255, 255, 255]) image.draw_font(frame_size + margin_size, frame_size + margin_size, lines.join("\n"), font, [0, 0, 0]) @balloon = Sprite.new(self.x, self.y, image) end |
#start ⇒ Object
152 153 154 155 156 |
# File 'lib/smalruby/character.rb', line 152 def start @event_handlers[:start].try(:each) do |h| @threads << h.call end end |
#turn ⇒ Object
振り返る
56 57 58 59 60 |
# File 'lib/smalruby/character.rb', line 56 def turn @vector[:x] *= -1 @vector[:y] *= -1 self.scale_x *= -1 end |
#turn_if_reach_wall ⇒ Object
もし端に着いたら、跳ね返る
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/smalruby/character.rb', line 63 def turn_if_reach_wall max_width = Window.width - image.width if self.x < 0 self.x = 0 turn elsif self.x >= max_width self.x = max_width - 1 turn end end |