Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rewin123 committed Dec 6, 2023
2 parents 488b110 + c76cf66 commit 9e00562
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 65 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ lto = "thin"
[features]
dev = [
"bevy/dynamic_linking",
"dep:bevy-inspector-egui"
]
default = ["dev"]

# All of Bevy's default features exept for the audio related ones (bevy_audio, vorbis), since they clash with bevy_kira_audio
# and android_shared_stdcxx, since that is covered in `mobile`
Expand Down Expand Up @@ -63,7 +65,7 @@ webbrowser = { version = "0.8", features = ["hardened"] }
winit = { version = "0.28.7", default-features = false }
image = { version = "0.24", default-features = false }
bevy_egui = "0.23.0"
bevy-inspector-egui = "0.21.0"
bevy-inspector-egui = {version = "0.21.0", optional = true }

[build-dependencies]
embed-resource = "1.4"
5 changes: 3 additions & 2 deletions src/debug_diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ impl Plugin for DiagnosticPlugin {
)
.chain(),
)
.add_systems(Update, (fps_counting, sheep_counter_text))
.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default());
.add_systems(Update, (fps_counting, sheep_counter_text));
#[cfg(feature = "dev")]
app.add_plugins(bevy_inspector_egui::quick::WorldInspectorPlugin::default());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy::{input::mouse::MouseWheel, pbr::ExtendedMaterial, prelude::*, window::PrimaryWindow};
use bevy::{input::mouse::MouseWheel, prelude::*, window::PrimaryWindow};

use crate::{
get_sprite_rotation,
physics::Velocity,
sprite_material::{SpriteExtension, SpriteMaterial, create_plane_mesh},
sprite_material::{create_plane_mesh, SpriteExtension, SpriteMaterial},
};

const DOG_PATH: &str = "test/dog.png";
Expand Down Expand Up @@ -78,7 +78,7 @@ fn spawn_player_by_event(
mut event_reader: EventReader<SpawnPlayer>,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
_materials: ResMut<Assets<StandardMaterial>>,
mut sprite_material: ResMut<Assets<SpriteMaterial>>,
) {
for event in event_reader.read() {
Expand Down
26 changes: 18 additions & 8 deletions src/safe_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ impl SafeArea {

dx.abs() < size.x / 2.0 && dy.abs() < size.y / 2.0
}
SafeArea::Ellipse { pos1, pos2, radius } => {
SafeArea::Ellipse {
pos1,
pos2,
radius: _,
} => {
let d = (*pos1 - *pos2).length();
let r = (*pos1 - sheep_pos).length();
r * r <= d * d
Expand All @@ -43,10 +47,12 @@ impl SafeArea {

pub fn get_center(&self) -> Vec3 {
match self {
SafeArea::Rect { pos, size } => Vec3::new(pos.x, 0.0, pos.y),
SafeArea::Ellipse { pos1, pos2, radius } => {
Vec3::new((pos1.x + pos2.x) / 2.0, 0.0, (pos1.y + pos2.y) / 2.0)
}
SafeArea::Rect { pos, size: _ } => Vec3::new(pos.x, 0.0, pos.y),
SafeArea::Ellipse {
pos1,
pos2,
radius: _,
} => Vec3::new((pos1.x + pos2.x) / 2.0, 0.0, (pos1.y + pos2.y) / 2.0),
}
}
}
Expand All @@ -62,7 +68,11 @@ fn draw_safe_area(mut gizmos: Gizmos, query: Query<&SafeArea>) {
Color::ORANGE,
);
}
SafeArea::Ellipse { pos1, pos2, radius } => {}
SafeArea::Ellipse {
pos1: _,
pos2: _,
radius: _,
} => {}
}
}
}
Expand All @@ -73,8 +83,8 @@ pub struct SheepCounter {
}

fn count_sheeps(
mut safe_areas: Query<&SafeArea>,
mut sheep: Query<&Transform, With<Sheep>>,
safe_areas: Query<&SafeArea>,
sheep: Query<&Transform, With<Sheep>>,
mut counter: ResMut<SheepCounter>,
) {
let mut count = 0;
Expand Down
23 changes: 15 additions & 8 deletions src/sheep.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use std::f32::consts::PI;

use bevy::{pbr::ExtendedMaterial, prelude::*, gltf::GltfMesh, render::{render_resource::PrimitiveTopology, mesh::Indices}};
use bevy::{
gltf::GltfMesh,
pbr::ExtendedMaterial,
prelude::*,
render::{mesh::Indices, render_resource::PrimitiveTopology},
};
use rand::Rng;

use crate::{physics::Velocity, player::Bark, sprite_material::{SpriteExtension, create_plane_mesh}, get_sprite_rotation, test_level::TEST_LEVEL_SIZE};
use crate::{
get_sprite_rotation,
physics::Velocity,
player::Bark,
sprite_material::{create_plane_mesh, SpriteExtension},
test_level::TEST_LEVEL_SIZE,
};

const SHEEP_PATH: &str = "test/sheep.png";

Expand Down Expand Up @@ -88,18 +99,14 @@ pub fn update_scared_sheeps(
}
}



pub fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
mut sprite_material: ResMut<Assets<ExtendedMaterial<StandardMaterial, SpriteExtension>>>,
// mut _sprite_material: ResMut<Assets<ExtendedMaterial<StandardMaterial, SpriteExtension>>>,
) {
let square = meshes.add(
create_plane_mesh()
);
let square = meshes.add(create_plane_mesh());
let sheep_texture: Handle<Image> = asset_server.load(SHEEP_PATH);

let sheep_material = materials.add(StandardMaterial {
Expand Down
58 changes: 27 additions & 31 deletions src/sprite_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,37 @@
use bevy::{
pbr::{ExtendedMaterial, MaterialExtension},
prelude::*,
render::{render_resource::{AsBindGroup, ShaderRef, PrimitiveTopology}, mesh::Indices},
render::{
mesh::Indices,
render_resource::{AsBindGroup, PrimitiveTopology, ShaderRef},
},
};


pub fn create_plane_mesh() -> Mesh {
Mesh::new(PrimitiveTopology::TriangleList)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION,
vec![
[-0.5, 0.0, 0.0],
[0.5, 0.0, 0.0],
[-0.5, 0.0, 1.0],
[0.5, 0.0, 1.0],
])
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0,
vec![
[0.0, 1.0],
[1.0, 1.0],
[0.0, 0.0],
[1.0, 0.0],
]
)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL,
vec![
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
]
)
.with_indices(Some(
Indices::U32(vec![
0, 1, 2,
2, 1, 3
])
))
.with_inserted_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
[-0.5, 0.0, 0.0],
[0.5, 0.0, 0.0],
[-0.5, 0.0, 1.0],
[0.5, 0.0, 1.0],
],
)
.with_inserted_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![[0.0, 1.0], [1.0, 1.0], [0.0, 0.0], [1.0, 0.0]],
)
.with_inserted_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
],
)
.with_indices(Some(Indices::U32(vec![0, 1, 2, 2, 1, 3])))
}

pub type SpriteMaterial = ExtendedMaterial<StandardMaterial, SpriteExtension>;
Expand Down
16 changes: 9 additions & 7 deletions src/test_level.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use bevy::{pbr::CascadeShadowConfigBuilder, prelude::*, core_pipeline::clear_color::ClearColorConfig};
use bevy::{
core_pipeline::clear_color::ClearColorConfig, pbr::CascadeShadowConfigBuilder, prelude::*,
};
use rand::prelude::*;
use std::f32::consts::PI;

use crate::{player::SpawnPlayer, safe_area::SafeArea, torch::SpawnTorch, sprite_material::create_plane_mesh, get_sprite_rotation};
use crate::{
get_sprite_rotation, player::SpawnPlayer, safe_area::SafeArea,
sprite_material::create_plane_mesh, torch::SpawnTorch,
};

const TREE_PATH: &str = "test/pine.png";

Expand All @@ -27,14 +32,13 @@ pub fn setup(
hdr: true,
..default()
},
camera_3d : Camera3d {
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::BLACK),
..default()
},
..default()
});


//spawn sun
let mut cascades = CascadeShadowConfigBuilder::default();
cascades.maximum_distance = 100.0;
Expand All @@ -57,8 +61,7 @@ pub fn setup(
brightness: 1.0,
});

let square = meshes.add(create_plane_mesh()
);
let square = meshes.add(create_plane_mesh());
let tree_texture: Handle<Image> = asset_server.load(TREE_PATH);

let r = TEST_LEVEL_SIZE;
Expand Down Expand Up @@ -100,7 +103,6 @@ pub fn setup(
});
}


//green plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
Expand Down
8 changes: 3 additions & 5 deletions src/torch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ fn spawn_torch(
.spawn((
torch,
PbrBundle {
transform: Transform::from_translation(
event.position,
)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(2.0 / 7.0, 2.0 / 7.0, 2.0)),
transform: Transform::from_translation(event.position)
.with_rotation(get_sprite_rotation())
.with_scale(Vec3::new(2.0 / 7.0, 2.0 / 7.0, 2.0)),
material: torch_material.0.clone(),
mesh: common_storage.plane.clone(),
..default()
Expand Down

0 comments on commit 9e00562

Please sign in to comment.