Class: GamesParadise::Billiard::GameWindow

Inherits:
Gosu::Window show all
Includes:
Gosu
Defined in:
lib/games_paradise/gui/gosu/billiard/billiard.rb

Constant Summary collapse

WIDTH =
#

WIDTH

#
1000
HEIGHT =
#

HEIGHT

#
660
TITLE =
#

TITLE

#
'Just another ruby project'

Constants included from Gosu

Gosu::COLOUR_AQUA, Gosu::COLOUR_BLACK, Gosu::COLOUR_BLUE, Gosu::COLOUR_CYAN, Gosu::COLOUR_FUCHSIA, Gosu::COLOUR_GOLD, Gosu::COLOUR_GRAY, Gosu::COLOUR_GREEN, Gosu::COLOUR_NONE, Gosu::COLOUR_RED, Gosu::COLOUR_STEELBLUE, Gosu::COLOUR_WHITE, Gosu::COLOUR_YELLOW

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gosu

#gosu_tilable_image

Methods inherited from Gosu::Window

#gosu_button_down?, #image, #image10?, #image1?, #image2?, #image3?, #image4?, #image5?, #image6?, #image7?, #image8?, #image9?, #on_left_arrow_pressed?, #on_right_arrow_pressed?, #q_means_quit, #reset, #set_font, #set_title, #sqrt, #tab_key?, #write_this_text

Constructor Details

#initializeGameWindow

#

initialize

#


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
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 48

def initialize
  super(WIDTH, HEIGHT, false)
  set_title(TITLE)

  $window_width = 1000.0
  $window_height = 650.0

  $universe_width = 800.0
  $universe_height = 400.0

  instantiate_the_images

  @font       = Gosu::Font.new(self, Gosu.default_font_name, 16)
  @font_small = Gosu::Font.new(self, Gosu.default_font_name, 12)

  $camera_x = $universe_width/2
  $camera_y = $universe_height/2

  # ======================================================================= #
  # === $path_blockers
  #
  # Used to determine the path that the cue ball will take after 
  # being shot.
  # ======================================================================= #
  $path_blockers = []

  $pocket_coords = [
    [0, 26],
    [26, 0],
    [$universe_width-26, 0],
    [$universe_width, 26],
    [$universe_width, $universe_height-26],
    [$universe_width-26, $universe_height], 
    [26, $universe_height], 
    [ 0, $universe_height-26], 
    [$universe_width/2-21, 0], 
    [$universe_width/2+21, 0], 
    [$universe_width/2-21, $universe_height], 
    [$universe_width/2+21, $universe_height]
  ]

  # ======================================================================= #
  # === $lines_array
  # ======================================================================= #
  $lines_array = [
    [26, 0, 0, -26],
    [0, 26, -26, 0],
    [$universe_width-26, 0, $universe_width, -26],
    [$universe_width, 26, $universe_width+26, 0],
    [$universe_width, $universe_height-26, $universe_width+26, $universe_height], 
    [$universe_width-26, $universe_height, $universe_width, $universe_height+26], 
    [26, $universe_height, 0, $universe_height+26],
    [0, $universe_height-26, -26, $universe_height],
    [$universe_width/2-21, 0, $universe_width/2-21+5, -25], 
    [$universe_width/2+21, 0, $universe_width/2+21-5, -25], 
    [$universe_width/2-21, $universe_height, $universe_width/2-21+5, $universe_height+25], 
    [$universe_width/2+21, $universe_height, $universe_width/2+21-5, $universe_height+25]
  ]

  # ======================================================================= #
  # === $walls_array_path
  # ======================================================================= #
  $walls_array_path = [
    [26, 11, $universe_width/2-21, 11], # Top left wall
    [$universe_width/2+21, 11, $universe_width-26, 11], # Top Right
    [26, $universe_height-11, $universe_width/2-21, $universe_height-11], # Bottom left
    [$universe_width/2+21, $universe_height-11, $universe_width-26, $universe_height-11], # Bottom right
    [11, 26, 11, $universe_height-26],  # Left
    [$universe_width-11, 26, $universe_width-11, $universe_height-26]] # Right

  $pocket_holes = [
    [-4, -4],
    [$universe_width+4, -4],
    [$universe_width+4, $universe_height+4],
    [-4, $universe_height+4],
    [$universe_width/2, -17],
    [$universe_width/2, $universe_height+17]
  ]

  # ======================================================================= #
  # === @id_colors
  #
  # We specify the colours for use of the billiard balls.
  # ======================================================================= #
  @id_colors = [
    0xffFFDDAE,
    0xffFECC15, # This is some yellow colour-variant.
    0xff375CB5,
    0xffEE1122,
    0xff513D9C, 
    0xffFD5413, 
    0xff2E5F32, 
    0xff79282E, 
    0xff191919, 
    0xffFECC15, 
    0xff375CB5, 
    0xffEE1122, 
    0xff513D9C, 
    0xffFD5413, 
    0xff2E5F32, 
    0xff79282E  # This is some reddish colour-variant.
  ]
  restart
end

Instance Attribute Details

#circle_imgObject (readonly)

Returns the value of attribute circle_img.



42
43
44
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 42

def circle_img
  @circle_img
end

#font_smallObject (readonly)

Returns the value of attribute font_small.



43
44
45
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 43

def font_small
  @font_small
end

Instance Method Details

#ball_in_holeObject

#

ball_in_hole

This will increment how many balls are in a hole.

#


404
405
406
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 404

def ball_in_hole
  @balls_in_hole += 1
end

#balls_in_hole?Boolean Also known as: balls_in_hole

#

balls_in_hole?

#

Returns:

  • (Boolean)


222
223
224
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 222

def balls_in_hole?
  @balls_in_hole
end

#button_down(id) ⇒ Object

#

button_down

#


270
271
272
273
274
275
276
277
278
279
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 270

def button_down(id)
  case id
  when Gosu::KbEscape
    close
  when Gosu::KbZ
    restart
  when Gosu::MsLeft
    $balls.each { |inst| inst.checkMouseClick }
  end
end

#button_up(id) ⇒ Object

#

button_up

#


260
261
262
263
264
265
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 260

def button_up(id)
  case id
  when Gosu::MsLeft
    $balls.each { |inst| inst.release }
  end
end

#check_ball_collisionObject

#

check_ball_collision

#


344
345
346
347
348
349
350
351
352
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 344

def check_ball_collision
  second_index = 1
  for i in 0..$balls.length-2  ## Ignore the last ball, since we have all the collisions checked by then
    for q in second_index..$balls.length-1  ### Check every ball from second_index
      $balls[i].check_collision($balls[q])
    end
    second_index += 1
  end
end

#check_path_collision(x1, y1, x2, y2) ⇒ Object

#

check_path_collision

#


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 357

def check_path_collision(x1, y1, x2, y2)
  $balls.each { |inst|
    inst.collision_path(x1, y1, x2, y2)
  }

  for i in 0..$walls_array_path.length-1
    $balls.each { |inst|
      inst.collision_wall_path(
        $walls_array_path[i][0],
        $walls_array_path[i][1],
        $walls_array_path[i][2],
        $walls_array_path[i][3],
        i # Wall-index.
      )
    }
  end

end

#create_ball(x, y, dir, vel, rad) ⇒ Object

#

create_ball

This method will create a ball (and put it into the map).

#


420
421
422
423
424
425
426
427
428
429
430
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 420

def create_ball(x, y, dir, vel, rad)
  for i in 0..199
    if !@ball_ids.include? i
      id = i
      @ball_ids << id
      a_new_ball = Ball.new(self, x, y, dir, vel, rad, id, @id_colors[i])
      $balls << a_new_ball
      break
    end
  end
end

#destroy_ball(inst) ⇒ Object

#

destroy_ball

#


394
395
396
397
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 394

def destroy_ball(inst)
  @ball_ids.delete(inst.id)
  $balls.delete(inst)
end

#drawObject

#

draw

#


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 284

def draw
  @billiard_logo_img.draw(0, 0, 1, 1)
  draw_quad(
    0, 0, 0xffffffff,
    $window_width, 0, 0xffffffff,
    $window_width, $window_height, 0xffffffff, 
    0, $window_height, 0xffffffff, 0
  )

  brown_wall_size = 40

  draw_quad(
    0-brown_wall_size+$window_width/2-$camera_x, 0-brown_wall_size+$window_height/2-$camera_y, 0xff773E2F,
    $universe_width+brown_wall_size+$window_width/2-$camera_x, 0-brown_wall_size+$window_height/2-$camera_y, 0xff773E2F,
    $universe_width+brown_wall_size+$window_width/2-$camera_x, $universe_height+brown_wall_size+$window_height/2-$camera_y, 0xff773E2F, 
    0-brown_wall_size+$window_width/2-$camera_x, $universe_height+brown_wall_size+$window_height/2-$camera_y, 0xff773E2F, 0
  )

  green_wall_size = 21

  draw_quad(
    0-green_wall_size+$window_width/2-$camera_x, 0-green_wall_size+$window_height/2-$camera_y, 0xff144429,
    $universe_width+green_wall_size+$window_width/2-$camera_x, 0-green_wall_size+$window_height/2-$camera_y, 0xff144429,
    $universe_width+green_wall_size+$window_width/2-$camera_x, $universe_height+green_wall_size+$window_height/2-$camera_y, 0xff144429, 
    0-green_wall_size+$window_width/2-$camera_x, $universe_height+green_wall_size+$window_height/2-$camera_y, 0xff144429, 0
  )

  # Corner Pockets
  # Clockwise order
  @pocket_corner_img.draw_rot(0-brown_wall_size+$window_width/2-$camera_x, 0-brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.0, 0.0)
  @pocket_corner_img.draw_rot($universe_width+brown_wall_size+$window_width/2-$camera_x, 0-brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.0, 0.0, -1.0, 1.0)
  @pocket_corner_img.draw_rot($universe_width+brown_wall_size+$window_width/2-$camera_x, $universe_height+brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.0, 0.0, -1.0, -1.0)
  @pocket_corner_img.draw_rot(0-brown_wall_size+$window_width/2-$camera_x, $universe_height+brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.0, 0.0, 1.0, -1.0)

  # Middle Pockets
  @pocket_middle_img.draw_rot($universe_width/2+$window_width/2-$camera_x, 0-brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.5, 0.0, 1.0, 1.0)
  @pocket_middle_img.draw_rot($universe_width/2+$window_width/2-$camera_x, $universe_height+brown_wall_size+$window_height/2-$camera_y, 1, 0, 0.5, 0.0, 1.0, -1.0)

  draw_quad(
    0+$window_width/2-$camera_x, 0+$window_height/2-$camera_y, 0xff38BC73,
    $universe_width+$window_width/2-$camera_x, 0+$window_height/2-$camera_y, 0xff38BC73,
    $universe_width+$window_width/2-$camera_x, $universe_height+$window_height/2-$camera_y, 0xff38BC73, 
    0+$window_width/2-$camera_x, $universe_height+$window_height/2-$camera_y, 0xff38BC73, 0
  )
  # Draw all balls next:
  $balls.each { |inst| inst.draw }

  # # Draw the collision hitbox for the pocket holes (white circles)
  # for i in 0..$pocket_holes.length-1
    # @circle_img.draw_rot($pocket_holes[i][0]+$window_width/2-$camera_x, $pocket_holes[i][1]+$window_height/2-$camera_y, 2, 0, 0.5, 0.5, 1.0*(POCKET_RADIUS/50.0), 1.0*(POCKET_RADIUS/50.0))
  # end

  # for i in 0..$walls_array_path.length-1
    # draw_line($walls_array_path[i][0]+$window_width/2-$camera_x, $walls_array_path[i][1]+$window_height/2-$camera_y, 0xffffffff, $walls_array_path[i][2]+$window_width/2-$camera_x, $walls_array_path[i][3]+$window_height/2-$camera_y, 0xffffffff, 3)
  # end
end

#font?Boolean Also known as: font

#

font?

#

Returns:

  • (Boolean)


435
436
437
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 435

def font?
  @font
end

#instantiate_the_imagesObject

#

instantiate_the_images

#


195
196
197
198
199
200
201
202
203
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 195

def instantiate_the_images
  _ = ::GamesParadise.project_image_directory?+'billiard/'
  @point_img         = gosu_tilable_image("#{_}Point2.png")
  @pocket_corner_img = gosu_tilable_image("#{_}pocket_corner.png")
  @pocket_middle_img = gosu_tilable_image("#{_}pocket_middle.png")
  @circle_img        = gosu_tilable_image("#{_}filled_circle.png")
  @stripe_img        = gosu_tilable_image("#{_}billiard_circle.png")
  @billiard_logo_img = gosu_tilable_image("#{_}billiard_ball.png")
end

#needs_cursor?Boolean

#

needs_cursor?

#

Returns:

  • (Boolean)


379
380
381
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 379

def needs_cursor?
  true
end

#pocket_corner_img?Boolean Also known as: pocket_corner_img

#

pocket_corner_img?

#

Returns:

  • (Boolean)


208
209
210
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 208

def pocket_corner_img?
  @pocket_corner_img
end

#pocket_middle_img?Boolean Also known as: pocket_middle_img

#

pocket_middle_img?

#

Returns:

  • (Boolean)


411
412
413
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 411

def pocket_middle_img?
  @pocket_middle_img
end

#restartObject

#

restart

#


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 156

def restart
  # ======================================================================= #
  # All balls are stored in the following Array.
  # ======================================================================= #
  $balls = []
  # ======================================================================= #
  # === @ball_ids
  # ======================================================================= #
  @ball_ids = []
  # ======================================================================= #
  # === @balls_in_hole
  # ======================================================================= #
  @balls_in_hole = 0
  # ======================================================================= #
  # The cue ball will be created next.
  # ======================================================================= #
  create_ball($universe_width*3/4, $universe_height/2, 271, 0, 11.0)

  # ======================================================================= #
  # The triangle balls.
  # ======================================================================= #
  for i in 0..4
    create_ball(110, $universe_height/2-44+i*22, 0, 0, 11.0)
  end
  for i in 0..3
    create_ball(110+19.10, $universe_height/2-33+i*22, 0, 0, 11.0)
  end
  for i in 0..2
    create_ball(110+19.10*2, $universe_height/2-22+i*22, 0, 0, 11.0)
  end
  for i in 0..1
    create_ball(110+19.10*3, $universe_height/2-11+i*22, 0, 0, 11.0)
  end
  create_ball(110+19.10*4, $universe_height/2, 0, 0, 11.0)
end

#stripe_img?Boolean Also known as: stripe_img

#

strip_img?

#

Returns:

  • (Boolean)


215
216
217
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 215

def stripe_img?
  @stripe_img
end

#updateObject

#

update

#


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
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 229

def update
  $path_blockers = []
  set_title("Billiard  -  [FPS: #{Gosu::fps.to_s}]")
  # Called twice for 120 physics updates per second
  $balls.each { |inst| inst.update }
  check_ball_collision

  $balls.each { |inst| inst.update }
  check_ball_collision

  # ======================================================================= #
  # The next lines can be used to re-position the main camera.
  # ======================================================================= #

  # ======================================================================= #
  # === The A key is pressed.
  # ======================================================================= #
  if button_down? Gosu::KbA
    $camera_x = [$camera_x-5, $universe_width/2-400].max
  elsif button_down? Gosu::KbD
    $camera_x = [$camera_x+5, $universe_width/2+400].min
  elsif button_down? Gosu::KbW
    $camera_y = [$camera_y-5, $universe_height/2-400].max
  elsif button_down? Gosu::KbS
    $camera_y = [$camera_y+5, $universe_height/2+400].min
  end
end

#warp_camera(x, y) ⇒ Object

#

warp_camera

#


386
387
388
389
# File 'lib/games_paradise/gui/gosu/billiard/billiard.rb', line 386

def warp_camera(x, y)
  $camera_x = x
  $camera_y = y
end