Skip to content

Commit

Permalink
Add more images and update scale to fit to width and make 1 unit = 1 …
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
lucasmerlin committed Dec 10, 2023
1 parent 1f5067e commit d24b132
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 79 deletions.
Binary file modified assets/parallax/01_far_city.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/parallax/02_middle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 66 additions & 5 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::block::BLOCK_SIZE;
use bevy::prelude::*;
use bevy::sprite::Anchor;
use bevy_rapier2d::geometry::Collider;
use bevy_rapier2d::plugin::systems::apply_scale;
use bevy_rapier2d::prelude::{Friction, RigidBody, Velocity};

use crate::cursor_system::CursorCoords;
use crate::level::{Level, LevelLifecycle};
use crate::state::LevelState;
use crate::HORIZONTAL_VIEWPORT_SIZE;

pub struct BasePlugin;

Expand All @@ -14,21 +18,73 @@ impl Plugin for BasePlugin {
}
}

#[derive(Debug, Clone)]
pub enum BaseType {
T2,
T3,
T4,
T7,
T9,
}

impl BaseType {
pub fn name(&self) -> &str {
match self {
BaseType::T2 => "t-2",
BaseType::T3 => "t-3",
BaseType::T4 => "t-4",
BaseType::T7 => "t-7",
BaseType::T9 => "t-9",
}
}

pub fn width(&self) -> f32 {
let image_width = self.image_width();
let _4k_width = 3840.0;
let scale = HORIZONTAL_VIEWPORT_SIZE / _4k_width;
image_width * scale
}

pub fn image_width(&self) -> f32 {
match self {
BaseType::T2 => 466.0,
BaseType::T3 => 636.0,
BaseType::T4 => 762.0,
BaseType::T7 => 1324.0,
BaseType::T9 => 1652.0,
}
}

pub fn asset(&self) -> String {
format!("bases/{}.png", self.name())
}
}

#[derive(Component)]
pub struct Base;

pub fn setup_base(mut commands: Commands, mut assets: ResMut<AssetServer>, mut level: Res<Level>) {
let height = 20.0;
let width = level.bases[0].width;
let height = BLOCK_SIZE;

// Since the spot in the bg image is not centered, we need to offset the base a bit
let additional_transform = Vec2::new(0.5, 0.0);

for base in level.bases {
let width = base.base_type.width();

let image_width = base.base_type.image_width();

let image_scale = width / image_width;

let texture = assets.load(base.base_type.asset());

commands
.spawn((
Base,
LevelLifecycle,
SpatialBundle::from(
Transform::from_translation(Vec3::from((
base.translation + Vec2::new(0.0, height / 2.0),
base.translation + additional_transform + Vec2::new(0.0, height / 2.0),
0.0,
)))
.with_rotation(Quat::from_rotation_z(base.rotation)),
Expand All @@ -41,8 +97,13 @@ pub fn setup_base(mut commands: Commands, mut assets: ResMut<AssetServer>, mut l
.with_children(|parent| {
parent.spawn(SpriteBundle {
// transform: Transform::from_xyz(0.0, 0.0, 0.0),
texture: assets.load("fortress.png"),
transform: Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(0.1)),
texture,
transform: Transform::from_xyz(0.0, height / 2.0, -7.0)
.with_scale(Vec3::splat(image_scale)),
sprite: Sprite {
anchor: Anchor::TopCenter,
..Default::default()
},
..Default::default()
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum BlockType {
L,
}

pub const BLOCK_SIZE: f32 = 20.0;
pub const BLOCK_SIZE: f32 = 1.0;

pub const BLOCKS: [BlockType; 7] = [
BlockType::I,
Expand Down
37 changes: 23 additions & 14 deletions src/camera_movement.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::block::Falling;
use crate::MainCamera;
use crate::block::{Block, Falling};
use crate::launch_platform::LaunchPlatform;
use crate::{MainCamera, HORIZONTAL_VIEWPORT_SIZE};
use bevy::prelude::*;

#[derive(Resource)]
Expand All @@ -17,33 +18,41 @@ impl Default for CameraMovement {
// Camera is only moving up, not down
pub fn camera_movement_system(
mut camera_movement: ResMut<CameraMovement>,
mut camera_query: Query<&mut Transform, With<MainCamera>>,
mut query: Query<(
mut camera_query: Query<(&mut Transform, &GlobalTransform, &Camera), With<MainCamera>>,
mut query: Query<
&Transform,
&crate::block::Block,
Without<MainCamera>,
Without<Falling>,
)>,
(
Without<MainCamera>,
Without<Falling>,
Or<(With<Block>, With<LaunchPlatform>)>,
),
>,
) {
let mut highest = 100.0;
for (transform, block, ..) in query.iter_mut() {
let start_move_at = 20.0;

let mut highest = start_move_at;
for transform in query.iter_mut() {
if transform.translation.y > highest {
highest = transform.translation.y;
}
}

let target_height = highest - 100.0;
let target_height = highest - start_move_at;

let increase = 1.0;
let increase = 0.1;

if target_height > camera_movement.height {
camera_movement.height += increase;
} else if target_height < camera_movement.height - increase {
camera_movement.height -= increase;
}

for mut transform in camera_query.iter_mut() {
transform.translation.y = camera_movement.height;
for (mut transform, global_transform, camera) in camera_query.iter_mut() {
let viewport = camera.logical_viewport_size().unwrap();

let scene_height = HORIZONTAL_VIEWPORT_SIZE * viewport.y / viewport.x;

transform.translation.y = camera_movement.height + scene_height / 2.0;
// camera should slowly zoom out as we get higher
//transform.scale = Vec3::new(1.0, 1.0, 1.0) * (1.0 + camera_movement.height / 1000.0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/effect/magnetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct MagneticEffect {
impl Default for MagneticEffect {
fn default() -> Self {
Self {
range: 200.0,
range: 10.0,
force: 2.0,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/effect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl EffectType {
parent.spawn(SpriteBundle {
texture: assets.load(texture),
sprite: Sprite {
custom_size: Some(Vec2::new(24.0, 24.0)),
custom_size: Some(Vec2::new(1.0, 1.0)),
..Default::default()
},
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions src/environment/beam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn spawn_beam_system(mut commands: Commands, mut beam_events: EventReader<Be
SpriteBundle {
sprite: Sprite {
color: Color::rgba_u8(255, 255, 255, 200),
custom_size: Some(Vec2::new(10.0, 10.0)),
custom_size: Some(Vec2::new(0.0, 0.0)),
anchor: Anchor::CenterLeft,
..Default::default()
},
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn update_beam_system(
- beam_global_transform.translation())
.length();

sprite.custom_size = Some(Vec2::new(beam_length, 10.0));
sprite.custom_size = Some(Vec2::new(beam_length, 0.5));
}
}
}
10 changes: 5 additions & 5 deletions src/environment/car.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rand::{random, thread_rng, Rng};
use crate::block::{DestroyBlockOnContact, BLOCK_COLLISION_GROUP};
use crate::floor::FLOOR_COLLISION_GROUP;
use crate::level::{LevelLifecycle, UpdateLevelStats};
use crate::{CAR_MAX_HEIGHT, CAR_MIN_HEIGHT};
use crate::{CAR_MAX_HEIGHT, CAR_MIN_HEIGHT, HORIZONTAL_VIEWPORT_SIZE};

pub struct CarPlugin;

Expand Down Expand Up @@ -64,18 +64,18 @@ pub fn spawn_car_system(
car_spawner.spawn_timer.tick(time.delta());
if car_spawner.spawn_timer.just_finished() {
let car_size = Vec2::new(
thread_rng().gen_range(50.0..80.0),
thread_rng().gen_range(20.0..30.0),
thread_rng().gen_range(3.0..5.0),
thread_rng().gen_range(1.5..2.0),
);

let direction = if random() { 1.0 } else { -1.0 };

let car_position = Vec2::new(
direction * 1000.0,
direction * HORIZONTAL_VIEWPORT_SIZE,
thread_rng().gen_range(CAR_MIN_HEIGHT..CAR_MAX_HEIGHT),
);

let car_velocity = Vec2::new(direction * -thread_rng().gen_range(70.0..100.0), 0.0);
let car_velocity = Vec2::new(direction * -thread_rng().gen_range(7.0..10.0), 0.0);

commands.spawn((
Car::default(),
Expand Down
15 changes: 8 additions & 7 deletions src/environment/city.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bevy::prelude::*;
use bevy::sprite::Anchor;

use crate::camera_movement::CameraMovement;
use crate::state::LevelState;
use crate::{MainCamera, FLOOR_HEIGHT, VERTICAL_VIEWPORT_SIZE};
use crate::{MainCamera, FLOOR_HEIGHT, HORIZONTAL_VIEWPORT_SIZE};

pub struct CityPlugin;

Expand Down Expand Up @@ -79,15 +80,15 @@ pub fn setup_city(mut commands: Commands, assets: Res<AssetServer>) {
));
commands.spawn((
AutoWidth {
aspect_ratio,
aspect_ratio: 1465.0 / 3840.0,
open_top: true,
parallax: -0.2,
},
SpriteBundle {
transform: Transform::from_xyz(0.0, 0.0, -20.0),
texture: assets.load("parallax/02_middle.png"),
sprite: Sprite {
custom_size: Some(Vec2::new(1.0, aspect_ratio)),
custom_size: Some(Vec2::new(1.0, 1465.0 / 3840.0)),
anchor: Anchor::BottomCenter,
..Default::default()
},
Expand All @@ -96,15 +97,15 @@ pub fn setup_city(mut commands: Commands, assets: Res<AssetServer>) {
));
commands.spawn((
AutoWidth {
aspect_ratio,
aspect_ratio: 1810.0 / 3840.0,
open_top: true,
parallax: -0.4,
},
SpriteBundle {
transform: Transform::from_xyz(0.0, 0.0, -30.0),
texture: assets.load("parallax/01_far_city.png"),
sprite: Sprite {
custom_size: Some(Vec2::new(1.0, aspect_ratio)),
custom_size: Some(Vec2::new(1.0, 1810.0 / 3840.0)),
anchor: Anchor::BottomCenter,
..Default::default()
},
Expand Down Expand Up @@ -134,6 +135,7 @@ pub fn setup_city(mut commands: Commands, assets: Res<AssetServer>) {
pub fn update_auto_width(
mut query: Query<(&AutoWidth, &mut Transform)>,
transform: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
movement: Res<CameraMovement>,
) {
let (camera_transform, camera) = transform.single();
let viewport = camera.logical_viewport_size().unwrap();
Expand All @@ -158,8 +160,7 @@ pub fn update_auto_width(
for (auto_width, mut transform) in &mut query.iter_mut() {
// the images should be scaled so they always fill the screen

transform.translation.y =
-VERTICAL_VIEWPORT_SIZE / 2.0 - camera_transform.translation().y * auto_width.parallax;
transform.translation.y = -movement.height * auto_width.parallax;

if world_aspect_ratio > auto_width.aspect_ratio || auto_width.open_top {
// the world is wider than the image
Expand Down
17 changes: 9 additions & 8 deletions src/environment/tow_truck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy_rapier2d::prelude::*;
use crate::environment::beam::BeamEvent;
use crate::environment::car::{Car, CarCrashedEvent};
use crate::level::LevelLifecycle;
use crate::HORIZONTAL_VIEWPORT_SIZE;

pub struct TowTruckPlugin;

Expand Down Expand Up @@ -31,18 +32,18 @@ pub fn spawn_tow_truck_system(
mut car_crashed_events: EventReader<CarCrashedEvent>,
) {
for event in car_crashed_events.read() {
let size = Vec2::new(60.0, 30.0);
let size = Vec2::new(8.0, 4.0);

commands.spawn((
TowTruck {
target: event.entity,
phase: TowTruckPhase::MovingToTarget,
},
LevelLifecycle,
SpatialBundle::from(Transform::from_xyz(-1000.0, 0.0, 0.0)),
SpatialBundle::from(Transform::from_xyz(-HORIZONTAL_VIEWPORT_SIZE, 5.0, 0.0)),
RigidBody::KinematicVelocityBased,
Collider::cuboid(size.x / 2.0, size.y / 2.0),
Velocity::linear(Vec2::new(50.0, 0.0)),
Velocity::linear(Vec2::new(5.0, 0.0)),
Sensor,
));
}
Expand All @@ -66,20 +67,20 @@ pub fn tow_car_system(
.translation
.distance(car_transform.translation);

if distance < 100.0 {
if distance < 5.0 {
tow_truck.phase = TowTruckPhase::RaisingCar;
beam_event_writer.send(BeamEvent {
source: tow_truck_entity,
target: car_entity,
source_offset: Vec3::new(15.0, 0.0, 0.0),
source_offset: Vec3::new(1.5, 0.0, 0.0),
});
commands
.entity(tow_truck_entity)
.add_child(tow_truck.target);

commands.entity(car_entity).insert((
RigidBody::KinematicVelocityBased,
Velocity::linear(Vec2::new(0.5, 15.0)),
Velocity::linear(Vec2::new(0.05, 1.5)),
));
tow_truck_velocity.linvel = Vec2::ZERO;

Expand All @@ -88,14 +89,14 @@ pub fn tow_car_system(
}
}
TowTruckPhase::RaisingCar => {
if car_transform.translation.y > -30.0 {
if car_transform.translation.y > 1.6 {
tow_truck.phase = TowTruckPhase::TowingTarget;
commands
.entity(car_entity)
.insert((RigidBody::Fixed, Velocity::linear(Vec2::new(0.0, 0.0))));
car_velocity.linvel = Vec2::ZERO;

tow_truck_velocity.linvel = Vec2::new(50.0, 0.0);
tow_truck_velocity.linvel = Vec2::new(5.0, 0.0);
}
}
TowTruckPhase::TowingTarget => {}
Expand Down
Loading

0 comments on commit d24b132

Please sign in to comment.