fungus /

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# encoding: utf-8

"""An audio scene preview for Arceron."""

#### Imports ####

from os import path
from pyglet.window import key
# TODO: Remove the need to import images via pyglet - use the core object we pass to the scene. 

from fungus_scene import BaseScene
from fungus_core import __copyright__

# for timing the scene we need the current unix time
from time import time

# For the battle we need random choice
from random import random, choice

# Also listdir to see the files in the audio dir
from os import listdir
from os.path import isfile, join

#### An example audio scene ####

### Things needed for the scene ###

## An audio file ##
sword_file = "sword.wav"
menu_loop = "Arceron-Intro-loop.ogg"
intro = "Intro.ogg"
battle_background = "Kampftrommel.ogg"
battle_sounds = ["Grooar!.ogg"]
for i in range(7): 
    battle_sounds.append("combat-sword-clash-" + str(i) + ".ogg")
for i in listdir("audio/wesnoth/"): 
    if i.endswith(".ogg"): 
	battle_sounds.append(join("wesnoth", i))
end_credits = "Dadadida-mehrere-stimmen.ogg"

### The Scene itself. 

class Scene(BaseScene): 
    """A dummy scene - mostly just the Scene API."""
    def __init__(self, core, *args, **kwds): 
        """Initialize the scene with a core object for basic functions."""
        ## Get the necessary attributes for any scene. 
        # This gets the 'visible', 'colliding' and 'overlay' lists 
        # as well as the scene switch 'switch_to_scene' 
        # which can be assigned a scene to switch to. 
        super(Scene, self).__init__(core, *args, **kwds)
	#: A list of players which can play
	self.players = []
	#: Player for the background music = self.core.load_player(menu_loop, loop=True)
	#: Current state of the scene
	self.state = {"scene": None}
	#: The time when we started the scene
	self.start_time = time()

    def update(self): 
        """Update the stats of all scene objects. 

Don't blit them, though. That's done by the Game itself.

To show something, add it to the self.visible list. 
To add a collider, add it to the self.colliding list. 
To add an overlay sprite, add it to the self.overlay list. 
	###Check all players if they finished and if they should loop. If they shouldn't loop, they get deleted.
	for player in self.players[:]: 
		if not player.playing: 
	# Start the menu in the beginning
	if self.state["scene"] is None: 
	# After 10s we switch to the intro
	if self.state["scene"] == "menu" and time() - self.start_time >= 30: 
	    if > 0.0: -= 0.001
	# If we're in the intro, switch to the next scene as soon as the intro player finishes
	if self.state["scene"] == "intro": 
	    if not self.intro_player in self.players: 
	if self.state["scene"] == "battle" and time() - self.start_time < 100: 
	if self.state["scene"] == "battle" and < 1.0: += 0.001
	if  self.state["scene"] == "battle" and time() - self.start_time >= 100: 
	    if > 0.0: -= 0.001
	if self.state["scene"] == "credits" and < 0.6: += 0.001
	if time() - self.start_time > 150: -= 0.001
	if time() - self.start_time > 155: = True
    def playSoundEffect(self, sound_file=None, static_source=None): 
        """Play the given sound file."""
	# If we have a sound file, we check if there's a player we can use, else we create a new one.
	if sound_file is not None: 
	    source = self.core.load_media_source(sound_file)
	elif static_source is not None:
    def start_menu(self): 
	"""Start the menu"""
	print "start menu"
	self.state["scene"] = "menu"
    def start_intro(self): 
	"""Start the intro playback."""
	print "start intro"
	self.state["scene"] = "intro"
	self.intro_player = self.core.load_player(intro)
    def start_battle(self): 
	"""Start the battle playback"""
	print "start battle"
	self.state["scene"] = "battle"
	source = self.core.load_media_source(battle_background)
    def start_credits(self): 
	"""Start the end credits."""
	print "start credits"
	self.state["scene"] = "credits"
	source = self.core.load_media_source(end_credits)
    def update_battle(self): 
	"""Play battle sounds."""
	# Fade in bg
	if < 1.0: += 0.001
	# Play a battle sound at random times
	if len(self.players) < 3 and random() < 0.001: 
	    player = self.core.load_player(choice(battle_sounds))
	    player.volume = 0.6
    def on_key_press(self, symbol, modifiers): 
        """Forwarded keyboard input."""
        # Use the escape key as a means to exit the game. 
        if symbol == key.ESCAPE: 
   = True