Skip to content

Commit

Permalink
added dvd and tty logo savers
Browse files Browse the repository at this point in the history
  • Loading branch information
cxreiff committed Aug 16, 2024
1 parent 7c7d870 commit 516ca4c
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
repository: "cxreiff/tap"
repository: "cxreiff/homebrew-tap"
token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
# So we have access to the formula
- name: Fetch homebrew formulae
Expand Down
Binary file added assets/dvd_logo.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 added assets/tty_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ use bevy::prelude::*;
pub(super) fn plugin(app: &mut App) {
let prefix = "src/";
embedded_asset!(app, prefix, "../assets/bubble.png");
embedded_asset!(app, prefix, "../assets/dvd_logo.png");
embedded_asset!(app, prefix, "../assets/tty_logo.png");
}
10 changes: 0 additions & 10 deletions src/bubbles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,6 @@ fn bubbles_setup_system(
camera.camera.target = ratatui_render.target("main").unwrap_or_default();
commands.spawn(camera);

commands.spawn(PointLightBundle {
transform: Transform::from_xyz(0., 0., -5.),
point_light: PointLight {
intensity: 10_000.,
shadows_enabled: true,
..default()
},
..default()
});

let rng = ChaCha8Rng::seed_from_u64(19878367467712);
commands.insert_resource(BubbleRng(rng));
commands.insert_resource(BubbleSprite(
Expand Down
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use std::time::Duration;
use bevy::{app::ScheduleRunnerPlugin, log::LogPlugin, prelude::*, window::ExitCondition};
use bevy_ratatui::RatatuiPlugins;
use bevy_ratatui_render::RatatuiRenderPlugin;
use logo::LogoPath;
pub use logo::{LOGO_PATH_DVD, LOGO_PATH_TTY};
use rand::{distributions::Standard, prelude::Distribution, Rng};

mod assets;
mod bubbles;
mod common;
mod logo;
mod maze;
mod pipes;

pub struct AppPlugin(pub SaverVariant);

Expand All @@ -26,17 +28,21 @@ impl Plugin for AppPlugin {
.disable::<LogPlugin>(),
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1. / 60.)),
RatatuiPlugins::default(),
RatatuiRenderPlugin::new("main", (1, 1)).autoresize(),
RatatuiRenderPlugin::new("main", (256, 256)).autoresize(),
))
.insert_resource(Msaa::Off)
.init_resource::<Flags>();

app.add_plugins((assets::plugin, common::plugin));

if let SaverVariant::Logo(ref logo_path) = self.0 {
app.insert_resource(LogoPath(logo_path.into()));
}

app.add_plugins(match self.0 {
SaverVariant::Maze => maze::plugin,
SaverVariant::Pipes => pipes::plugin,
SaverVariant::Bubbles => bubbles::plugin,
SaverVariant::Logo(_) => logo::plugin,
SaverVariant::Maze => maze::plugin,
});
}
}
Expand All @@ -48,17 +54,17 @@ pub struct Flags {
}

pub enum SaverVariant {
Maze,
Pipes,
Bubbles,
Logo(String),
Maze,
}

impl Distribution<SaverVariant> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> SaverVariant {
match rng.gen_range(0..=2) {
0 => SaverVariant::Maze,
1 => SaverVariant::Pipes,
_ => SaverVariant::Bubbles,
0 => SaverVariant::Bubbles,
1 => SaverVariant::Logo(LOGO_PATH_TTY.into()),
_ => SaverVariant::Maze,
}
}
}
123 changes: 123 additions & 0 deletions src/logo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use bevy::prelude::*;
use bevy_ratatui::event::ResizeEvent;
use bevy_ratatui_render::RatatuiRenderContext;
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;

pub const LOGO_PATH_DVD: &str = "embedded://ttysvr/../assets/dvd_logo.png";
pub const LOGO_PATH_TTY: &str = "embedded://ttysvr/../assets/tty_logo.png";

const ORTHO_SCALING: f32 = 0.5;
const LOGO_RADIUS: f32 = 32.;
const LOGO_SPEED: f32 = 24.;

pub(super) fn plugin(app: &mut App) {
app.init_resource::<LogoVisibleRegion>()
.add_systems(Startup, logo_setup_system)
.add_systems(Update, (handle_resize_system, logo_movement_system));
}

#[derive(Component, Deref, DerefMut)]
pub struct Logo {
#[deref]
velocity: Vec2,
}

#[derive(Resource, Deref)]
pub struct LogoPath(pub String);

#[derive(Resource, Deref, DerefMut, Default)]
pub struct LogoVisibleRegion(Vec2);

#[derive(Bundle)]
pub struct LogoBundle {
logo: Logo,
sprite: SpriteBundle,
}

impl LogoBundle {
fn new(rng: &mut ChaCha8Rng, texture: Handle<Image>, region: Rectangle) -> Self {
Self {
logo: Logo {
velocity: Vec2::new(LOGO_SPEED, -LOGO_SPEED),
},
sprite: SpriteBundle {
transform: Transform::from_translation(region.sample_interior(rng).extend(0.)),
texture,
sprite: Sprite {
color: Color::hsl(0., 1.0, 0.6),
custom_size: Some(Vec2::splat(LOGO_RADIUS * 2.)),
..default()
},
..default()
},
}
}
}

fn logo_setup_system(
mut commands: Commands,
ratatui_render: Res<RatatuiRenderContext>,
asset_server: Res<AssetServer>,
mut visible_region: ResMut<LogoVisibleRegion>,
logo_path: Res<LogoPath>,
) {
let mut camera = Camera2dBundle::default();
camera.projection.scale = ORTHO_SCALING;
camera.camera.target = ratatui_render.target("main").unwrap_or_default();
commands.spawn(camera);

**visible_region = get_visible_region(&ratatui_render);
let texture = asset_server.load(&**logo_path);
let mut rng = ChaCha8Rng::seed_from_u64(19878367467712);
commands.spawn(LogoBundle::new(
&mut rng,
texture,
Rectangle::from_size(**visible_region * 0.5 - LOGO_RADIUS * 2.),
));
}

fn handle_resize_system(
mut resize_events: EventReader<ResizeEvent>,
mut visible_region: ResMut<LogoVisibleRegion>,
ratatui_render: Res<RatatuiRenderContext>,
) {
for _ in resize_events.read() {
**visible_region = get_visible_region(&ratatui_render);
}
}

fn logo_movement_system(
time: Res<Time>,
mut logo: Query<(&mut Transform, &mut Sprite, &mut Logo)>,
visible_region: Res<LogoVisibleRegion>,
) {
for (mut transform, mut sprite, mut velocity) in &mut logo {
let visible_area = Rectangle::from_size(**visible_region - LOGO_RADIUS * 1.95);
let visible_half = visible_area.half_size.extend(0.);

transform.translation += velocity.extend(0.) * time.delta_seconds();

if (transform.translation.x < -visible_half.x && velocity.x < 0.)
|| (transform.translation.x > visible_half.x && velocity.x > 0.)
{
velocity.x *= -1.;
let next_hue = (sprite.color.hue() + 68.) % 360.;
sprite.color.set_hue(next_hue);
}

if (transform.translation.y < -visible_half.y - LOGO_RADIUS * 0.55 && velocity.y < 0.)
|| (transform.translation.y > visible_half.y + LOGO_RADIUS * 0.55 && velocity.y > 0.)
{
velocity.y *= -1.;
let next_hue = (sprite.color.hue() + 68.) % 360.;
sprite.color.set_hue(next_hue);
}
}
}

fn get_visible_region(ratatui_render: &RatatuiRenderContext) -> Vec2 {
let (width, height) = ratatui_render.dimensions("main").unwrap();
let terminal_dimensions = Vec2::new(width as f32, height as f32);
terminal_dimensions * ORTHO_SCALING
}
42 changes: 35 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{env, fmt::Display};

use bevy::app::App;
use clap::{Parser, Subcommand};
use ttysvr::{AppPlugin, SaverVariant};
use ttysvr::{AppPlugin, SaverVariant, LOGO_PATH_DVD, LOGO_PATH_TTY};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -31,17 +31,41 @@ struct Cli {

#[derive(Subcommand)]
pub enum Variant {
Maze,
Pipes,
Bubbles,
Logo {
#[command(subcommand)]
variant: Option<LogoVariant>,
},
Maze,
}

#[derive(Subcommand)]
pub enum LogoVariant {
Dvd,
Tty,
}

impl Display for Variant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Variant::Maze => write!(f, "maze"),
Variant::Pipes => write!(f, "pipes"),
Variant::Bubbles => write!(f, "bubbles"),
Variant::Logo { variant } => {
if let Some(variant) = variant {
write!(f, "logo {}", variant)
} else {
write!(f, "logo")
}
}
Variant::Maze => write!(f, "maze"),
}
}
}

impl Display for LogoVariant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogoVariant::Dvd => write!(f, "dvd"),
LogoVariant::Tty => write!(f, "tty"),
}
}
}
Expand All @@ -54,9 +78,13 @@ fn main() {
} = Cli::parse();

let saver_variant = match variant {
Some(Variant::Maze) => SaverVariant::Maze,
Some(Variant::Pipes) => SaverVariant::Pipes,
Some(Variant::Bubbles) => SaverVariant::Bubbles,
Some(Variant::Logo { ref variant }) => match variant {
Some(LogoVariant::Dvd) => SaverVariant::Logo(LOGO_PATH_DVD.into()),
Some(LogoVariant::Tty) => SaverVariant::Logo(LOGO_PATH_TTY.into()),
None => SaverVariant::Logo(LOGO_PATH_TTY.into()),
},
Some(Variant::Maze) => SaverVariant::Maze,
None => rand::random(),
};

Expand Down
28 changes: 0 additions & 28 deletions src/pipes.rs

This file was deleted.

0 comments on commit 516ca4c

Please sign in to comment.