Verified Commit 9d54b8af authored by betseg's avatar betseg 🏳🌈

updates

parent 9ec1e98d
......@@ -17,10 +17,10 @@ pub const PADDLE_WIDTH: i32 = 100;
pub const PADDLE_HEIGHT: i32 = 10;
// dodgerblue
pub const PADDLE_COLOR: [u8; 4] = [30, 144, 255, 255];
pub const BOUNCINESS: f32 = 2.;
pub const BOUNCINESS: f32 = 1.;
pub const PADDLE_SPEED: f32 = 8.;
pub const BRICK_WIDTH: i32 = 43;
pub const BRICK_WIDTH: i32 = 45;
pub const BRICK_HEIGHT: i32 = 35;
pub const BRICK_ROWS: usize = 4;
pub const BRICK_COLS: usize = 12;
......@@ -37,3 +37,9 @@ pub const BRICK_COLORS: [[u8; 4]; 4] = [
[144, 238, 144, 255],
[135, 206, 250, 255],
];
pub const SCORE_MULT: u32 = 25;
pub const SCORE_POS: Vec2<f32> = Vec2::new(10., 10.);
pub const TEXT_SIZE: f32 = 20.;
pub const SCORE_TEXT: &'static str = "Score: ";
pub const PAUSED_TEXT: &'static str = "Paused.";
......@@ -2,11 +2,11 @@ use tetra::ContextBuilder;
mod consts;
mod entity;
mod state;
mod gamestate;
fn main() -> tetra::Result {
ContextBuilder::new("brek blok", 640, 480)
.quit_on_escape(true)
.build()?
.run(state::GameState::new)
.run(gamestate::GameState::new)
}
use tetra::{
graphics::{self, Texture},
input::{self, Key},
math::Vec2,
window, Context, State,
};
use crate::consts::*;
use crate::entity::Entity;
pub struct GameState {
bricks: Vec<Entity>,
ball: Entity,
paddle: Entity,
}
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 brick_texs = BRICK_COLORS
.iter()
.map(|c| Self::create_texture(ctx, BRICK_WIDTH, BRICK_HEIGHT, *c))
.collect::<tetra::Result<Vec<_>>>()?;
let paddle_pos = Vec2::new(
(WINDOW_WIDTH - paddle_tex.height() as f32) / 2.,
WINDOW_HEIGHT - PADDLE_HEIGHT as f32 - MARGIN,
);
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();
for i in 0..BRICK_ROWS {
for j in 0..BRICK_COLS {
bricks.push(Entity::new(
brick_texs[i].clone(),
Vec2::new(
HORZ_OUT_MGN + j as f32 * HORZ_IN_MGN,
VERT_OUT_MGN + i as f32 * VERT_IN_MGN,
),
));
}
}
Ok(GameState {
bricks,
ball,
paddle,
})
}
fn create_texture(
ctx: &mut Context,
width: i32,
height: i32,
color: [u8; 4],
) -> tetra::Result<Texture> {
Texture::from_rgba(
ctx,
width,
height,
&std::iter::repeat(&color)
.take((width * height) as usize)
.flatten()
.copied()
.collect::<Vec<_>>(),
)
}
}
impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
self.ball.position += self.ball.velocity;
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
{
self.paddle.position.x += PADDLE_SPEED;
}
let ball_bounds = self.ball.bounds();
let paddle_bounds = self.paddle.bounds();
for i in 0..self.bricks.len() {
if self.bricks[i].bounds().intersects(&ball_bounds) {
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() {
self.ball.velocity.x = -self.ball.velocity.x;
} 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;
break;
}
}
if self.bricks.is_empty() {
window::quit(ctx);
println!("noice");
}
if ball_bounds.intersects(&paddle_bounds) {
self.ball.velocity.y = -self.ball.velocity.y;
self.ball.position += self.ball.velocity;
let offset = (self.paddle.center().x - self.ball.center().x) / self.paddle.width();
self.ball.velocity.rotate_z(BOUNCINESS * -offset);
self.ball.velocity *= BALL_ACCELERATION;
} else if self.ball.position.y <= MARGIN {
self.ball.velocity.y = -self.ball.velocity.y;
} else if self.ball.position.y >= WINDOW_HEIGHT {
window::quit(ctx);
}
if self.ball.position.x <= MARGIN
|| self.ball.position.x + self.ball.texture.width() as f32 >= WINDOW_WIDTH - MARGIN
{
self.ball.velocity.x = -self.ball.velocity.x;
}
Ok(())
}
fn draw(&mut self, ctx: &mut Context) -> tetra::Result {
graphics::clear(ctx, BACKGROUND);
graphics::draw(ctx, &self.ball.texture, self.ball.position);
graphics::draw(ctx, &self.paddle.texture, self.paddle.position);
for i in self.bricks.iter() {
graphics::draw(ctx, &i.texture, i.position);
}
Ok(())
}
}
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