main
commit
a208e39535
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
.DS_Store
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "moria"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.5.0"
|
|
@ -0,0 +1,27 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
const DIS: f32 = 300.0;
|
||||
const SIZE: f32 = 20.0;
|
||||
const HEIGHT: f32 = 400.0;
|
||||
|
||||
pub fn spawn_columns(
|
||||
commands: &mut Commands,
|
||||
meshes: &mut Assets<Mesh>,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
) {
|
||||
let mut column_material: StandardMaterial = Color::rgb(0.7, 0.7, 0.7).into();
|
||||
column_material.metallic = 0.0;
|
||||
column_material.reflectance = 0.0;
|
||||
let column_material = materials.add(column_material);
|
||||
let col_mesh = meshes.add(Mesh::from(shape::Box::new(SIZE, HEIGHT, SIZE)));
|
||||
for i in -8..8 {
|
||||
for j in -8..8 {
|
||||
commands.spawn_bundle(PbrBundle {
|
||||
mesh: col_mesh.clone(),
|
||||
material: column_material.clone(),
|
||||
transform: Transform::from_xyz(i as f32 * DIS, HEIGHT / 2.0, j as f32 * DIS),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::player::Player;
|
||||
|
||||
pub struct LightBall;
|
||||
pub fn light_up_ball_when_close_to_player(
|
||||
mut commands: Commands,
|
||||
player: Query<(&Transform, &Player), Without<LightBall>>,
|
||||
mut thingies: Query<(
|
||||
Entity,
|
||||
&Transform,
|
||||
&LightBall,
|
||||
&mut Handle<StandardMaterial>,
|
||||
Option<&mut Light>,
|
||||
)>,
|
||||
materials: Res<LightBallMaterials>,
|
||||
) {
|
||||
let player_pos = if let Some(player) = player.iter().next() {
|
||||
player.0.translation
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
for (entity, trans, _, mut material, light) in thingies.iter_mut() {
|
||||
let dis = trans.translation.distance(player_pos);
|
||||
if dis < 20.0 {
|
||||
*material = materials.lit.clone();
|
||||
|
||||
// change intensity,
|
||||
// add light if there isn't one
|
||||
if let Some(mut l) = light {
|
||||
l.intensity = 300.0 * (20.0 - dis) / 20.0;
|
||||
} else {
|
||||
commands.entity(entity).insert(Light {
|
||||
color: Color::rgb(15.0, 15.0, 15.0),
|
||||
intensity: 300.0 * (20.0 - dis) / 20.0,
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
} else {
|
||||
*material = materials.unlit.clone();
|
||||
|
||||
// remove light if there is one
|
||||
if light.is_some() {
|
||||
commands.entity(entity).remove::<Light>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_light_ball(
|
||||
commands: &mut Commands,
|
||||
materials: &LightBallMaterials,
|
||||
translation: Vec3,
|
||||
) {
|
||||
commands
|
||||
.spawn_bundle(PbrBundle {
|
||||
mesh: materials.mesh.clone(),
|
||||
material: materials.unlit.clone(),
|
||||
transform: Transform::from_translation(translation),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(LightBall);
|
||||
}
|
||||
|
||||
pub struct LightBallMaterials {
|
||||
unlit: Handle<StandardMaterial>,
|
||||
lit: Handle<StandardMaterial>,
|
||||
mesh: Handle<Mesh>,
|
||||
}
|
||||
impl FromWorld for LightBallMaterials {
|
||||
fn from_world(world: &mut World) -> Self {
|
||||
let world = world.cell();
|
||||
let mut materials = world
|
||||
.get_resource_mut::<Assets<StandardMaterial>>()
|
||||
.unwrap();
|
||||
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
|
||||
|
||||
let mut unlit: StandardMaterial = Color::rgb(0.9, 0.9, 0.9).into();
|
||||
unlit.metallic = 0.0;
|
||||
unlit.reflectance = 0.0;
|
||||
let unlit = materials.add(unlit);
|
||||
|
||||
let mut lit: StandardMaterial = Color::rgb(15.0, 15.0, 15.0).into();
|
||||
lit.metallic = 0.5;
|
||||
lit.reflectance = 0.5;
|
||||
lit.emissive = Color::rgb(15.0, 15.0, 15.0);
|
||||
let lit = materials.add(lit);
|
||||
|
||||
let mesh = meshes.add(Mesh::from(shape::Icosphere {
|
||||
radius: 2.0,
|
||||
subdivisions: 5,
|
||||
}));
|
||||
|
||||
Self { lit, unlit, mesh }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
use bevy::{input::system::exit_on_esc_system, pbr::AmbientLight, prelude::*};
|
||||
|
||||
mod light_balls;
|
||||
use light_balls::*;
|
||||
mod player;
|
||||
use player::*;
|
||||
mod columns;
|
||||
use columns::*;
|
||||
|
||||
fn main() {
|
||||
App::build()
|
||||
.insert_resource(Msaa { samples: 4 })
|
||||
.add_plugins(DefaultPlugins)
|
||||
.init_resource::<LightBallMaterials>()
|
||||
.add_startup_system(setup.system())
|
||||
.add_system(exit_on_esc_system.system())
|
||||
.add_system(light_movement.system())
|
||||
.add_system(move_player.system())
|
||||
.add_system(camera_follow_player.system())
|
||||
.add_system(light_up_ball_when_close_to_player.system())
|
||||
.run();
|
||||
}
|
||||
|
||||
pub struct Camera;
|
||||
|
||||
/// set up a simple 3D scene
|
||||
fn setup(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut ambient: ResMut<AmbientLight>,
|
||||
light_ball_materials: Res<LightBallMaterials>,
|
||||
) {
|
||||
// set ambient light to 0
|
||||
ambient.color = Color::rgb(0.3, 0.3, 0.3);
|
||||
ambient.brightness = 0.02;
|
||||
|
||||
// floor
|
||||
let mut floor_material: StandardMaterial = Color::rgb(0.1, 0.1, 0.1).into();
|
||||
floor_material.metallic = 0.0;
|
||||
floor_material.reflectance = 0.0;
|
||||
let floor_material = materials.add(floor_material);
|
||||
commands.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Plane { size: 10000.0 })),
|
||||
material: floor_material,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
// columns
|
||||
spawn_columns(&mut commands, &mut meshes, &mut materials);
|
||||
|
||||
// player
|
||||
spawn_player(&mut commands, &mut meshes, &mut materials);
|
||||
|
||||
// light balls
|
||||
for i in 1..10 {
|
||||
spawn_light_ball(
|
||||
&mut commands,
|
||||
&light_ball_materials,
|
||||
Vec3::new(i as f32 * 30.0, 2.0, 6.0),
|
||||
);
|
||||
spawn_light_ball(
|
||||
&mut commands,
|
||||
&light_ball_materials,
|
||||
Vec3::new(i as f32 * 30.0, 2.0, -6.0),
|
||||
);
|
||||
}
|
||||
|
||||
// camera
|
||||
commands
|
||||
.spawn_bundle(PerspectiveCameraBundle {
|
||||
transform: Transform::from_xyz(-20.0, 100.0, 50.0).looking_at(Vec3::ZERO, Vec3::Y),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Camera);
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::Camera;
|
||||
|
||||
pub struct Player;
|
||||
pub struct PlayerLight {
|
||||
i: u8,
|
||||
}
|
||||
|
||||
pub fn spawn_player(
|
||||
commands: &mut Commands,
|
||||
meshes: &mut Assets<Mesh>,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
) {
|
||||
commands
|
||||
.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Capsule {
|
||||
radius: 0.5,
|
||||
..Default::default()
|
||||
})),
|
||||
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
|
||||
transform: Transform::from_xyz(33.0, 1.0, 0.0),
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Player)
|
||||
.with_children(|parent| {
|
||||
// light
|
||||
let mut light_material: StandardMaterial = Color::rgb(0.3, 0.5, 0.3).into();
|
||||
light_material.metallic = 0.5;
|
||||
light_material.reflectance = 0.5;
|
||||
light_material.emissive = Color::rgb(15.0, 15.0, 15.0);
|
||||
let light_material = materials.add(light_material);
|
||||
|
||||
for i in 0..5 {
|
||||
parent
|
||||
.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Icosphere {
|
||||
radius: 0.2,
|
||||
subdivisions: 5,
|
||||
})),
|
||||
material: light_material.clone(),
|
||||
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
||||
..Default::default()
|
||||
})
|
||||
.insert_bundle(LightBundle {
|
||||
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
light: Light {
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.insert(PlayerLight { i: i as u8 + 1 });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn light_movement(mut query: Query<(&mut Transform, &PlayerLight)>, time: Res<Time>) {
|
||||
let t = time.seconds_since_startup();
|
||||
for (mut trans, light) in query.iter_mut() {
|
||||
let i = light.i as f64;
|
||||
let i2 = i / 2.0;
|
||||
trans.translation.y = 6.0 * (t * 1.1 * i2 + i).sin() as f32 + 7.0;
|
||||
trans.translation.x = 5.0 * i2 as f32 * (t * 0.4 * i2 + i).cos() as f32;
|
||||
trans.translation.z = 5.0 * (t * 0.4 * i + i).sin() as f32;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_player(
|
||||
mut query: Query<(&mut Transform, &Player)>,
|
||||
input: Res<Input<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let ds = time.delta_seconds() * 5.0;
|
||||
|
||||
// TODO rotate according to camera position
|
||||
// but make it snap to coord system
|
||||
|
||||
for (mut transform, _) in query.iter_mut() {
|
||||
if input.pressed(KeyCode::W) {
|
||||
transform.translation += Vec3::X * ds;
|
||||
}
|
||||
if input.pressed(KeyCode::S) {
|
||||
transform.translation -= Vec3::X * ds;
|
||||
}
|
||||
if input.pressed(KeyCode::A) {
|
||||
transform.translation -= Vec3::Z * ds;
|
||||
}
|
||||
if input.pressed(KeyCode::D) {
|
||||
transform.translation += Vec3::Z * ds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn camera_follow_player(
|
||||
player: Query<(&Transform, &Player), Without<Camera>>,
|
||||
mut camera: Query<(&mut Transform, &Camera), Without<Player>>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let player_pos = if let Some(player) = player.iter().next() {
|
||||
player.0.translation
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
let ds = time.delta_seconds() * 5.0;
|
||||
for (mut trans, _) in camera.iter_mut() {
|
||||
trans.look_at(player_pos, Vec3::Y);
|
||||
|
||||
// keep a distance to the player
|
||||
if trans.translation.distance(player_pos) > 170.0 {
|
||||
let mut d = trans.rotation * Vec3::Z * ds;
|
||||
d.y = 0.0;
|
||||
trans.translation -= d;
|
||||
}
|
||||
if trans.translation.distance(player_pos) < 100.0 {
|
||||
let mut d = trans.rotation * Vec3::Z * ds;
|
||||
d.y = 0.0;
|
||||
trans.translation += d;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue