#NeoPeewee Pygame Engine (January 4, 2020)

import pygame

#Import Global Memory
from Scripts import Global

class Makishroom:
    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 = ['Maki Shrooms grow very large around the swamp...',
                     'Smaller ones that cane be used for alchemy grow on top of the larger#ones!',
                     'Try shaking the large Maki Shroom rythmatically to get some of the#smaller shrooms to fall.',
                     'Press Z to shake the giant shroom when you see it sway.',
                     'Lose your rythme, and the game ends!']
        this.rythme = 60
        this.window = 20
        this.failcount = 0
        this.safe = 0
        this.combo = 0
        this.shrooms = 0
        this.falling = []
        Global.audio.ReplaceMusic('Sounds/Music/A_Mermaids_Song.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] = 30
                    #Rythme
                    if i == 2:
                        if this.safe == 0:
                            this.failcount += 1
                        if this.failcount > 3:
                            this.state = 3
                            this.alarm[0] = 90
                        if this.safe == 1:
                            this.safe = 0
                            this.failcount = 0
                            this.combo += 1
                            if this.combo == 5:
                                this.shrooms += int((22-this.window)/2)
                                this.alarm[5] = 60
                                for j in range(int((22-this.window)/2)):
                                    this.falling.append([Global.Random(150, 250), Global.Random(0, 32)])
                                    this.falling[j][1] -= 64
                                this.combo = 0
                                this.rythme -= 5
                                if this.rythme < 20:
                                    this.rythme = 20
                                this.window -= 3
                                if this.window < 2:
                                    this.window = 2
                        if this.state == 2:
                            this.alarm[2] = this.rythme
                    #Return to map
                    if i == 3:
                        this.state = 0
                        Global.map.state = 1
                        Global.inventory.makishroom += this.shrooms
                        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 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:
                    if this.alarm[2] > 0 and this.alarm[2] < this.window and this.safe == 0:
                        this.safe = 1
                        this.alarm[4] = 15
                    else:
                        this.failcount += 1
                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.swamp, (0, 0))
            if this.state == 1:
                Global.screen.blit(Global.graphics.mushrooma, (120, 0))
                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, 'Maki Shrooms: '+str(this.shrooms))
                if this.alarm[4] > 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Nice Rythme!')
                if this.alarm[2] > 0 and this.alarm[2] < this.window and this.safe == 0:
                    Global.screen.blit(Global.graphics.mushroomb, (120, 0))
                else:
                    Global.screen.blit(Global.graphics.mushrooma, (120, 0))
                for i in range(len(this.falling)):
                    if this.falling[i][1] < 156:
                        Global.screen.blit(Global.graphics.makishroom, (this.falling[i][0], this.falling[i][1]))
            if this.state == 3:
                Global.screen.blit(Global.graphics.mushroomb, (120, 0))
                Global.TextDisplay(Global, 1, (0, 0, 0), 3, 206, 'You lost the rythme, and the Maki Shroom is standing still again!')
                if this.alarm[0] == 0:
                    Global.TextDisplay(Global, 1, (0, 0, 0), 3, 222, 'Press Z to return to the map...')
