summaryrefslogtreecommitdiff |
diff options
author | nsensfel <SpamShield0@noot-noot.org> | 2018-04-16 14:53:01 +0200 |
---|---|---|
committer | nsensfel <SpamShield0@noot-noot.org> | 2018-04-16 14:53:01 +0200 |
commit | bcc13588a84dd182f35d153c49b9150648f7ad98 (patch) | |
tree | d1a000a46dd5ad77dca90fe97ddffeb89677c5f4 /src/battlemap | |
parent | 717cd47723ba0eda65e5cc74d24b25cdd30542a7 (diff) |
Deads are no longer littering the map.
Diffstat (limited to 'src/battlemap')
-rw-r--r-- | src/battlemap/src/reply/add_char.erl | 7 | ||||
-rw-r--r-- | src/battlemap/src/struct/battle_action.erl | 8 | ||||
-rw-r--r-- | src/battlemap/src/struct/character_instance.erl | 13 | ||||
-rw-r--r-- | src/battlemap/src/struct/location.erl | 33 |
4 files changed, 47 insertions, 14 deletions
diff --git a/src/battlemap/src/reply/add_char.erl b/src/battlemap/src/reply/add_char.erl index b3ef128..990b23e 100644 --- a/src/battlemap/src/reply/add_char.erl +++ b/src/battlemap/src/reply/add_char.erl @@ -41,10 +41,15 @@ attributes_as_json (Attributes) -> -> {list(any())}. generate (IX, CharacterInstance, PlayerID) -> Character = character_instance:get_character(CharacterInstance), - Location = character_instance:get_location(CharacterInstance), + IsAlive = character_instance:get_is_alive(CharacterInstance), Attributes = character:get_attributes(Character), {ActiveWeapon, SecondaryWeapon} = character:get_weapon_ids(Character), OwnerID = character:get_owner_id(Character), + Location = + case IsAlive of + true -> character_instance:get_location(CharacterInstance); + _ -> location:get_nowhere() + end, { [ diff --git a/src/battlemap/src/struct/battle_action.erl b/src/battlemap/src/struct/battle_action.erl index 8aaaef9..f40de0d 100644 --- a/src/battlemap/src/struct/battle_action.erl +++ b/src/battlemap/src/struct/battle_action.erl @@ -204,9 +204,11 @@ when is_record(BattleAction, move) -> array:foldl ( fun (IX, CharInst, Prev) -> - case IX of - CharacterInstanceIX -> Prev; - _ -> [character_instance:get_location(CharInst)|Prev] + IsAlive = character_instance:get_is_alive(CharInst), + if + (IX == CharacterInstanceIX) -> Prev; + (not IsAlive) -> Prev; + true -> [character_instance:get_location(CharInst)|Prev] end end, [], diff --git a/src/battlemap/src/struct/character_instance.erl b/src/battlemap/src/struct/character_instance.erl index 9b64f9a..b657f10 100644 --- a/src/battlemap/src/struct/character_instance.erl +++ b/src/battlemap/src/struct/character_instance.erl @@ -36,6 +36,7 @@ get_character/1, get_location/1, get_current_health/1, + get_is_alive/1, get_is_active/1, set_character/2, @@ -81,17 +82,23 @@ find_random_location (BattlemapWidth, BattlemapHeight, ForbiddenLocations) -> get_character (CharInst) -> CharInst#character_instance.character. -spec get_location (struct()) -> {non_neg_integer(), non_neg_integer()}. -get_location (CharInst) -> CharInst#character_instance.location. +get_location (CharInst) -> + true = get_is_alive(CharInst), + CharInst#character_instance.location. -spec get_current_health (struct()) -> non_neg_integer(). get_current_health (CharInst) -> CharInst#character_instance.current_health. +-spec get_is_alive (struct()) -> boolean(). +get_is_alive (CharInst) -> + (CharInst#character_instance.current_health > 0). + -spec get_is_active (struct()) -> boolean(). get_is_active (CharInst) -> ( CharInst#character_instance.active and - (CharInst#character_instance.current_health > 0) + get_is_alive(CharInst) ). -spec set_character (character:struct(), struct()) -> struct(). @@ -117,7 +124,7 @@ set_location (Location, CharInst) -> set_current_health (Health, CharInst) -> CharInst#character_instance { - current_health = Health + current_health = max(0, Health) }. -spec set_is_active (boolean(), struct()) -> struct(). diff --git a/src/battlemap/src/struct/location.erl b/src/battlemap/src/struct/location.erl index b8e2bf3..e54584b 100644 --- a/src/battlemap/src/struct/location.erl +++ b/src/battlemap/src/struct/location.erl @@ -3,7 +3,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --type type() :: {non_neg_integer(), non_neg_integer()}. +-type type() :: ({non_neg_integer(), non_neg_integer()} | 'nowhere'). -export_type([type/0]). @@ -14,7 +14,8 @@ ( [ decode/1, - encode/1 + encode/1, + get_nowhere/0 ] ). @@ -31,13 +32,18 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -spec validate ({integer(), integer()}) -> type(). validate ({X, Y}) -> - true = (X >= 0), - true = (Y >= 0), - {X, Y}. + if + (X < 0) -> nowhere; + (Y < 0) -> nowhere; + true -> {X, Y} + end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec get_nowhere () -> type(). +get_nowhere () -> nowhere. + -spec apply_direction (direction:enum(), type()) -> type(). apply_direction (left, {X, Y}) -> validate({(X - 1), Y}); @@ -46,11 +52,17 @@ apply_direction (right, {X, Y}) -> apply_direction (up, {X, Y}) -> validate({X, (Y - 1)}); apply_direction (down, {X, Y}) -> - validate({X, (Y + 1)}). + validate({X, (Y + 1)}); +apply_direction (_, nowhere) -> + error("Trying to move from 'nowhere'."), + nowhere. -spec dist(type(), type()) -> non_neg_integer(). dist ({OX, OY}, {DX, DY}) -> - (abs(DY - OY) + abs(DX - OX)). + (abs(DY - OY) + abs(DX - OX)); +dist (_, _) -> + error("Trying to measure distance to 'nowhere'"), + 999. -spec encode (type()) -> {list(any())}. encode ({X, Y}) -> @@ -59,6 +71,13 @@ encode ({X, Y}) -> {<<"x">>, X}, {<<"y">>, Y} ] + }; +encode (nowhere) -> + { + [ + {<<"x">>, -1}, + {<<"y">>, -1} + ] }. -spec decode (map()) -> type(). |