Skip to content

Commit

Permalink
Move render scale factor into Resource, prepare for configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramirisu committed Sep 17, 2024
1 parent a392b4b commit bf49090
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod palette;
pub mod piece;
pub mod player;
pub mod plugin;
mod render;
mod score;
mod tick;
mod timer;
pub mod transform;
pub mod transition;
3 changes: 0 additions & 3 deletions src/game/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use bevy::{prelude::*, time::Stopwatch};
use super::{
board::Board,
drop_speed::DropSpeed,
render::RenderConfig,
tick::{EntryDelayTick, FallTick, LineClearTick},
timer::{DelayAutoShiftTimer, GameTimer, PressDownTimer},
transition::Transition,
Expand Down Expand Up @@ -41,7 +40,6 @@ impl Default for PlayerConfig {

#[derive(Resource)]
pub struct PlayerData {
pub rc: RenderConfig,
pub board: Board,
pub game_stopwatch: Stopwatch,
pub game_timer: GameTimer,
Expand All @@ -59,7 +57,6 @@ pub struct PlayerData {
impl PlayerData {
pub fn new(config: PlayerConfig) -> Self {
Self {
rc: RenderConfig::default(),
board: Board::new(config.start_level, config.transition),
game_stopwatch: Stopwatch::new(),
game_timer: GameTimer::default(),
Expand Down
98 changes: 51 additions & 47 deletions src/game/plugin.rs

Large diffs are not rendered by default.

66 changes: 34 additions & 32 deletions src/game/render.rs → src/game/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@ const SQUARE_LAYER: f32 = 3.0;
const CURR_PIECE_LAYER: f32 = 4.0;
const COVER_LAYER: f32 = 5.0;

pub struct RenderConfig {
unit: f32,
#[derive(Clone, Copy, Resource)]
pub struct GameTransform {
scale: f32,
}

impl RenderConfig {
pub fn new() -> Self {
Self { unit: 36.0 }
impl GameTransform {
pub fn new(scale: f32) -> Self {
Self { scale }
}

pub fn unit(&self) -> f32 {
self.unit
pub fn scale(&self) -> f32 {
self.scale
}

fn square_width(&self) -> f32 {
self.unit
self.scale * 36.0
}

fn square_height(&self) -> f32 {
self.unit
self.scale * 36.0
}

pub fn square_size(&self) -> Vec2 {
Expand All @@ -38,30 +39,27 @@ impl RenderConfig {
}

fn board_width(&self) -> f32 {
self.unit * Board::BOARD_COLS as f32
self.square_width() * Board::BOARD_COLS as f32
}

fn board_height(&self) -> f32 {
self.unit * Board::BOARD_ROWS as f32
self.square_height() * Board::BOARD_ROWS as f32
}

pub fn board_size(&self) -> Vec2 {
Vec2::new(self.board_width(), self.board_height())
}

pub fn board_background_size(&self) -> Vec2 {
Vec2::new(
self.board_width() + self.unit / 10.0,
self.board_height() + self.unit / 20.0,
)
Vec2::new(self.board_width() * 1.01, self.board_height() * 1.0025)
}

pub fn board_background_translation(&self) -> Vec3 {
Vec3::new(0.0, -self.unit / 20.0, BOARD_BACKGROUND_LAYER)
Vec3::new(0.0, -self.board_height() * 0.0025, BOARD_BACKGROUND_LAYER)
}

pub fn board_cover_size(&self) -> Vec2 {
Vec2::new(self.unit * 8.0, self.unit * 3.0)
Vec2::new(self.square_width() * 8.0, self.square_height() * 3.0)
}

pub fn board_cover_translation(&self) -> Vec3 {
Expand All @@ -85,15 +83,15 @@ impl RenderConfig {
}

pub fn lines_translation(&self) -> Vec3 {
Vec3::new(-self.board_width(), self.unit * 9.0, BOARD_LAYER)
Vec3::new(-self.board_width(), self.square_height() * 9.0, BOARD_LAYER)
}

pub fn score_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), self.unit * 9.0, BOARD_LAYER)
Vec3::new(self.board_width(), self.square_height() * 9.0, BOARD_LAYER)
}

pub fn level_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), -self.unit * 6.0, BOARD_LAYER)
Vec3::new(self.board_width(), -self.square_height() * 6.0, BOARD_LAYER)
}

pub fn next_piece_translation(&self, x: i32, y: i32) -> Vec3 {
Expand All @@ -109,53 +107,57 @@ impl RenderConfig {
}

pub fn next_piece_slot_size(&self) -> Vec2 {
Vec2::new(self.unit * 6.0, self.unit * 6.0)
Vec2::new(self.square_width() * 6.0, self.square_height() * 6.0)
}

pub fn next_piece_slot_background_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), 0.0, BOARD_BACKGROUND_LAYER)
}

pub fn next_piece_slot_background_size(&self) -> Vec2 {
Vec2::new(self.unit * 6.1, self.unit * 6.1)
Vec2::new(self.square_width() * 6.1, self.square_height() * 6.1)
}

pub fn statistics_translation(&self) -> Vec3 {
Vec3::new(-self.board_width(), self.unit * 3.0, BOARD_LAYER)
Vec3::new(-self.board_width(), self.square_height() * 3.0, BOARD_LAYER)
}

pub fn das_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), -self.unit * 8.0, BOARD_LAYER)
Vec3::new(self.board_width(), -self.square_height() * 8.0, BOARD_LAYER)
}

pub fn game_stopwatch_translation(&self) -> Vec3 {
Vec3::new(self.board_width(), -self.unit * 10.0, BOARD_LAYER)
Vec3::new(
self.board_width(),
-self.square_height() * 10.0,
BOARD_LAYER,
)
}

pub fn piece_count_square_size(&self) -> Vec2 {
Vec2::splat(self.unit / 2.0)
Vec2::new(self.square_width() * 0.5, self.square_height() * 0.5)
}

pub fn piece_count_translation(&self, index: usize, x: i32, y: i32) -> Vec3 {
(Vec2::new(x as f32 + 0.5, y as f32) * self.piece_count_square_size()
+ Vec2::new(
-self.board_width() - self.unit,
-self.unit * 2.0 - self.unit * 1.5 * index as f32,
-self.board_width() - self.square_width(),
-self.square_height() * 2.0 - self.square_height() * 1.5 * index as f32,
))
.extend(BOARD_LAYER)
}

pub fn piece_count_counter_translation(&self, index: usize) -> Vec3 {
Vec3::new(
-self.board_width() + self.unit * 2.0,
-self.unit * 2.0 - self.unit * 1.5 * index as f32,
-self.board_width() + self.square_width() * 2.0,
-self.square_height() * 2.0 - self.square_height() * 1.5 * index as f32,
BOARD_LAYER,
)
}
}

impl Default for RenderConfig {
impl Default for GameTransform {
fn default() -> Self {
Self::new()
Self::new(1.0)
}
}
1 change: 1 addition & 0 deletions src/game_option_menu/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod plugin;
pub mod transform;
24 changes: 16 additions & 8 deletions src/game_option_menu/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ use crate::{
#[cfg(not(target_arch = "wasm32"))]
use bevy::window::WindowMode;

use super::transform::GameOptionMenuTransform;

pub fn setup(app: &mut App) {
app.insert_resource(GameOptionMenuData::default())
app.insert_resource(GameOptionMenuTransform::default())
.insert_resource(GameOptionMenuData::default())
.add_systems(OnEnter(AppState::GameModeMenu), setup_screen)
.add_systems(
Update,
Expand Down Expand Up @@ -95,8 +98,13 @@ impl Default for GameOptionMenuData {
}
}

fn setup_screen(mut commands: Commands, mut image_assets: ResMut<Assets<Image>>) {
fn setup_screen(
mut commands: Commands,
mut image_assets: ResMut<Assets<Image>>,
game_option_menu_transform: Res<GameOptionMenuTransform>,
) {
let logo_images = load_logo_images(&mut image_assets);
let scale = game_option_menu_transform.scale();

commands
.spawn((
Expand All @@ -120,7 +128,7 @@ fn setup_screen(mut commands: Commands, mut image_assets: ResMut<Assets<Image>>)
style: Style {
display: Display::Grid,
grid_template_columns: vec![GridTrack::auto(); TETRIS_BITMAP[0].len()],
margin: UiRect::all(Val::Px(40.0)),
margin: UiRect::all(Val::Px(scale * 40.0)),
..default()
},
..default()
Expand All @@ -131,8 +139,8 @@ fn setup_screen(mut commands: Commands, mut image_assets: ResMut<Assets<Image>>)
parent.spawn((
NodeBundle {
style: Style {
width: Val::Px(24.0),
height: Val::Px(24.0),
width: Val::Px(scale * 24.0),
height: Val::Px(scale * 24.0),
..default()
},
..default()
Expand All @@ -153,7 +161,7 @@ fn setup_screen(mut commands: Commands, mut image_assets: ResMut<Assets<Image>>)
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
margin: UiRect::all(Val::Px(40.0)),
margin: UiRect::all(Val::Px(scale * 40.0)),
..default()
},
..default()
Expand All @@ -163,12 +171,12 @@ fn setup_screen(mut commands: Commands, mut image_assets: ResMut<Assets<Image>>)
parent.spawn((
TextBundle::from_sections(vec![
TextSection::from_style(TextStyle {
font_size: 40.0,
font_size: scale * 36.0,
color: WHITE.into(),
..default()
}),
TextSection::from_style(TextStyle {
font_size: 40.0,
font_size: scale * 36.0,
color: WHITE.into(),
..default()
}),
Expand Down
22 changes: 22 additions & 0 deletions src/game_option_menu/transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use bevy::prelude::*;

#[derive(Clone, Copy, Resource)]
pub struct GameOptionMenuTransform {
scale: f32,
}

impl GameOptionMenuTransform {
pub fn new(scale: f32) -> Self {
Self { scale }
}

pub fn scale(&self) -> f32 {
self.scale
}
}

impl Default for GameOptionMenuTransform {
fn default() -> Self {
Self::new(1.0)
}
}
1 change: 1 addition & 0 deletions src/level_menu/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod plugin;
pub mod transform;
Loading

0 comments on commit bf49090

Please sign in to comment.