| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/battle/struct/btl_map.erl')
| -rw-r--r-- | src/battle/struct/btl_map.erl | 100 | 
1 files changed, 100 insertions, 0 deletions
| diff --git a/src/battle/struct/btl_map.erl b/src/battle/struct/btl_map.erl new file mode 100644 index 0000000..ef34833 --- /dev/null +++ b/src/battle/struct/btl_map.erl @@ -0,0 +1,100 @@ +-module(btl_map). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-type id() :: binary(). + +-record +( +   map, +   { +      id :: id(), +      width :: integer(), +      height :: integer(), +      tile_class_ids :: array:array(btl_tile:class_id()) +   } +). + +-opaque type() :: #map{}. + +-export_type([type/0, id/0]). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-export +( +   [ +      get_id/1, +      get_width/1, +      get_height/1, +      get_tile_class_ids/1, +      get_tile_class_id/2 +   ] +). + +-export +( +   [ +      from_list/4 +   ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec location_to_array_index +   ( +      non_neg_integer(), +      btl_location:type() +   ) +   -> ('error' | non_neg_integer()). +location_to_array_index (ArrayWidth, {X, Y}) -> +   if +      (X < 0) -> error; +      (Y < 0) -> error; +      (X >= ArrayWidth) -> error; +      true -> ((Y * ArrayWidth) + X) +   end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%% Accessors +-spec get_id (type()) -> id(). +get_id (Battlemap) -> Battlemap#map.id. + +-spec get_width (type()) -> integer(). +get_width (Battlemap) -> Battlemap#map.width. + +-spec get_height (type()) -> integer(). +get_height (Battlemap) -> Battlemap#map.height. + +-spec get_tile_class_ids (type()) -> array:array(btl_tile:class_id()). +get_tile_class_ids (Battlemap) -> Battlemap#map.tile_class_ids. + +-spec get_tile_class_id (btl_location:type(), type()) -> btl_tile:class_id(). +get_tile_class_id (Location, Battlemap) -> +   TileIX = location_to_array_index(Battlemap#map.width, Location), +   array:get(TileIX, Battlemap#map.tile_class_ids). + +-spec from_list +   ( +      non_neg_integer(), +      non_neg_integer(), +      non_neg_integer(), +      list(non_neg_integer()) +   ) +   -> type(). +from_list (ID, Width, Height, List) -> +   TileClassIDs = lists:map(fun btl_tile:class_id_from_int/1, List), + +   #map +   { +      id = list_to_binary(integer_to_list(ID)), +      width = Width, +      height = Height, +      tile_class_ids = array:from_list(TileClassIDs) +   }. | 


