22require 'propane'
33
44class ArrayObjects < Propane ::App
5- # Demonstrates the syntax for creating an array of custom objects.
5+ # Demonstrates the syntax for creating an array of custom objects.
66
7- UNIT = 40
8- attr_reader :mods
7+ UNIT = 40
8+ attr_reader :mods
99
10- def setup
11- sketch_title 'Array of Objects'
12- wide_count = width / UNIT
13- height_count = height / UNIT
14- @mods = [ ]
15- wide_count . times do |i |
16- height_count . times do |j |
17- mods << CustomObject . new ( j * UNIT , i * UNIT , UNIT / 2 , UNIT / 2 , rand ( 0.05 ..0.8 ) )
18- end
19- end
20- no_stroke
21- end
10+ def setup
11+ sketch_title 'Array of Objects'
12+ wide_count = width / UNIT
13+ height_count = height / UNIT
14+ @mods = [ ]
15+ grid ( wide_count , height_count ) do |i , j |
16+ mods << CustomObject . new ( j * UNIT , i * UNIT , UNIT / 2 , UNIT / 2 , rand ( 0.05 ..0.8 ) )
17+ end
18+ no_stroke
19+ end
2220
23- def draw
24- background 0
25- mods . each ( &:run )
26- end
21+ def draw
22+ background 0
23+ mods . each ( &:run )
24+ end
2725
28- def settings
29- size 640 , 360 , FX2D
30- end
26+ def settings
27+ size 640 , 360 , FX2D
28+ end
3129
32- module Runnable
33- def run
34- update
35- draw
36- end
37- end
30+ module Runnable
31+ def run
32+ update
33+ draw
34+ end
35+ end
3836
39- # the custom object
40- class CustomObject
41- include Processing ::Proxy , Runnable
42- attr_reader :x , :y , :mx , :my , :size
43- def initialize ( mx , my , x , y , speed )
44- @mx , @my = my , mx # This is backwards to match original example.
45- @x , @y = x . to_i , y . to_i
46- @xdir , @ydir = 1 , 1
47- @speed = speed
48- @size = UNIT
49- end
37+ # the custom object
38+ class CustomObject
39+ include Propane ::Proxy , Runnable
40+ attr_reader :x , :y , :mx , :my , :size
41+ def initialize ( mx , my , x , y , speed )
42+ @mx , @my = my , mx # This is backwards to match original example.
43+ @x , @y = x . to_i , y . to_i
44+ @xdir , @ydir = 1 , 1
45+ @speed = speed
46+ @size = UNIT
47+ end
5048
51- def update
52- @x += @speed * @xdir
53- unless ( 0 ..size ) . cover? x
54- @xdir *= -1
55- @x += @xdir
56- @y += @ydir
57- end
58- return unless @y >= @size || x <= 0
59- @ydir *= -1
60- @y += @ydir
61- end
49+ def update
50+ @x += @speed * @xdir
51+ unless ( 0 ..size ) . cover? x
52+ @xdir *= -1
53+ @x += @xdir
54+ @y += @ydir
55+ end
56+ return unless @y >= @size || x <= 0
57+ @ydir *= -1
58+ @y += @ydir
59+ end
6260
63- def draw
64- fill ( 255 )
65- ellipse ( mx + x , my + y , 6 , 6 )
66- end
67- end
61+ def draw
62+ fill ( 255 )
63+ ellipse ( mx + x , my + y , 6 , 6 )
64+ end
65+ end
6866end
6967
70- ArrayObjects . new
68+ ArrayObjects . new
0 commit comments