parallax!!!!!!
This commit is contained in:
parent
3944aabd27
commit
6a550cfcb8
4 changed files with 136 additions and 1 deletions
|
@ -1,4 +1,26 @@
|
||||||
header
|
header
|
||||||
|
parallax.size = 2000x1000
|
||||||
|
parallax.tiles = 2
|
||||||
|
parallax.factor.y = 100000
|
||||||
|
parallax.tex = BG0_himmel_blau.png
|
||||||
|
parallax.factor = 30
|
||||||
|
parallax.depth = 10
|
||||||
|
parallax.offset = -200
|
||||||
|
parallax.enable = true
|
||||||
|
parallax.tex = BG1_Berge_blau.png
|
||||||
|
parallax.factor = 10
|
||||||
|
parallax.depth = 9
|
||||||
|
parallax.enable = true
|
||||||
|
parallax.tex = BG2_Berge_gau.png
|
||||||
|
parallax.factor = 8
|
||||||
|
parallax.depth = 8
|
||||||
|
parallax.offset = -350
|
||||||
|
parallax.enable = true
|
||||||
|
parallax.tex = BG3_Berge_gruen.png
|
||||||
|
parallax.factor = 6
|
||||||
|
parallax.depth = 7
|
||||||
|
parallax.offset = 100
|
||||||
|
parallax.enable = true
|
||||||
block-size = 60
|
block-size = 60
|
||||||
.P = [player]
|
.P = [player]
|
||||||
.S = [spawn]
|
.S = [spawn]
|
||||||
|
|
|
@ -9,6 +9,7 @@ use bevy_rapier2d::prelude::{Collider, *};
|
||||||
use readformat::{readf, readf1};
|
use readformat::{readf, readf1};
|
||||||
|
|
||||||
use crate::game::WORLD_DEPTH;
|
use crate::game::WORLD_DEPTH;
|
||||||
|
use crate::parallax::{parallax, Parallax};
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
|
|
||||||
use super::player::PlayerSpawnOneshot;
|
use super::player::PlayerSpawnOneshot;
|
||||||
|
@ -77,6 +78,7 @@ pub(super) fn import_text_world(
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut wi = WorldInfo::default();
|
let mut wi = WorldInfo::default();
|
||||||
|
let mut parallax_info = Parallax::default();
|
||||||
for line in info_string.lines() {
|
for line in info_string.lines() {
|
||||||
let [name, val] = &readf(" {} = {}", line).expect("invalid line in info section")[..]
|
let [name, val] = &readf(" {} = {}", line).expect("invalid line in info section")[..]
|
||||||
else {
|
else {
|
||||||
|
@ -85,6 +87,29 @@ pub(super) fn import_text_world(
|
||||||
|
|
||||||
match name.as_str() {
|
match name.as_str() {
|
||||||
"block-size" => wi.block_size = val.parse().unwrap(),
|
"block-size" => wi.block_size = val.parse().unwrap(),
|
||||||
|
"parallax.offset" => parallax_info.offset = val.parse().unwrap(),
|
||||||
|
"parallax.size" => {
|
||||||
|
parallax_info.size = {
|
||||||
|
let x = readf("{}x{}", val).unwrap();
|
||||||
|
Vec2::new(x[0].parse().unwrap(), x[1].parse().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"parallax.tiles" => parallax_info.tiles = val.parse().unwrap(),
|
||||||
|
"parallax.tex" => parallax_info.tex = val.clone(),
|
||||||
|
"parallax.factor" => parallax_info.factor = val.parse().unwrap(),
|
||||||
|
"parallax.factor.y" => parallax_info.y_factor = val.parse().unwrap(),
|
||||||
|
"parallax.depth" => parallax_info.depth = val.parse().unwrap(),
|
||||||
|
"parallax.enable" => {
|
||||||
|
if val == "true" {
|
||||||
|
parallax(
|
||||||
|
&mut commands,
|
||||||
|
&mut materials,
|
||||||
|
&mut meshes,
|
||||||
|
&assets,
|
||||||
|
parallax_info.clone(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
x if x.starts_with(".") => {
|
x if x.starts_with(".") => {
|
||||||
let x = readf1(".{}", x).unwrap();
|
let x = readf1(".{}", x).unwrap();
|
||||||
wi.blocks.push((x, val.to_owned()));
|
wi.blocks.push((x, val.to_owned()));
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use game::game_plugin;
|
use game::game_plugin;
|
||||||
|
use parallax::parallax_plugin;
|
||||||
|
|
||||||
mod game;
|
mod game;
|
||||||
mod parallax;
|
mod parallax;
|
||||||
|
@ -42,6 +43,7 @@ fn main() {
|
||||||
//.add_plugins(RapierDebugRenderPlugin::default())
|
//.add_plugins(RapierDebugRenderPlugin::default())
|
||||||
.add_systems(Startup, setup_camera)
|
.add_systems(Startup, setup_camera)
|
||||||
.add_plugins(game_plugin)
|
.add_plugins(game_plugin)
|
||||||
|
.add_plugins(parallax_plugin)
|
||||||
.init_state::<AppState>()
|
.init_state::<AppState>()
|
||||||
.init_state::<PausedState>()
|
.init_state::<PausedState>()
|
||||||
.insert_state(AppState::InGame) // TODO dont
|
.insert_state(AppState::InGame) // TODO dont
|
||||||
|
|
|
@ -1 +1,87 @@
|
||||||
use bevy::prelude::*;
|
use bevy::{
|
||||||
|
prelude::*,
|
||||||
|
render::{
|
||||||
|
primitives::Aabb,
|
||||||
|
view::{NoFrustumCulling, VisibilitySystems},
|
||||||
|
},
|
||||||
|
sprite::MaterialMesh2dBundle,
|
||||||
|
};
|
||||||
|
use bevy_rapier2d::parry::simba::scalar::SupersetOf;
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct ParallaxTile(usize);
|
||||||
|
|
||||||
|
#[derive(Component, Clone)]
|
||||||
|
pub struct Parallax {
|
||||||
|
pub offset: f32,
|
||||||
|
pub factor: f32,
|
||||||
|
pub y_factor: f32,
|
||||||
|
pub size: Vec2,
|
||||||
|
pub depth: f32,
|
||||||
|
pub tiles: usize,
|
||||||
|
pub tex: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Parallax {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
offset: 0.,
|
||||||
|
factor: 10.,
|
||||||
|
y_factor: 10.,
|
||||||
|
size: Vec2::new(2000., 1000.),
|
||||||
|
depth: 100.,
|
||||||
|
tiles: 4,
|
||||||
|
tex: "".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parallax_plugin(app: &mut App) {
|
||||||
|
app.add_systems(Update, (update_parallax,));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parallax(
|
||||||
|
commands: &mut Commands,
|
||||||
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
||||||
|
meshes: &mut ResMut<Assets<Mesh>>,
|
||||||
|
assets: &Res<AssetServer>,
|
||||||
|
parallax_info: Parallax,
|
||||||
|
) {
|
||||||
|
for i in 0..parallax_info.tiles {
|
||||||
|
commands.spawn((
|
||||||
|
ParallaxTile(i),
|
||||||
|
parallax_info.clone(),
|
||||||
|
MaterialMesh2dBundle {
|
||||||
|
mesh: meshes.add(Rectangle::from_size(parallax_info.size)).into(),
|
||||||
|
material: materials.add(assets.load(parallax_info.tex.clone())),
|
||||||
|
transform: Transform::from_xyz(0.0, 0.0, -parallax_info.depth),
|
||||||
|
visibility: Visibility::Visible,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
NoFrustumCulling,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_parallax(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut query: Query<(Entity, &Parallax, &ParallaxTile, &mut Transform)>,
|
||||||
|
camera_query: Query<(&Camera2d, &Transform), (Without<Parallax>)>,
|
||||||
|
) {
|
||||||
|
let (_, cam_transform) = camera_query.single();
|
||||||
|
let cam_transform = cam_transform.translation;
|
||||||
|
for (entity, parallax_info, ParallaxTile(tile), mut transform) in query.iter_mut() {
|
||||||
|
let position_on_parallax = cam_transform.xy()
|
||||||
|
- Vec2::new(
|
||||||
|
cam_transform.x / parallax_info.factor,
|
||||||
|
cam_transform.y / parallax_info.y_factor,
|
||||||
|
);
|
||||||
|
let first_parallax_on_cam =
|
||||||
|
(position_on_parallax / parallax_info.size).floor() * parallax_info.size;
|
||||||
|
let position = first_parallax_on_cam
|
||||||
|
+ Vec2::new(*tile as f32, 0.) * parallax_info.size
|
||||||
|
+ position_on_parallax % parallax_info.size;
|
||||||
|
transform.translation =
|
||||||
|
position.extend(-parallax_info.depth) + Vec3::new(0., parallax_info.offset, 0.);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue