From 1b44ddcad67726aac12025991dbd7f2c72267cec Mon Sep 17 00:00:00 2001 From: nsensfel Date: Fri, 13 Dec 2019 18:29:02 +0100 Subject: ... --- src/battle/src/ElmModule/Update.elm | 4 +- src/battle/src/Struct/Puppeteer.elm | 213 ++++++++----------------- src/battle/src/Update/HandleAnimationEnded.elm | 127 --------------- src/battle/src/Update/HandleServerReply.elm | 71 ++++++--- src/battle/src/Update/Puppeteer.elm | 180 +++++++++++++++++++++ src/battle/src/Update/Puppeteer/Focus.elm | 1 - 6 files changed, 292 insertions(+), 304 deletions(-) delete mode 100644 src/battle/src/Update/HandleAnimationEnded.elm create mode 100644 src/battle/src/Update/Puppeteer.elm diff --git a/src/battle/src/ElmModule/Update.elm b/src/battle/src/ElmModule/Update.elm index 437e921..a56becd 100644 --- a/src/battle/src/ElmModule/Update.elm +++ b/src/battle/src/ElmModule/Update.elm @@ -10,9 +10,9 @@ import Update.ChangeScale import Update.DisplayCharacterInfo import Update.EndTurn import Update.GoToMainMenu -import Update.HandleAnimationEnded import Update.HandleServerReply import Update.LookForCharacter +import Update.Puppeteer import Update.RequestDirection import Update.SelectCharacter import Update.SelectCharacterOrTile @@ -53,7 +53,7 @@ update event model = (Update.AttackWithoutMoving.apply_to new_model) Struct.Event.AnimationEnded -> - (Update.HandleAnimationEnded.apply_to model) + (Update.Puppeteer.apply_to model) (Struct.Event.DirectionRequested d) -> (Update.RequestDirection.apply_to new_model d) diff --git a/src/battle/src/Struct/Puppeteer.elm b/src/battle/src/Struct/Puppeteer.elm index 937b9fd..c68f10d 100644 --- a/src/battle/src/Struct/Puppeteer.elm +++ b/src/battle/src/Struct/Puppeteer.elm @@ -2,20 +2,23 @@ module Struct.Puppeteer exposing ( Type, new, - append, - is_active, - requires_priming + append_forward, + append_backward, + forward, + backward, + step, + get_is_playing_forward, + set_is_playing_forward, + try_getting_current_action ) -- Elm ------------------------------------------------------------------------- -import Array -import Set +import List --- Battle Map ------------------------------------------------------------------ -import BattleMap.Struct.DataSet +-- Shared ---------------------------------------------------------------------- +import Util.List -- Local Module ---------------------------------------------------------------- -import Struct.Battle import Struct.TurnResult import Struct.PuppeteerAction @@ -24,125 +27,14 @@ import Struct.PuppeteerAction -------------------------------------------------------------------------------- type alias Type = { - actions : (List Struct.PuppeteerAction.Type), - primed : Bool + forward_actions : (List Struct.PuppeteerAction.Type), + backward_actions : (List Struct.PuppeteerAction.Type), + is_playing_forward : Bool } -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- -turn_result_to_actions : Struct.TurnResult.Type -> (List Action) -turn_result_to_actions turn_result = - case turn_result of - (Struct.TurnResult.Attacked attack) -> - let - attacker_ix = (Struct.TurnResult.get_actor_index turn_result) - defender_ix = (Struct.TurnResult.get_attack_defender_index attack) - in - [ - (Focus attacker_ix), - (Focus defender_ix), - (AttackSetup (attacker_ix, defender_ix)), - (TurnResult turn_result), - (RefreshCharacter attacker_ix), - (RefreshCharacter defender_ix) - ] - - _ -> - let actor_ix = (Struct.TurnResult.get_actor_index turn_result) in - [ - (Focus actor_ix), - (TurnResult turn_result), - (RefreshCharacter actor_ix) - ] - -initialize_animator : Type -> Type -initialize_animator model = - let - timeline_list = (Array.toList model.timeline) - (characters, players) = - (List.foldr - (\event (pcharacters, pplayers) -> - (Struct.TurnResult.apply_inverse_step - (tile_omnimods_fun model) - event - pcharacters - pplayers - ) - ) - (model.characters, model.players) - timeline_list - ) - in - {model | - animator = - (Struct.TurnResultAnimator.maybe_new - (List.reverse timeline_list) - True - ), - ui = (Struct.UI.default), - characters = characters, - players = players - } - -move_animator_to_next_step : (Maybe Type) -> (Maybe Type) -move_animator_to_next_step maybe_animator = - case maybe_animator of - Nothing -> maybe_animator - (Just animator) -> - (Struct.TurnResultAnimator.maybe_trigger_next_step animator) - --- case (Struct.TurnResultAnimator.maybe_trigger_next_step animator) of --- Nothing -> --- (Set.foldl --- (regenerate_attack_of_opportunity_markers) --- {model | animator = Nothing } --- (Struct.TurnResultAnimator.get_animated_character_indices --- animator --- ) --- ) --- --- just_next_animator -> {model | animator = just_next_animator } - -apply_animator_step : ( - BattleMap.Struct.DataSet.Type -> - Type -> - Struct.Battle.Type -> - Struct.Battle.Type - ) -apply_animator_step dataset animator battle = - case (Struct.TurnResultAnimator.get_current_animation animator) of - (Struct.TurnResultAnimator.TurnResult turn_result) -> - let - (characters, players) = - (Struct.TurnResult.apply_step - (Struct.Battle.get_tile_omnimods_fun dataset battle) - turn_result - battle - ) - in - (Struct.Battle.set_players - players - (Struct.Battle.set_characters characters battle) - ) - - _ -> battle - -pop : Type -> (Type, (Maybe Action)) -pop puppeteer = - case (Util.List.pop puppeteer.remaining_animations) of - Nothing -> (puppeteer, Nothing) - (Just (action, remaining_animations)) -> - ( - {puppeteer | - remaining_animations = remaining_animations, - primed = - if (List.isEmpty remaining_animations) - then False - else puppeteer.primed - }, - action - ) -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- @@ -150,36 +42,59 @@ pop puppeteer = new : Type new = { - remaining_animations = [], - primed = False + forward_actions = [], + backward_actions = [], + is_playing_forward = True } -append : (List Struct.TurnResult.Type) -> Type -> Type -append turn_results puppeteer = +append_forward : (List Struct.PuppeteerAction.Type) -> Type -> Type +append_forward actions puppeteer = {puppeteer | - remaining_animations = - (List.concat - puppeteer.remaining_animations - (List.map (turn_result_to_actions) turn_results) - ) + forward_actions = (List.concat puppeteer.forward_actions actions) } -is_active : Type -> Bool -is_active puppeteer = (not (List.isEmpty puppeteer.remaining_animations)) - -requires_priming : Type -> Bool -requires_priming puppeteer = (is_active and (not puppeteer.is_primed)) - -forward : Struct.Battle.Type -> Type -> (Struct.Battle.Type, Type) -forward battle puppeteer = - case (pop puppeteer) of - (next_puppeteer, Nothing) -> (battle, next_puppeteer) - (next_puppeteer, (Just action)) -> - ((apply_action action battle), next_puppeteer) +append_backward : (List Struct.PuppeteerAction.Type) -> Type -> Type +append_backward actions puppeteer = + {puppeteer | + backward_actions = (List.concat actions puppeteer.backward_actions) + } -forward : Struct.Battle.Type -> Type -> (Struct.Battle.Type, Type) -forward battle puppeteer = - case (pop puppeteer) of - (next_puppeteer, Nothing) -> (battle, next_puppeteer) - (next_puppeteer, (Just action)) -> - ((apply_action action battle), next_puppeteer) +forward : Type -> Type +forward puppeteer = + case (Util.List.pop puppeteer.forward_actions) of + ([], Nothing) -> puppeteer + (forward_actions, (Just action)) -> + {puppeteer | + forward_actions = forward_actions, + backward_actions = [action|puppeteer.backward_actions], + is_playing_forward = true + } + +backward : Type -> Type +backward puppeteer = + case (Util.List.pop puppeteer.backward_actions) of + ([], Nothing) -> puppeteer + (backward_actions, (Just action)) -> + {puppeteer | + forward_actions = [action|forward_actions], + backward_actions = backward_actions, + is_playing_forward = false + } + +step : Type -> Type +step puppeteer = + if (puppeteer.is_playing_forward) + then (forward puppeteer) + else (backward puppeteer) + +get_is_playing_forward : Type -> Bool +get_is_playing_forward puppeteer = puppeteer.is_playing_forward + +set_is_playing_forward : Bool -> Type -> Type +set_is_playing_forward val puppeteer = {puppeteer | is_playing_forward = val} + +try_getting_current_action : Type -> (Maybe (Struct.PuppeteerAction.Type)) +try_getting_current_action puppeteer = + if (puppeteer.is_playing_forward) + then (List.head puppeteer.forward_actions) + else (List.head puppeteer.backward_actions) diff --git a/src/battle/src/Update/HandleAnimationEnded.elm b/src/battle/src/Update/HandleAnimationEnded.elm deleted file mode 100644 index 6bde87a..0000000 --- a/src/battle/src/Update/HandleAnimationEnded.elm +++ /dev/null @@ -1,127 +0,0 @@ -module Update.HandleAnimationEnded exposing (apply_to) - --- Elm ------------------------------------------------------------------------- -import Array - -import Delay - -import Time - -import Task - --- Local Module ---------------------------------------------------------------- -import Action.Scroll - -import Struct.Battle -import Struct.Character -import Struct.Event -import Struct.Model -import Struct.TurnResult -import Struct.TurnResultAnimator - --------------------------------------------------------------------------------- --- LOCAL ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -handle_char_focus : ( - Struct.Model.Type -> - Struct.TurnResultAnimator.Type -> - Int -> - (Struct.Model.Type, (Cmd Struct.Event.Type)) - ) -handle_char_focus model animator char_index = - case (Struct.Battle.get_character char_index model.battle) of - (Just char) -> - if (Struct.TurnResultAnimator.waits_for_focus animator) - then - ( - model, - (Cmd.batch - [ - (Task.attempt - (Struct.Event.attempted) - (Action.Scroll.to - (Struct.Character.get_location char) - model.ui - ) - ), - (Delay.after 2.0 Delay.Second Struct.Event.AnimationEnded) - ] - ) - ) - else - ( - model, - (Cmd.batch - [ - (Task.attempt - (Struct.Event.attempted) - (Action.Scroll.to - (Struct.Character.get_location char) - model.ui - ) - ), - (Delay.after 0.3 Delay.Second Struct.Event.AnimationEnded) - ] - ) - ) - - - _ -> - ( - model, - (Delay.after 1.0 Delay.Millisecond Struct.Event.AnimationEnded) - ) - -prepare_next_animation : ( - Struct.Model.Type -> - Struct.TurnResultAnimator.Type -> - (Struct.Model.Type, (Cmd Struct.Event.Type)) - ) -prepare_next_animation model animator = - case (Struct.TurnResultAnimator.get_current_animation animator) of - (Struct.TurnResultAnimator.Focus char_index) -> - (handle_char_focus model animator char_index) - - (Struct.TurnResultAnimator.AttackSetup _) -> - ( - model, - (Delay.after 1.0 Delay.Second Struct.Event.AnimationEnded) - ) - - (Struct.TurnResultAnimator.TurnResult turn_result) -> - case turn_result of - (Struct.TurnResult.Attacked _) -> - ( - model, - (Delay.after 3.0 Delay.Second Struct.Event.AnimationEnded) - ) - - _ -> - ( - model, - (Delay.after 0.1 Delay.Second Struct.Event.AnimationEnded) - ) - - _ -> - ( - model, - (Delay.after 0.3 Delay.Second Struct.Event.AnimationEnded) - ) - --------------------------------------------------------------------------------- --- EXPORTED -------------------------------------------------------------------- --------------------------------------------------------------------------------- -apply_to : ( - Struct.Model.Type -> - (Struct.Model.Type, (Cmd Struct.Event.Type)) - ) -apply_to model = - let - new_model = - (Struct.Model.apply_animator_step - (Struct.Model.move_animator_to_next_step model) - ) - in - case new_model.animator of - Nothing -> (new_model, Cmd.none) - (Just animator) -> (prepare_next_animation new_model animator) diff --git a/src/battle/src/Update/HandleServerReply.elm b/src/battle/src/Update/HandleServerReply.elm index 1779db0..31dc97a 100644 --- a/src/battle/src/Update/HandleServerReply.elm +++ b/src/battle/src/Update/HandleServerReply.elm @@ -42,6 +42,8 @@ import Struct.TurnResult import Struct.TurnResultAnimator import Struct.UI +import Update.Puppeteer + -------------------------------------------------------------------------------- -- TYPES ----------------------------------------------------------------------- -------------------------------------------------------------------------------- @@ -179,32 +181,40 @@ add_to_timeline : ( (Struct.Model.Type, (List (Cmd Struct.Event.Type))) ) add_to_timeline turn_results current_state = - let (model, cmds) = current_state in - ( - {model | - animator = - (Struct.TurnResultAnimator.maybe_new - (List.reverse turn_results) - False - ), - ui = - (Struct.UI.set_displayed_tab - Struct.UI.TimelineTab - model.ui - ), - battle = - (Struct.Battle.set_timeline - (Array.append - (Array.fromList turn_results) - (Struct.Battle.get_timeline model.battle) - ) - model.battle - ) - }, - ( - (Delay.after 1 Delay.Millisecond Struct.Event.AnimationEnded) - :: cmds + let + (model, cmds) = current_state + (next_model, more_cmds) = + (Update.Puppeteer.apply_to + ( + {model | + puppeteer = + (List.foldl + (\tr puppeteer -> + (Struct.Puppeteer.append_forward + (Struct.PuppeteerAction.from_turn_result tr) + puppeteer + ) + ) + model.puppeteer + turn_results + ), + battle = + (Struct.Battle.set_timeline + (Array.append + (Array.fromList turn_results) + (Struct.Battle.get_timeline model.battle) + ) + model.battle + ) + } + ) ) + in + ( + next_model, + if (mode_cmds == Cmd.none) + then cmd + else [more_cmds|cmd] ) set_timeline : ( @@ -216,6 +226,17 @@ set_timeline turn_results current_state = let (model, cmds) = current_state in ( {model | + puppeteer = + (List.foldr + (\tr puppeteer -> + (Struct.Puppeteer.append_backward + (Struct.PuppeteerAction.from_turn_result tr) + puppeteer + ) + ) + model.puppeteer + turn_results + ), battle = (Struct.Battle.set_timeline (Array.fromList turn_results) diff --git a/src/battle/src/Update/Puppeteer.elm b/src/battle/src/Update/Puppeteer.elm new file mode 100644 index 0000000..76aa1f9 --- /dev/null +++ b/src/battle/src/Update/Puppeteer.elm @@ -0,0 +1,180 @@ +module Update.Puppeteer exposing (apply_to) + +-- Elm ------------------------------------------------------------------------- +import Delay + +-- Local module ---------------------------------------------------------------- +import Struct.Event +import Struct.Model +import Struct.Puppeteer +import Struct.PuppeteerAction + +import Update.Puppeteer.AnnounceLoss +import Update.Puppeteer.AnnounceVictory +import Update.Puppeteer.Focus +import Update.Puppeteer.Hit +import Update.Puppeteer.Move +import Update.Puppeteer.RefreshCharacter +import Update.Puppeteer.RefreshCharactersOf +import Update.Puppeteer.StartTurn +import Update.Puppeteer.SwapWeapons +import Update.Puppeteer.Target + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +apply_effect_forward : ( + Struct.PuppeteerAction.Effect -> + Struct.Model.Type -> + (Struct.Model.Type, (List (Cmd Struct.Event.Type))) + ) +apply_effect_forward effect model = + case effect of + (AnnounceLoss player_ix) -> + (Update.PuppeteerAction.AnnounceLoss.forward player_ix model) + + (AnnounceVictory player_ix) -> + (Update.PuppeteerAction.AnnounceVictory.forward player_ix model) + + (Focus character_ix) -> + (Update.PuppeteerAction.Focus.forward character_ix model) + + (Hit attack) -> + (Update.PuppeteerAction.Hit.forward attack model) + + (Move (character_ix, direction)) -> + (Update.PuppeteerAction.Move.forward character_ix direction model) + + (RefreshCharacter (on_forward, character_ix)) -> + (Update.PuppeteerAction.RefreshCharacter.forward + on_forward + character_ix + model + ) + + (RefreshCharactersOf (on_forward, player_ix)) -> + (Update.PuppeteerAction.RefreshCharactersOf.forward + on_forward + player_ix + model + ) + + (StartTurn player_ix) -> + (Update.PuppeteerAction.StartTurn.forward player_ix model) + + (SwapWeapons character_ix) -> + (Update.PuppeteerAction.SwapWeapons.forward character_ix model) + + (Target (actor_ix, target_ix)) -> + (Update.PuppeteerAction.Target.forward actor_ix target_ix model) + +apply_effect_backward : ( + Struct.PuppeteerAction.Effect -> + Struct.Model.Type -> + (Struct.Model.Type, (List (Cmd Struct.Event.Type))) + ) +apply_effect_backward effect model = + case effect of + (AnnounceLoss player_ix) -> + (Update.PuppeteerAction.AnnounceLoss.backward player_ix model) + + (AnnounceVictory player_ix) -> + (Update.PuppeteerAction.AnnounceVictory.backward player_ix model) + + (Focus character_ix) -> + (Update.PuppeteerAction.Focus.backward character_ix model) + + (Hit attack) -> + (Update.PuppeteerAction.Hit.backward attack model) + + (Move (character_ix, direction)) -> + (Update.PuppeteerAction.Move.backward character_ix direction model) + + (RefreshCharacter (on_forward, character_ix)) -> + (Update.PuppeteerAction.RefreshCharacter.backward + on_backward + character_ix + model + ) + + (RefreshCharactersOf (on_forward, player_ix)) -> + (Update.PuppeteerAction.RefreshCharactersOf.backward + on_forward + player_ix + model + ) + + (StartTurn player_ix) -> + (Update.PuppeteerAction.StartTurn.backward player_ix model) + + (SwapWeapons character_ix) -> + (Update.PuppeteerAction.SwapWeapons.backward character_ix model) + + (Target (actor_ix, target_ix)) -> + (Update.PuppeteerAction.Target.backward actor_ix target_ix model) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +apply_to : ( + Struct.Model.Type -> + (Struct.Model.Type, (Cmd Struct.Event.Type)) + ) +apply_to model = + case (Struct.Puppeteer.try_getting_current_action model.puppeteer) of + Nothing -> (model, (Cmd.none)) + (Just action) -> + case action of + (Perform effects) -> + if (Struct.Puppeteer.get_is_playing_forward model.puppeteer) + then + -- TODO: iterate over the effects + let updated_model = (forward effect model) in + (apply_to + {updated_model| + puppeteer = + (Struct.Puppeteer.forward + updated_model.puppeteer + ) + } + ) + else + -- TODO: iterate over the effects + let updated_model = (backward effect model) in + (apply_to + {updated_model| + puppeteer = + (Struct.Puppeteer.backward + updated_model.puppeteer + ) + } + ) + + (PerformFor (time, effects)) -> + ( + ( + if + (Struct.Puppeteer.get_is_playing_forward + model.puppeteer + ) + then + -- TODO: iterate over the effects + let updated_model = (forward effect model) in + {updated_model| + puppeteer = + (Struct.Puppeteer.forward + updated_model.puppeteer + ) + } + else + -- TODO: iterate over the effects + let updated_model = (backward effect model) in + {updated_model| + puppeteer = + (Struct.Puppeteer.backward + updated_model.puppeteer + ) + } + ), + (Delay.after time Delay.Second Struct.Event.AnimationEnded) + ) diff --git a/src/battle/src/Update/Puppeteer/Focus.elm b/src/battle/src/Update/Puppeteer/Focus.elm index 04c57c2..e76b7fc 100644 --- a/src/battle/src/Update/Puppeteer/Focus.elm +++ b/src/battle/src/Update/Puppeteer/Focus.elm @@ -37,7 +37,6 @@ forward actor_ix model = ] ) - backward : ( Int -> Struct.Model.Type -> -- cgit v1.2.3-70-g09d2