Skip to content

Commit 1259fa2

Browse files
committed
hype swarm example
1 parent 58de516 commit 1259fa2

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# encoding: utf-8
2+
load_library :hype
3+
include_package 'hype'
4+
# Access through Hype namespace
5+
module Hype
6+
java_import 'hype.extended.behavior.HSwarm'
7+
java_import 'hype.extended.behavior.HTimer'
8+
java_import 'hype.extended.behavior.HTween'
9+
java_import 'hype.extended.colorist.HColorPool'
10+
java_import 'hype.interfaces.HLocatable'
11+
end
12+
13+
PALETTE = %w(#FFFFFF #F7F7F7 #ECECEC #333333 #0095a8 #00616f #FF3300 #FF6600).freeze
14+
PALETTE2 = %w(#242424 #111111 #ECECEC).freeze
15+
include Hype
16+
17+
attr_reader :swarm, :canvas, :colors, :pool, :tween, :count
18+
19+
def settings
20+
size(640, 640)
21+
end
22+
23+
def setup
24+
sketch_title 'Hype Swarm'
25+
H.init(self)
26+
@count = 0
27+
@colors = HColorPool.new(web_to_color_array(PALETTE))
28+
colors2 = web_to_color_array(PALETTE2)
29+
H.background(colors2[0])
30+
H.add(@canvas = HCanvas.new.auto_clear(false).fade(40))
31+
@swarm = HSwarm.new
32+
.speed(4)
33+
.turn_ease(0.1)
34+
.twitch(15)
35+
.idle_goal(width / 2, height / 2)
36+
@pool = HDrawablePool.new(10)
37+
pool.auto_add_to_stage
38+
.add(HRect.new(10).rounding(5))
39+
.on_create do |obj|
40+
obj.stroke_weight(2)
41+
.stroke(colors2[1])
42+
.fill(colors2[2])
43+
.loc(rand(100..540), rand(100..540))
44+
.anchor_at(H::CENTER)
45+
.rotation(45)
46+
@tween = HTween.new
47+
.target(obj).property(H::LOCATION)
48+
.start(obj.x, obj.y)
49+
.end(rand(0..width), rand(0..height))
50+
.ease(0.01)
51+
.spring(0.9)
52+
on_anim = proc do
53+
tween.start(obj.x, obj.y)
54+
.end(rand(100..540), rand(100..540))
55+
.ease(0.01)
56+
.spring(0.9)
57+
.register
58+
end
59+
HTimer.new.interval(2_000).callback(&on_anim)
60+
end
61+
.request_all
62+
end
63+
64+
def draw
65+
if count < 100
66+
swarm.add_target(
67+
canvas.add(
68+
HRect.new(8, 2)
69+
.rounding(4)
70+
.anchor_at(H::CENTER)
71+
.no_stroke
72+
.fill(colors.get_color)
73+
)
74+
)
75+
@count += 1
76+
end
77+
H.draw_stage
78+
begin
79+
it = swarm.goals.iterator
80+
it.remove
81+
it.next
82+
end unless swarm.goals.iterator.has_next
83+
pool.each do |d|
84+
swarm.add_goal(d.x, d.y)
85+
end
86+
end

0 commit comments

Comments
 (0)