Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/examples/simple_game.rb

Constant Summary collapse

GRAVITATIONAL_CONST =
6.673 * (10 ** -4)
START_POSITION =
Vector3.new(0.75,15,0)
SMOOTH =
true

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Game

Returns a new instance of Game.



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
# File 'lib/examples/simple_game.rb', line 38

def initialize(width,height)
  
  @width = width
  @height = height
  @queue = EventQueue.new
  @queue.enable_new_style_events
  @clock = Clock.new
  @lighting = Lighting.new
  @x_trans = 0; @y_trans = -5; @z_trans = -25
  @font = TTF.new "/Library/Fonts/Times New Roman.ttf",point_size
  
  @world = ParticleWorld.new(20, 1)
  


  @anchor = Particle.new
  @anchor.mass = 1
  @anchor.position = Vector3.new(-5,0,0)
  @anchor.damping = 1
  @anchor.velocity = Vector3.new(0,0.0,0)
  @anchor.material.specular = ANCHOR_SPECULAR
  @anchor.material.diffuse = PARTICLE_DIFFUSE
  
  #@world.add_gravity()  
  
  
  @ball = Particle.new
  @ball.mass = 1
  @ball.position = START_POSITION.dup
  @ball.damping = 1
  @ball.material.specular = BALL_SPECULAR
  @ball.material.diffuse = PARTICLE_DIFFUSE
@ball.frozen = true
  		
  
  @current_ball = Particle.new
  @current_ball.position = @ball.position
  @current_ball.velocity = @ball.velocity
  @current_ball.mass = @ball.mass
  @current_ball.damping = @ball.damping
  @current_ball.frozen = true
  @world.registry.add( @current_ball, ParticleGravity.new())
  @world.particles << @current_ball
 
 	 
 particle1 = Particle.new
 particle1.mass = 1
 particle1.position = Vector3.new(5,2,0)
 particle1.damping = 1
 particle1.velocity = Vector3.new(0,2.0,0)
 particle1.material.specular = ANCHOR_SPECULAR
 particle1.material.diffuse = PARTICLE_DIFFUSE
 
 particle2 = Particle.new
 particle2.mass = 1
 particle2.position = Vector3.new(0,2,0)
 particle2.damping = 1
 particle2.velocity = Vector3.new(0,0.0,0)
 particle2.material.specular = ANCHOR_SPECULAR
 particle2.material.diffuse = PARTICLE_DIFFUSE
 
 prod = ParticleRod.new(particle1, particle2, 2.0)
 
 #@world.particles << particle1 
 #@world.particles << particle2
#@world.contact_generators << prod
 
 	@world.contact_generators << ParticleGroundContacts.new(@world.particles)
 	@world.contact_generators << ParticleParticleContacts.new(@world.particles)
 
  for i in 1..5
   for j in 1..5
    part = Particle.new
    part.mass = 0
    offset = j % 2 == 0 ? 1 : 0
    part.position = Vector3.new( -5 + i * 2 + offset, j * 2, 0)
    part.material.specular = SPRING_SPECULAR
    part.material.diffuse = PARTICLE_DIFFUSE
    @world.particles << part
  end
end


  puts "PARTICLE COUNT [ #{@world.particles.size} ] "
  puts "Contact Generators count: #{@world.contact_generators.size}"
  
  
  @w_down, @a_down, @s_down, @d_down = false, false, false, false
  
end

Instance Method Details

#init_gameObject



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
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/examples/simple_game.rb', line 129

def init_game()
  Rubygame.init

  Rubygame::GL.set_attrib(Rubygame::GL::RED_SIZE, 5)
  Rubygame::GL.set_attrib(Rubygame::GL::GREEN_SIZE, 5)
  Rubygame::GL.set_attrib(Rubygame::GL::BLUE_SIZE, 5)
  Rubygame::GL.set_attrib(Rubygame::GL::DEPTH_SIZE, 16)
  Rubygame::GL.set_attrib(Rubygame::GL::DOUBLEBUFFER, 1)

	@maximum_resolution = Screen.get_resolution
  @screen = Screen.new([@width,@height], 16, [OPENGL])
  @clock.target_framerate = 30
  @clock.calibrate
  @clock.enable_tick_events
  ObjectSpace.garbage_collect
    
  glViewport( 0, 0, WIDE, HIGH )
  glMatrixMode( GL::PROJECTION )
  glLoadIdentity( )
  #GLU::Perspective( -35, WIDE/(HIGH.to_f), 0.5, 30.0)
  GLU::Perspective( 45, WIDE/(HIGH.to_f), 0.5, 100.0)
  glMatrixMode( GL::MODELVIEW )
  glLoadIdentity( )
  

  
  @lighting.setup
  
  glShadeModel (GL_SMOOTH);
  glEnable(GL::DEPTH_TEST)
  glEnable(GL_LIGHTING)
  glEnable(GL_LIGHT0)
  glDepthFunc(GL::LESS)
  

    
end

#runObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/examples/simple_game.rb', line 168

def run()
  catch(:rubygame_quit) do
  	loop do
  		@queue.each do |event|
  			case event
  			when Rubygame::Events::KeyPressed
#            puts event.key
  				case event.key
  				when :escape
  					throw :rubygame_quit 
  				when :q
  					throw :rubygame_quit 
  				when :w
  				  @w_down = true
  			  when :s
  			    @s_down = true 
  		    when :a
  		      @a_down = true
  	      when :d
  	        @d_down = true
        when :l
          @world.particles.each do |part|
            puts part.position
            puts "NUM CONTACTS: #{@world.contacts.size}"
           end
  	      when :space
   	      @current_ball.frozen = !@current_ball.frozen
          when :r
           puts "RESET"
           @current_ball = Particle.new
           @current_ball.position = START_POSITION.dup
           @current_ball.velocity = @ball.velocity.dup
           @current_ball.mass = @ball.mass
           @current_ball.damping = @ball.damping
           @current_ball.frozen = true
           @world.registry.add( @current_ball, ParticleGravity.new())
           @world.particles << @current_ball
          end
        when Rubygame::Events::KeyReleased
          case event.key
          when :w
            @w_down = false
          when :a
            @a_down = false
          when :d
            @d_down = false
          when :s
            @s_down = false
          end
        when Rubygame::Events::MouseMoved
           puts "Mouse moved to #{event.rel.inspect} with #{event.buttons.inspect} pressed"
           if( event.buttons.include? :mouse_right )
             puts "mouse_right"
             @current_ball.position.x += event.rel[0] / 100.0
             @current_ball.position.y -= event.rel[1] / 100.0
           end
        when Rubygame::Events::QuitRequested
          throw :rubygame_quit
        end
      end

      if @w_down;@current_ball.position.y += 0.01; end;
      if @s_down;@current_ball.position.y -= 0.01; end;
      if @a_down;@current_ball.position.x -= 0.01; end;
      if @d_down;@current_ball.position.x += 0.01; end;

      @time_elapsed = @clock.tick()
      @time_alive = @clock.lifetime
      seconds_elapsed = @time_elapsed.milliseconds / 1000.0
      @world.start_frame

      glClearColor(0.0, 0.0, 0.0, -1.0)
  	glClear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT)
      glLoadIdentity()
      
      glTranslatef(@x_trans,@y_trans,@z_trans)

      glPushMatrix()
         glBegin(GL_QUADS)
         glVertex3f(-1000, -1 , 1000)
         glVertex3f(-1000, -1, -1000)
         glVertex3f(1000, -1, -1000)
         glVertex3f(1000, -1, 1000)
         glEnd
      glPopMatrix()

      
      draw_skybox()

      #glClear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT)
      @world.particles.each do |particle|
        p = particle.position
        glPushMatrix()
        glTranslatef(p.x, p.y, p.z)
        particle.material.setup_material()
        glutSolidSphere(particle.radius, 16, 16)
        glPopMatrix()
      end

      @world.run_physics(seconds_elapsed)

  	Rubygame::GL.swap_buffers()
@screen.update
      @screen.flip
  	ObjectSpace.garbage_collect

  	end
  end

end