Class: GamesAndRpgParadise::ParticleSimulator::Simulation
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- GamesAndRpgParadise::ParticleSimulator::Simulation
show all
- Defined in:
- lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb
Instance Method Summary
collapse
#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, #set_font, #set_title, #sqrt, #tab_key?, #write_this_text
Constructor Details
29
30
31
32
33
34
35
36
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 29
def initialize
super(WIDTH, HEIGHT, false)
self.caption = 'Who Let the Dogs Out?'
@prev_time = Gosu.milliseconds
@font = Gosu::Font.new(self, Gosu.default_font_name, 15)
reset
resimulate
end
|
Instance Method Details
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 104
def button_down(id)
close if id == Button::KbEscape
if id == Button::KbD
@font_color.alpha = (@font_color.alpha == 0) ? 150 : 0
end
if id == Button::KbSpace
@timer = 0
resimulate
end
end
|
#do_collision(h1, h2) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 147
def do_collision(h1, h2)
return if Gosu.distance(h1.x, h1.y, h2.x, h2.y) > 2 * H_RADIUS
x21 = h2.x - h1.x
y21 = h2.y - h1.y
vx21 = h2.vx - h1.vx
vy21 = h2.vy - h1.vy
return if vx21*x21 + vy21*y21 >= 0
a = y21 / x21
dvx2 = -(vx21 + a*vy21)/(1 + a*a)
advx2 = a*dvx2
h2.vx += dvx2
h2.vy += advx2
h1.vx -= dvx2
h1.vy -= advx2
end
|
#draw ⇒ Object
96
97
98
99
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 96
def draw
(@hunters + @preys).each {|x| x.draw}
draw_scoreboard
end
|
#draw_scoreboard ⇒ Object
177
178
179
180
181
182
183
184
185
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 177
def draw_scoreboard
avg_text = (((@avg.to_f/6.0).round)/10.0).to_s + 'sec'
cur_text = (((@timer.to_f/6.0).round)/10.0).to_s + 'sec'
last_text = (((@last.to_f/6.0).round)/10.0).to_s + 'sec'
runs_text = @runs.to_s
remaining_text = @preys.size.to_s
display = "AVG: #{avg_text}, CUR: #{cur_text}, LAST: #{last_text}, RUNS: #{runs_text}, REMAIN: #{remaining_text}"
@font.draw_text(display, 10, 10, 3, 1, 1, @font_color)
end
|
#draw_square(x, y, r, color) ⇒ Object
#
draw_square
draws a square at x,y with radius r
#
137
138
139
140
141
142
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 137
def draw_square(x, y, r, color)
draw_quad(x + r, y + r, color,
x - r, y + r, color,
x + r, y - r, color,
x - r, y - r, color)
end
|
#get_avg(array) ⇒ Object
190
191
192
193
194
195
196
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 190
def get_avg(array)
return if array.empty?
avg = 0
array.each {|i| avg += i }
avg /= array.size
return avg
end
|
#reset ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 41
def reset
@accum_fps = 0
@timer = 0
@font_color = Gosu::Color.new(150, 255, 255, 255)
@times = []
@avg = 0
@last = 0
@runs = 0
end
|
#resimulate ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 118
def resimulate
if @timer != 0
@times << @timer
@last = @times.last
@runs += 1
end
@avg = get_avg(@times)
@timer = 0
@hunters = [0xffff0000, 0xffffffff, 0xff0000ff, 0xffffff00, 0xff00ffff].map do |c|
Hunter.new(self, c)
end
@preys = Array.new(100) {Prey.new(self)}
end
|
#update ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/games_and_rpg_paradise/gui/gosu/particle_simulator/main.rb', line 68
def update
@cur_sec ||= 0
@accum_fps += 1
now = Gosu.milliseconds
if @cur_sec != now / 1000
@cur_sec = now / 1000
self.caption = "Who Let the Dogs Out? FPS: #{@accum_fps}"
@accum_fps = 0
end
@timer += 1
(0...4).each { |i|
(i+1...5).each { |j|
do_collision(@hunters[i], @hunters[j])
}
}
if !@preys.empty?
@hunters.each {|h| h.update(@preys)}
@preys.reject! {|p| !p.update(@hunters)}
else
resimulate
end
end
|