Skip to content

Instantly share code, notes, and snippets.

@yovanoc
Last active February 24, 2019 11:26
Show Gist options
  • Save yovanoc/4b63716bb541facf842e27739761079f to your computer and use it in GitHub Desktop.
Save yovanoc/4b63716bb541facf842e27739761079f to your computer and use it in GitHub Desktop.
MapsManager
use crate::DT_CONSTANTS;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Map {
pub id: usize,
pub top_neighbour_id: usize,
pub bottom_neighbour_id: usize,
pub left_neighbour_id: usize,
pub right_neighbour_id: usize,
pub shadow_bonus_on_entities: i16,
pub cells: Vec<Cell>,
pub midground_layer: HashMap<u16, Vec<GraphicalElement>>,
pub atlas_layout: AtlasLayout,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AtlasLayout {
pub width: usize,
pub height: usize,
pub graphics_positions: HashMap<usize, GraphicSizes>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct GraphicSizes {
pub sx: usize,
pub sy: usize,
pub sw: usize,
pub sh: usize,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct GraphicalElement {
pub g: Option<usize>,
pub x: f64,
pub y: f64,
pub sx: Option<f64>,
pub sy: Option<f64>,
pub hue: Vec<i16>,
pub cx: Option<f64>,
pub cy: Option<f64>,
pub cw: Option<f64>,
pub ch: Option<f64>,
pub rotation: Option<f64>,
pub alpha: Option<f64>,
pub look: Option<usize>,
pub anim: Option<usize>,
pub astc: Option<usize>,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Cell {
pub l: Option<u16>,
pub f: Option<i16>,
pub c: Option<i16>,
pub s: Option<u16>,
pub z: Option<u16>,
}
impl Cell {
pub fn is_walkable(&self, is_fight_mode: bool) -> bool {
match self.l {
None => false,
Some(l) => (l & (if is_fight_mode { 5 } else { 1 })) == 1,
}
}
pub fn is_farm_cell(&self) -> bool {
match self.l {
None => false,
Some(l) => (l & 32) == 32,
}
}
pub fn is_visible(&self) -> bool {
match self.l {
None => false,
Some(l) => (l & 64) == 64,
}
}
pub fn is_obstacle(&self) -> bool {
match self.l {
None => false,
Some(l) => (l & 2) != 2,
}
}
}
pub fn get_map(id: usize) -> Map {
reqwest::get(&format!(
"{}/maps/{}.json",
DT_CONSTANTS.config.assets_url, id,
))
.unwrap()
.json()
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maps_manager() {
let map_id = 81002496;
let map = get_map(map_id);
assert_eq!(map.id, map_id);
assert_eq!(map.top_neighbour_id, 81002753);
assert_eq!(map.bottom_neighbour_id, 81002497);
assert_eq!(map.left_neighbour_id, 81134080);
assert_eq!(map.right_neighbour_id, 81003008);
assert_eq!(map.shadow_bonus_on_entities, -1);
assert_eq!(map.cells.len(), 560);
assert_eq!(map.cells.first().unwrap().is_obstacle(), false);
assert_eq!(map.midground_layer.get(&207).unwrap()[0].g, Some(29018));
assert_eq!(map.atlas_layout.width, 1329);
assert_eq!(map.atlas_layout.height, 1289);
assert_eq!(
map.atlas_layout.graphics_positions.get(&7399).unwrap().sx,
340
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment