#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Gryphonegg:
    def __init__(this):
        #Main Variables
        this.state = 1
        this.doControls = 1
        this.doGraphics = 1
        this.alarm = [0, 0, 0, 0, 0, 0]
        #Class Variables
        this.text = ['The Gryphons are sleeping peacefully...',
                     'If you stay calm and quit you should be able to snag some of their#eggs!',
                     'Press the arrow key that appears on screen. Get enough correct#and you get some eggs!',
                     'Press too late, and the game will end with the egss you collected.',
                     'Press the wrong arrow key, and the Gryphons will wake up and you#lose all your eggs!']
        this.direction = 0
        this.combo = 0
        this.time = 60
        this.eggs = 0
        this.falling = []
        Global.audio.ReplaceMusic('Sounds/Music/Through_The_Forest.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
                    if i == 1:
                        this.state = 2
                        this.NextInput()
                    #Input Time
                    if i == 2:
                        this.state = 3
                        this.alarm[0] = 90
                    #Return to map
                    if i == 3:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.gryphonegg += this.eggs
                        Global.audio.ReplaceMusic('Sounds/Music/Town_Theme_Busy.mp3')
                    #Animation
                    if i == 4:
                        pass
                    #Falling
                    if i == 5:
                        this.falling = []

        #Do Controls
        if this.doControls == 1:
            for i in range(len(this.falling)):
                this.falling[i][1] += 4
            if leftpress == 1 and this.state == 2:
                if this.direction == 3:
                    this.NextInput()
                else:
                    this.MisInput()
            if rightpress == 1 and this.state == 2:
                if this.direction == 1:
                    this.NextInput()
                else:
                    this.MisInput()
            if uppress == 1 and this.state == 2:
                if this.direction == 0:
                    this.NextInput()
                else:
                    this.MisInput()
            if downpress == 1 and this.state == 2:
                if this.direction == 2:
                    this.NextInput()
                else:
                    this.MisInput()
            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:
                    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.gryphon, (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:
                if this.alarm[1] > 0:
                    Global.TextDisplay(Global, 2, (0, 0, 0), 3, 206, 'Snagged '+str(int(this.combo/10))+' Gryphon eggs!')
                else:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Gryphon Eggs: '+str(this.eggs))
                    if this.direction == 0:
                        Global.screen.blit(Global.graphics.arrow_up, (184, 104-this.alarm[4]))
                    if this.direction == 1:
                        Global.screen.blit(Global.graphics.arrow_right, (184, 104-this.alarm[4]))
                    if this.direction == 2:
                        Global.screen.blit(Global.graphics.arrow_down, (184, 104-this.alarm[4]))
                    if this.direction == 3:
                        Global.screen.blit(Global.graphics.arrow_left, (184, 104-this.alarm[4]))
                for i in range(len(this.falling)):
                    if this.falling[i][1] < 170:
                        Global.screen.blit(Global.graphics.gryphonegg, (this.falling[i][0], this.falling[i][1]))
            if this.state == 3:
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Maybe you should stop before a Gryphon spots you...')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')
            if this.state == 4:
                Global.screen.blit(Global.graphics.shadow, (140, 64))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'Uh oh! Looks like a Gryphon spotted you! Drop all your eggs and run!')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')

    def NextInput(this):
        this.combo += 1
        this.alarm[4] = 3
        if this.combo%10 == 0:
            this.alarm[1] = 30
            this.time -= 10
            if this.time < 20:
                this.time = 20
            this.eggs += int(this.combo/10)
            for i in range(int(this.combo/10)):
                this.falling.append([Global.Random(150, 250), Global.Random(80, 144)])
            this.alarm[5] = 60
            this.alarm[2] = 0
        else:
            this.alarm[2] = this.time
            this.direction = Global.Random(0, 3)

    def MisInput(this):
        this.state = 4
        this.eggs = 0
        this.alarm[0] = 90
        this.alarm[2] = 0
