Class: Smalruby::Character
- Inherits:
-
Sprite
- Object
- Sprite
- Smalruby::Character
- Extended by:
- Forwardable
- 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
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.
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 |
# File 'lib/smalruby/character.rb', line 12 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.
9 10 11 |
# File 'lib/smalruby/character.rb', line 9 def event_handlers @event_handlers end |
#threads ⇒ Object
Returns the value of attribute threads.
10 11 12 |
# File 'lib/smalruby/character.rb', line 10 def threads @threads end |
Instance Method Details
#alive? ⇒ Boolean
183 184 185 |
# File 'lib/smalruby/character.rb', line 183 def alive? return @threads.any?(&:alive?) end |
#click(buttons) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/smalruby/character.rb', line 174 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
102 103 104 105 106 |
# File 'lib/smalruby/character.rb', line 102 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
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 |
# File 'lib/smalruby/character.rb', line 110 def draw if @balloon @balloon.x = self.x + image.width / 2 if @balloon.x < 0 @balloon.x = 0 elsif @balloon.x + @balloon.image.width >= Window.width @balloon.x = Window.width - @balloon.image.width end @balloon.y = self.y - @balloon.image.height if @balloon.y < 0 @balloon.y = 0 elsif @balloon.y + @balloon.image.height >= Window.height @balloon.y = Window.height - @balloon.image.height end @balloon.draw end 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
187 188 189 |
# File 'lib/smalruby/character.rb', line 187 def join @threads.each(&:join) end |
#key_down(keys) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/smalruby/character.rb', line 156 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
165 166 167 168 169 170 171 172 |
# File 'lib/smalruby/character.rb', line 165 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
191 192 193 194 195 196 |
# File 'lib/smalruby/character.rb', line 191 def loop(&block) Kernel.loop do yield Smalruby.await end end |
#move(val = 1) ⇒ Object
( )歩動かす
45 46 47 48 |
# File 'lib/smalruby/character.rb', line 45 def move(val = 1) self.x += @vector[:x] * val self.y += @vector[:y] * val end |
#on(event, *options, &block) ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/smalruby/character.rb', line 139 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 |
#say(message) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/smalruby/character.rb', line 73 def say() lines = .to_s.lines.map { |l| l.scan(/.{1,10}/) }.flatten font = Font.new(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
150 151 152 153 154 |
# File 'lib/smalruby/character.rb', line 150 def start @event_handlers[:start].try(:each) do |h| @threads << h.call end end |
#turn ⇒ Object
振り返る
51 52 53 54 55 |
# File 'lib/smalruby/character.rb', line 51 def turn @vector[:x] *= -1 @vector[:y] *= -1 self.scale_x *= -1 end |
#turn_if_reach_wall ⇒ Object
もし端に着いたら、跳ね返る
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/smalruby/character.rb', line 58 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 |