import sys, os, os.path
import soya, soya.sdlconst

soya.init()

soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
scene = soya.World()

calshape = soya.Cal3dShape.get("mindirt")
print "Available meshes    :", calshape.meshes    .keys()
print "Available animations:", calshape.animations.keys()

calvol = soya.Cal3dVolume(scene, calshape)
calvol.x = -1.0
calvol.rotate_lateral(180)
calvol.animate_blend_cycle("twist") #"rot")

class RotatingVolume(soya.Volume):
    def advance_time(self, proportion):
        soya.Volume.advance_time(self, proportion)
        self.rotate_lateral(proportion * 5.0)

basicshape = soya.Shape.get("sword")
basicvol = RotatingVolume(scene, basicshape)
basicvol.rotate_lateral(90.0)
basicvol.x = 2.0

basicshape = soya.Shape.get("mindirtcopy")
basicvol = RotatingVolume(scene, basicshape)
basicvol.rotate_lateral(90.0)
basicvol.x = 1.0

class MovableCamera(soya.Camera):
  def __init__(self, parent):
    soya.Camera.__init__(self, parent)
    
    self.speed = soya.Vector(self)
    self.rotation_lateral_speed  = 0.0
    self.rotation_vertical_speed = 0.0
    
  def begin_round(self):
    soya.Camera.begin_round(self)
    
    for event in soya.process_event():
      if event[0] == soya.sdlconst.KEYDOWN:
        if   event[1] == soya.sdlconst.K_UP:     self.speed.z = -1.0
        elif event[1] == soya.sdlconst.K_DOWN:   self.speed.z =  1.0
        elif event[1] == soya.sdlconst.K_LEFT:   self.rotation_lateral_speed =  10.0
        elif event[1] == soya.sdlconst.K_RIGHT:  self.rotation_lateral_speed = -10.0
        elif event[1] == soya.sdlconst.K_q:      soya.IDLER.stop()
        elif event[1] == soya.sdlconst.K_ESCAPE: soya.IDLER.stop()
      if event[0] == soya.sdlconst.KEYUP:
        if   event[1] == soya.sdlconst.K_UP:     self.speed.z = 0.0
        elif event[1] == soya.sdlconst.K_DOWN:   self.speed.z = 0.0
        elif event[1] == soya.sdlconst.K_LEFT:   self.rotation_lateral_speed = 0.0
        elif event[1] == soya.sdlconst.K_RIGHT:  self.rotation_lateral_speed = 0.0

  def advance_time(self, proportion):
    self.add_mul_vector(proportion, self.speed)
    self.turn_lateral (self.rotation_lateral_speed  * proportion)
    self.turn_vertical(self.rotation_vertical_speed * proportion)

camera = MovableCamera(scene)
camera.set_xyz(0.0, 0.0, 10.0)

light = soya.Light(scene)
light.set_xyz(5.0, 5.0, 2.0)
light.ambient = (1.0, 1.0, 1.0, 1.0)

scene.set_xyz(0.0, 0.0, 0.0)
scene.atmosphere = soya.SkyAtmosphere()
scene.atmosphere.bg_color = (0.0, 0.0, 1.0, 1.0)
scene.atmosphere.ambient  = (0.0, 0.5, 0.0, 1.0)
scene.atmosphere.fog_color = (0.0, 0.0, 1.0, 1.0)
scene.atmosphere.fog_start = 10.0
scene.atmosphere.fog_end = 50.0
scene.atmosphere.fog = 0
scene.atmosphere.fog_type = 0

soya.set_root_widget(camera)
soya.Idler(scene).idle()

