Add import to pyworlds to avoid errors.
[pyworlds.git] / tests / soya-traveling-camera-1.py
blob2c6347b580b585fd1b42ea4b8c027e297eff89b3
1 #!/usr/bin/python
2 # -*- indent-tabs-mode: t -*-
4 # Soya 3D tutorial
5 # Copyright (C) 2004 Jean-Baptiste LAMY
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # traveling-camera-1: Traveling camera : a keyboard-controlled caterpillar, followed by the camera!
24 # In this lesson, our caterpillar will obey you !
27 # Import the Soya module.
28 # The soya.sdlconst module contains all the SDL constants.
30 import sys, os, os.path, soya, soya.sdlconst
32 soya.init()
33 soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
35 # Creates a scene.
37 scene = soya.World()
40 # The CaterpillarHead class is very similar to the CaterpillarHead class of the basic-*
41 # lessons.
43 class CaterpillarHead(soya.Body):
44 def __init__(self, parent):
45 soya.Body.__init__(self, parent, soya.Model.get("caterpillar_head"))
46 self.speed = soya.Vector(self, 0.0, 0.0, 0.0)
47 self.rotation_y_speed = 0.0
49 # VERY IMPORTANT!
51 # The TravelingCamera use raypicking for determining through what the camera can go or not.
52 # We thus have to DISABLE raypicking on the caterpillar itself!
53 # This can be done by setting the caterpillar's solid attribute to 0.
55 #self.solid = 0
57 def begin_round(self):
58 soya.Body.begin_round(self)
60 for event in soya.process_event():
61 if event[0] == soya.sdlconst.KEYDOWN:
62 if event[1] == soya.sdlconst.K_UP: self.speed.z = -1.2
63 elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.1
64 elif event[1] == soya.sdlconst.K_LEFT: self.rotation_y_speed = 10.0
65 elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_y_speed = -10.0
66 elif event[1] == soya.sdlconst.K_q: soya.MAIN_LOOP.stop()
67 elif event[1] == soya.sdlconst.K_ESCAPE: soya.MAIN_LOOP.stop()
69 if event[0] == soya.sdlconst.KEYUP:
70 if event[1] == soya.sdlconst.K_UP: self.speed.z = 0.0
71 elif event[1] == soya.sdlconst.K_DOWN: self.speed.z = 0.0
72 elif event[1] == soya.sdlconst.K_LEFT: self.rotation_y_speed = 0.0
73 elif event[1] == soya.sdlconst.K_RIGHT: self.rotation_y_speed = 0.0
75 self.rotate_y(self.rotation_y_speed)
77 def advance_time(self, proportion):
78 soya.Body.advance_time(self, proportion)
79 self.add_mul_vector(proportion, self.speed)
82 class CaterpillarPiece(soya.Body):
83 def __init__(self, parent, previous):
84 soya.Body.__init__(self, parent, soya.Model.get("caterpillar"))
85 self.previous = previous
86 self.speed = soya.Vector(self, 0.0, 0.0, -0.2)
88 # VERY IMPORTANT!
90 # See above.
92 # self.solid = 0
94 def begin_round(self):
95 soya.Body.begin_round(self)
96 self.look_at(self.previous)
97 if self.distance_to(self.previous) < 1.5: self.speed.z = 0.0
98 else: self.speed.z = -1.2
100 def advance_time(self, proportion):
101 soya.Body.advance_time(self, proportion)
102 self.add_mul_vector(proportion, self.speed)
105 caterpillar_head = CaterpillarHead(scene)
106 caterpillar_head.rotate_y(90.0)
108 previous_caterpillar_piece = caterpillar_head
109 for i in range(10):
110 previous_caterpillar_piece = CaterpillarPiece(scene, previous_caterpillar_piece)
111 previous_caterpillar_piece.x = i + 1
113 # Creates a light.
115 light = soya.Light(scene)
116 light.set_xyz(2.0, 5.0, 0.0)
118 # The camera should created here!
119 camera = soya.TravelingCamera(scene)
121 traveling = soya.ThirdPersonTraveling(caterpillar_head)
122 traveling.distance = 35.0
124 camera.add_traveling(traveling)
125 camera.speed = 0.1
126 camera.set_xyz(0.0, 35.0, 35.0)
127 camera.look_at(caterpillar_head)
128 soya.set_root_widget(camera)
130 soya.MainLoop(scene).main_loop()