Skip to content

Commit

Permalink
feat: migrate improvements from jam 🧃
Browse files Browse the repository at this point in the history
feat: add pixel and resizable features
refactor: game camera now has its own module
change: now the background color is rendered differently
change: different ui cleaning
  • Loading branch information
eerii committed Dec 15, 2023
1 parent 3663e70 commit a859b3a
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ jobs:
uses: actions/download-artifact@v3
with:
name: wasm
path: ./builds/wasm
path: ./builds

- name: Install butler
run: |
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dev = [
"bevy/file_watcher", # Enables hot reloading of assets
"bevy/dynamic_linking", # Enables dynamic linking for faster compilation
]
pixel_perfect = []
resizable = []
save_schedule = []
mock_touch = []

Expand Down
47 changes: 4 additions & 43 deletions examples/dvd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use bevy::{
prelude::*,
sprite::MaterialMesh2dBundle,
};
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
use bevy_persistent::Persistent;
use hello_bevy::{
Expand All @@ -27,16 +24,12 @@ impl Plugin for SampleGamePlugin {
app.add_event::<CollisionEvent>()
.add_systems(
OnEnter(GameState::Play),
(
init_sample.run_if(run_once()),
resume_game,
),
init_sample.run_if(run_once()),
)
.add_systems(
Update,
(update_sample, on_collision).run_if(in_state(GameState::Play)),
)
.add_systems(OnExit(GameState::Play), pause_game);
);
}
}

Expand All @@ -50,9 +43,6 @@ struct Velocity(Vec2);
#[derive(Component)]
struct Counter(u32);

#[derive(Component)]
struct GameCamera;

// ······
// Events
// ······
Expand All @@ -64,24 +54,7 @@ struct CollisionEvent(Entity);
// Systems
// ·······

fn init_sample(
mut cmd: Commands,
assets: Res<CoreAssets>,
opts: Res<Persistent<GameOptions>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
// Camera
cmd.spawn((Camera2dBundle::default(), GameCamera));

// Background
cmd.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
transform: Transform::from_xyz(0., 0., -10.).with_scale(SIZE.extend(1.)),
material: materials.add(ColorMaterial::from(opts.color.darker)),
..default()
});

fn init_sample(mut cmd: Commands, assets: Res<CoreAssets>, opts: Res<Persistent<GameOptions>>) {
// Sprites
for velocity in [
Vec2::new(300., 250.),
Expand Down Expand Up @@ -176,18 +149,6 @@ fn on_collision(
}
}

fn resume_game(mut cam: Query<&mut Camera, With<GameCamera>>) {
if let Ok(mut cam) = cam.get_single_mut() {
cam.is_active = true;
}
}

fn pause_game(mut cam: Query<&mut Camera, With<GameCamera>>) {
if let Ok(mut cam) = cam.get_single_mut() {
cam.is_active = false;
}
}

// ·····
// Extra
// ·····
Expand Down
20 changes: 1 addition & 19 deletions examples/jump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use bevy::{
prelude::*,
sprite::MaterialMesh2dBundle,
};
use bevy_persistent::Persistent;
use hello_bevy::{
Expand Down Expand Up @@ -69,24 +68,7 @@ struct GameCamera;
// Systems
// ·······

fn init_sample(
mut cmd: Commands,
assets: Res<CoreAssets>,
opts: Res<Persistent<GameOptions>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
// Camera
cmd.spawn((Camera2dBundle::default(), GameCamera));

// Background
cmd.spawn(MaterialMesh2dBundle {
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
transform: Transform::from_xyz(0., 0., -10.).with_scale(SIZE.extend(1.)),
material: materials.add(ColorMaterial::from(opts.color.darker)),
..default()
});

fn init_sample(mut cmd: Commands, assets: Res<CoreAssets>, opts: Res<Persistent<GameOptions>>) {
// Player
cmd.spawn((
SpriteBundle {
Expand Down
10 changes: 10 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,21 @@ impl Plugin for AssetLoaderPlugin {
// They are loaded inmediately after the app is fired, no effect on loading state
#[derive(AssetCollection, Resource)]
pub struct CoreAssets {
#[cfg(not(feature = "pixel_perfect"))]
#[asset(path = "icons/bevy.png")]
pub bevy_icon: Handle<Image>,

#[cfg(not(feature = "pixel_perfect"))]
#[asset(path = "fonts/sans.ttf")]
pub font: Handle<Font>,

#[cfg(feature = "pixel_perfect")]
#[asset(path = "icons/pixelbevy.png")]
pub bevy_icon: Handle<Image>,

#[cfg(feature = "pixel_perfect")]
#[asset(path = "fonts/pixel.ttf")]
pub font: Handle<Font>,
}

// Example assets
Expand Down
67 changes: 67 additions & 0 deletions src/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use bevy::{
core_pipeline::clear_color::ClearColorConfig,
prelude::*,
};
use bevy_persistent::Persistent;

use crate::{
GameOptions,
GameState,
};

// TODO: Option for pixel perfect upscaling camera

// ······
// Plugin
// ······

pub struct CameraPlugin;

impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Play), init_camera)
.add_systems(
Update,
change_background.run_if(
in_state(GameState::Play).and_then(resource_changed::<
Persistent<GameOptions>,
>()),
),
)
.add_systems(OnExit(GameState::Play), pause_camera);
}
}

// ··········
// Components
// ··········

#[derive(Component)]
struct GameCamera;

// ·······
// Systems
// ·······

fn init_camera(mut cmd: Commands, mut cam: Query<&mut Camera, With<GameCamera>>) {
if let Ok(mut cam) = cam.get_single_mut() {
cam.is_active = true;
} else {
cmd.spawn((Camera2dBundle::default(), GameCamera));
}
}

fn change_background(
opts: Res<Persistent<GameOptions>>,
mut cam: Query<&mut Camera2d, With<GameCamera>>,
) {
if let Ok(mut cam) = cam.get_single_mut() {
cam.clear_color = ClearColorConfig::Custom(opts.color.dark);
}
}

fn pause_camera(mut cam: Query<&mut Camera, With<GameCamera>>) {
if let Ok(mut cam) = cam.get_single_mut() {
cam.is_active = false;
}
}
14 changes: 2 additions & 12 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
FONT_MULTIPLIERS,
FONT_SIZES,
},
GameState,
MovementAxis,
};

Expand All @@ -29,10 +28,7 @@ pub struct DataPlugin;

impl Plugin for DataPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
OnEnter(GameState::Loading),
init_persistence,
);
app.add_systems(Startup, init_persistence);
}
}

Expand Down Expand Up @@ -62,7 +58,6 @@ pub struct ColorPalette {
pub light: Color,
pub mid: Color,
pub dark: Color,
pub darker: Color,
}

impl Default for ColorPalette {
Expand All @@ -78,12 +73,7 @@ impl Default for ColorPalette {
173.0 / 255.0,
118.0 / 255.0,
),
dark: Color::rgb(
43.0 / 255.0,
115.0 / 255.0,
77.0 / 255.0,
),
darker: Color::rgb(55.0 / 255.0, 84.0 / 255.0, 70.0 / 255.0),
dark: Color::rgb(55.0 / 255.0, 84.0 / 255.0, 70.0 / 255.0),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ impl Plugin for InputPlugin {
handle_input_mouse,
handle_input_gamepad,
handle_input_touch,
)
.run_if(resource_exists::<Persistent<Keybinds>>()),
),
)
.add_systems(PostUpdate, clear_input);

Expand Down
46 changes: 30 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod assets;
mod audio;
mod camera;
mod data;
mod debug;
mod input;
Expand All @@ -9,7 +10,6 @@ mod utils;
use bevy::{
asset::AssetMetaCheck,
prelude::*,
window::WindowResolution,
};

// Exports for examples
Expand All @@ -30,9 +30,9 @@ pub use crate::{
},
};

// TODO: Port improvements from the game jam
// TODO: Add compilation guards for extra features (such as pixel art or resizable)
// TODO: Option for pixel perfect upscaling camera
// [CHANGE]: Game title and resolution
pub const GAME_TITLE: &str = "Hello Bevy!";
pub const INITIAL_RESOLUTION: Vec2 = Vec2::new(600., 600.);

// Game state
#[derive(States, Debug, Default, Clone, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -64,20 +64,33 @@ impl Plugin for GamePlugin {
}

// Default plugins
app.add_plugins(
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Hello Bevy!".to_string(), // [CHANGE]: Game title
resolution: WindowResolution::new(600., 600.),
resizable: false, // Or use fit_canvas_to_parent: true for resizing on the web
canvas: Some("#bevy".to_string()),
prevent_default_event_handling: false,
..default()
}),
#[allow(unused_mut)]
let mut window_plugin = WindowPlugin {
primary_window: Some(Window {
title: GAME_TITLE.into(),
resolution: INITIAL_RESOLUTION.into(),
resizable: false,
canvas: Some("#bevy".to_string()),
prevent_default_event_handling: false,
..default()
}),
//.set(ImagePlugin::default_nearest()), // [CHANGE]: Use if your game is pixel art
);
..default()
};

#[cfg(feature = "resizable")]
{
let win = window_plugin.primary_window.as_mut().unwrap();
win.resizable = true;
win.fit_canvas_to_parent = true;
}

#[cfg(not(feature = "pixel_perfect"))]
let image_plugin = ImagePlugin::default();

#[cfg(feature = "pixel_perfect")]
let image_plugin = ImagePlugin::default_nearest();

app.add_plugins(DefaultPlugins.set(window_plugin).set(image_plugin));

// Game
app.add_state::<GameState>().add_plugins((
Expand All @@ -86,6 +99,7 @@ impl Plugin for GamePlugin {
data::DataPlugin,
input::InputPlugin,
audio::AudioPlugin,
camera::CameraPlugin
));

// Debug only plugins
Expand Down
Loading

0 comments on commit a859b3a

Please sign in to comment.