save
Some checks failed
Rust Checks / checks (push) Failing after 5m46s
Rust Checks / checks (pull_request) Failing after 3m9s

This commit is contained in:
CoCo_Sol 2024-02-26 22:35:05 +01:00
parent 0d336d2f85
commit 965a268bfd
5 changed files with 90 additions and 74 deletions

75
Cargo.lock generated
View file

@ -1317,9 +1317,8 @@ dependencies = [
"bevy", "bevy",
"bevy_egui", "bevy_egui",
"noise", "noise",
"num",
"partial-min-max",
"paste", "paste",
"rand 0.8.5",
] ]
[[package]] [[package]]
@ -3019,40 +3018,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af"
dependencies = [
"num-bigint",
"num-complex",
"num-integer",
"num-iter",
"num-rational",
"num-traits",
]
[[package]]
name = "num-bigint"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.3.3" version = "0.3.3"
@ -3064,38 +3029,6 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-iter"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d869c01cc0c455284163fd0092f1f93835385ccab5a98a0dcc497b2f8bf055a9"
dependencies = [
"autocfg",
"num-integer",
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.18" version = "0.2.18"
@ -3353,12 +3286,6 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "partial-min-max"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6448add382c60bbbc64f9dab41309a12ec530c05191601042f911356ac09758c"
[[package]] [[package]]
name = "paste" name = "paste"
version = "1.0.14" version = "1.0.14"

View file

@ -15,3 +15,4 @@ bevy = "0.12.1"
bevy_egui = "0.24.0" bevy_egui = "0.24.0"
noise = "0.8.2" noise = "0.8.2"
paste = "1.0.14" paste = "1.0.14"
rand = "0.8.5"

View file

@ -1,11 +1,43 @@
//! The main entry point of the game. //! The main entry point of the game.
use bevy::prelude::*; use bevy::prelude::*;
use border_wars::map::generation::{EndMapGeneration, MapGenerationPlugin, StartMapGeneration};
use border_wars::map::renderer::RendererPlugin;
use border_wars::scenes::ScenesPlugin; use border_wars::scenes::ScenesPlugin;
use rand::Rng;
fn setup(mut writer: EventWriter<StartMapGeneration>) {
writer.send(StartMapGeneration {
seed: 9876,
radius: 7,
});
}
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn update(mut writer: EventWriter<StartMapGeneration>, mut reader: EventReader<EndMapGeneration>) {
for _ in reader.read() {
let rdm_seed = rand::thread_rng().gen_range(0..1000000);
writer.send(StartMapGeneration {
seed: rdm_seed,
radius: 10,
});
}
}
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(ScenesPlugin) .add_plugins(ScenesPlugin)
.add_plugins(RendererPlugin)
.add_plugins(MapGenerationPlugin)
.add_systems(OnEnter(border_wars::CurrentScene::Game), setup)
.add_systems(Startup, setup_camera)
.add_systems(
Update,
update.run_if(in_state(border_wars::CurrentScene::Game)),
)
.run(); .run();
} }

View file

@ -2,6 +2,7 @@
pub mod generation; pub mod generation;
pub mod hex; pub mod hex;
pub mod renderer;
use bevy::prelude::*; use bevy::prelude::*;

View file

@ -0,0 +1,55 @@
//! All functions related to the rendering of the map.
use bevy::prelude::*;
use crate::map::{Tile, TilePosition};
const IMAGE_WIDTH: f32 = 92.;
const OFFSET: f32 = 4.;
const IMAGE_HEIGHT: f32 = 40.;
/// A plugin to render the map.
pub struct RendererPlugin;
impl Plugin for RendererPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
render_map.run_if(in_state(crate::CurrentScene::Game)),
);
}
}
impl Tile {
/// Returns the texture handle of the tile.
pub fn get_texture(&self, asset_server: &AssetServer) -> Handle<Image> {
match self {
Tile::Grass => asset_server.load("tiles/grass.png"),
Tile::Forest => asset_server.load("tiles/forest.png"),
Tile::Hill => asset_server.load("tiles/hill.png"),
}
}
}
/// Renders the map.
fn render_map(
query: Query<(Entity, &TilePosition, &Tile), Changed<Tile>>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
for (entity, position, tile) in query.iter() {
let pixel_position_ratio = position.to_pixel_coordinates();
let position_x = (IMAGE_WIDTH / 2. + OFFSET) * pixel_position_ratio.x;
let position_y = pixel_position_ratio.y * (IMAGE_WIDTH / 3. + OFFSET);
commands.entity(entity).insert(SpriteBundle {
transform: Transform::from_translation(Vec3 {
x: position_x,
y: position_y,
z: -position_y,
}),
texture: tile.get_texture(&*asset_server),
..default()
});
}
}