Verified Commit 72902dbb authored by betseg's avatar betseg 🏳🌈

fmt

parent c43ee56b
......@@ -27,8 +27,7 @@ pub const BRICK_COLS: usize = 12;
pub const HORZ_OUT_MGN: f32 = BRICK_WIDTH as f32 / 2.;
pub const VERT_OUT_MGN: f32 = BRICK_HEIGHT as f32 * 1.5;
pub const HORZ_IN_MGN: f32 =
(WINDOW_WIDTH - 2. * HORZ_OUT_MGN - BRICK_WIDTH as f32)
/ (BRICK_COLS as f32 - 1.);
(WINDOW_WIDTH - 2. * HORZ_OUT_MGN - BRICK_WIDTH as f32) / (BRICK_COLS as f32 - 1.);
pub const VERT_IN_MGN: f32 = HORZ_IN_MGN - (BRICK_WIDTH - BRICK_HEIGHT) as f32;
// lightsalmon, khaki, lightgreen, lightskyblue
......
......@@ -14,11 +14,7 @@ impl Entity {
Self::with_velocity(texture, position, Vec2 { x: 0., y: 0. })
}
pub const fn with_velocity(
texture: Texture,
position: Vec2<f32>,
velocity: Vec2<f32>,
) -> Self {
pub const fn with_velocity(texture: Texture, position: Vec2<f32>, velocity: Vec2<f32>) -> Self {
Self {
texture,
position,
......
......@@ -18,14 +18,8 @@ pub struct GameState {
impl GameState {
pub fn new(ctx: &mut Context) -> tetra::Result<GameState> {
let ball_tex =
Self::create_texture(ctx, BALL_WIDTH, BALL_HEIGHT, BALL_COLOR)?;
let paddle_tex = Self::create_texture(
ctx,
PADDLE_WIDTH,
PADDLE_HEIGHT,
PADDLE_COLOR,
)?;
let ball_tex = Self::create_texture(ctx, BALL_WIDTH, BALL_HEIGHT, BALL_COLOR)?;
let paddle_tex = Self::create_texture(ctx, PADDLE_WIDTH, PADDLE_HEIGHT, PADDLE_COLOR)?;
let brick_texs = BRICK_COLORS
.iter()
.map(|c| Self::create_texture(ctx, BRICK_WIDTH, BRICK_HEIGHT, *c))
......@@ -36,11 +30,7 @@ impl GameState {
WINDOW_HEIGHT - PADDLE_HEIGHT as f32 - MARGIN,
);
let ball = Entity::with_velocity(
ball_tex,
paddle_pos + STARTING_POS,
STARTING_VEL,
);
let ball = Entity::with_velocity(ball_tex, paddle_pos + STARTING_POS, STARTING_VEL);
let paddle = Entity::new(paddle_tex, paddle_pos);
let mut bricks = Vec::new();
......@@ -62,11 +52,7 @@ impl GameState {
paddle,
is_paused: false,
score: (
Text::new(
format!("{}{}", SCORE_TEXT, 0),
Font::default(),
TEXT_SIZE,
),
Text::new(format!("{}{}", SCORE_TEXT, 0), Font::default(), TEXT_SIZE),
0,
),
})
......@@ -95,11 +81,9 @@ impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
if self.is_paused {
if input::is_key_pressed(ctx, Key::Space) {
self.score.0.set_content(format!(
"{}{}",
SCORE_TEXT,
self.score.1 * SCORE_MULT
));
self.score
.0
.set_content(format!("{}{}", SCORE_TEXT, self.score.1 * SCORE_MULT));
self.is_paused = false;
}
return Ok(());
......@@ -112,16 +96,13 @@ impl State for GameState {
self.ball.position += self.ball.velocity;
if (input::is_key_down(ctx, Key::A)
|| input::is_key_down(ctx, Key::Left))
if (input::is_key_down(ctx, Key::A) || input::is_key_down(ctx, Key::Left))
&& self.paddle.position.x > MARGIN
{
self.paddle.position.x -= PADDLE_SPEED;
}
if (input::is_key_down(ctx, Key::D)
|| input::is_key_down(ctx, Key::Right))
&& self.paddle.position.x + (PADDLE_WIDTH as f32)
< WINDOW_WIDTH - MARGIN
if (input::is_key_down(ctx, Key::D) || input::is_key_down(ctx, Key::Right))
&& self.paddle.position.x + (PADDLE_WIDTH as f32) < WINDOW_WIDTH - MARGIN
{
self.paddle.position.x += PADDLE_SPEED;
}
......@@ -134,23 +115,17 @@ impl State for GameState {
let ball_prev_pos = self.ball.center() - self.ball.velocity;
let brick = self.bricks.remove(i).bounds();
if ball_prev_pos.x < brick.left()
|| ball_prev_pos.x > brick.right()
{
if ball_prev_pos.x < brick.left() || ball_prev_pos.x > brick.right() {
self.ball.velocity.x = -self.ball.velocity.x;
} else if ball_prev_pos.y < brick.bottom()
|| ball_prev_pos.y > brick.top()
{
} else if ball_prev_pos.y < brick.bottom() || ball_prev_pos.y > brick.top() {
self.ball.velocity.y = -self.ball.velocity.y;
}
self.ball.position += self.ball.velocity;
self.score.1 += self.ball.velocity.magnitude() as u32;
self.score.0.set_content(format!(
"{}{}",
SCORE_TEXT,
self.score.1 * SCORE_MULT
));
self.score
.0
.set_content(format!("{}{}", SCORE_TEXT, self.score.1 * SCORE_MULT));
break;
}
}
......@@ -163,10 +138,9 @@ impl State for GameState {
if ball_bounds.intersects(&paddle_bounds) {
self.ball.position -= self.ball.velocity;
let offset = (self.paddle.center().x - self.ball.center().x)
/ self.paddle.width();
self.ball.velocity = Vec2::new(0., self.ball.velocity.magnitude())
.rotated_z(BOUNCINESS * offset);
let offset = (self.paddle.center().x - self.ball.center().x) / self.paddle.width();
self.ball.velocity =
Vec2::new(0., self.ball.velocity.magnitude()).rotated_z(BOUNCINESS * offset);
self.ball.velocity.y = -self.ball.velocity.y;
if self.ball.velocity.magnitude() < 10. {
self.ball.velocity *= BALL_ACCELERATION;
......@@ -180,8 +154,7 @@ impl State for GameState {
}
if self.ball.position.x <= MARGIN
|| self.ball.position.x + self.ball.texture.width() as f32
>= WINDOW_WIDTH - MARGIN
|| self.ball.position.x + self.ball.texture.width() as f32 >= WINDOW_WIDTH - MARGIN
{
self.ball.velocity.x = -self.ball.velocity.x;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment