#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Lizardstail:
    def __init__(this):
        #Main Variables
        this.state = 1
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0, 0]
        #Class Variables
        this.text = ['Lizars around here have magical tails, very popular among#magicians and aclhemists.',
                     'Their tails tend to slip right off just by a touch, and they feel no harm#from it!',
                     'They tend to run around a lot, so try to catch one as it runs by!',
                     'Press Z when a lizard is right in the center to slip its tail off.',
                     'Miss too many times, and they might all run off though!']
        this.tails = 0
        this.failcount = 0
        this.lizards = []
        Global.audio.ReplaceMusic('Sounds/Music/The_Animal_Den.mp3')

    #Main Control Method
    def Control(this, leftpress, rightpress, uppress, downpress, zpress, xpress):
        #Manage Alarms
        for i in range(len(this.alarm)):
            if this.alarm[i] > 0:
                this.alarm[i] -= 1
                if this.alarm[i] == 0:
                    #Text Control
                    if i == 0:
                        pass
                    #Start Game/Check for Game End
                    if i == 1:
                        this.state = 2
                        this.alarm[2] = 5
                    #Spawn Lizard
                    if i == 2:
                        this.SpawnLizard()
                    #Return to map
                    if i == 3:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.lizardstail += this.tails
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')

        #Do Controls
        if this.doControls == 1:
            for i in range(len(this.lizards)):
                if this.lizards[i][0] == 0:
                    this.lizards[i][2] += this.lizards[i][1]
                    if this.lizards[i][1] >= 368:
                        this.lizards[i][0] = 4
                if this.lizards[i][0] == 1:
                    this.lizards[i][2] += this.lizards[i][1]
                    if this.lizards[i][1] >= 168:
                        this.lizards[i][0] = 4
                if this.lizards[i][0] == 2:
                    this.lizards[i][2] -= this.lizards[i][1]
                    if this.lizards[i][1] <= 0:
                        this.lizards[i][0] = 4
                if this.lizards[i][0] == 3:
                    this.lizards[i][2] -= this.lizards[i][1]
                    if this.lizards[i][1] <= 0:
                        this.lizards[i][0] = 4
            if zpress == 1:
                if this.state == 1 and this.alarm[0] == 0 and len(this.text) > 0:
                    del(this.text[0])
                    if len(this.text) == 0:
                        this.alarm[1] = 50
                    else:
                        this.alarm[0] = 4
                if this.state == 2:
                    for i in range(len(this.lizards)):
                        if ((this.lizards[i][0] == 0 or this.lizards[i][0] == 2) and (this.lizards[i][2] > 150 and this.lizards[i][2] < 220)) or ((this.lizards[i][0] == 1 or this.lizards[i][0] == 3) and (this.lizards[i][2] > 50 and this.lizards[i][2] < 100)):
                            this.tails += 1
                            del(this.lizards[i])
                            break
                        if i == len(this.lizards)-1:
                            this.failcount += 1
                            if this.failcount > 2:
                                this.state = 3
                                this.alarm[0] = 90
                if this.state == 3 and this.alarm[0] == 0:
                    Global.DoTransition(Global, 10)
                    this.doControls = 0
                    this.alarm[3] = 10
            if xpress > 0 and this.state == 1 and this.alarm[0] == 0 and len(this.text) > 0:
                del(this.text[0])
                if len(this.text) == 0:
                    this.alarm[1] = 50
                else:
                    this.alarm[0] = 4

        #Do Graphics
        if this.doGraphics == 1:
            Global.screen.blit(Global.graphics.lizards, (0, 0))
            if this.state == 1:
                if len(this.text) > 0:
                    Global.TextDisplay(Global, 1, (0+(this.alarm[0]*20), 0+(this.alarm[0]*20), 0+(this.alarm[0]*20)), 3, 206-this.alarm[0], this.text[0].split('#')[0])
                    if len(this.text[0].split('#')) > 1:
                        Global.TextDisplay(Global, 1, (0+(this.alarm[0]*50), 0+(this.alarm[0]*50), 0+(this.alarm[0]*50)), 3, 222-this.alarm[0], this.text[0].split('#')[1])
                if this.alarm[1] > 20:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Ready?')
                elif this.alarm[1] > 0:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Go!')
            if this.state == 2:
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Lizard Tails: '+str(this.tails))
                for i in range(len(this.lizards)):
                    if this.lizards[i][0] == 0:
                        Global.screen.blit(Global.graphics.lizard_right, (this.lizards[i][2], 68))
                    if this.lizards[i][0] == 1 and this.lizards[i][2] < 169:
                        Global.screen.blit(Global.graphics.lizard_down, (168, this.lizards[i][2]))
                    if this.lizards[i][0] == 2:
                        Global.screen.blit(Global.graphics.lizard_left, (this.lizards[i][2], 100))
                    if this.lizards[i][0] == 3:
                        Global.screen.blit(Global.graphics.lizard_up, (200, this.lizards[i][2]))
            if this.state == 3:
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'The Lizards all fled!')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')

    def SpawnLizard(this):
        this.lizards.append([Global.Random(0, 3), Global.Random(6, 8+int(this.tails*0.1)), 0])
        if this.lizards[len(this.lizards)-1][0] == 2:
            this.lizards[len(this.lizards)-1][2] = 368
        if this.lizards[len(this.lizards)-1][0] == 3:
            this.lizards[len(this.lizards)-1][2] = 168
        for i in range(len(this.lizards)):
            if this.lizards[i][0] == 4:
                del(this.lizards[i])
                break
        this.alarm[2] = 30-int(this.tails*0.4)
        if this.alarm[2] < 5:
            this.alarm[2] = 5
