###################################################################
# Panda Railshooter Demo (Version 1.0)
# Main Program Section
# Written by Jason A. Yeung, 2003
# Send all Questions to etc-panda3d@lists.andrew.cmu.edu
###################################################################

# DESCRIPTION:
# This is the main program file for the Railshooter.  It contains the main 
# World Class, which is instanced to start the game.  The world class also
# creates the various structures needed to run the game (such as controls
# and the title screen).

# We use a lot of sounds, so we have to override the
# default sound limit.
import direct
from pandac.PandaModules import ConfigVariableInt
ConfigVariableInt("audio-cache-limit",128).setValue(128)

# Load the panda libraries
import direct.directbase.DirectStart
from direct.showbase.ShowBaseGlobal import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase import DirectObject
from direct.actor import Actor
from direct.task import Task

# Import Framerate Meter
import FPSMeter

# Import other Railshooter Specific Modules
import EnemyFormations
import HeadsUpDisplay
import LoadModels
import LoadTextures
import SoundFX
import ClassGlobals
import StartMenu
import LoadParticles


########################################
# Create a Direct Object Event Handler #
########################################
class World(DirectObject.DirectObject):
    def __init__(self):

	self.gameOver = 0
	self.gameStarted = 0

  	########################################
   	# Load Title Screen
   	########################################

	# Load Title Screen
	titleScreen = StartMenu.StartMenu()
	taskMgr.add(waitStart, 'waitStart')


	########################################
	# Create Heads-Up-Display
	########################################

	# Create Heads-up-Display
	ClassGlobals.hud1.createHUD()


	########################################
	# Activate Framerate Meter
	########################################

        # Create fps meter
        # self.fpsMeter = FPSMeter.FPSMeter()

	# Start FPS Meter
        # self.fpsMeter.enable()


	########################################
	# Accept Input
	########################################

	# Create a list to keep track of Key Presses
        self.keydown = {'shoot1':0, 'shoot2':0}

	# Movement Keys
	self.accept('mouse1', self.HandleKeyEvent, ['shoot1', 1])
	self.accept('mouse1-up', self.HandleKeyEvent, ['shoot1', 0])
	self.accept('mouse3', self.HandleKeyEvent, ['shoot2', 1])
	self.accept('mouse3-up', self.HandleKeyEvent, ['shoot2', 0])

	# Exit Game Menu Key
	self.accept('escape', self.exitMenu)

	# 1 Key Toggles Wireframe Display Mode
	self.accept('1', self.wireframeMode)

 	# 2 Key Toggles Texture Display Mode
	self.accept('2', self.noTextureMode)

	# Accept this Event to Start the Game Over
	self.accept('continue', self.playAgain)

	# S Button Takes Screenshots
	self.accept('s', base.screenshot)


    	########################################
   	# Load the Particle Effects
   	########################################
	LoadParticles.cloudSCUD()

	# Can Load Windtrails, but effect was less than
	# desired
	# LoadParticles.windTrail(1.25, -0.1, 0.2)
	# LoadParticles.windTrail(-1.25, -0.1, 0.2)
	
	# Debug - Keyboard driving
	# base.useDrive()


    ########################################
    # Define Methods
    ########################################

    # Begin the Level
    def startGame(self):

	# Hide Preloader Boss Model
	LoadModels.hidePreloaders()

	# Remove Black Screen
	ClassGlobals.hud1.fadeIn(1, 0, 0, 0, 1)

	# Begin Level
	ClassGlobals.stage1.playLevel()

	# Start Mouse Controls
	ClassGlobals.mouser.startMouse()

	# Reset the Score
	ClassGlobals.p1.score = 0


    # Resets the Game and Starts it Over after Game Over
    def playAgain(self):
	self.gameOver = 0
	ClassGlobals.gameIsOver = 0
	ClassGlobals.hud1.hideGameOver()

	# Reset Player Class
	ClassGlobals.p1.resetPlayer()

	# Start the Game
	self.startGame()


    # Function to maintain keyboard state
    def HandleKeyEvent(self, key, value):
        self.keydown[key] = value
	
	# Broadcasts the current Key Config to the Player Classes
	ClassGlobals.p1.keypressHolder = self.keydown


    # Bring up the Game Menu
    def exitMenu(self):
	if ClassGlobals.mouseIsEnabled == 1 and ClassGlobals.gameIsOver == 0 and self.gameStarted == 1:
	    print 'Mouse Disabled'
	    ClassGlobals.mouser.stopMouse()

	    # Reset Crosshair to Eliminate Jittering
	    ClassGlobals.p1.craftDummy.setHpr(0, 0, 0)	
	elif ClassGlobals.p1.isAlive == 1 and ClassGlobals.gameIsOver == 0 and self.gameStarted == 1:
	    print 'Mouse Enabled'
	    ClassGlobals.mouser.startMouse()


    ########################################
    # Change Display Methods
    ########################################

    # Toggles Wireframe Mode
    def wireframeMode(self):
	base.toggleWireframe()

    # Toggles Texture Mode
    def noTextureMode(self):
 	base.toggleTexture()


########################################
# Load up Title Screen
########################################

# Starts Running the World
def waitStart(task):
    global myGame
    if ClassGlobals.gameStart == 1:
	ClassGlobals.gameStart = 0
	myGame.gameStarted = 1
	myGame.startGame()
	return Task.done
    else:
	return Task.cont

# Create Instance of Game
myGame = World()


########################################
# RUN THE RAILSHOOTER!
########################################
run()



























