annieversary 2021-10-01 19:56:26 +02:00
parent d727f52ff0
commit 6b2075c2a4
7 changed files with 92 additions and 24 deletions

BIN
assets/textures/floor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -15,6 +15,7 @@ pub fn camera_follow_player(
}; };
let ds = time.delta_seconds() * 5.0; let ds = time.delta_seconds() * 5.0;
// TODO Change this to normal movement
for (mut trans, _) in camera.iter_mut() { for (mut trans, _) in camera.iter_mut() {
trans.look_at(player_pos, Vec3::Y); trans.look_at(player_pos, Vec3::Y);

View File

@ -1,3 +1,5 @@
#![allow(dead_code)]
use bevy::prelude::*; use bevy::prelude::*;
const DIS: f32 = 300.0; const DIS: f32 = 300.0;

View File

@ -2,6 +2,8 @@ use bevy::prelude::*;
use crate::player::Player; use crate::player::Player;
const RANGE: f32 = 25.0;
pub struct LightBall; pub struct LightBall;
pub fn light_up_ball_when_close_to_player( pub fn light_up_ball_when_close_to_player(
mut commands: Commands, mut commands: Commands,
@ -23,17 +25,17 @@ pub fn light_up_ball_when_close_to_player(
for (entity, trans, _, mut material, light) in thingies.iter_mut() { for (entity, trans, _, mut material, light) in thingies.iter_mut() {
let dis = trans.translation.distance(player_pos); let dis = trans.translation.distance(player_pos);
if dis < 20.0 { if dis < RANGE {
*material = materials.lit.clone(); *material = materials.lit.clone();
// change intensity, // change intensity,
// add light if there isn't one // add light if there isn't one
if let Some(mut l) = light { if let Some(mut l) = light {
l.intensity = 300.0 * (20.0 - dis) / 20.0; l.intensity = 300.0 * (RANGE - dis) / RANGE;
} else { } else {
commands.entity(entity).insert(Light { commands.entity(entity).insert(Light {
color: Color::rgb(15.0, 15.0, 15.0), color: Color::rgb(15.0, 15.0, 15.0),
intensity: 300.0 * (20.0 - dis) / 20.0, intensity: 300.0 * (RANGE - dis) / RANGE,
..Default::default() ..Default::default()
}); });
} }

33
src/loading.rs Normal file
View File

@ -0,0 +1,33 @@
use bevy::{
prelude::*,
render::texture::{AddressMode, FilterMode},
};
use crate::AppState;
pub struct LoadedAssets {
pub floor: Handle<Texture>,
}
impl FromWorld for LoadedAssets {
fn from_world(world: &mut World) -> Self {
let server = world.get_resource::<AssetServer>().unwrap();
let floor = server.load("textures/floor.png");
Self { floor }
}
}
pub fn loading(
assets: Res<LoadedAssets>,
mut textures: ResMut<Assets<Texture>>,
mut state: ResMut<State<AppState>>,
) {
if let Some(t) = textures.get_mut(&assets.floor) {
t.sampler.address_mode_u = AddressMode::MirrorRepeat;
t.sampler.address_mode_v = AddressMode::Repeat;
t.sampler.address_mode_w = AddressMode::Repeat;
t.sampler.mipmap_filter = FilterMode::Linear;
state.set(AppState::Game).unwrap();
}
}

View File

@ -9,24 +9,43 @@ use player::*;
mod camera; mod camera;
use camera::*; use camera::*;
mod columns; mod columns;
use columns::*;
mod loading;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum AppState {
Loading,
Game,
}
fn main() { fn main() {
App::build() App::build()
.insert_resource(Msaa { samples: 4 }) .insert_resource(Msaa { samples: 4 })
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(rendering::CustomPlugins) .add_plugins(rendering::CustomPlugins)
.init_resource::<LightBallMaterials>() .init_resource::<LightBallMaterials>()
.add_startup_system(setup.system()) .init_resource::<loading::LoadedAssets>()
.add_system(exit_on_esc_system.system()) .add_state(AppState::Loading)
.add_system(light_movement.system()) // loading
.add_system(move_player.system()) .add_system_set(
.add_system(camera_follow_player.system()) SystemSet::on_update(AppState::Loading).with_system(loading::loading.system()),
.add_system(light_up_ball_when_close_to_player.system()) )
// game
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup.system()))
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(exit_on_esc_system.system())
.with_system(light_movement.system())
.with_system(move_player.system())
.with_system(camera_follow_player.system())
.with_system(light_up_ball_when_close_to_player.system()),
)
.run(); .run();
} }
fn setup( fn setup(
mut commands: Commands, mut commands: Commands,
assets: Res<loading::LoadedAssets>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient: ResMut<AmbientLight>, mut ambient: ResMut<AmbientLight>,
@ -34,21 +53,30 @@ fn setup(
) { ) {
// set ambient light to very low // set ambient light to very low
ambient.color = Color::rgb(0.3, 0.3, 0.3); ambient.color = Color::rgb(0.3, 0.3, 0.3);
ambient.brightness = 0.02; ambient.brightness = 0.01;
// floor // floor
let mut floor_material: StandardMaterial = Color::rgb(0.1, 0.1, 0.1).into(); let mut floor_material = StandardMaterial {
base_color_texture: Some(assets.floor.clone()),
base_color: Color::rgb(0.3, 0.3, 0.3),
..Default::default()
};
floor_material.metallic = 0.0; floor_material.metallic = 0.0;
floor_material.reflectance = 0.0; floor_material.reflectance = 0.0;
let floor_material = materials.add(floor_material); let floor_material = materials.add(floor_material);
commands.spawn_bundle(PbrBundle { for i in -10..10 {
mesh: meshes.add(Mesh::from(shape::Plane { size: 10000.0 })), for j in -10..10 {
material: floor_material, commands.spawn_bundle(PbrBundle {
..Default::default() mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
}); material: floor_material.clone(),
transform: Transform::from_xyz(100.0 * i as f32, 0.0, 100.0 * j as f32),
..Default::default()
});
}
}
// columns // columns
spawn_columns(&mut commands, &mut meshes, &mut materials); // columns::spawn_columns(&mut commands, &mut meshes, &mut materials);
// player // player
spawn_player(&mut commands, &mut meshes, &mut materials); spawn_player(&mut commands, &mut meshes, &mut materials);
@ -58,19 +86,19 @@ fn setup(
spawn_light_ball( spawn_light_ball(
&mut commands, &mut commands,
&light_ball_materials, &light_ball_materials,
Vec3::new(i as f32 * 30.0, 2.0, 6.0), Vec3::new(i as f32 * 30.0, 2.0, 10.0),
); );
spawn_light_ball( spawn_light_ball(
&mut commands, &mut commands,
&light_ball_materials, &light_ball_materials,
Vec3::new(i as f32 * 30.0, 2.0, -6.0), Vec3::new(i as f32 * 30.0, 2.0, -10.0),
); );
} }
// camera // camera
commands commands
.spawn_bundle(PerspectiveCameraBundle { .spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-20.0, 100.0, 50.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(-40.0, 110.0, 40.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default() ..Default::default()
}) })
.insert(Camera); .insert(Camera);

View File

@ -17,16 +17,17 @@ pub fn spawn_player(
..Default::default() ..Default::default()
})), })),
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()), material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
transform: Transform::from_xyz(33.0, 1.0, 0.0), transform: Transform::from_xyz(0.0, 1.0, 0.0),
..Default::default() ..Default::default()
}) })
.insert(Player) .insert(Player)
.with_children(|parent| { .with_children(|parent| {
// light // light
let mut light_material: StandardMaterial = Color::rgb(0.3, 0.5, 0.3).into(); let mut light_material: StandardMaterial =
Color::rgb(0.737255, 0.560784, 0.560784).into();
light_material.metallic = 0.5; light_material.metallic = 0.5;
light_material.reflectance = 0.5; light_material.reflectance = 0.5;
light_material.emissive = Color::rgb(15.0, 15.0, 15.0); light_material.emissive = Color::rgb(7.52, 5.72, 5.72);
let light_material = materials.add(light_material); let light_material = materials.add(light_material);
for i in 0..6 { for i in 0..6 {
@ -43,6 +44,7 @@ pub fn spawn_player(
.insert_bundle(LightBundle { .insert_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0), transform: Transform::from_xyz(0.0, 0.0, 0.0),
light: Light { light: Light {
color: Color::rgb(7.52, 5.72, 5.72),
..Default::default() ..Default::default()
}, },
..Default::default() ..Default::default()