Skip to content

Commit

Permalink
优化资源定位
Browse files Browse the repository at this point in the history
  • Loading branch information
atopx committed Oct 28, 2023
1 parent 456c01d commit 4ca4a89
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

1. 由于刚学习使用bevy框架,代码结构比较乱,先探索功能,后期整理代码
2. 系统设置菜单,分辨率、刷新率、声音、音效等
3. 目前资源位置是绝对定位,无法适配各种系统,优化屏幕缩放
3. ~~目前资源位置是绝对定位,无法适配各种系统,优化屏幕缩放~~
4. 联机对弈待实现,计划使用`Peer to Peer`建立连接,无中心服务器

# 游戏截图
Expand Down
128 changes: 6 additions & 122 deletions src/chess/broad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::component::PieceColor;
use crate::{component, player};
use crate::{
game::Data,
public::{self, BROAD_SIZE, PIECE_SIZE, WIN_SIZE},
public::{self, BROAD_SIZE, WIN_SIZE},
};
use bevy::prelude::*;

Expand All @@ -20,10 +20,10 @@ fn make_piece_bundle(
ButtonBundle {
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(left),
bottom: Val::Px(bottom),
height: Val::Px(PIECE_SIZE.h - 3_f32),
width: Val::Px(PIECE_SIZE.w - 3_f32),
left: Val::Percent(left),
bottom: Val::Percent(bottom),
height: Val::Percent(9_f32),
width: Val::Percent(9_f32),
..Default::default()
},
background_color: BackgroundColor::from(Color::NONE),
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn setup_running(
for (col, piece_some) in rows_data.iter_mut().enumerate() {
if let Some(piece) = piece_some {
if let Some(image) = pieces.get_handle(piece, false) {
let (left, bottom) = PIECE_POS_MAP[row][col];
let (left, bottom) = public::get_piece_render_percent(row, col);
if piece.color == PieceColor::White {
make_piece_bundle(
parent,
Expand Down Expand Up @@ -190,7 +190,6 @@ pub fn setup_running(
..default()
})
.with_children(|parent| {

// 头像
parent.spawn(ImageBundle {
image: UiImage::new(images.white_avatar.clone()),
Expand Down Expand Up @@ -221,7 +220,6 @@ pub fn setup_running(
..default()
});


// 计时器

// 机器评分
Expand All @@ -237,117 +235,3 @@ pub fn cleanup_chessbroad(mut commands: Commands, entity: ResMut<public::EntityR
.entity(entity.chessbroad.unwrap())
.despawn_recursive();
}

// 棋子坐标配置
pub const PIECE_POS_MAP: [[(f32, f32); 9]; 10] = [
[
(70_f32, 100_f32),
(138_f32, 100_f32),
(207_f32, 100_f32),
(275_f32, 100_f32),
(344_f32, 100_f32),
(413_f32, 100_f32),
(480_f32, 100_f32),
(549_f32, 100_f32),
(618_f32, 100_f32),
],
[
(70_f32, 168_f32),
(138_f32, 168_f32),
(207_f32, 168_f32),
(275_f32, 168_f32),
(344_f32, 168_f32),
(413_f32, 168_f32),
(480_f32, 168_f32),
(549_f32, 168_f32),
(618_f32, 168_f32),
],
[
(70_f32, 236_f32),
(138_f32, 236_f32),
(207_f32, 236_f32),
(275_f32, 236_f32),
(344_f32, 236_f32),
(413_f32, 236_f32),
(480_f32, 236_f32),
(549_f32, 236_f32),
(618_f32, 236_f32),
],
[
(70_f32, 304_f32),
(138_f32, 304_f32),
(207_f32, 304_f32),
(275_f32, 304_f32),
(344_f32, 304_f32),
(413_f32, 304_f32),
(480_f32, 304_f32),
(549_f32, 304_f32),
(618_f32, 304_f32),
],
[
(70_f32, 372_f32),
(138_f32, 372_f32),
(207_f32, 372_f32),
(275_f32, 372_f32),
(344_f32, 372_f32),
(413_f32, 372_f32),
(480_f32, 372_f32),
(549_f32, 372_f32),
(618_f32, 372_f32),
],
[
(70_f32, 440_f32),
(138_f32, 440_f32),
(207_f32, 440_f32),
(275_f32, 440_f32),
(344_f32, 440_f32),
(413_f32, 440_f32),
(480_f32, 440_f32),
(549_f32, 440_f32),
(618_f32, 440_f32),
],
[
(70_f32, 508_f32),
(138_f32, 508_f32),
(207_f32, 508_f32),
(275_f32, 508_f32),
(344_f32, 508_f32),
(413_f32, 508_f32),
(480_f32, 508_f32),
(549_f32, 508_f32),
(618_f32, 508_f32),
],
[
(70_f32, 576_f32),
(138_f32, 576_f32),
(207_f32, 576_f32),
(275_f32, 576_f32),
(344_f32, 576_f32),
(413_f32, 576_f32),
(480_f32, 576_f32),
(549_f32, 576_f32),
(618_f32, 576_f32),
],
[
(70_f32, 644_f32),
(138_f32, 644_f32),
(207_f32, 644_f32),
(275_f32, 644_f32),
(344_f32, 644_f32),
(413_f32, 644_f32),
(480_f32, 644_f32),
(549_f32, 644_f32),
(618_f32, 644_f32),
],
[
(70_f32, 712_f32),
(138_f32, 712_f32),
(207_f32, 712_f32),
(275_f32, 712_f32),
(344_f32, 712_f32),
(413_f32, 712_f32),
(480_f32, 712_f32),
(549_f32, 712_f32),
(618_f32, 712_f32),
],
];
18 changes: 9 additions & 9 deletions src/chess/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,50 @@ pub fn make_chess_buttons(parent: &mut ChildBuilder, font: Handle<Font>) {
font.clone(),
GAME_MENU_NEW_GAME_TEXT,
ChessButton::NewGame,
60_f32,
8_f32,
);

make_text_bundle(
parent,
font.clone(),
GAME_MENU_RETRACT_TEXT,
ChessButton::Retract,
150_f32,
20_f32,
);
make_text_bundle(
parent,
font.clone(),
GAME_MENU_PEACE_TEXT,
ChessButton::Peact,
240_f32,
32_f32,
);
make_text_bundle(
parent,
font.clone(),
GAME_MENU_PROMPT_TEXT,
ChessButton::Prompt,
330_f32,
44_f32,
);
make_text_bundle(
parent,
font.clone(),
GAME_MENU_ADMIT_DEFEAT_TEXT,
ChessButton::AdmitDefeat,
420_f32,
56_f32,
);
make_text_bundle(
parent,
font.clone(),
GAME_MENU_SWAP_TEXT,
ChessButton::Swap,
510_f32,
68_f32,
);
make_text_bundle(
parent,
font.clone(),
GAME_MENU_ROLL_TEXT,
ChessButton::Roll,
600_f32,
80_f32,
);
}

Expand All @@ -101,8 +101,8 @@ pub fn make_text_bundle(
style: Style {
width: Val::Px(90_f32),
height: Val::Px(50_f32),
left: Val::Px(left_px),
bottom: Val::Px(20_f32),
left: Val::Percent(left_px),
bottom: Val::Percent(3_f32),
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()
Expand Down
15 changes: 8 additions & 7 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum MainMenu {
// 退出游戏
ExitGame,
}

fn make_main_menu_text_bundle(
parent: &mut ChildBuilder,
font: Handle<Font>,
Expand All @@ -44,7 +45,7 @@ fn make_main_menu_text_bundle(
style: Style {
width: Val::Px(204_f32),
height: Val::Px(65_f32),
top: Val::Px(top_px),
top: Val::Percent(top_px),
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()
Expand Down Expand Up @@ -85,42 +86,42 @@ pub fn setup_pending(mut commands: Commands, fonts: Res<public::asset::Fonts>) {
fonts.xiaoli.clone(),
MAIN_MENU_CONTINUE_GAME_TEXT,
MainMenu::ContinueGame,
140_f32,
16_f32,
);
make_main_menu_text_bundle(
parent,
fonts.xiaoli.clone(),
MAIN_MENU_AI_GAME_TEXT,
MainMenu::NewAiGame,
230_f32,
26_f32,
);
make_main_menu_text_bundle(
parent,
fonts.xiaoli.clone(),
MAIN_MENU_INTER_GAME_TEXT,
MainMenu::NewInterGame,
320_f32,
36_f32,
);
make_main_menu_text_bundle(
parent,
fonts.xiaoli.clone(),
MAIN_MENU_DEDUCE_GAME_TEXT,
MainMenu::NewDeduceGame,
410_f32,
46_f32,
);
make_main_menu_text_bundle(
parent,
fonts.xiaoli.clone(),
MAIN_MENU_SETTING_GAME_TEXT,
MainMenu::SettingGame,
500_f32,
56_f32,
);
make_main_menu_text_bundle(
parent,
fonts.xiaoli.clone(),
MAIN_MENU_EXIT_GAME_TEXT,
MainMenu::ExitGame,
590_f32,
66_f32,
);
})
.id();
Expand Down
9 changes: 7 additions & 2 deletions src/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ pub const WIN_SIZE: Size = Size {
w: 1280_f32,
h: 842_f32,
};
pub const PIECE_SIZE: Size = Size { w: 76_f32, h: 77_f32 };
pub const ROUTE_OFFSET: (u8, u8) = (97, 48);


pub fn get_piece_render_percent(row: usize, col: usize) -> (f32, f32) {
(
9.9_f32 + (col as f32 * 8.8),
12_f32 + (row as f32 * 8.0)
)
}

#[derive(Resource)]
pub struct EntityResources {
pub main_menus: Option<Entity>,
Expand Down
22 changes: 19 additions & 3 deletions src/setup/image.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use bevy::prelude::*;
use bevy::{prelude::*, window::WindowResized};

use crate::public;

#[derive(Component)]
pub struct Background;

pub fn loading(mut commands: Commands, asset_server: Res<AssetServer>) {
trace!("loading asset images");

Expand All @@ -21,7 +24,7 @@ pub fn loading(mut commands: Commands, asset_server: Res<AssetServer>) {
};

// 背景
commands.spawn(SpriteBundle {
commands.spawn((SpriteBundle {
texture: images.background.clone(),
sprite: Sprite {
custom_size: Some(Vec2 {
Expand All @@ -31,7 +34,7 @@ pub fn loading(mut commands: Commands, asset_server: Res<AssetServer>) {
..Default::default()
},
..Default::default()
});
}, Background));

commands.insert_resource(images);

Expand Down Expand Up @@ -97,3 +100,16 @@ pub fn loading(mut commands: Commands, asset_server: Res<AssetServer>) {
};
commands.insert_resource(pieces);
}

pub fn on_window_resize(
mut q: Query<&mut Sprite, With<Background>>,
mut resize_events: EventReader<WindowResized>,
) {
let mut bg = q.single_mut();
for e in resize_events.iter() {
bg.custom_size = Some(Vec2 {
x: e.width,
y: e.height,
})
}
}
2 changes: 1 addition & 1 deletion src/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Plugin for AssetLoading {
image::loading,
animate::loading,
),
);
).add_systems(Update, image::on_window_resize);
}
}

Expand Down

0 comments on commit 4ca4a89

Please sign in to comment.