Skip to content
Snippets Groups Projects
types.rs 17.1 KiB
Newer Older
Louis's avatar
Louis committed
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
Louis's avatar
Louis committed
use std::ops::{Deref, DerefMut};
Louis's avatar
Louis committed
use bevy::math::{IVec2, Rect, UVec2, Vec2};
use ldtk_rust::{EntityInstance, FieldInstance, LayerInstance, Level, TileInstance};
use num_traits::AsPrimitive;
Louis's avatar
Louis committed
use quadtree_rs::area::{Area, AreaBuilder};
use quadtree_rs::point::Point;
use quadtree_rs::Quadtree;
Louis's avatar
Louis committed
use serde::{Deserialize, Serialize};
use serde_json::{Map, Number, Value};
use crate::ldtk::{EntityInstance, FieldInstance, LayerInstance, Level, TileInstance};
use crate::utils::{Indexer, SerdeClone};
Louis's avatar
Louis committed
use crate::{get_ldtk_tile_scale, px_to_grid, MapQuery};

#[derive(Default, Clone, Debug, Ord, PartialOrd, PartialEq, Eq)]
pub struct LevelDataUpdated(pub String);

pub struct TileRef<'a> {
	pub tile: &'a TileInstance,
}

#[repr(C)]
Louis's avatar
Louis committed
#[derive(Serialize, Deserialize, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Default, Debug)]
pub enum TileFlip {
	#[default]
	None = 0,
	Horizontal = 1,
	Vertical = 2,
	Both = 3,
}

impl TileFlip {
	pub fn is_horizontal(&self) -> bool {
		match self {
			Self::None | Self::Vertical => false,
			Self::Horizontal | Self::Both => true,
		}
	}
	pub fn is_vertical(&self) -> bool {
		match self {
			Self::None | Self::Horizontal => false,
			Self::Vertical | Self::Both => true,
		}
	}
}

impl<'a> TileRef<'a> {
	pub fn new(tile: &'a TileInstance) -> Self {
		TileRef { tile }
	}
	pub fn gid(&self) -> usize {
		(self.tile.src[0] * self.tile.src[1]).unsigned_abs() as usize
	}
	pub fn get_flip(&self) -> TileFlip {
		match self.tile.f {
			1 => TileFlip::Horizontal,
			2 => TileFlip::Vertical,
			3 => TileFlip::Both,
			_ => TileFlip::None,
		}
	}
}

impl<'a> Debug for TileRef<'a> {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("TileRef")
			.field("gid", &self.gid())
			.field("tile.t", &self.tile.t)
			.field("tile.d", &self.tile.d)
			.field("tile.px", &self.tile.px)
			.field("tile.src", &self.tile.src)
			.finish()
	}
}

Louis's avatar
Louis committed
#[derive(Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub struct SpatialIndex(i64, i64);
impl<A, B> From<(A, B)> for SpatialIndex
where
	A: AsPrimitive<i64>,
	B: AsPrimitive<i64>,
{
	fn from(value: (A, B)) -> Self {
		Self(value.0.as_(), value.1.as_())
	}
}
impl From<UVec2> for SpatialIndex {
	fn from(value: UVec2) -> Self {
		Self(value.x as i64, value.y as i64)
	}
}
impl From<IVec2> for SpatialIndex {
	fn from(value: IVec2) -> Self {
		Self(value.x as i64, value.y as i64)
	}
}

Louis's avatar
Louis committed
pub type ColliderQuadtree = Quadtree<i64, LocatedEntity>;

pub struct LdtkLevel {
	level: Level,
Louis's avatar
Louis committed
	properties: HashMap<String, Value>,
	processed_layers: Vec<LdtkLayer>,
Louis's avatar
Louis committed
	colliders: ColliderQuadtree,
Louis's avatar
Louis committed
	pub fn width(&self) -> f32 {
		self.level.px_wid as f32
	}
	pub fn width_i(&self) -> i64 {
		self.level.px_wid
	}
	pub fn height(&self) -> f32 {
		self.level.px_hei as f32
	}
	pub fn height_i(&self) -> i64 {
		self.level.px_hei
	}
	pub fn level_ref(&self) -> &Level {
		&self.level
	}
	pub fn level_ref_mut(&mut self) -> &mut Level {
		&mut self.level
	}
Louis's avatar
Louis committed
	pub fn colliders_ref(&self) -> &ColliderQuadtree {
		&self.colliders
	}
	pub fn colliders_ref_mut(&mut self) -> &mut ColliderQuadtree {
		&mut self.colliders
	}
	pub fn layers(&self) -> impl DoubleEndedIterator<Item = &LdtkLayer> {
		self.processed_layers.iter()
	}
	pub fn layers_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut LdtkLayer> {
		self.processed_layers.iter_mut()
	}
Louis's avatar
Louis committed
	pub fn properties_ref(&self) -> &HashMap<String, Value> {
		&self.properties
	}
	pub fn properties_mut(&mut self) -> &mut HashMap<String, Value> {
		&mut self.properties
	}
	pub fn property(&self, name: impl ToString) -> Option<&Value> {
		self.properties.get(&name.to_string())
	}
	pub fn property_or_null(&self, name: impl ToString) -> &Value {
		self.property(name).unwrap_or_else(|| &Value::Null)
	}
	pub fn get_indexer(&self) -> Indexer {
		Indexer::new(px_to_grid(self.level.px_wid), px_to_grid(self.level.px_hei))
	}
}

Louis's avatar
Louis committed
#[derive(Debug, Clone, Default)]
pub struct LocatedEntity {
	pub position: IVec2,
	pub size: IVec2,
	pub properties: HashMap<String, Value>,
	pub collides: bool,
}

impl From<Level> for LdtkLevel {
	fn from(mut value: Level) -> Self {
		let layers = value.layer_instances.take();
Louis's avatar
Louis committed
		let mut properties = HashMap::with_capacity(value.field_instances.len());
		let fields = std::mem::take(&mut value.field_instances);
Louis's avatar
Louis committed
		for field in fields {
			properties.insert(field.identifier, field.value.unwrap_or_else(|| Value::Null));
		}

		let level_width = value.px_wid;
		let level_height = value.px_hei;

		let mut level = Self {
			colliders: Quadtree::new(0),
			processed_layers: layers
				.unwrap_or_default()
				.into_iter()
Louis's avatar
Louis committed
				.rev()
				.enumerate()
				.map(|(idx, inst)| LdtkLayer::new(idx, inst))
				.collect(),
			level: value,
Louis's avatar
Louis committed
			properties,
		};

		let mut collider_quads = Quadtree::<i64, LocatedEntity>::new(32);
		for entity in MapQuery::get_entities_of(&level) {
			if entity.tags.contains(&String::from("Collider")) {
				let mut properties = HashMap::with_capacity(entity.field_instances.len());
				for field in entity.field_instances.iter() {
					properties.insert(
						field.identifier.clone(),
						field.value.as_ref().unwrap_or_else(|| &Value::Null).clone(),
					);
				}

				let width = entity.width;
				let height = entity.height;
				let left_x = entity.px[0];
				let bottom_y = level_height - (entity.px[1] + height);

				let size = IVec2::new(entity.width as i32, entity.height as i32);
				let position = IVec2::new(level.level.px_wid as i32, level.level.px_hei as i32)
					- (IVec2::new(entity.px[0] as i32, entity.px[1] as i32) + size);

				let entity = LocatedEntity {
					position,
					size,
					properties,
					collides: true,
				};

				match AreaBuilder::default()
					.anchor(Point::from((position.x as i64, position.y as i64)))
					.dimensions((size.x as i64, size.y as i64))
					.build()
				{
					Ok(point) => {
						collider_quads.insert(point, entity);
					}
					Err(_) => {}
				}
			}
Louis's avatar
Louis committed

		level.colliders = collider_quads;
		level
Louis's avatar
Louis committed
	pub level: usize,
	layer: LayerInstance,
	position_lookup: HashMap<SpatialIndex, TileInstance>,
	indexer: Indexer,
}

impl LdtkLayer {
	pub fn new(index: usize, layer: LayerInstance) -> Self {
		let tile_list = layer.grid_tiles.iter().chain(&layer.auto_layer_tiles);
		let mut position_lookup =
			HashMap::with_capacity(layer.grid_tiles.len() + layer.auto_layer_tiles.len());

		let scale = get_ldtk_tile_scale() as i64;
		let indexer = Indexer::new(layer.c_wid, layer.c_hei);

		for tile in tile_list {
			let x = tile.px[0] / scale;
			let y = tile.px[1] / scale;

			position_lookup.insert((x, y).into(), tile.serde_clone());
		}

		Self {
			level: index,
			indexer,
			layer,
			position_lookup,
		}
	}

Louis's avatar
Louis committed
	pub fn name(&self) -> &String {
		&self.layer.identifier
	}

	pub fn indexer(&self) -> Indexer {
		self.indexer
	}

	pub fn has_tiles(&self) -> bool {
		!(self.layer.auto_layer_tiles.is_empty() && self.layer.grid_tiles.is_empty())
	}

	pub fn for_each_tile(&self, mut cb: impl FnMut(i64, i64, TileRef)) {
		self.position_lookup.iter().for_each(|(pos, tile)| {
			cb(pos.0, pos.1, TileRef::new(tile));
		});
	}

	pub fn get_z_delta(&self) -> f32 {
		(self.level as f32) / 100.0
	}

	pub fn get_tile(&self, pos: impl Into<SpatialIndex>) -> Option<TileRef> {
		self.position_lookup.get(&pos.into()).map(TileRef::new)
	}
	pub fn get_tile_at(
		&self,
		a: impl AsPrimitive<i64>,
		b: impl AsPrimitive<i64>,
	) -> Option<TileRef> {
		self.position_lookup
			.get(&SpatialIndex(a.as_(), b.as_()))
			.map(TileRef::new)
	}

	/// Returns the inferred name of the tileset used for this layer. This is assumed to be the
	/// name of the tileset file, without the preceding path segments or the file extension. Case
	/// remains unchanged
	pub fn infer_tileset_name(&self) -> Option<String> {
		self.layer.tileset_rel_path.as_ref().and_then(|path| {
			Path::new(path)
				.file_stem()
				.and_then(|stem| stem.to_str())
				.map(String::from)
		})
	}
}

impl AsRef<LayerInstance> for LdtkLayer {
	fn as_ref(&self) -> &LayerInstance {
		&self.layer
	}
}
Louis's avatar
Louis committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WorldTile {
	gid: usize,
	rotation: TileFlip,
}

impl From<&TileInstance> for WorldTile {
	fn from(value: &TileInstance) -> Self {
		Self {
			gid: value.t.max(0) as usize,
			rotation: match value.f {
				1 => TileFlip::Horizontal,
				2 => TileFlip::Vertical,
				3 => TileFlip::Both,
				_ => TileFlip::None,
			},
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub enum EntityPosition {
	Point { position: IVec2 },
	Zone { position: IVec2, size: IVec2 },
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WorldEntity {
	entity_type: String,
	position: Rect,
	properties: Properties,
	tags: Vec<String>,
	colour: String,
}

impl From<&EntityInstance> for WorldEntity {
	fn from(value: &EntityInstance) -> Self {
		let x = value.px[0];
		let y = value.px[1];

		Self {
			entity_type: value.identifier.clone(),
			position: Rect::from_corners(
				Vec2::new(x as f32, y as f32),
				Vec2::new((x + value.width) as f32, (y + value.height) as f32),
			),
			properties: Properties::from(&value.field_instances),
			tags: value.tags.clone(),
			colour: value.smart_color.clone(),
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Properties(HashMap<String, Value>);
impl Deref for Properties {
	type Target = HashMap<String, Value>;

	fn deref(&self) -> &Self::Target {
		&self.0
	}
}

impl DerefMut for Properties {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.0
	}
}

impl From<HashMap<String, Value>> for Properties {
	fn from(value: HashMap<String, Value>) -> Self {
		Self(value)
	}
}

impl From<Vec<FieldInstance>> for Properties {
	fn from(value: Vec<FieldInstance>) -> Self {
		(&value).into()
	}
}

impl From<&Vec<FieldInstance>> for Properties {
	fn from(value: &Vec<FieldInstance>) -> Self {
		value
			.iter()
			.map(|field| {
				(
					field.identifier.clone(),
					field.value.clone().unwrap_or(Value::Null),
				)
			})
			.collect::<HashMap<String, Value>>()
			.into()
	}
}

fn convert_map_types(map: &Map<String, Value>) -> HashMap<String, Value> {
	map.iter()
		.map(|(name, value)| (name.clone(), value.clone()))
		.collect()
}

impl Properties {
	pub fn as_string(&self, name: impl ToString) -> Option<String> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::String(value) => Some(format!("{}", value)),
			Value::Bool(value) => Some(format!("{}", value)),
			Value::Number(value) => Some(format!("{}", value)),
			_ => None,
		})
	}

	pub fn as_number_cast<T>(&self, name: impl ToString) -> Option<T>
	where
		T: 'static + Copy,
		f64: AsPrimitive<T>,
	{
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Number(number) => {
				let num = number.as_f64();
				num.map(|val| val.as_())
			}
			_ => None,
		})
	}

	pub fn as_bool(&self, name: impl ToString) -> Option<bool> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Bool(value) => Some(*value),
			_ => None,
		})
	}

	pub fn as_vec(&self, name: impl ToString) -> Option<&Vec<Value>> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Array(value) => Some(value),
			_ => None,
		})
	}

	pub fn as_vec_owned(&self, name: impl ToString) -> Option<Vec<Value>> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Array(value) => Some(value.clone()),
			_ => None,
		})
	}

	pub fn as_map(&self, name: impl ToString) -> Option<&Map<String, Value>> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Object(value) => Some(value),
			_ => None,
		})
	}

	pub fn as_map_owned(&self, name: impl ToString) -> Option<Map<String, Value>> {
		self.get(&name.to_string()).and_then(|value| match value {
			Value::Object(value) => Some(value.clone()),
			_ => None,
		})
	}

	pub fn matches(&self, name: impl ToString, value: Value) -> bool {
		match (self.0.get(&name.to_string()), value) {
			(Some(Value::String(a)), Value::String(ref b)) => a == b,
			(Some(Value::Number(a)), Value::Number(ref b)) => a == b,
			(Some(Value::Bool(a)), Value::Bool(ref b)) => a == b,
			(Some(Value::Array(a)), Value::Array(ref b)) => a == b,
			(Some(Value::Object(a)), Value::Object(ref b)) => a == b,
			(Some(Value::Null), Value::Null) => true,
			_ => false,
		}
	}
	pub fn is_null(&self, name: impl ToString) -> bool {
		match self.0.get(&name.to_string()) {
			Some(Value::Null) => true,
			_ => false,
		}
	}

	pub fn is_null_or_undefined(&self, name: impl ToString) -> bool {
		match self.0.get(&name.to_string()) {
			None | Some(Value::Null) => true,
			_ => false,
		}
	}

	pub fn is_null_or_falsy(&self, name: impl ToString) -> bool {
		match self.0.get(&name.to_string()) {
			None | Some(Value::Null) => true,
			Some(Value::Bool(value)) => !*value,
			Some(Value::String(value)) => value.is_empty(),
			Some(Value::Number(num)) => num == &Number::from(0),
			Some(Value::Array(value)) => value.is_empty(),
			Some(Value::Object(value)) => value.is_empty(),
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum LayerType {
	Entities(Vec<WorldEntity>),
	GidTiles(HashMap<SpatialIndex, WorldTile>),
	IntTiles(Vec<usize>),
}

fn infer_tileset_name(layer: &LayerInstance) -> Option<String> {
	layer.tileset_rel_path.as_ref().and_then(|path| {
		Path::new(path)
			.file_stem()
			.and_then(|stem| stem.to_str())
			.map(String::from)
	})
}

impl From<&LayerInstance> for LayerType {
	fn from(value: &LayerInstance) -> Self {
		match value.layer_instance_type.as_str() {
			"IntGrid" => LayerType::IntTiles(
				value
					.int_grid_csv
					.iter()
					.map(|num| (*num).max(0) as usize)
					.collect(),
			),
			"Entities" => LayerType::Entities(
				value
					.entity_instances
					.iter()
					.map(|entity| entity.into())
					.collect(),
			),
			"Tiles" => LayerType::GidTiles(
				value
					.grid_tiles
					.iter()
					.map(|tile| (SpatialIndex(tile.px[0], tile.px[1]), tile.into()))
					.collect(),
			),
			"AutoLayer" => LayerType::GidTiles(
				value
					.auto_layer_tiles
					.iter()
					.map(|tile| (SpatialIndex(tile.px[0], tile.px[1]), tile.into()))
					.collect(),
			),
			_ => {
				panic!("Invalid layer type {}", value.layer_instance_type.as_str());
			}
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct WorldLayer {
	id: String,
	depth: usize,
	world_width: i64,
	world_height: i64,
	tile_width: i64,
	tile_height: i64,
	layer_data: LayerType,
	tileset_name: Option<String>,
}

impl WorldLayer {
	pub fn from_layer(depth: usize, layer: &LayerInstance) -> Self {
		let tile_size = get_ldtk_tile_scale() as i64;

		Self {
			depth,
			id: layer.identifier.clone(),
			tileset_name: infer_tileset_name(layer),
			world_width: layer.c_wid * tile_size,
			world_height: layer.c_hei * tile_size,
			tile_width: layer.c_wid,
			tile_height: layer.c_hei,
			layer_data: layer.into(),
		}
	}

	pub fn has_tiles(&self) -> bool {
		match &self.layer_data {
			LayerType::GidTiles(map) => !map.is_empty(),
			_ => false,
		}
	}

	pub fn for_each_tile(&self, mut cb: impl FnMut(i64, i64, &WorldTile)) {
		match &self.layer_data {
			LayerType::GidTiles(map) => {
				map.iter().for_each(|(pos, tile)| {
					cb(pos.0, pos.1, tile);
				});
			}
			_ => {}
		}
	}

	pub fn for_each_tile_mut(&mut self, mut cb: impl FnMut(i64, i64, &mut WorldTile)) {
		match &mut self.layer_data {
			LayerType::GidTiles(map) => {
				map.iter_mut().for_each(|(pos, tile)| {
					cb(pos.0, pos.1, tile);
				});
			}
			_ => {}
		}
	}

	pub fn get_z_delta(&self) -> f32 {
		(self.depth as f32) / 100.0
	}

	pub fn get_tile(
		&self,
		x: impl AsPrimitive<i32>,
		y: impl AsPrimitive<i32>,
	) -> Option<&WorldTile> {
		match &self.layer_data {
			LayerType::GidTiles(map) => map.get(&IVec2::from((x.as_(), y.as_())).into()),
			_ => None,
		}
	}

	pub fn get_tile_mut(
		&mut self,
		x: impl AsPrimitive<i32>,
		y: impl AsPrimitive<i32>,
	) -> Option<&mut WorldTile> {
		match &mut self.layer_data {
			LayerType::GidTiles(map) => map.get_mut(&IVec2::from((x.as_(), y.as_())).into()),
			_ => None,
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct WorldData {
	layers: Vec<WorldLayer>,
	properties: Properties,
	meta: Properties,
}

impl From<&Level> for WorldData {
	fn from(value: &Level) -> Self {
		let mut properties: Properties = (&value.field_instances).into();
		let meta: Properties = properties
			.as_map("meta")
			.map(convert_map_types)
			.unwrap_or_default()
			.into();
		properties.remove("meta");

		Self {
			properties,
			meta,
			layers: value
				.layer_instances
				.as_ref()
				.map(|insts| {
					insts
						.iter()
						.enumerate()
						.map(|(idx, layer)| WorldLayer::from_layer(idx, layer))
						.collect()
				})
				.unwrap_or_default(),
		}
	}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LayerSet(pub Vec<WorldLayer>);

trait AsWorldLayer {
	fn as_world_layer(&self, depth: usize) -> WorldLayer;
}

// impl<T, V> From<V> for LayerSet
// where
// 	T: AsWorldLayer,
// 	V: Iterator<Item = T>,
// {
// 	fn from(value: V) -> Self {
// 		Self(value.enumerate().map(T::as_world_layer).collect())
// 	}
// }

// impl<T> From<T> for WorldData
// where
// 	T: Into<LayerSet>,
// {
// 	fn from(value: T) -> Self {
// 		let layers = value.into();
// 		WorldData {
// 			layers: layers.0,
// 			..Default::default()
// 		}
// 	}
// }