Skip to content

Commit

Permalink
Yeet bevy_ecs_tilemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed May 8, 2024
1 parent d2228bb commit a65bebb
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 155 deletions.
16 changes: 1 addition & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ repository = "https://github.com/Seldom-SE/seldom_pixel"

[features]
line = ["dep:line_drawing"]
map = ["dep:bevy_ecs_tilemap"]
nav = ["dep:seldom_map_nav"]
particle = ["dep:bevy_turborand"]
state = ["dep:seldom_state"]

[dependencies]
bevy_ecs_tilemap = { git = "https://github.com/rparrett/bevy_ecs_tilemap", rev = "a7b308a", default-features = false, optional = true }
bevy_turborand = { version = "0.8.1", optional = true }
line_drawing = { version = "1.0", optional = true }
seldom_fn_plugin = "0.6.0"
seldom_map_nav = { git = "https://github.com/Seldom-SE/seldom_map_nav", rev = "b085403", optional = true }
seldom_map_nav = { version = "0.6.0", optional = true }
seldom_pixel_macros = { version = "0.1.0", path = "macros" }
seldom_state = { version = "0.10.0", optional = true }

Expand All @@ -37,10 +35,6 @@ leafwing-input-manager = "0.13.3"
rand = "0.8.5"
seldom_state = { version = "0.10.0", features = ["leafwing_input"] }

[[example]]
name = "animated_tilemap"
required-features = ["map"]

[[example]]
name = "line"
required-features = ["line"]
Expand All @@ -53,13 +47,5 @@ required-features = ["particle"]
name = "state"
required-features = ["state"]

[[example]]
name = "tilemap"
required-features = ["map"]

[[example]]
name = "tilemap_filter"
required-features = ["map"]

[package.metadata.docs.rs]
all-features = true
40 changes: 19 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@

`seldom_pixel` is a Bevy plugin for limited color palette pixel art games. It handles:

* Sprites
* Filters (defined through images; apply to layers or individual entities)
* Simple UI (text, buttons, and sprites locked to the camera)
* Tilemaps (through `bevy_ecs_tilemap`; enable `map` feature)
* Animations (for sprites, filters, tilesets, and text; supports dithering!)
* Custom layers
* Particles (with pre-simulation! Enable `particle` feature)
* Palette changing
* Typefaces
* An in-game cursor
* Camera
* Lines (enable `line` feature)
* And more to come!
- Sprites
- Filters (defined through images; apply to layers or individual entities)
- Simple UI (text, buttons, and sprites locked to the camera)
- Tilemaps
- Animations (for sprites, filters, tilesets, and text; supports dithering!)
- Custom layers
- Particles (with pre-simulation! Enable `particle` feature)
- Palette changing
- Typefaces
- An in-game cursor
- Camera
- Lines (enable `line` feature)
- And more to come!

It also features optional integration with:

* `bevy_ecs_tilemap` (only rectangular maps; `map` feature;
disable `bevy_ecs_tilemap`'s default features)
* `seldom_state` (for animation state machines; `state` feature)
* `seldom_map_nav` (makes `SubPxPosition` implement TODO; `nav` feature)
- `seldom_state` (for animation state machines; `state` feature)
- `seldom_map_nav` (makes `SubPxPosition` implement `Position2`; `nav` feature)

See the `examples` directory for examples. If you need help, feel free to ping me
on [the Bevy Discord server](https://discord.com/invite/bevy) (`@Seldom`)! If any of the docs
need improvement, feel free to submit an issue or pr!

## Philosophies

* Assets are created through images
- Assets are created through images

All assets, including filters, are loaded from images. `seldom_pixel`'s scope is limited
to rendered things, so this doesn't apply to things like levels and sounds. I recommend
Expand All @@ -43,7 +41,7 @@ about [Aseprite](https://github.com/aseprite/aseprite/), which you can use for f
can compile it. I've only used this plugin on `.png` files, so I recommend using that format,
but feel free to try it on other lossless formats.

* It is what it looks like
- It is what it looks like

This crate's position component, `PxPosition`, uses an `IVec2` (2-dimensional `i32` vector)
to store positions. This means that entities are located at exact pixel positions.
Expand All @@ -54,7 +52,7 @@ which I recommend using when possible. I also recommend resetting the `SubPxPosi
to `PxPosition`'s value when it stops moving, so moving objects feel consistent to the player.
This is less of a concern for games with smaller pixels.

* Sacrifice versatility for productivity
- Sacrifice versatility for productivity

If you are already interested in making a limited color palette pixel art game,
this is an easy win for you. Filters in `seldom_pixel` are just maps from each color
Expand All @@ -73,7 +71,7 @@ This crate is currently in maintenance mode, so I'm not currently adding new fea
- [ ] More advanced particle system
- [ ] More shape primitives
- [ ] Spatial filters that can filter defined areas, and apply their animations over space
instead of time. For effects like lighting and bloom.
instead of time. For effects like lighting and bloom.
- [ ] Make the rendering happen in the render world

## Usage
Expand Down
29 changes: 13 additions & 16 deletions examples/animated_tilemap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// In this program, animated tilemaps are spawned

use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
use seldom_pixel::prelude::*;

Expand All @@ -25,21 +24,21 @@ fn main() {
fn init(mut commands: Commands, mut tilesets: PxAssets<PxTileset>) {
commands.spawn(Camera2dBundle::default());

let map_size = TilemapSize { x: 2, y: 4 };
let mut storage = TileStorage::empty(map_size);
let mut map = PxMap::new(UVec2::new(2, 4));
let mut rng = thread_rng();

for x in 0..2 {
for y in 0..4 {
// Each tile must be added to the `TileStorage`
storage.set(
&TilePos { x, y },
commands
.spawn(PxTileBundle {
texture: TileTextureIndex(rng.gen_range(0..4)),
..default()
})
.id(),
map.set(
Some(
commands
.spawn(PxTileBundle {
tile: rng.gen_range(0..4).into(),
..default()
})
.id(),
),
UVec2::new(x, y),
);
}
}
Expand All @@ -49,8 +48,7 @@ fn init(mut commands: Commands, mut tilesets: PxAssets<PxTileset>) {
// Spawn the map
commands.spawn((
PxMapBundle::<Layer> {
size: map_size,
storage: storage.clone(),
map: map.clone(),
tileset: tileset.clone(),
..default()
},
Expand All @@ -64,8 +62,7 @@ fn init(mut commands: Commands, mut tilesets: PxAssets<PxTileset>) {

commands.spawn((
PxMapBundle::<Layer> {
size: map_size,
storage,
map,
tileset,
position: IVec2::new(8, 0).into(),
..default()
Expand Down
26 changes: 12 additions & 14 deletions examples/tilemap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// In this program, a tilemap is spawned

use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
use seldom_pixel::prelude::*;

Expand All @@ -25,29 +24,28 @@ fn main() {
fn init(mut commands: Commands, mut tilesets: PxAssets<PxTileset>) {
commands.spawn(Camera2dBundle::default());

let map_size = TilemapSize { x: 4, y: 4 };
let mut storage = TileStorage::empty(map_size);
let mut map = PxMap::new(UVec2::splat(4));
let mut rng = thread_rng();

for x in 0..4 {
for y in 0..4 {
// Each tile must be added to the `TileStorage`
storage.set(
&TilePos { x, y },
commands
.spawn(PxTileBundle {
texture: TileTextureIndex(rng.gen_range(0..4)),
..default()
})
.id(),
map.set(
Some(
commands
.spawn(PxTileBundle {
tile: rng.gen_range(0..4).into(),
..default()
})
.id(),
),
UVec2::new(x, y),
);
}
}

// Spawn the map
commands.spawn(PxMapBundle::<Layer> {
size: map_size,
storage,
map,
tileset: tilesets.load("tileset/tileset.png", UVec2::splat(4)),
..default()
});
Expand Down
29 changes: 14 additions & 15 deletions examples/tilemap_filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// In this program, a filter is applied to a tilemap and its tiles

use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use rand::{thread_rng, Rng};
use seldom_pixel::prelude::*;

Expand Down Expand Up @@ -29,33 +28,33 @@ fn init(
) {
commands.spawn(Camera2dBundle::default());

let map_size = TilemapSize { x: 4, y: 4 };
let mut map = PxMap::new(UVec2::splat(4));
let dim = filters.load("filter/dim.png");
let mut storage = TileStorage::empty(map_size);
let mut rng = thread_rng();

for x in 0..4 {
for y in 0..4 {
// Each tile must be added to the `TileStorage`
storage.set(
&TilePos { x, y },
commands
.spawn(PxTileBundle {
texture: TileTextureIndex(rng.gen_range(0..4)),
..default()
})
// Insert a filter on the tile
.insert(dim.clone())
.id(),
map.set(
Some(
commands
.spawn(PxTileBundle {
tile: rng.gen_range(0..4).into(),
..default()
})
// Insert a filter on the tile
.insert(dim.clone())
.id(),
),
UVec2::new(x, y),
);
}
}

// Spawn the map
commands
.spawn(PxMapBundle::<Layer> {
size: map_size,
storage,
map,
tileset: tilesets.load("tileset/tileset.png", UVec2::splat(4)),
..default()
})
Expand Down
2 changes: 0 additions & 2 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::Duration;

use bevy::utils::Instant;

#[cfg(feature = "map")]
use crate::map::PxTilesetData;
use crate::{
asset::{PxAsset, PxAssetData},
Expand Down Expand Up @@ -34,7 +33,6 @@ pub(crate) fn animation_plugin(app: &mut App) {
.in_set(PxSet::FinishAnimations),
);

#[cfg(feature = "map")]
app.add_systems(
PostUpdate,
finish_animations::<PxTilesetData>.in_set(PxSet::FinishAnimations),
Expand Down
10 changes: 2 additions & 8 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use bevy::{

use seldom_fn_plugin::FnPluginExt;

#[cfg(feature = "map")]
use crate::map::PxTilesetData;
use crate::{
filter::PxFilterData,
Expand All @@ -30,9 +29,8 @@ pub(crate) fn asset_plugin(app: &mut App) {
)
.fn_plugin(px_asset_plugin::<PxSpriteData>)
.fn_plugin(px_asset_plugin::<PxTypefaceData>)
.fn_plugin(px_asset_plugin::<PxFilterData>);
#[cfg(feature = "map")]
app.fn_plugin(px_asset_plugin::<PxTilesetData>);
.fn_plugin(px_asset_plugin::<PxFilterData>)
.fn_plugin(px_asset_plugin::<PxTilesetData>);
}

fn px_asset_plugin<D: PxAssetData>(app: &mut App) {
Expand Down Expand Up @@ -172,7 +170,6 @@ impl<'w, 's> PxAssets<'w, 's, PxSprite> {
}
}

#[cfg(feature = "map")]
impl<'w, 's> PxAssets<'w, 's, PxTileset> {
/// Loads a tileset. Works for animated tilesets.
pub fn load<'a>(
Expand Down Expand Up @@ -241,15 +238,13 @@ pub(crate) fn get_asset<'a, D: PxAssetData>(
}

mod sealed {
#[cfg(feature = "map")]
use crate::map::PxTilesetData;
use crate::{filter::PxFilterData, prelude::*, sprite::PxSpriteData, text::PxTypefaceData};

pub trait PxAssetTraitSealed {}

impl PxAssetTraitSealed for PxSprite {}

#[cfg(feature = "map")]
impl PxAssetTraitSealed for PxTileset {}

impl PxAssetTraitSealed for PxTypeface {}
Expand All @@ -260,7 +255,6 @@ mod sealed {

impl PxAssetDataSealed for PxSpriteData {}

#[cfg(feature = "map")]
impl PxAssetDataSealed for PxTilesetData {}

impl PxAssetDataSealed for PxTypefaceData {}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod filter;
mod image;
#[cfg(feature = "line")]
mod line;
#[cfg(feature = "map")]
mod map;
pub mod math;
pub mod palette;
Expand Down
Loading

0 comments on commit a65bebb

Please sign in to comment.