forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example14_bounding_box_circle.rb
116 lines (96 loc) · 3.26 KB
/
example14_bounding_box_circle.rb
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
#!/usr/bin/env ruby
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu
#
# Demonstrating Chingu-traits bounding_circle, bounding_box and collision_detection.
#
class Game < Chingu::Window
def initialize
super(640,400)
self.input = {:esc => :exit, :q => :decrease_size, :w => :increase_size, :a => :decrease_speed, :s => :increase_speed}
self.factor = 1
20.times { Circle.create(:x => width/2, :y => height/2) }
20.times { Box.create(:x => width/2, :y => height/2) }
@blue = Color.new(0xFF0000FF)
@white = Color.new(0xFFFFFFFF)
end
def increase_size
game_objects.each { |go| go.factor += 1 }
end
def decrease_size
game_objects.each { |go| go.factor -= 1 if go.factor > 1 }
end
def increase_speed
game_objects.each { |go| go.velocity_x *= 1.2; go.velocity_y *= 1.2; }
end
def decrease_speed
game_objects.each { |go| go.velocity_x *= 0.8; go.velocity_y *= 0.8; }
end
def update
super
game_objects.each { |go| go.color = @white }
#
# Collide Boxes/Circles, Boxes/Boxes and Circles/Circles (basicly all objects on screen)
#
# Before optmization: 25 FPS (20 boxes and 20 circles)
# Cached radius and rects:
#
[Box, Circle].each_collision(Box, Circle) { |o, o2| o.color, o2.color = @blue, @blue }
#
# Only collide boxes with other boxes
#
## Box.each_collision(Box) { |o, o2| o.color, o2.color = @blue, @blue }
#
# Only collide circles with other circles
#
## Circle.each_collision(Circle) { |o, o2| o.color, o2.color = @blue, @blue }
#
# Only collide Boxes with Boxes and Circles
#
## Box.each_collision(Box,Circle) { |o, o2| o.color, o2.color = @blue, @blue }
self.caption = "traits bounding_box/circle & collision_detection. Q/W: Size. A/S: Speed. FPS: #{fps} Objects: #{game_objects.size}"
end
end
class Circle < GameObject
trait :bounding_circle, :debug => true
traits :velocity, :collision_detection
def setup
@image = Image["circle.png"]
self.velocity_x = 3 - rand * 6
self.velocity_y = 3 - rand * 6
self.input = [:holding_left, :holding_right, :holding_down, :holding_up] # NOTE: giving input an Array, not a Hash
cache_bounding_circle
end
def holding_left; @x -= 1; end
def holding_right; @x += 1; end
def holding_down; @y += 1; end
def holding_up; @y -= 1; end
def update
self.velocity_x = -self.velocity_x if @x < 0 || @x > $window.width
self.velocity_y = -self.velocity_y if @y < 0 || @y > $window.height
end
end
class Box < GameObject
trait :bounding_box, :debug => true
traits :velocity, :collision_detection
def setup
@image = Image["rect.png"]
self.velocity_x = 3 - rand * 6
self.velocity_y = 3 - rand * 6
# Test to make sure the bounding_box works with all bellow combos
#self.factor = 2
#self.factor = -2
self.rotation_center = :left_top
#self.rotation_center = :center
#self.rotation_center = :right_bottom
cache_bounding_box
end
def update
self.velocity_x = -self.velocity_x if @x < 0 || @x > $window.width
self.velocity_y = -self.velocity_y if @y < 0 || @y > $window.height
end
end
Game.new.show