# Balazar Brothers
# Copyright (C) 2006 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import cerealizer, soya, tofu, soya.tofu4soya as tofu4soya

import balazar_brothers
from balazar_brothers.character import BlueBalazar, RedBalazar, UpdateState
from balazar_brothers.level     import Level, create_demo_level

class Player(tofu4soya.Player):
  def __init__(self, filename, password, client_side_data = ""):
    tofu4soya.Player.__init__(self, filename, password)
    
    self.score = 0
    self.keys  = 0
    
    self.current_level_name  = balazar_brothers.globdef.START_LEVEL + "_1"
    
    #self.current_level_name  = "tutorial_1"
    #self.current_level_name  = "tutorial_6"
    #self.current_level_name  = "test"
    #self.current_level_name  = "coqueliform_field_8"
    #self.current_level_name  = "pompon_forest_6"
    
    self.previous_level_name = "__new_perso__"
    self.create_characters()
    
  def restart_world(self):
    self.current_level_name  = self.current_level_name[:self.current_level_name.rfind("_")] + "_1"
    self.previous_level_name = "__new_perso__"
    for mobile in self.mobiles: mobile.die()
    
  def add_bonus(self, score = 0, keys = 0):
    self.score += score
    self.keys  += keys
    
    for character in self.mobiles:
      character.doer.action_done(UpdateState(character, self.score, self.keys))
      
  def create_characters(self):
    level      = Level.get(self.current_level_name)
    
    character  = RedBalazar()
    character.previous_level_name = self.previous_level_name
    character.bot = 0
    character.score = self.score
    character.keys  = self.keys
    level.add_mobile(character)
    self .add_mobile(character)
    
    character  = BlueBalazar()
    character.previous_level_name = self.previous_level_name
    character.bot = 0
    character.score = self.score
    character.keys  = self.keys
    level.add_mobile(character)
    self .add_mobile(character)
    
    
  def login(self, notifier, address, client_side_data = ""):
    if not self.mobiles:
      self.create_characters()
      
    tofu4soya.Player.login(self, notifier, address, client_side_data)

      
  def remove_mobile(self, mobile):
    for character in self.mobiles:
      if not character.dying: character.die()
      
    tofu4soya.Player.remove_mobile(self, mobile)
    
    
  def kill(self, last_mobile):
    self.score = max(0, self.score - 1)
    self.logout(0)
    self.discard()
cerealizer.register(Player, tofu.SavedInAPathHandler(Player))
    
