# 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 random

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

import balazar_brothers, balazar_brothers.globdef as globdef




class Fruit(tofu4soya.Mobile):
  animating = 0
  def __init__(self):
    tofu4soya.Mobile.__init__(self)
    
    self.bot    = 1
    self.volume = soya.Cal3dVolume(self, soya.Cal3dShape.get("fruit"))
    self.volume.animate_blend_cycle("balance")
    self.kill_point = soya.World(self)
    self.volume.attach_to_bone(self.kill_point, "fruit2")

    self.animation = "balance"
    self.volume.animate_blend_cycle("balance")
    self.volume.begin_round()
    self.volume.advance_time(0.1)
    
  def __getstate__(self):
    state = super(Fruit, self).__getstate__()
    if self.animating:
      del state[1]["animating"]
    return state
  
  def __setstate__(self, state):
    super(Fruit, self).__setstate__(state)
    if isinstance(self.kill_point, soya.World):
      self.kill_point_world = self.kill_point
      self.kill_point = soya.Point(self.kill_point_world, 0.0, 1.0, 0.0)
      
  def do_action(self, action):
    if not self.animating:
      self.animating = 1
      self.volume.animate_blend_cycle(self.animation or "balance")
      
    for character in self.level.mobiles:
      if isinstance(character, balazar_brothers.character.Character):
        if self.kill_point.distance_to(character.center) < 1.5:
          if (character.dying == 0) or (character.dying > 10):
            character.doer.action_done(balazar_brothers.character.SoundState(character, "coup.wav", 12.0))
          if self.animation == "balance2":
            if character.platform and isinstance(character.platform, balazar_brothers.character.Character): continue
            character.die(soya.Vector(self, 1.1, 4.0, -0.0))
          else:
            character.die(soya.Vector(self, 0.0, 1.0, -2.0))
            
cerealizer.register(Fruit)


class RotatingFruit(Fruit):
  def advance_time(self, proportion):
    Fruit.advance_time(self, proportion)
    self.rotate_lateral(5.0 * proportion)
cerealizer.register(RotatingFruit)
  
  




