Source

fungus / fungus_arceron_audio.py

The default branch has multiple heads

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

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

#### Call the correct fungus_game when called from the command line ####

if __name__ == "__main__": 
    # Call this Scene via fungus_game
    from fungus_core import call_this_scene
    # pass the commandline args
    from sys import argv
    call_this_scene(__file__, argv)
    

#### 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.bg = 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.
	self.bg.dispatch_events()
	for player in self.players[:]: 
		player.dispatch_events()
		if not player.playing: 
			self.players.remove(player)
	
	# Start the menu in the beginning
	if self.state["scene"] is None: 
	    self.start_menu()
	
	# After 10s we switch to the intro
	if self.state["scene"] == "menu" and time() - self.start_time >= 30: 
	    if self.bg.volume > 0.0: 
		self.bg.volume -= 0.001
	    else: 
		self.start_intro()
	
	# 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: 
		self.start_battle()
	
	if self.state["scene"] == "battle" and time() - self.start_time < 100: 
	    self.update_battle()
	
	if self.state["scene"] == "battle" and self.bg.volume < 1.0: 
	    self.bg.volume += 0.001
	
	
	if  self.state["scene"] == "battle" and time() - self.start_time >= 100: 
	    if self.bg.volume > 0.0: 
		self.bg.volume -= 0.001
	    else: 
		self.start_credits()
	
	if self.state["scene"] == "credits" and self.bg.volume < 0.6: 
	    self.bg.volume += 0.001
	
	if time() - self.start_time > 150: 
	    self.bg.volume -= 0.001
	    self.playSoundEffect(battle_sounds[0])
	
	if time() - self.start_time > 155: 
	    self.core.win.has_exit = 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)
	    source.play()
	elif static_source is not None: 
	    static_source.play()
    
    def start_menu(self): 
	"""Start the menu"""
	print "start menu"
	self.state["scene"] = "menu"
	self.bg.play()
	
    
    def start_intro(self): 
	"""Start the intro playback."""
	print "start intro"
	self.state["scene"] = "intro"
	self.intro_player = self.core.load_player(intro)
	self.bg.pause()
	self.intro_player.play()
	self.players.append(self.intro_player)
    
    def start_battle(self): 
	"""Start the battle playback"""
	print "start battle"
	self.state["scene"] = "battle"
	source = self.core.load_media_source(battle_background)
	self.bg.queue(source)
	self.bg.play()
	self.bg.next()
    
    def start_credits(self): 
	"""Start the end credits."""
	print "start credits"
	self.state["scene"] = "credits"
	source = self.core.load_media_source(end_credits)
	self.bg.pause()
	self.bg.queue(source)
	self.bg.play()
	self.bg.next()
    
    def update_battle(self): 
	"""Play battle sounds."""
	# Fade in bg
	if self.bg.volume < 1.0: 
	    self.bg.volume += 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
	    player.play()
	    self.players.append(player)
    
    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: 
            self.core.win.has_exit = True