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;
// TODO Change this to normal movement
for (mut trans, _) in camera.iter_mut() {
trans.look_at(player_pos, Vec3::Y);

View File

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

View File

@ -2,6 +2,8 @@ use bevy::prelude::*;
use crate::player::Player;
const RANGE: f32 = 25.0;
pub struct LightBall;
pub fn light_up_ball_when_close_to_player(
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() {
let dis = trans.translation.distance(player_pos);
if dis < 20.0 {
if dis < RANGE {
*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;
l.intensity = 300.0 * (RANGE - dis) / RANGE;
} else {
commands.entity(entity).insert(Light {
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()
});
}

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;
use camera::*;
mod columns;
use columns::*;
mod loading;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum AppState {
Loading,
Game,
}
fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.add_plugins(rendering::CustomPlugins)
.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())
.init_resource::<loading::LoadedAssets>()
.add_state(AppState::Loading)
// loading
.add_system_set(
SystemSet::on_update(AppState::Loading).with_system(loading::loading.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();
}
fn setup(
mut commands: Commands,
assets: Res<loading::LoadedAssets>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient: ResMut<AmbientLight>,
@ -34,21 +53,30 @@ fn setup(
) {
// set ambient light to very low
ambient.color = Color::rgb(0.3, 0.3, 0.3);
ambient.brightness = 0.02;
ambient.brightness = 0.01;
// 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.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()
});
for i in -10..10 {
for j in -10..10 {
commands.spawn_bundle(PbrBundle {
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
spawn_columns(&mut commands, &mut meshes, &mut materials);
// columns::spawn_columns(&mut commands, &mut meshes, &mut materials);
// player
spawn_player(&mut commands, &mut meshes, &mut materials);
@ -58,19 +86,19 @@ fn setup(
spawn_light_ball(
&mut commands,
&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(
&mut commands,
&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
commands
.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()
})
.insert(Camera);

View File

@ -17,16 +17,17 @@ pub fn spawn_player(
..Default::default()
})),
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()
})
.insert(Player)
.with_children(|parent| {
// 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.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);
for i in 0..6 {
@ -43,6 +44,7 @@ pub fn spawn_player(
.insert_bundle(LightBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0),
light: Light {
color: Color::rgb(7.52, 5.72, 5.72),
..Default::default()
},
..Default::default()