2022-01-03 01:39:17 +01:00
|
|
|
require "../src/game"
|
|
|
|
require "../src/shape"
|
|
|
|
require "../src/entity"
|
|
|
|
require "../src/entity/circle_collision"
|
|
|
|
|
|
|
|
module PF
|
|
|
|
class Ball < Entity
|
|
|
|
include CircleCollision
|
|
|
|
|
2024-10-07 07:02:31 +02:00
|
|
|
getter frame : Array(PF2d::Vec2(Float64))
|
2022-01-10 00:50:21 +01:00
|
|
|
getter color = Pixel.random
|
2022-01-03 01:39:17 +01:00
|
|
|
|
|
|
|
def initialize(size : Float64)
|
|
|
|
@frame = Shape.circle(size.to_i32, size.to_i32)
|
|
|
|
@mass = size
|
|
|
|
@radius = size
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Balls < Game
|
2022-01-10 00:50:21 +01:00
|
|
|
ADD_BALL = 2.0
|
2022-01-03 01:39:17 +01:00
|
|
|
@balls : Array(Ball) = [] of Ball
|
2022-01-10 00:50:21 +01:00
|
|
|
@ball_clock = ADD_BALL
|
2024-10-07 07:02:31 +02:00
|
|
|
@font = Pixelfont::Font.new("#{__DIR__}/../lib/pixelfont/fonts/pixel-5x7.txt")
|
2022-01-03 01:39:17 +01:00
|
|
|
|
|
|
|
def initialize(*args, **kwargs)
|
|
|
|
super
|
2022-01-10 00:50:21 +01:00
|
|
|
add_ball
|
|
|
|
end
|
2022-01-03 01:39:17 +01:00
|
|
|
|
2022-01-10 00:50:21 +01:00
|
|
|
def add_ball
|
2024-10-07 07:02:31 +02:00
|
|
|
position = PF2d::Vec[rand(0.0_f64..width.to_f64), rand(0.0_f64..height.to_f64)]
|
2022-01-10 00:50:21 +01:00
|
|
|
ball = Ball.new(rand(10.0..30.0))
|
|
|
|
ball.position = position
|
2024-10-07 07:02:31 +02:00
|
|
|
ball.velocity = PF2d::Vec[rand(-50.0..50.0), rand(-50.0..50.0)]
|
2022-01-10 00:50:21 +01:00
|
|
|
@balls << ball
|
2022-01-03 01:39:17 +01:00
|
|
|
end
|
|
|
|
|
2022-02-27 18:45:51 +01:00
|
|
|
def update(dt)
|
2022-01-10 00:50:21 +01:00
|
|
|
@ball_clock -= dt
|
|
|
|
if @ball_clock < 0
|
|
|
|
@ball_clock = ADD_BALL
|
|
|
|
add_ball
|
|
|
|
end
|
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
@balls.each do |b|
|
|
|
|
b.update(dt)
|
2022-01-10 00:50:21 +01:00
|
|
|
|
|
|
|
b.position.x = width + b.radius if b.position.x < -b.radius
|
|
|
|
b.position.y = height + b.radius if b.position.y < -b.radius
|
|
|
|
b.position.x = -b.radius if b.position.x > width + b.radius
|
|
|
|
b.position.y = -b.radius if b.position.y > height + b.radius
|
2022-01-03 01:39:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
collission_pairs = [] of Tuple(Ball, Ball)
|
2022-01-10 00:50:21 +01:00
|
|
|
|
2022-01-03 01:39:17 +01:00
|
|
|
@balls.each do |a|
|
|
|
|
@balls.each do |b|
|
|
|
|
next if a == b
|
|
|
|
next if collission_pairs.includes?({a, b})
|
|
|
|
|
|
|
|
if a.collides_with?(b)
|
|
|
|
collission_pairs << {a, b}
|
|
|
|
a.resolve_collision(b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def draw
|
|
|
|
clear(10, 10, 30)
|
|
|
|
@balls.each do |ball|
|
2022-01-10 00:50:21 +01:00
|
|
|
fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32), ball.color)
|
2022-01-03 01:39:17 +01:00
|
|
|
end
|
2024-10-07 07:02:31 +02:00
|
|
|
draw_string("Balls: #{@balls.size}", 5, 5, @font, Pixel::White)
|
2022-01-03 01:39:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
balls = PF::Balls.new(600, 400, 2)
|
|
|
|
balls.run!
|