Compare commits
4
Commits
c9d43770b8
...
5d422ecef0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d422ecef0 | ||
|
|
0e5c2407ed | ||
|
|
fe88997cd1 | ||
|
|
582f9ed226 |
+11
-16
@@ -1,39 +1,34 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::world::Chunk;
|
||||
|
||||
// Test checkboard background
|
||||
|
||||
const COLS: usize = 35;
|
||||
const ROWS: usize = 500;
|
||||
const CELL_SIZE: f32 = 60.0;
|
||||
const COLOR_A: Color = Color::srgb(0.0, 0.0, 0.0);
|
||||
const COLOR_B: Color = Color::srgb(0.6, 0.2, 0.8);
|
||||
const ROWS_PER_CHUNK: i64 = 2;
|
||||
const CELL_SIZE: f32 = crate::world::CHUNK_HEIGHT / ROWS_PER_CHUNK as f32;
|
||||
const BOARD_WIDTH: f32 = 1920.0;
|
||||
const COLS: i64 = (BOARD_WIDTH / CELL_SIZE).ceil() as i64;
|
||||
|
||||
fn spawn_board(mut commands: Commands) {
|
||||
for row in 0..ROWS {
|
||||
pub fn spawn_background_chunk(commands: &mut Commands, id: i64) {
|
||||
for row in (id * ROWS_PER_CHUNK)..(id * ROWS_PER_CHUNK + ROWS_PER_CHUNK) {
|
||||
for col in 0..COLS {
|
||||
// Altern colors
|
||||
let color = if (row + col) % 2 == 0 {
|
||||
let color = if (row + col as i64) % 2 == 0 {
|
||||
COLOR_A
|
||||
} else {
|
||||
COLOR_B
|
||||
};
|
||||
|
||||
commands.spawn((
|
||||
Chunk { id },
|
||||
Sprite::from_color(color, Vec2::new(CELL_SIZE, CELL_SIZE)),
|
||||
Transform::from_xyz(
|
||||
(col as f32 + 0.5 - COLS as f32 / 2.0) * CELL_SIZE,
|
||||
(row as f32 + 0.5 - ROWS as f32 / 2.0) * CELL_SIZE,
|
||||
(row as f32 + 0.5) * CELL_SIZE,
|
||||
0.0,
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BackgroundPlugin;
|
||||
|
||||
impl Plugin for BackgroundPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_board);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,10 +1,10 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{collision::Collider, player::Player};
|
||||
use crate::{camera::MainCamera, collision::Collider, player::Player};
|
||||
|
||||
fn constrain_player(
|
||||
mut player: Single<(&Collider, &mut Transform), With<Player>>,
|
||||
camera: Single<&Camera, With<Camera2d>>,
|
||||
camera: Single<&Camera, With<MainCamera>>,
|
||||
) {
|
||||
let viewport = camera
|
||||
.logical_viewport_size()
|
||||
|
||||
+8
-5
@@ -3,14 +3,17 @@ use bevy::prelude::*;
|
||||
use crate::player::Player;
|
||||
use crate::player::move_player;
|
||||
|
||||
fn spawn_camera(mut commands: Commands) {
|
||||
commands.spawn(Camera2d);
|
||||
#[derive(Component)]
|
||||
pub struct MainCamera;
|
||||
|
||||
fn spawn_main_camera(mut commands: Commands) {
|
||||
commands.spawn((Camera2d, MainCamera));
|
||||
}
|
||||
|
||||
// Follow the player with the camera
|
||||
fn follow_player(
|
||||
player: Single<&Transform, (With<Player>, Without<Camera2d>)>,
|
||||
mut camera: Single<&mut Transform, (With<Camera2d>, Without<Player>)>,
|
||||
player: Single<&Transform, (With<Player>, Without<MainCamera>)>,
|
||||
mut camera: Single<&mut Transform, (With<MainCamera>, Without<Player>)>,
|
||||
// time: Res<Time>,
|
||||
) {
|
||||
camera.translation.y = player.translation.y; // without delay
|
||||
@@ -22,7 +25,7 @@ pub struct CameraPlugin;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_camera)
|
||||
app.add_systems(Startup, spawn_main_camera)
|
||||
.add_systems(Update, follow_player.after(move_player));
|
||||
}
|
||||
}
|
||||
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{camera::MainCamera, player::Player};
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DebugCamera;
|
||||
|
||||
fn spawn_debug_camera(mut commands: Commands) {
|
||||
commands.spawn((
|
||||
DebugCamera,
|
||||
Camera2d,
|
||||
Camera {
|
||||
is_active: false,
|
||||
..default()
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn setup_debug_camera(mut camera: Single<&mut Projection, With<DebugCamera>>) {
|
||||
if let Projection::Orthographic(ref mut projection) = **camera {
|
||||
projection.scale = 4.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_debug_camera(
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
mut main_camera: Single<&mut Camera, (With<MainCamera>, Without<DebugCamera>)>,
|
||||
mut debug_camera: Single<&mut Camera, (With<DebugCamera>, Without<MainCamera>)>,
|
||||
) {
|
||||
if input.just_pressed(KeyCode::Tab) {
|
||||
main_camera.is_active = !main_camera.is_active;
|
||||
debug_camera.is_active = !debug_camera.is_active;
|
||||
}
|
||||
}
|
||||
|
||||
fn follow_player_debug(
|
||||
player: Single<&Transform, (With<Player>, Without<DebugCamera>)>,
|
||||
mut debug_camera: Single<&mut Transform, (With<DebugCamera>, Without<Player>)>,
|
||||
) {
|
||||
debug_camera.translation.y = player.translation.y;
|
||||
}
|
||||
|
||||
enum FrameSide {
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct FramePart(FrameSide);
|
||||
|
||||
fn spawn_debug_frame(mut commands: Commands) {
|
||||
for side in [
|
||||
FrameSide::Top,
|
||||
FrameSide::Bottom,
|
||||
FrameSide::Left,
|
||||
FrameSide::Right,
|
||||
] {
|
||||
let size = match side {
|
||||
FrameSide::Top | FrameSide::Bottom => Vec2::new(1936.0, 8.0),
|
||||
FrameSide::Left | FrameSide::Right => Vec2::new(8.0, 1096.0),
|
||||
};
|
||||
|
||||
commands.spawn((
|
||||
FramePart(side),
|
||||
Sprite::from_color(Color::srgb(1.0, 0.0, 0.0), size),
|
||||
Visibility::Hidden,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_view_frame(
|
||||
main_camera: Single<&Transform, (With<MainCamera>, Without<DebugCamera>)>,
|
||||
debug_camera: Single<&Camera, (With<DebugCamera>, Without<MainCamera>)>,
|
||||
mut frame: Query<
|
||||
(&FramePart, &mut Transform, &mut Visibility),
|
||||
(Without<MainCamera>, Without<DebugCamera>),
|
||||
>,
|
||||
) {
|
||||
let center = main_camera.translation;
|
||||
let half_width = 960.0;
|
||||
let half_height = 540.0;
|
||||
|
||||
let visible = if debug_camera.is_active {
|
||||
Visibility::Visible
|
||||
} else {
|
||||
Visibility::Hidden
|
||||
};
|
||||
|
||||
for (frame_part, mut frame_transform, mut visibility) in &mut frame {
|
||||
let position = match frame_part.0 {
|
||||
FrameSide::Top => Vec3::new(center.x, center.y + half_height, 10.0),
|
||||
FrameSide::Bottom => Vec3::new(center.x, center.y - half_height, 10.0),
|
||||
FrameSide::Left => Vec3::new(center.x - half_width, center.y, 10.0),
|
||||
FrameSide::Right => Vec3::new(center.x + half_width, center.y, 10.0),
|
||||
};
|
||||
frame_transform.translation = position;
|
||||
*visibility = visible;
|
||||
}
|
||||
}
|
||||
pub struct DebugPlugin;
|
||||
|
||||
impl Plugin for DebugPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_debug_camera)
|
||||
.add_systems(Startup, setup_debug_camera.after(spawn_debug_camera))
|
||||
.add_systems(Startup, spawn_debug_frame)
|
||||
.add_systems(Update, draw_view_frame)
|
||||
.add_systems(Update, follow_player_debug)
|
||||
.add_systems(Update, toggle_debug_camera);
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -1,20 +1,23 @@
|
||||
use bevy::{prelude::*, window::PresentMode};
|
||||
|
||||
use crate::background::BackgroundPlugin;
|
||||
use crate::bounds::BoundsPlugin;
|
||||
use crate::camera::CameraPlugin;
|
||||
use crate::collision::CollisionPlugin;
|
||||
use crate::debug::DebugPlugin;
|
||||
use crate::obstacle::ObstaclePlugin;
|
||||
use crate::player::PlayerPlugin;
|
||||
use crate::score::ScorePlugin;
|
||||
use crate::world::WorldPlugin;
|
||||
|
||||
mod background;
|
||||
mod bounds;
|
||||
mod camera;
|
||||
mod collision;
|
||||
mod debug;
|
||||
mod obstacle;
|
||||
mod player;
|
||||
mod score;
|
||||
mod world;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
@@ -27,12 +30,13 @@ fn main() {
|
||||
}),
|
||||
..default()
|
||||
}))
|
||||
.add_plugins(BackgroundPlugin) // Checkerboard background
|
||||
.add_plugins(PlayerPlugin) // Player
|
||||
.add_plugins(CameraPlugin) // Camera
|
||||
.add_plugins(CollisionPlugin) // Collisions
|
||||
.add_plugins(ObstaclePlugin) // Obstacles
|
||||
.add_plugins(BoundsPlugin) // Bound player
|
||||
.add_plugins(ScorePlugin) // Score
|
||||
.add_plugins(WorldPlugin) // Chunk logic
|
||||
.add_plugins(DebugPlugin) // Debug
|
||||
.run();
|
||||
}
|
||||
|
||||
+6
-1
@@ -1,6 +1,10 @@
|
||||
use crate::{collision::Collider, player::reset_player};
|
||||
use bevy::prelude::*;
|
||||
use rand::RngExt;
|
||||
use rand::SeedableRng;
|
||||
|
||||
//TODO: add chunk logic to obstacles
|
||||
// TODO: add seed logic to obstacles
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Obstacle;
|
||||
@@ -64,7 +68,8 @@ pub struct ObstaclePlugin;
|
||||
|
||||
impl Plugin for ObstaclePlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, spawn_obstacles)
|
||||
app
|
||||
// .add_systems(Startup, spawn_obstacles)
|
||||
.init_resource::<RegenerateObstacles>()
|
||||
.add_systems(Update, regenerate_obstacles.after(reset_player));
|
||||
}
|
||||
|
||||
+2
-1
@@ -101,7 +101,8 @@ pub fn reset_player(
|
||||
|
||||
has_collided.0 = false;
|
||||
fall_started.0 = false;
|
||||
regenerate_obstacles.0 = true;
|
||||
// TODO: uncomment when obstacle chunk logic is implemented
|
||||
// regenerate_obstacles.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{camera::MainCamera, player::Player};
|
||||
|
||||
pub const CHUNK_HEIGHT: f32 = 700.0; // Height of a cunk
|
||||
const MARGIN_BELLOW: f32 = 400.0; // Height bellow the camera view it'll be gen
|
||||
const MARGIN_ABOVE: f32 = 400.0; // Height above the camera view it'll be gen
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Chunk {
|
||||
pub id: i64,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct RunSeed(pub u64);
|
||||
|
||||
fn stream_world(
|
||||
player: Single<&Transform, With<Player>>,
|
||||
entities: Query<(Entity, &Chunk)>,
|
||||
mut commands: Commands,
|
||||
camera: Single<&Camera, With<MainCamera>>,
|
||||
run_seed: Res<RunSeed>,
|
||||
) {
|
||||
let (min_id, max_id) = visible_chunk_range(
|
||||
player.translation.y,
|
||||
camera
|
||||
.logical_viewport_size()
|
||||
.unwrap_or(Vec2::new(1920.0, 1080.0))
|
||||
.y,
|
||||
);
|
||||
|
||||
let existing_ids: Vec<i64> = entities.iter().map(|(_, chunk)| chunk.id).collect();
|
||||
|
||||
for (entity, chunk) in entities {
|
||||
// Out of bounds implies despawn
|
||||
if chunk.id < min_id || chunk.id > max_id {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
|
||||
for id in min_id..=max_id {
|
||||
if !existing_ids.contains(&id) {
|
||||
spawn_chunk(&mut commands, id, run_seed.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bounds of chunk's ids to be generated
|
||||
fn visible_chunk_range(player_y: f32, camera_y: f32) -> (i64, i64) {
|
||||
(
|
||||
((player_y - camera_y / 2.0 - MARGIN_BELLOW) / CHUNK_HEIGHT).floor() as i64,
|
||||
((player_y + camera_y / 2.0 + MARGIN_ABOVE) / CHUNK_HEIGHT).floor() as i64,
|
||||
)
|
||||
}
|
||||
|
||||
fn spawn_chunk(commands: &mut Commands, id: i64, run_seed: u64) {
|
||||
crate::background::spawn_background_chunk(commands, id);
|
||||
// TODO: obstacles
|
||||
}
|
||||
pub struct WorldPlugin;
|
||||
|
||||
impl Plugin for WorldPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(RunSeed(rand::random::<u64>()))
|
||||
.add_systems(Update, stream_world);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user