Skip to content

Commit

Permalink
feat: use decor layer offset
Browse files Browse the repository at this point in the history
  • Loading branch information
buxx committed Mar 20, 2024
1 parent 8769871 commit 0982aee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
15 changes: 13 additions & 2 deletions battle_core/src/map/decor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::PathBuf;

use crate::types::Offset;

#[derive(Clone)]
pub struct DecorTile {
pub tileset_i: usize, // Used to rely tileset/sprite_batch in Decor
Expand Down Expand Up @@ -44,11 +46,16 @@ impl DecorTile {
pub struct Decor {
image_paths: Vec<PathBuf>,
tiles: Vec<DecorTile>,
offset: Offset,
}

impl Decor {
pub fn new(image_paths: Vec<PathBuf>, tiles: Vec<DecorTile>) -> Self {
Self { image_paths, tiles }
pub fn new(image_paths: Vec<PathBuf>, tiles: Vec<DecorTile>, offset: Offset) -> Self {
Self {
image_paths,
tiles,
offset,
}
}

pub fn image_paths(&self) -> &Vec<PathBuf> {
Expand All @@ -58,4 +65,8 @@ impl Decor {
pub fn tiles(&self) -> &Vec<DecorTile> {
&self.tiles
}

pub fn offset(&self) -> Offset {
self.offset
}
}
33 changes: 19 additions & 14 deletions battle_core/src/map/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use tiled::{
TileLayer, Tileset,
};

use crate::game::flag::{Flag, FlagName};
use crate::{
game::flag::{Flag, FlagName},
types::Offset,
};

use super::{
decor::{Decor, DecorTile},
Expand Down Expand Up @@ -296,10 +299,11 @@ impl MapReader {
}
}

fn decor_layer(&self) -> Result<FiniteTileLayer, MapReaderError> {
match self.layer(DECOR_LAYER_NAME)?.layer_type() {
fn decor_layer(&self) -> Result<(Layer, FiniteTileLayer), MapReaderError> {
let decor_layer = self.layer(DECOR_LAYER_NAME)?;
match decor_layer.layer_type() {
LayerType::TileLayer(layer) => match layer{
TileLayer::Finite(layer) => Ok(layer),
TileLayer::Finite(layer_) => Ok((decor_layer, layer_)),
TileLayer::Infinite(_) => Result::Err(MapReaderError::InvalidLayer(format!(
"Layer '{}' in map {} is an infinite tile layer, but on finite layer is supported",
DECOR_LAYER_NAME, self.name,
Expand Down Expand Up @@ -433,14 +437,14 @@ impl MapReader {
}

fn decor_tilesets(&self) -> Result<DecorTilesets, MapReaderError> {
let layer = self.decor_layer()?;
let (_, layer_) = self.decor_layer()?;
let mut tileset_indexes = vec![];
let mut tilesets = vec![];
let mut positions = HashMap::new();

for x in 0..layer.width() {
for y in 0..layer.height() {
if let Some(layer_tile_data) = layer.get_tile_data(x as i32, y as i32) {
for x in 0..layer_.width() {
for y in 0..layer_.height() {
if let Some(layer_tile_data) = layer_.get_tile_data(x as i32, y as i32) {
if !tileset_indexes.contains(&layer_tile_data.tileset_index()) {
tileset_indexes.push(layer_tile_data.tileset_index());
}
Expand Down Expand Up @@ -478,7 +482,7 @@ impl MapReader {
}

fn decor(&self) -> Result<Decor, MapReaderError> {
let decor_layer = self.decor_layer()?;
let (decor_layer, decor_layer_) = self.decor_layer()?;
let (_, tilesets_positions) = self.decor_tilesets()?;
let images = self.decor_images()?;
let image_paths = images
Expand All @@ -497,9 +501,9 @@ impl MapReader {

let mut tiles = vec![];

for x in 0..decor_layer.width() {
for y in 0..decor_layer.height() {
if let Some(layer_tile_data) = decor_layer.get_tile_data(x as i32, y as i32) {
for x in 0..decor_layer_.width() {
for y in 0..decor_layer_.height() {
if let Some(layer_tile_data) = decor_layer_.get_tile_data(x as i32, y as i32) {
let tileset = self.map.tilesets()[layer_tile_data.tileset_index()].clone();

let decor_tileset_position = *tilesets_positions
Expand Down Expand Up @@ -529,12 +533,13 @@ impl MapReader {
tile_y,
);

tiles.push(terrain_tile)
tiles.push(terrain_tile);
};
}
}

Ok(Decor::new(image_paths, tiles))
let offset = Offset::new(-decor_layer.offset_x, -decor_layer.offset_y);
Ok(Decor::new(image_paths, tiles, offset))
}

pub fn build(&self) -> Result<Map, MapReaderError> {
Expand Down
4 changes: 4 additions & 0 deletions battle_core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ impl Offset {
Self { x, y }
}

pub fn zero() -> Self {
Self { x: 0., y: 0. }
}

pub fn half() -> Self {
Self { x: 0.5, y: 0.5 }
}
Expand Down
12 changes: 9 additions & 3 deletions battle_gui/src/graphics/decors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use battle_core::{
game::control::MapControl,
map::Map,
types::{ScenePoint, WorldPoint},
types::{Offset, ScenePoint, WorldPoint},
};
use ggez::{
graphics::{DrawParam, Image, InstanceArray, Rect},
Expand All @@ -15,11 +15,16 @@ use super::{batch::QualifiedBatch, qualified::Zoom};
pub struct Decors {
sd: Vec<InstanceArray>,
hd: Vec<InstanceArray>,
offset: Offset,
}

impl Decors {
pub fn new(sd: Vec<InstanceArray>, hd: Vec<InstanceArray>) -> Self {
Self { sd, hd }
pub fn new(sd: Vec<InstanceArray>, hd: Vec<InstanceArray>, offset: Offset) -> Self {
Self { sd, hd, offset }
}

pub fn offset(&self) -> Offset {
self.offset
}
}

Expand Down Expand Up @@ -80,6 +85,7 @@ impl<'a> DecorsBuilder<'a> {
Ok(Decors::new(
self.build_for(&Zoom::default())?,
self.build_for(&Zoom::hd())?,
self.map.decor().offset(),
))
}

Expand Down
1 change: 1 addition & 0 deletions battle_gui/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ impl Graphics {
zoom: &Zoom,
) -> GameResult {
if draw_decor {
let draw_param = draw_param.offset(self.decor.offset().to_vec2() * zoom.factor());
self.decor
.drawable(zoom)
.iter()
Expand Down

0 comments on commit 0982aee

Please sign in to comment.