Skip to content

Commit

Permalink
Updated to Bevy 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanma committed Aug 5, 2022
1 parent 1deeb7c commit 8f9b329
Show file tree
Hide file tree
Showing 13 changed files with 557 additions and 439 deletions.
877 changes: 499 additions & 378 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ keywords = ["gamedev", "bevy", "roguelike"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "=0.7.0", features = ["dynamic"] }
# bevy = "=0.7.0"
bevy = { version = "=0.8.0", features = ["dynamic"] }
# bevy = "=0.8.0"
bracket-lib = "=0.8.1"
rand = "=0.8.5"
serde = "=1.0.136"
ron = "=0.7.0"
serde = "=1.0.142"
ron = "=0.7.1"

[workspace]
resolver = "2" # Important! wgpu/Bevy needs this!
2 changes: 1 addition & 1 deletion assets/template.ron
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Templates(
name: "Healing Potion", glyph: 'P', levels: [0, 1, 2],
description: Some("Heals 6 Health Points."),
provides: Some([ ("Healing", 6) ]),
frequency: 0
frequency: 1
),
Template(
entity_type: Item,
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod ui;
mod prelude {
pub use bevy::prelude::*;
pub use bevy::winit::WinitSettings;
pub use bevy::render::texture::ImageSettings;
pub use bracket_lib::prelude::*;
pub const SCREEN_WIDTH: i32 = 80;
pub const SCREEN_HEIGHT: i32 = 80;
Expand Down Expand Up @@ -42,12 +43,10 @@ fn setup(
commands.insert_resource(CharsetAsset { atlas: texture_atlas_handle.clone() });

// Add a 2D Camera
let mut cam = OrthographicCameraBundle::new_2d();
let mut cam = Camera2dBundle::default();
cam.transform.scale = Vec3::new(0.5, 0.5, 1.0);
commands.spawn_bundle(cam)
.insert(MainCamera);
// UI camera
commands.spawn_bundle(UiCameraBundle::default());
}


Expand All @@ -62,6 +61,7 @@ fn main() {
})
// Power-saving reactive rendering for applications.
.insert_resource(WinitSettings::desktop_app())
.insert_resource(ImageSettings::default_nearest())
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins)
.add_state(TurnState::StartScreen)
Expand Down
32 changes: 17 additions & 15 deletions src/renderutils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::prelude::*;

pub fn size_scaling(windows: Res<Windows>, mut q: Query<(&TileSize, &mut Transform)>) {
let window = windows.get_primary().unwrap();
for (sprite_size, mut transform) in q.iter_mut() {
let scale = Vec3::new(
sprite_size.width / SCREEN_WIDTH as f32 * window.width() as f32,
sprite_size.height / SCREEN_HEIGHT as f32 * window.height() as f32,
1.0,
);
transform.scale = scale;
if let Some(window) = windows.get_primary() {
for (sprite_size, mut transform) in q.iter_mut() {
let scale = Vec3::new(
sprite_size.width / SCREEN_WIDTH as f32 * window.width() as f32,
sprite_size.height / SCREEN_HEIGHT as f32 * window.height() as f32,
1.0,
);
transform.scale = scale;
}
}
}

Expand All @@ -18,12 +19,13 @@ pub fn convert_pos(pos: f32, bound_window: f32, bound_game: f32) -> f32 {
}

pub fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Transform)>) {
let window = windows.get_primary().unwrap();
for (pos, mut transform) in q.iter_mut() {
transform.translation = Vec3::new(
convert_pos(pos.x as f32, window.width() as f32, SCREEN_WIDTH as f32),
convert_pos((pos.y+UI_HEIGHT/2) as f32, window.height() as f32, SCREEN_HEIGHT as f32),
pos.z as f32,
);
if let Some(window) = windows.get_primary() {
for (pos, mut transform) in q.iter_mut() {
transform.translation = Vec3::new(
convert_pos(pos.x as f32, window.width() as f32, SCREEN_WIDTH as f32),
convert_pos((pos.y+UI_HEIGHT/2) as f32, window.height() as f32, SCREEN_HEIGHT as f32),
pos.z as f32,
);
}
}
}
1 change: 0 additions & 1 deletion src/systems/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub fn camera_move(
for player_position in player_query.iter() {
let mut camera_transform = camera_query.single_mut();
// get camera transform and window
//let mut camera_transform = camera_query.single_mut().unwrap();
let window = windows.get_primary().unwrap();
// calculate new coordinates and update
let cam_x = convert_pos(player_position.x as f32, window.width() as f32, SCREEN_WIDTH as f32);
Expand Down
3 changes: 1 addition & 2 deletions src/systems/chasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ pub fn chasing(
1024.0
);

movers.iter()
.for_each(| (entity, pos, fov) | {
movers.iter().for_each(| (entity, pos, fov) | {
// if monster cannot see player, then just return and do nothing
if !fov.visible_tiles.contains( &((*player_pos).into()) ) {
return;
Expand Down
5 changes: 2 additions & 3 deletions src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl Plugin for AwaitingInputPlugin {
.add_system_set(
SystemSet::on_update(TurnState::AwaitingInput)
.label("awaiting_input")
//.with_system(player_input::player_input)
.with_system(camera::camera_move)
.with_system(fov::fov)
.with_system(update_entities_visibility::update_entities_visibility)
Expand All @@ -32,7 +31,7 @@ impl Plugin for PlayerPlugin {
fn build(&self, app: &mut App) {
app
.add_system_set(
SystemSet::on_enter(TurnState::PlayerTurn)
SystemSet::on_update(TurnState::PlayerTurn)
.label("player")
.with_system(use_items::use_items)
.with_system(combat::combat)
Expand All @@ -49,7 +48,7 @@ impl Plugin for MonsterPlugin {
app
//.add_stage_after(CoreStage::Update, "enemies_state", SystemStage::parallel())
.add_system_set(
SystemSet::on_enter(TurnState::MonsterTurn)
SystemSet::on_update(TurnState::MonsterTurn)
.label("enemies")
.with_system(chasing::chasing.after("player_end").label("chasing"))
.with_system(combat::combat.after("chasing").label("combat"))
Expand Down
7 changes: 6 additions & 1 deletion src/systems/player_input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use bevy::app::AppExit;

pub fn player_input(
mut commands: Commands,
Expand All @@ -7,7 +8,8 @@ pub fn player_input(
player_position: Query<(Entity, &Position), With<Player>>,
enemies: Query<(Entity, &Position), With<Enemy>>,
items: Query<(Entity, &Position, &Naming), With<Item>>,
mut turn_state: ResMut<State<TurnState>>
mut turn_state: ResMut<State<TurnState>>,
mut exit: EventWriter<AppExit>
) {

let (player_ent, pos) = player_position.single();
Expand Down Expand Up @@ -45,6 +47,9 @@ pub fn player_input(
turn_state.push(TurnState::EquipmentPopup).unwrap();
action = false;
}
KeyCode::Escape => {
exit.send(AppExit);
}
_ => wait = true,
}

Expand Down
30 changes: 13 additions & 17 deletions src/ui/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn bottom_hud(
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(50.0), Val::Percent(100.0)),
border: Rect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(5.0)),
..Default::default()
},
color: UiColor(Color::rgb(0.65, 0.65, 0.65)),
Expand All @@ -62,7 +62,7 @@ fn bottom_hud(
parent.spawn_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
margin: Rect::all(Val::Px(5.0)),
margin: UiRect::all(Val::Px(5.0)),
..Default::default()
},
// Use `Text` directly
Expand Down Expand Up @@ -117,7 +117,7 @@ fn bottom_hud(
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(50.0), Val::Percent(100.0)),
border: Rect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(5.0)),
..Default::default()
},
color: Color::rgb(0.65, 0.65, 0.65).into(),
Expand Down Expand Up @@ -166,22 +166,21 @@ fn bottom_hud(
style: Style {
// Set height to font size * number of text lines
size: Size::new(Val::Auto, Val::Px(20. * 1.)),
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
bottom: Val::Auto,
top: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
"HP: 17 / 20".to_string(),
TextStyle {
font_size: 20.0,
font: font.clone(),
color: Color::rgb(0.99, 0.99, 0.99),
},
Default::default(),
),
..Default::default()
})
Expand All @@ -192,8 +191,8 @@ fn bottom_hud(
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(63.0), Val::Px(20. * 1.)),
border: Rect::all(Val::Px(5.0)),
margin: Rect {
border: UiRect::all(Val::Px(5.0)),
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
bottom: Val::Auto,
Expand Down Expand Up @@ -246,22 +245,21 @@ fn bottom_hud(
// Set height to font size * number of text lines
size: Size::new(Val::Auto, Val::Px(20. * 1.)),
// Set left margin to auto to push the text to the right
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
bottom: Val::Auto,
top: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
"(I)nventory".to_string(),
TextStyle {
font_size: 20.0,
font: font.clone(),
color: Color::rgb(0.99, 0.99, 0.99),
},
Default::default(),
),
..Default::default()
})
Expand All @@ -287,22 +285,21 @@ fn bottom_hud(
// Set height to font size * number of text lines
size: Size::new(Val::Auto, Val::Px(20. * 1.)),
// Set left margin to auto to push the text to the right
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
bottom: Val::Auto,
top: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
"(E)quipment".to_string(),
TextStyle {
font_size: 20.0,
font: font.clone(),
color: Color::rgb(0.99, 0.99, 0.99),
},
Default::default(),
),
..Default::default()
})
Expand Down Expand Up @@ -337,22 +334,21 @@ fn bottom_hud(
// Set height to font size * number of text lines
size: Size::new(Val::Auto, Val::Px(20. * 1.)),
// Set left margin to auto to push the text to the right
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
bottom: Val::Auto,
top: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
"Dungeon Level: 1".to_string(),
TextStyle {
font_size: 20.0,
font: font.clone(),
color: Color::rgb(0.99, 0.99, 0.99),
},
Default::default(),
),
..Default::default()
})
Expand Down
18 changes: 8 additions & 10 deletions src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ fn popup_ui(
style: Style {
size: Size::new(Val::Percent(50.), Val::Percent(50.)),
position_type: PositionType::Absolute,
position: Rect {
position: UiRect {
left: Val::Percent(25.0),
bottom: Val::Percent(30.0),
..Default::default()
},
border: Rect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(5.0)),
..Default::default()
},
color: UiColor(Color::rgb(0.65, 0.65, 0.65)),
Expand Down Expand Up @@ -80,22 +80,21 @@ fn popup_ui(
parent.spawn_bundle(TextBundle {
style: Style {
size: Size::new(Val::Auto, Val::Px(50. * 1.)),
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
title.to_string(),
TextStyle {
font_size: 50.0,
font: font.clone(),
color: Color::GOLD,
},
Default::default(),
),
..Default::default()
});
Expand Down Expand Up @@ -126,7 +125,7 @@ fn popup_ui(
parent.spawn_bundle(TextBundle {
style: Style {
size: Size::new(Val::Auto, Val::Px(20. * (INVENTORY_SLOTS+1) as f32)),
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
Expand All @@ -149,7 +148,7 @@ fn popup_ui(
style: Style {
size: Size::new(Val::Percent(100.0), Val::Auto),
flex_direction: FlexDirection::ColumnReverse,
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
Expand All @@ -164,22 +163,21 @@ fn popup_ui(
parent.spawn_bundle(TextBundle {
style: Style {
size: Size::new(Val::Auto, Val::Px(20.)),
margin: Rect {
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
},
..Default::default()
},
text: Text::with_section(
text: Text::from_section(
" ".to_string(),
TextStyle {
font_size: 20.0,
font: font.clone(),
color: Color::WHITE,
},
Default::default(),
),
..Default::default()
})
Expand Down
Loading

0 comments on commit 8f9b329

Please sign in to comment.