my first crate
This commit is contained in:
65
src/bin/minesweeper.rs
Normal file
65
src/bin/minesweeper.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use bevy::{prelude::*, window::WindowResolution};
|
||||
use glite::debug_plugin;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::ops::{Add, Sub};
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
use bevy_inspector_egui::{prelude::*, quick::WorldInspectorPlugin};
|
||||
|
||||
#[cfg_attr(feature = "debug", derive(bevy_inspector_egui::InspectorOptions))]
|
||||
#[derive(Component, Debug, Default, Copy, Clone, Eq, PartialEq)]
|
||||
pub struct Position {
|
||||
pub x: u16,
|
||||
pub y: u16,
|
||||
}
|
||||
|
||||
impl Add for Position {
|
||||
type Output = Self;
|
||||
fn add(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x + other.x,
|
||||
y: self.y + other.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for Position {
|
||||
type Output = Self;
|
||||
fn sub(self, other: Self) -> Self {
|
||||
Self {
|
||||
x: self.x.saturating_sub(other.x),
|
||||
y: self.y.saturating_sub(other.y),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Position {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut app = App::new();
|
||||
app.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: "Minesweeper".to_string(),
|
||||
resolution: WindowResolution::new(1280., 777.),
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
}));
|
||||
|
||||
app.add_systems(Startup, camera_setup);
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
app.add_plugins(debug_plugin::debug_plugin);
|
||||
|
||||
// Run the app
|
||||
app.run();
|
||||
}
|
||||
|
||||
fn camera_setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2d {});
|
||||
commands.spawn(Position { x: 0, y: 0 });
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use bevy::{prelude::*, time::common_conditions::on_timer, window::PrimaryWindow};
|
||||
use bevy::{prelude::*, reflect, time::common_conditions::on_timer, window::PrimaryWindow};
|
||||
use bevy_inspector_egui::prelude::*;
|
||||
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
||||
use rand::prelude::random;
|
||||
|
||||
@ -47,7 +48,7 @@ struct LastTailPosition(Option<Position>);
|
||||
#[derive(Component)]
|
||||
struct Food;
|
||||
|
||||
#[derive(Component, Clone, Copy, Eq, PartialEq, Debug)]
|
||||
#[derive(Component, Clone, Copy, Eq, PartialEq, Debug, InspectorOptions)]
|
||||
struct Position {
|
||||
x: i32,
|
||||
y: i32,
|
||||
@ -268,7 +269,7 @@ fn snake_growth(
|
||||
}
|
||||
|
||||
fn setup_camera(mut commands: Commands) {
|
||||
commands.spawn((Camera2d {},));
|
||||
commands.spawn(Camera2d {});
|
||||
}
|
||||
|
||||
pub struct SnakePlugin;
|
||||
|
6
src/debug_plugin/mod.rs
Normal file
6
src/debug_plugin/mod.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_inspector_egui::{prelude::*, quick::WorldInspectorPlugin};
|
||||
|
||||
pub fn debug_plugin(app: &mut App) {
|
||||
app.add_plugins(WorldInspectorPlugin::new());
|
||||
}
|
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod debug_plugin;
|
Reference in New Issue
Block a user