# 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, balazar_brothers.globdef as globdef
from balazar_brothers.character import State, Balazar
from balazar_brothers.player import Player


class BonusTakenState(State):
  def __init__(self, bonus, character):
    State.__init__(self, bonus)
    
    self.character = character
cerealizer.register(BonusTakenState)
  
class BonusResurrectedState(State): pass  
cerealizer.register(BonusResurrectedState)

class Bonus(tofu4soya.Mobile):
  TAKEN_SOUND      = "ouais.wav"
  
  def __init__(self):
    tofu4soya.Mobile.__init__(self)
    
    self.bot       = 1
    self.radius    = 1.0
    self.animation = 0
    
  def suitable_for(self, character): return isinstance(character, Balazar)
  
  def do_action(self, action):
    if self.visible and not self.animation:
      for character in self.level.mobiles:
        if self.suitable_for(character) and self.distance_to(character.center) < self.radius:
          self.doer.action_done(BonusTakenState(self, character))
          break
        
  def set_state(self, state):
    if   isinstance(state, BonusTakenState):
      self.taken_by(state.character)
      
    elif isinstance(state, BonusResurrectedState):
      self.resurrected()

  def taken_by(self, character):
    self.animation = 1
    balazar_brothers.sound.play(self.TAKEN_SOUND, self, gain=12.0)
    
  def resurrected(self):
    self.set_scale_factors(1.0, 1.0, 1.0)
    self.visible   = 1
    self.animation = 0
    
  def advance_time(self, proportion):
    if self.visible:
      if self.animation:
        self.rotate_lateral((0.2 * self.animation + 4.0) * proportion)
        
        self.animation += 1
        
        if   self.animation <  50.0: self.y += 0.15 * proportion
        elif self.animation <  80.0: pass
        elif self.animation < 100.0: self.scale(0.9, 0.9, 0.9)
        else:
          self.visible   = 0
          self.animation = 0
          
      else:
        self.rotate_lateral(4.0 * proportion)
cerealizer.register(Bonus)
        
        
class SmallChest(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.shape = soya.Shape.get("coffre1@typ1")
    self.lid   = soya.Volume(self, soya.Shape.get("coffre_couvercle1@typ1"))
    self.lid.set_xyz(0.0, 2.0, 1.0)
    self.scale(0.5, 0.5, 0.5)
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 2.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    
    if not self.doer.remote: Player.get(character.player_name).add_bonus(score = 1)
    
  def resurrected(self):
    Bonus.resurrected(self)
    self.set_scale_factors(0.5, 0.5, 0.5)
    self.lid.set_identity(); self.lid.set_xyz(0.0, 2.0, 1.0)
    
  def advance_time(self, proportion):
    Bonus.advance_time(self, proportion)
    
    if self.visible and self.animation: self.lid.rotate_vertical(3.0 * proportion)
cerealizer.register(SmallChest)
      

class BigChest(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.shape = soya.Shape.get("coffre1@typ2")
    self.lid   = soya.Volume(self, soya.Shape.get("coffre_couvercle1@typ2"))
    self.lid.set_xyz(0.0, 2.0, 1.0)
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 6.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    
    if not self.doer.remote: Player.get(character.player_name).add_bonus(score = 5)
    
  def resurrected(self):
    Bonus.resurrected(self)
    self.lid.set_identity(); self.lid.set_xyz(0.0, 2.0, 1.0)
    
  def advance_time(self, proportion):
    Bonus.advance_time(self, proportion)
    
    if self.visible and self.animation: self.lid.rotate_vertical(3.0 * proportion)
cerealizer.register(BigChest)
      
      
class Key(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.shape = soya.Shape.get("clef")
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 6.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    
    if not self.doer.remote: Player.get(character.player_name).add_bonus(keys = 1)
cerealizer.register(Key)
    
