use crate::bevy_ryot::drawing::*;
use bevy::ecs::system::Command;
#[derive(Debug, Clone)]
pub struct UpdateTileContent {
pub new: Vec<DrawingInfo>,
pub old: Vec<DrawingInfo>,
}
impl UpdateTileContent {
pub fn new(new: Vec<DrawingInfo>, old: Vec<DrawingInfo>) -> Self {
if new.len() != old.len() {
panic!("The new and old content must have the same length");
}
Self { new, old }
}
pub fn for_new_bundle(bundles: Vec<DrawingBundle>) -> Self {
Self::new(
bundles
.iter()
.copied()
.map(|bundle| bundle.into())
.collect::<Vec<DrawingInfo>>(),
bundles
.into_iter()
.map(|bundle| (bundle.tile_pos, bundle.layer, bundle.visibility, None))
.collect(),
)
}
pub fn revert(&self) -> Self {
Self::new(self.old.clone(), self.new.clone())
}
}
impl From<UpdateTileContent> for CommandState {
fn from(_: UpdateTileContent) -> Self {
CommandState::default()
}
}
impl Command for UpdateTileContent {
fn apply(self, world: &mut World) {
for (index, info) in self.new.iter().enumerate() {
let old = self.old[index];
if *info == old {
continue;
}
if info.3.is_none() && old.3.is_none() {
continue;
}
update(world, *info, old, self.clone().into());
}
}
}