Skip to content

Commit

Permalink
Update to bevy 0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Troels51 committed Oct 10, 2023
1 parent 776017a commit 7efca02
Show file tree
Hide file tree
Showing 10 changed files with 1,779 additions and 1,564 deletions.
3,196 changes: 1,710 additions & 1,486 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 5 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,19 @@ opt-level = 1
lto = true
codegen-units = 1

[features]
default = [
"bevy/bevy_winit",
"bevy/render",
"bevy/png",
"bevy/x11",
"bevy_kira_audio/ogg"
]

dev = [
"bevy/dynamic",
]

[dependencies]
bevy = { version = "0.8", default-features = false }
bevy_kira_audio = { version = "0.11" }
bevy_asset_loader = { version = "0.12" }
#bevy_mod_picking = "0.6.1"
#bevy_4x_camera = {git = "https://github.com/Troels51/bevy_4x_camera.git", branch = "master"}
bevy = { version = "0.11" }
bevy_asset_loader = { version = "0.17" }
typetag = "0.1"
serde = "1.0"
rand = "0.8.3"
hex2d = "1.1.0"
bevy_common_assets = { version = "0.3.0", features = ["ron"]}
#bevy_editor_pls = {git = "https://github.com/jakobhellermann/bevy_editor_pls.git", branch = "main"}
bevy_egui = "0.15"
bevy_common_assets = { version = "0.7.0", features = ["ron"]}
bevy_egui = "0.22"


[target.'cfg(target_os = "linux")'.dependencies]
winit = { version = "0.25", features=["x11"]}
winit = { version = "0.28", features=["x11"]}

[build-dependencies]
embed-resource = "1.4"
6 changes: 3 additions & 3 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::{prelude::Component, reflect::TypeUuid};
use bevy::{prelude::{Component, Resource}, reflect::{TypeUuid, TypePath}};
use hex2d::Angle;
use serde::{Deserialize, Serialize};

#[derive(Debug)]
#[derive(Debug, Resource)]
pub struct Board {
hexes: Vec<Vec<Hex>>,
possible_hexes: Vec<Hex>,
Expand Down Expand Up @@ -51,7 +51,7 @@ fn match_sides(side1: &HexSides, side2: &HexSides) -> bool {
false
}

#[derive(Serialize, Deserialize, TypeUuid, Debug, Clone, PartialEq, Component)]
#[derive(Serialize, Deserialize, TypeUuid, Debug, Clone, PartialEq, Component, TypePath)]
#[uuid = "64271346-f11a-4736-ae17-6876acf29761"]
pub struct Hex {
pub sides: HexSides,
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use bevy::prelude::*;
use ui::UIPlugin;
use world::WorldPlugin;

#[derive(Clone, Eq, PartialEq, Debug, Hash)]
#[derive(Clone, Eq, PartialEq, Debug, Hash, States, Default)]
pub enum GameState {
#[default]
Loading,
Playing,
GameOver,
Expand All @@ -22,10 +23,10 @@ pub struct GamePlugin;

impl Plugin for GamePlugin {
fn build(&self, app: &mut App) {
app.add_state(GameState::Loading)
.add_plugin(LoadingPlugin)
.add_plugin(UIPlugin)
.add_plugin(WorldPlugin);
app.add_state::<GameState>()
.add_plugins(LoadingPlugin)
.add_plugins(UIPlugin)
.add_plugins(WorldPlugin);
#[cfg(debug_assertions)]
{
// app.add_plugin(FrameTimeDiagnosticsPlugin::default())
Expand Down
8 changes: 4 additions & 4 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ pub struct LoadingPlugin;

impl Plugin for LoadingPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(RonAssetPlugin::<Hex>::new(&["hex"]));
app.add_plugins(RonAssetPlugin::<Hex>::new(&["hex"]));
app.add_loading_state(
LoadingState::new(GameState::Loading)
.continue_to_state(GameState::Playing)
.with_collection::<hex_models::HexImageAssets>()
.with_collection::<hex_descriptions::HexDescriptions>(),
);
)
.add_collection_to_loading_state::<_, hex_models::HexImageAssets>(GameState::Loading)
.add_collection_to_loading_state::<_, hex_descriptions::HexDescriptions>(GameState::Loading);
}
}
4 changes: 2 additions & 2 deletions src/loading/hex_descriptions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::asset::HandleUntyped;
use bevy::ecs::world::World;
use bevy::prelude::{AssetServer, Component, Handle};
use bevy::prelude::{AssetServer, Component, Handle, Resource};
use bevy_asset_loader::prelude::AssetCollection;
use serde::{Deserialize, Serialize};

Expand All @@ -11,7 +11,7 @@ use bevy::ecs::world::Mut;
pub struct Angle(hex2d::Angle);

// Loading a folder doesnt work for web. Maybe we need to figure out a way to get both?
#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct HexDescriptions {
//#[asset(path = "hexes/blank.hex")]
//pub bland: Handle<Hex>,
Expand Down
2 changes: 1 addition & 1 deletion src/loading/hex_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Index;
use bevy::prelude::*;
use bevy_asset_loader::prelude::AssetCollection;

#[derive(AssetCollection)]
#[derive(AssetCollection, Resource)]
pub struct HexImageAssets {
#[asset(path = "textures/blank.png")]
pub blank: Handle<Image>,
Expand Down
26 changes: 16 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use bevy::prelude::{App, Msaa, WindowDescriptor};
use bevy::prelude::{App, Msaa, PluginGroup, default};
use bevy::DefaultPlugins;
use bevy::window::{WindowPlugin, Window};
use hex_map_bevy::GamePlugin;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.insert_resource(WindowDescriptor {
title: "Map of hexes".to_string(),
width: 1920.,
height: 1200.,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(GamePlugin)
.insert_resource(Msaa::Off)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Hex Map".to_string(),
resolution: (800., 600.).into(),
// Bind to canvas included in `index.html`
canvas: Some("#bevy".to_owned()),
// Tells wasm not to override default event handling, like F5 and Ctrl+R
prevent_default_event_handling: false,
..default()
}),
..default()
}))
.add_plugins(GamePlugin)
.run()
}
12 changes: 6 additions & 6 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::{App, EventWriter, OrthographicProjection, Plugin, Query, ResMut};
use bevy_egui::{egui, EguiContext, EguiPlugin};
use bevy::prelude::{App, EventWriter, OrthographicProjection, Plugin, Query, ResMut, Resource, Update};
use bevy_egui::{egui, EguiContext, EguiPlugin, EguiContexts};

use crate::world::BoardGenerateEvent;

Expand All @@ -9,18 +9,18 @@ pub struct UIPlugin;
/// Player logic is only active during the State `GameState::Playing`
impl Plugin for UIPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(EguiPlugin)
app.add_plugins(EguiPlugin)
.insert_resource::<UiState>(UiState { board_size: 10 })
.add_system(setup_egui);
.add_systems(Update, setup_egui);
}
}
#[derive(Default)]
#[derive(Default, Resource)]
pub struct UiState {
pub board_size: u32,
}

fn setup_egui(
mut egui_context: ResMut<EguiContext>,
mut egui_context: EguiContexts,
mut ui_state: ResMut<UiState>,
mut generate_board_events: EventWriter<BoardGenerateEvent>,
mut projection_query: Query<&mut OrthographicProjection>,
Expand Down
51 changes: 26 additions & 25 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::app::App;
use bevy::render::camera::Camera;
use bevy::{ecs::schedule::SystemSet, prelude::*};

use hex2d::{self, Coordinate, Spacing, Spin};
use hex2d::{self, Coordinate, Spin};
use rand::prelude::IteratorRandom;

use crate::board::{Board, Hex};
Expand All @@ -17,16 +17,14 @@ pub struct WorldPlugin;
impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(Board::new(BSIZE, BSIZE))
.insert_resource(Spacing::FlatTop(450f32))
.add_system_set(SystemSet::on_enter(GameState::Playing).with_system(setup))
.add_system_set(SystemSet::on_exit(GameState::Playing).with_system(teardown))
.add_system_set(SystemSet::on_exit(GameState::GameOver).with_system(teardown))
.insert_resource(Spacing(hex2d::Spacing::FlatTop(450f32)))
.add_systems(OnEnter(GameState::Playing),setup)
.add_systems(OnExit(GameState::Playing), teardown)
.add_systems(OnExit(GameState::GameOver), teardown)
.add_event::<BoardGenerateEvent>()
.add_system_set(
SystemSet::on_update(GameState::Playing).with_system(player_camera_control),
)
.add_system_set(SystemSet::on_update(GameState::Playing).with_system(generate_board))
.add_system_set(SystemSet::on_update(GameState::Playing).with_system(keyboard_react));
.add_systems(Update, player_camera_control.run_if(in_state(GameState::Playing)))
.add_systems(Update, generate_board.run_if(in_state(GameState::Playing)))
.add_systems(Update, keyboard_react.run_if(in_state(GameState::Playing)));
}
}

Expand All @@ -35,7 +33,7 @@ impl Plugin for WorldPlugin {

const BSIZE: usize = 1000;

#[derive(Default)]
#[derive(Default, Event)]
pub struct BoardGenerateEvent;

#[derive(Component)]
Expand All @@ -44,32 +42,35 @@ struct HexTag;
#[derive(Debug, Clone, Copy, Component)]
pub struct CellCoord(hex2d::Coordinate);

#[derive(Resource)]
struct Spacing(hex2d::Spacing);

fn setup(
mut commands: Commands,
hex_model_assets: Res<HexImageAssets>,
hex_desc_assets: Res<Assets<Hex>>,
spacing: Res<Spacing<f32>>,
spacing: Res<Spacing>,
mut board: ResMut<Board>,
ui_state: Res<UiState>,
) {
for (_handle, hex) in hex_desc_assets.iter() {
board.add_possible_hex(hex);
}
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.3,
});
// commands.insert_resource(AmbientLight {
// color: Color::WHITE,
// brightness: 0.3,
// });

let center = Coordinate::new(100, 100);
let center_pixel = center.to_pixel(*spacing);
let center_pixel = center.to_pixel(spacing.0);

//Spawn camera
let mut camera = Camera2dBundle {
transform: Transform::from_xyz(center_pixel.0, center_pixel.1, 0f32),
..Default::default()
};
camera.projection.scale = ui_state.board_size as f32;
commands.spawn_bundle(camera);
commands.spawn(camera);

spawn_board(
center,
Expand All @@ -85,16 +86,16 @@ fn spawn_board(
center: Coordinate,
mut board: ResMut<Board>,
hex_model_assets: Res<HexImageAssets>,
spacing: Res<Spacing<f32>>,
spacing: Res<Spacing>,
mut commands: Commands,
board_size: u32,
) {
let center_pixel = center.to_pixel(*spacing);
let center_pixel = center.to_pixel(spacing.0);
// spawn the game board
for ring_radius in 0..board_size as i32 {
let ring = center.ring_iter(ring_radius, Spin::CW(hex2d::Direction::YZ));
for cell_coord in ring {
let pixel = cell_coord.to_pixel(*spacing);
let pixel = cell_coord.to_pixel(spacing.0);
let posibility_space = board.get_possible_hexes_for_coordinate(cell_coord);
if let Some(hex) = posibility_space.iter().choose(&mut rand::thread_rng()) {
board.set(cell_coord, hex.clone());
Expand All @@ -107,7 +108,7 @@ fn spawn_board(
));

commands
.spawn_bundle(SpriteBundle {
.spawn(SpriteBundle {
texture: model.clone(),
transform,
..default()
Expand All @@ -120,7 +121,7 @@ fn spawn_board(
let transform =
Transform::from_xyz(pixel.0, 2f32 * center_pixel.1 - pixel.1, 0.0f32);
commands
.spawn_bundle(SpriteBundle {
.spawn(SpriteBundle {
texture: model.clone(),
transform,
..default()
Expand All @@ -135,10 +136,10 @@ fn generate_board(
mut board: ResMut<Board>,
hex_model_assets: Res<HexImageAssets>,
hex_entities: Query<Entity, With<HexTag>>,
spacing: Res<Spacing<f32>>,
spacing: Res<Spacing>,
mut commands: Commands,
ui_state: Res<UiState>,
generate_board_events: EventReader<BoardGenerateEvent>,
mut generate_board_events: EventReader<BoardGenerateEvent>,
) {
if !generate_board_events.is_empty() {
//trigger despawn of board
Expand Down

0 comments on commit 7efca02

Please sign in to comment.