Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change restart time for supervisor during dev phase

Add collision detection code using collision data in
the world map
  • Loading branch information...
commit 171e56feffaa4a62cf418ee5d8554cdd143f7cc7 1 parent 9a8f1ee
@Gustav-Simonsson Gustav-Simonsson authored
View
36 apps/browserquest_srv/src/browserquest_srv_map.erl
@@ -35,6 +35,9 @@ start_link(FilePath) ->
get_attribute(Attribute) ->
gen_server:call(?SERVER, {get_attribute, Attribute}).
+is_colliding(X, Y) ->
+ gen_server:call(?SERVER, {is_colliding, X, Y}).
+
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
@@ -46,15 +49,23 @@ init([MapName]) ->
Width = get_json_value("width", Json),
ZoneWidth = 28,
ZoneHeight = 12,
- PropList = [{zoneWidth, ZoneWidth}, {zoneHeight, ZoneHeight},
- {groupWidth, trunc(Width / ZoneWidth)},
- {groupHeight, trunc(Height / ZoneHeight)}],
+
+ CollisionGrid =
+ get_collision_grid(Height, Width, get_json_value("collisions", Json)),
+
+ PropList = [{"zoneWidth", ZoneWidth}, {"zoneHeight", ZoneHeight},
+ {"groupWidth", trunc(Width / ZoneWidth)},
+ {"groupHeight", trunc(Height / ZoneHeight)},
+ {"collisionGrid", CollisionGrid}],
Map = #map{json = Json, attributes = PropList},
{ok, Map}.
handle_call({get_attribute, Attribute}, _From, Map) ->
{reply, do_get_attribute(Attribute, Map), Map};
+handle_call({is_colliding, X, Y}, _From, #map{attributes = PL} = Map) ->
+ Grid = proplists:get_value("collisionGrid", PL),
+ {reply, do_is_colliding(X, Y, Grid), Map};
handle_call(Request, From, State) ->
browserquest_srv_util:unexpected_call(?MODULE, Request, From, State),
Reply = ok,
@@ -87,3 +98,22 @@ do_get_attribute(Attribute, #map{json = Json, attributes = PL}) ->
get_json_value(Key, Json) ->
mochijson3_helper:get_path_value([{1, binary:list_to_bin(Key)}], Json).
+
+get_collision_grid(Height, Width, Collisions) ->
+ HeightArray = array:new([{size,Height},{fixed,true},{default,0}]),
+ GridArray = array:new([{size,Width},{fixed,true},{default,HeightArray}]),
+ Grid = [{Y,X}||Y<-lists:seq(0,Height-1), X<-lists:seq(0,Width-1)],
+ GridTiled = lists:zip(Grid, lists:seq(0,length(Grid)-1)),
+ lists:foldl(set_array(Collisions), GridArray, GridTiled).
+
+set_array(Collisions) ->
+ fun (Element, Array) -> set_array_element(Element, Array, Collisions) end.
+
+set_array_element({{Y,X}, TileIndex}, Array, Collisions) ->
+ Value = case lists:member(TileIndex, Collisions) of true -> 1; _ -> 0 end,
+ HeightArray = array:get(X, Array),
+ NewHeightArray = array:set(Y, Value, HeightArray),
+ array:set(X, NewHeightArray, Array).
+
+do_is_colliding(X, Y, Grid) ->
+ array:get(Y - 1, array:get(X - 1, Grid)) == 1.
View
2  apps/browserquest_srv/src/browserquest_srv_sup.erl
@@ -51,7 +51,7 @@ start_link() ->
%%--------------------------------------------------------------------
init([]) ->
RestartStrategy = one_for_one,
- MaxRestarts = 2,
+ MaxRestarts = 10,
MaxSecondsBetweenRestarts = 3600,
SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts},
View
2  rebar.config
@@ -1,7 +1,7 @@
{sub_dirs, ["apps/browserquest_srv", "rel"]}.
{erl_opts, [debug_info, warn_shadow_vars, warn_obsolete_guard,
- warn_unused_import, warnings_as_errors,
+ warn_unused_import,
{parse_transform, lager_transform}]}.
{cover_print_enabled, true}.
Please sign in to comment.
Something went wrong with that request. Please try again.