public
Description: Code as Art, Art as Code. Processing and Ruby are meant for each other.
Homepage: http://github.com/jashkenas/ruby-processing/wikis
Clone URL: git://github.com/jashkenas/ruby-processing.git
ruby-processing / samples / space.rb
100644 153 lines (138 sloc) 2.939 kb
1
2
3
4
5
6
7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'ruby-processing'
 
class Space < Processing::App
  load_java_library "opengl"
 
  class << self; attr_accessor :instance; end
 
  class Velocity
    attr_accessor :dx, :dy, :dz
    def initialize(dx = 0, dy = 0, dz = 0)
      @dx, @dy, @dz = dx, dy, dz
    end
  end
 
  class PObject
    def method_missing(meth, *args, &block)
      Space.instance.send(meth, *args, &block)
    end
  end
 
  class Position < PObject
    attr_accessor :x, :y, :z
    def initialize(x = 0, y = 0, z = 0)
      @x, @y, @z = x, y, z
    end
    def move(vel)
      if vel
        @x += vel.dx
        @y += vel.dy
        @z += vel.dz
      end
      translate @x, @y, @z
    end
  end
 
  class Rotation < PObject
    attr_accessor :x, :y, :z
    def initialize(x = 0, y = 0, z = 0)
      @x, @y, @z = x, y, z
    end
    def move(spin)
      if spin
        @x += spin.dx
        @y += spin.dy
        @z += spin.dz
      end
      rotate_x @x
      rotate_y @y
      rotate_z @z
    end
  end
 
  class Shape < PObject
    attr_accessor :position, :velocity, :rotation, :spin, :size
    def initialize(pos = Position.new, rot = Rotation.new, sz = 10)
      @position, @rotation, @size = pos, rot, sz
    end
    def move
      @size = 0 if @size < 0
      position.move(@velocity)
      rotation.move(@spin)
      end
  end
 
  class Box < Shape
    def draw
      box size
    end
  end
 
  def initialize(*args)
    self.class.instance = self
    super
  end
 
  def setup
    render_mode OPENGL
    fill 204
    @vel = Velocity.new 0, 0, 0
    @spin = Velocity.new 0, 0, 0
    @growth = 0
    @object = Box.new(Position.new(0, 200, -20), Rotation.new, 50)
  end
 
  # KEYS = {UP => :up, DOWN => :down, LEFT => :left, RIGHT => :right, SHIFT => :shift}
  # class Keyboard
  # def method_missing(meth,*args,&block)
  # end
  # end
 
  def key_pressed
    if key == CODED
      shift = false
      case keyCode
      when UP
        if shift
          @vel.dy = -20
        else
          @vel.dz = -20
        end
      when DOWN
        if shift
          @vel.dy = 20
        else
          @vel.dz = 20
        end
      when LEFT
        @vel.dx = -5
      when RIGHT
        @vel.dx = 5
      else
        puts "unknown key: #{keyCode}"
      end
    else
      case keyCode
      when ?W
        @growth -= 1
      when ?E
        @growth += 1
      when ?S
        @spin.dy -= PI/40
      when ?D
        @spin.dy += PI/40
      when ?X
        @spin.dx -= PI/40
      when ?C
        @spin.dx += PI/40
      else
        puts "unknown key: #{keyCode}"
      end
    end
  end
 
  def key_released
    @vel, @spin, @growth = Velocity.new(0,0,0), Velocity.new(0,0,0), 0
  end
 
  def draw
    lights
    background 0
    no_stroke
    @object.velocity = @vel
    @object.spin = @spin
    @object.size += @growth
    @object.move
    @object.draw
  rescue => e
    puts e.to_s, *e.backtrace
    raise e
  end
end
 
Space.new :full_screen => true