######################################################################
# NOTE: This was the first mob I wrote in Python / Jython.           #
#       It is excessively commented to provide a reference for other #
#       Mobs to be written.                                          #
#                                                                    #
#                         88          88 88                          # 
#                         88          88 ""                          #
#                         88          88                             #
#  ,adPPYb,d8  ,adPPYba,  88,dPPYba,  88 88 8b,dPPYba,               #
# a8"    `Y88 a8"     "8a 88P'    "8a 88 88 88P'   `"8a              #
# 8b       88 8b       d8 88       d8 88 88 88       88              #
# "8a,   ,d88 "8a,   ,a8" 88b,   ,a8" 88 88 88       88              #
#  `"YbbdP"Y8  `"YbbdP"'  8Y"Ybbd8"'  88 88 88       88              #
#  aa,    ,88                                                        #
#   "Y8bbdP"                                                         #
#                                                                    #
######################################################################

def __goblin_tick__(g):
    ### RUN ALL THE ANIMATIONS
    g.runAnimations()
    
    # IF NOT ALIVE, DO AFTER DEATH
    if not g.alive():
        __goblin_after_death__(g)
        return
    
    # DISCOVER
    if not g.getDiscoveredStatus(): return
    
    ## IF HEALTH IS ABOVE THE MAX HEALTH, CLAMP
    g.normalizeHealth()
    
    ### IF THE GET HIT TIMER IS POSITIVE, START DECREMENTING IT
    g.getHitClock()
    
    ### COLLISION
    g.collision()
    
    ### POSITION
    g.updatePosition()
    
    ## IDLE ANIMATIONS (idles, walks)
    __goblin_idle__(g)
        
    ### IF IS ALREADY FIGHTING, THEN TICK HIS ATTACK SEQUENCE WITH RUNNING SPEED
    if g.inCombat: g.attackSequence(g.runningSpeed)
    ## IF NOT ALREADY FIGHTING, AND AGGRO-RADIUS INTERSECTS THE PLAYER, START FIGHTING
    elif Utils.intersects(g.center, g.aggroRadius, Player.center): g.startAttackSequence()
        
    # run bug fixes
    g.allBugFixes()

def __goblin_idle__(g):
    ## ONLY WORK IF *NOT* IN COMBAT / RUNNING AWAY
    if g.collided or g.runningAway or g.inCombat: return
        
    ## SUBTRACT FROM ANIMATION TIMER IF DOING IDLE ANIMATION
    if g.currentAnimation != ANIMATION.idleWalk: g.idleAnimationTimer -= 1
        
    ## SUBTRACT FROM IDLE WALK DISTANCE IF IDLE WALKING
    elif g.currentAnimation == ANIMATION.idleWalk: g.distanceToMove -= Utils.pythagoras(g.velX, g.velY)

    ## IF WALKING AROUND, AND HIS DISTANCE IS UP:
    if g.distanceToMove <= 0 and g.currentAnimation == ANIMATION.idleWalk:
        ### CHANCE OF CONTINUING ON WALKING INSTEAD OF IDLING
        if Utils.rand.nextInt(8) == 0: g.idleMove(g.walkingSpeed)
        else: #### ELSE SET HIM GOING TO A RANDOM IDLE ANIMATION
            g.idleAnimationTimer = Utils.rand.nextInt(200) + 100
            g.velX = g.velY = 0
            if Utils.COIN_FLIP(): g.currentAnimation = ANIMATION.idle1
            else: g.currentAnimation = ANIMATION.idle2
        
    ### IF ANIMATION DOES NOT EQUAL IDLE WALK AND THE IDLE ANIMATION TIMER IS UP, SET HIM WALKING
    if g.currentAnimation != ANIMATION.idleWalk and g.idleAnimationTimer <= 0: g.idleMove(g.walkingSpeed)
        
    ### IF GOBLIN IS OUTSIDE OF HIS HOME BUT ISNT COMBAT OR WALKING
    if not g.home.contains(g.center):
        g.currentAnimation = ANIMATION.idleWalk  ## some mobs may have a run animation
        ## IF SO FAR AWAY, RUN HOME, ELSE JUST WALK HOME
        if(g.center.distance(g.home.center) > 5000): g.goHome(g.runningSpeed) 
        else: g.goHome(g.walkingSpeed)

def __goblin_after_death__(g):
    ## SUBTRACT FROM DESTROYING TIMER
    g.destroyingTimer -= 1
    
    ## IF THE TIMER IS UP, DESTROY!
    if g.destroyingTimer < 0: g.destroy()
    
