Standev

CombatSession

Oct 7th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.40 KB | None | 0 0
  1. package world.mechanics.combat
  2.  
  3. import world.activity.Activity
  4. import world.activity.process.action.Action
  5. import world.activity.process.action.Action.State.*
  6. import world.mechanics.combat.supervision.DamageSupervisor
  7. import world.activity.result.Result
  8. import world.activity.process.causality.effects.Animation
  9. import world.activity.process.causality.effects.Graphic
  10. import world.activity.process.causality.effects.CombatProjectile
  11. import world.activity.force.Force
  12. import world.mechanics.Mechanic
  13. import world.mechanics.combat.supervision.behavior.CombatTactic
  14. import world.mechanics.combat.supervision.behavior.CombatTurn
  15. import java.util.*
  16.  
  17. /**
  18.  * This class represents the mechanics of a combat session between two [CombatAgent] entities.
  19.  *
  20.  * @param agent subject of this [CombatSession]
  21.  * @param target potential adversary of [agent]
  22.  * @param tactic behavioural instructions for this [CombatSession]
  23.  *
  24.  * Created by Stan van der Bend for Empyrean at 08/06/2018.
  25.  *
  26.  * @author https://www.rune-server.ee/members/icebrick/
  27.  */
  28. abstract class CombatSession(val agent: CombatAgent, val target: CombatAgent, private val tactic: CombatTactic) : Mechanic<CombatAgent>, Action() {
  29.  
  30.     lateinit var turn: CombatTurn
  31.  
  32.     /**
  33.      * Supervises the damage applied to this [agent] by incoming strikes.
  34.      */
  35.     private val damageSupervisor : DamageSupervisor = DamageSupervisor(agent)
  36.  
  37.     /**
  38.      * This function is called if either [turn] is not initialized or is [Action.State.TERMINATED].
  39.      *
  40.      * @return determine the next turn of [CombatSession.agent]
  41.      * @see CombatTurn.state
  42.      */
  43.     abstract fun createNextTurn(): CombatTurn
  44.  
  45.     /**
  46.      * While this sessions is alive, this will be called every game cycle.
  47.      *
  48.      * @see util.TimeUtil.GAME_CYCLES the execution interval
  49.      * @see Action.State the potential state of any [Action]
  50.      * @see CombatTactic the tactic instructing this [CombatSession]
  51.      * @see CombatTurn the turn in which [agent] is subject
  52.      */
  53.     override fun sequence() {
  54.  
  55.         // Determine whether current turn is either not initialized, or the former one is terminated.
  56.         // If either is true, create next turn and continue.
  57.         if(!this::turn.isInitialized || turn.state == TERMINATED)
  58.             turn = createNextTurn()
  59.  
  60.         turn.pulse()
  61.  
  62.         // Determine whether turn has successfully started and initiate behaviour as instructed by the active tactic.
  63.         if(turn.state == STARTED){
  64.  
  65.             tactic.pulse()
  66.  
  67.             // Determine whether visual effects should be negated in this sequence.
  68.             if(!tactic.skipVisualEffects(agent, target)) {
  69.                 initGraphic().ifPresent(agent::performGraphic)
  70.                 initAnimation().ifPresent(agent::performAnimation)
  71.                 initProjectile().ifPresent{run{ it.lockOn(target).send() }}
  72.                 finalGraphic().ifPresent(target::performGraphic)
  73.             }
  74.  
  75.             // Create an optional outgoing strike and apply it to the target.
  76.             turn.strike(agent, target).ifPresent { strike ->
  77.                 // Drain target's life by final strike damage
  78.                 run { strike.collide().apply(target) }
  79.             }
  80.  
  81.             turn.state = PULSING
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Handles outgoing forces.
  87.      *
  88.      * @param opponentSession the [CombatSession] with [target] as its subject
  89.      */
  90.     fun pulse(opponentSession: CombatSession){
  91.  
  92.         if(!tactic.skipNextPulse(agent, target)) {
  93.             pulse()
  94.             turn.strike(agent, target)
  95.                     .flatMap(opponentSession::apply)
  96.                     .ifPresent {
  97.                         action : Result<CombatAgent> -> run { action.apply(target) }
  98.                     }
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Adhere to resulting processes, inherited from [Mechanic].
  104.      *
  105.      * @param activity the incoming activity to be dealt with
  106.      *
  107.      * @return if the incoming activity is a force then it will be dealt with accordingly,
  108.      *         in every other case the method returns [Optional.empty]
  109.      */
  110.     override fun apply(activity: Activity): Optional<Result<CombatAgent>> {
  111.  
  112.         // We currently only deal with forces (strikes in specific)
  113.         if(activity is Force) {
  114.  
  115.             if(!tactic.negateNextHostileDamage(agent, target))
  116.                 damageSupervisor.resolve(activity)
  117.  
  118.             if(tactic.negateNextHostileForces(agent, target))
  119.                 return Optional.empty()
  120.  
  121.             return Optional.of(activity.collide())
  122.         }
  123.  
  124.         return Optional.empty()
  125.     }
  126.  
  127.     /**
  128.      * @return an optional animation performed at the start of the current [turn] by the [agent].
  129.      * @see Animation
  130.      */
  131.     open fun initAnimation(): Optional<Animation> { return Optional.empty() }
  132.     /**
  133.      * @return an optional projectile executed at the start of the current [turn].
  134.      * @see CombatProjectile
  135.      */
  136.     open fun initProjectile(): Optional<CombatProjectile> { return Optional.empty() }
  137.     /**
  138.      * @return an optional graphic executed at the start of the current [turn].
  139.      * @see Graphic
  140.      */
  141.     open fun initGraphic(): Optional<Graphic> { return Optional.empty() }
  142.     /**
  143.      * @return an optional graphic executed at the end of the current [turn].
  144.      * @see Graphic
  145.      */
  146.     open fun finalGraphic(): Optional<Graphic> { return Optional.empty() }
  147.  
  148. }
Add Comment
Please, Sign In to add comment