mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-01-06 05:24:17 +01:00
84 lines
2.1 KiB
Crystal
84 lines
2.1 KiB
Crystal
require "../src/game"
|
|
require "../src/shape"
|
|
require "../src/entity"
|
|
require "../src/entity/circle_collision"
|
|
require "../src/pixel_text"
|
|
|
|
module PF
|
|
class Ball < Entity
|
|
include CircleCollision
|
|
|
|
getter frame : Array(Vector2(Float64))
|
|
getter color = Pixel.random
|
|
|
|
def initialize(size : Float64)
|
|
@frame = Shape.circle(size.to_i32, size.to_i32)
|
|
@mass = size
|
|
@radius = size
|
|
end
|
|
end
|
|
|
|
class Balls < Game
|
|
ADD_BALL = 2.0
|
|
@balls : Array(Ball) = [] of Ball
|
|
@ball_clock = ADD_BALL
|
|
@text = PF::PixelText.new("assets/pf-font.png")
|
|
|
|
def initialize(*args, **kwargs)
|
|
super
|
|
@text.color(PF::Pixel.new(255, 255, 255))
|
|
add_ball
|
|
end
|
|
|
|
def add_ball
|
|
position = Vector[rand(0.0_f64..@width.to_f64), rand(0.0_f64..@height.to_f64)]
|
|
ball = Ball.new(rand(10.0..30.0))
|
|
ball.position = position
|
|
ball.velocity = Vector[rand(-50.0..50.0), rand(-50.0..50.0)]
|
|
@balls << ball
|
|
end
|
|
|
|
def update(dt, event)
|
|
@ball_clock -= dt
|
|
if @ball_clock < 0
|
|
@ball_clock = ADD_BALL
|
|
add_ball
|
|
end
|
|
|
|
@balls.each do |b|
|
|
b.update(dt)
|
|
|
|
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
|
|
end
|
|
|
|
collission_pairs = [] of Tuple(Ball, Ball)
|
|
|
|
@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|
|
|
fill_shape(Shape.translate(ball.frame, translation: ball.position).map(&.to_i32), ball.color)
|
|
# draw_circle(ball.position.to_i32, ball.radius.to_i32, Pixel.green)
|
|
end
|
|
@text.draw_to(screen, "#{@balls.size}", 5, 5)
|
|
end
|
|
end
|
|
end
|
|
|
|
balls = PF::Balls.new(600, 400, 2)
|
|
balls.run!
|