Skip to content

Commit d00d559

Browse files
committed
library_proxy examples
1 parent 6162888 commit d00d559

8 files changed

Lines changed: 243 additions & 16 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to k9 executable, and or opts as required
3+
4+
SAMPLES_DIR = './'
5+
6+
desc 'run demo'
7+
task default: [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each { |sample| run_sample sample }
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob('*.rb').each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|k9 -r #{sample_name}", 'r') do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'observer'
2+
include Observable
3+
load_libraries :library_proxy, :dead_grid
4+
attr_accessor :deadgrid, :player, :block_size
5+
SPACE = 32
6+
7+
def settings
8+
size 256, 256
9+
end
10+
11+
def setup
12+
sketch_title 'Dead Grid Events'
13+
@block_size = 16
14+
@deadgrid = DeadGrid.new self
15+
@player = Block.new self
16+
puts deadgrid
17+
add_observer(BlockedHandler.new)
18+
end
19+
20+
def draw
21+
background 0
22+
draw_grid_lines
23+
end
24+
25+
def draw_grid_lines
26+
stroke_weight 1
27+
stroke 40
28+
# for each block in deadgrid, draw lines using JRubyArt `grid` utility
29+
grid(width, height, block_size, block_size) do |x, y|
30+
line(x, 0, x, height)
31+
line(0, y, width, y)
32+
end
33+
end
34+
35+
def key_pressed
36+
case key_code
37+
when SPACE
38+
puts 'Randomize.'
39+
deadgrid.randomize
40+
# if grid spawns under player, remove it
41+
unless deadgrid.grid[player.x][player.y].zero?
42+
deadgrid.grid[player.x][player.y] = 0
43+
end
44+
when LEFT
45+
player.direction = 0
46+
return unless player.x > 0
47+
return player.x = player.x - 1 if deadgrid.can_move?(player, 1)
48+
changed
49+
notify_observers(player, deadgrid)
50+
when RIGHT
51+
player.direction = 2
52+
# check bounds
53+
return unless player.x < deadgrid.width
54+
return player.x = player.x + 1 if deadgrid.can_move?(player, 1)
55+
changed
56+
notify_observers(player, deadgrid)
57+
when UP
58+
player.direction = 1
59+
# check bounds
60+
return unless player.y > 0
61+
return player.y = player.y - 1 if deadgrid.can_move?(player, 1)
62+
changed
63+
notify_observers(player, deadgrid)
64+
when DOWN
65+
player.direction = 3
66+
return unless player.y < deadgrid.height
67+
return player.y = player.y + 1 if deadgrid.can_move?(player, 1)
68+
changed
69+
notify_observers(player, deadgrid)
70+
end
71+
end

processing_app/library/library_proxy/key_event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# A simple demonstration of vanilla processing 'reflection' methods using
22
# JRubyArt :library_proxy.
33
# See library/my_library/my_library.rb code for the guts.
4-
# press 'h' to hide ellipse, or 's' to show ellipse
4+
# Use 's' and 'h' keys to show and hide
55
load_libraries :library_proxy, :my_library
66

77
attr_reader :visible
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# By inheriting from LibraryProxy draw loop is by reflection sketch draw loop
2+
class DeadGrid < LibraryProxy
3+
attr_reader :app, :block_size
4+
attr_accessor :grid, :width, :height
5+
6+
def initialize(app)
7+
@app = app
8+
@block_size = app.block_size
9+
@width = app.width / block_size - 1
10+
@height = app.height / block_size - 1
11+
@grid = Array.new
12+
0.upto(width) do |i|
13+
grid[i] = Array.new
14+
0.upto(height) { |j| @grid[i][j] = 0 }
15+
end
16+
end
17+
18+
def to_s
19+
string = ""
20+
string << "\nDeadGrid\n"
21+
string << "-" * width << "\n"
22+
0.upto(width) do |i|
23+
0.upto(height) { |j| string << grid[j][i].to_s }
24+
string << "\n"
25+
end
26+
string
27+
end
28+
29+
def draw
30+
app.stroke(255, 255, 255, 125)
31+
app.stroke_weight(1)
32+
0.upto(width) do |i|
33+
0.upto(height) do |j|
34+
x = i * block_size
35+
y = j * block_size
36+
37+
if (@grid[i][j] != 0)
38+
if (@grid[i][j] == 1)
39+
app.fill(255, 0, 0)
40+
elsif (@grid[i][j] == 2)
41+
app.fill(120, 200, 50)
42+
end
43+
app.rect(x,y,block_size,block_size)
44+
end
45+
end
46+
end
47+
end
48+
49+
def randomize
50+
0.upto(width) do |i|
51+
0.upto(height) do |j|
52+
@grid[i][j] = rand(0..1)
53+
end
54+
end
55+
end
56+
57+
def can_move?(player, distance)
58+
case player.direction
59+
when 0 # left
60+
check_x = player.x - distance
61+
check_y = player.y
62+
return !there?(check_x, check_y) unless player.x.zero?
63+
false
64+
when 2 # right
65+
check_x = player.x + distance
66+
check_y = player.y
67+
return !there?(check_x, check_y) if player.x < grid.size
68+
false
69+
when 1 # up
70+
check_x = player.x
71+
check_y = player.y - distance
72+
return !there?(check_x, check_y) if player.y > 0
73+
false
74+
when 3 # down
75+
check_x = player.x
76+
check_y = player.y + distance
77+
return !there?(check_x, check_y) if player.y <= grid.size
78+
false
79+
end
80+
end
81+
82+
def there?(x,y)
83+
grid[x][y] != 0
84+
end
85+
end
86+
87+
# By inheriting from LibraryProxy draw loop is by reflection sketch draw loop
88+
class Block < LibraryProxy
89+
attr_reader :app, :block_size
90+
attr_accessor :x, :y, :width, :height, :direction
91+
92+
def initialize(app)
93+
@app = app
94+
@block_size = app.block_size
95+
@x = 7
96+
@y = 7
97+
@width = block_size
98+
@height = block_size
99+
@direction = 0
100+
end
101+
102+
def draw
103+
app.fill(55, 0, 255)
104+
app.rect(@x * block_size, @y * block_size, @width, @height)
105+
end
106+
end
107+
108+
# figures out which block to highlight on grid when player can't move
109+
class BlockedHandler
110+
def update(player, deadgrid)
111+
case player.direction
112+
when 0
113+
if deadgrid.grid[player.x - 1][player.y] == 1
114+
deadgrid.grid[player.x - 1][player.y] = 2
115+
end
116+
when 1
117+
if deadgrid.grid[player.x][player.y - 1] == 1
118+
deadgrid.grid[player.x][player.y - 1] = 2
119+
end
120+
when 2
121+
if deadgrid.grid[player.x + 1][player.y] == 1
122+
deadgrid.grid[player.x + 1][player.y] = 2
123+
end
124+
when 3
125+
if deadgrid.grid[player.x][player.y + 1] == 1
126+
deadgrid.grid[player.x][player.y + 1] = 2
127+
end
128+
end
129+
end
130+
end
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class MouseThing < LibraryProxy
2-
attr_reader :app, :count
3-
def initialize(app)
4-
@app = app
5-
@count = 0
2+
3+
attr_reader :app
4+
5+
def initialize(parent)
6+
@app = parent
67
end
78

89
def draw
@@ -11,13 +12,9 @@ def draw
1112
def mouseEvent(event)
1213
case event.action
1314
when MouseEvent::CLICK
14-
fstring = 'CLICK at x = %.1f, y = %.1f'
15-
puts format(fstring, event.x, event.y)
15+
puts 'CLICK'
1616
when MouseEvent::DRAG
1717
puts 'DRAG'
18-
when MouseEvent::WHEEL
19-
@count += event.getCount
20-
puts count
2118
end
2219
end
2320
end

processing_app/library/library_proxy/library/my_library/my_library.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def draw # optional
1919
def keyEvent(e) # NB: need camel case for reflection to work
2020
return unless e.get_action == KeyEvent::PRESS
2121
return if e.get_key >= 'z'.ord
22-
case e.get_key.chr.upcase
23-
when 'S'
22+
case e.get_key.chr
23+
when 's', 'S'
2424
app.send :hide, false
25-
when 'H'
25+
when 'h', 'H'
2626
app.send :hide, true
2727
else
28-
puts e.get_key.chr
28+
puts "Use 'h' or 's' keys to show/hide"
2929
end
3030
end
3131
end

processing_app/library/library_proxy/proxy_library.rb renamed to processing_app/library/library_proxy/library/proxy_library/proxy_library.rb

File renamed without changes.

processing_app/library/library_proxy/test_proxy.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# test_proxy.rb by Martin Prout
2-
load_library :library_proxy
3-
require_relative 'proxy_library'
2+
load_library :library_proxy, :proxy_library
43

54
def settings
65
size 300, 300

0 commit comments

Comments
 (0)