Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / game_skel-4.pyOpen in CoCalc
Author: William A. Stein
1
#! /usr/bin/python -O
2
3
# Game Skeleton
4
# Copyright (C) 2003-2004 Jean-Baptiste LAMY
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20
# Soya gaming tutorial, lesson 4
21
# Replacing the cube by a 3D animated character
22
23
# New stuff is in the Character class
24
25
26
# A bunch of import
27
import sys, os, os.path, math
28
import soya
29
import soya.widget as widget
30
import soya.sdlconst as sdlconst
31
32
# Inits Soya
33
soya.init()
34
35
# Define data path (=where to find models, textures, ...)
36
HERE = os.path.dirname(sys.argv[0])
37
soya.path.append(os.path.join(HERE, "data"))
38
39
40
class Level(soya.World):
41
"""A game level.
42
Level is a subclass of soya.World."""
43
44
45
class Action:
46
"""An action that the character can do."""
47
def __init__(self, action):
48
self.action = action
49
50
# The available actions
51
ACTION_WAIT = 0
52
ACTION_ADVANCE = 1
53
ACTION_ADVANCE_LEFT = 2
54
ACTION_ADVANCE_RIGHT = 3
55
ACTION_TURN_LEFT = 4
56
ACTION_TURN_RIGHT = 5
57
ACTION_GO_BACK = 6
58
ACTION_GO_BACK_LEFT = 7
59
ACTION_GO_BACK_RIGHT = 8
60
61
62
class KeyboardController:
63
"""A controller is an object that gives orders to a character.
64
Here, we define a keyboard based controller, but there may be mouse-based or IA-based
65
controllers.
66
Notice that the unique method is called "next", which allows to use Python generator
67
as controller."""
68
def __init__(self):
69
self.left_key_down = self.right_key_down = self.up_key_down = self.down_key_down = 0
70
71
def next(self):
72
"""Returns the next action"""
73
for event in soya.process_event():
74
if event[0] == sdlconst.KEYDOWN:
75
if (event[1] == sdlconst.K_q) or (event[1] == sdlconst.K_ESCAPE):
76
sys.exit() # Quit the game
77
elif event[1] == sdlconst.K_LEFT: self.left_key_down = 1
78
elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 1
79
elif event[1] == sdlconst.K_UP: self.up_key_down = 1
80
elif event[1] == sdlconst.K_DOWN: self.down_key_down = 1
81
82
elif event[0] == sdlconst.KEYUP:
83
if event[1] == sdlconst.K_LEFT: self.left_key_down = 0
84
elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 0
85
elif event[1] == sdlconst.K_UP: self.up_key_down = 0
86
elif event[1] == sdlconst.K_DOWN: self.down_key_down = 0
87
88
# People saying that Python doesn't have switch/select case are wrong...
89
# Remember this if you are coding a fighting game !
90
return Action({
91
(0, 0, 1, 0) : ACTION_ADVANCE,
92
(1, 0, 1, 0) : ACTION_ADVANCE_LEFT,
93
(0, 1, 1, 0) : ACTION_ADVANCE_RIGHT,
94
(1, 0, 0, 0) : ACTION_TURN_LEFT,
95
(0, 1, 0, 0) : ACTION_TURN_RIGHT,
96
(0, 0, 0, 1) : ACTION_GO_BACK,
97
(1, 0, 0, 1) : ACTION_GO_BACK_LEFT,
98
(0, 1, 0, 1) : ACTION_GO_BACK_RIGHT,
99
}.get((self.left_key_down, self.right_key_down, self.up_key_down, self.down_key_down), ACTION_WAIT))
100
101
102
class Character(soya.World):
103
"""A character in the game."""
104
def __init__(self, parent, controller):
105
soya.World.__init__(self, parent)
106
107
# Loads a Cal3D shape (=model)
108
# (NB Balazar is the name of a wizard).
109
balazar = soya.Cal3dShape.get("balazar")
110
111
# Creates a Cal3D volume displaying the "balazar" shape
112
self.perso = soya.Cal3dVolume(self, balazar)
113
114
# Starts playing the idling animation in loop
115
# "attente" is the French for "wait"
116
self.perso.animate_blend_cycle("attente")
117
118
# The current animation's name
119
self.current_animation = "attente"
120
121
# Disable raypicking on the character itself !!!
122
self.solid = 0
123
124
self.controller = controller
125
self.speed = soya.Vector(self)
126
self.rotation_speed = 0.0
127
128
# We need radius * sqrt(2)/2 < max speed (here, 0.35)
129
self.radius = 0.5
130
self.radius_y = 1.0
131
self.center = soya.Point(self, 0.0, self.radius_y, 0.0)
132
133
self.left = soya.Vector(self, -1.0, 0.0, 0.0)
134
self.right = soya.Vector(self, 1.0, 0.0, 0.0)
135
self.down = soya.Vector(self, 0.0, -1.0, 0.0)
136
self.up = soya.Vector(self, 0.0, 1.0, 0.0)
137
self.front = soya.Vector(self, 0.0, 0.0, -1.0)
138
self.back = soya.Vector(self, 0.0, 0.0, 1.0)
139
140
def play_animation(self, animation):
141
"""This method make the character playing the animation called ANIMATION in loop."""
142
# If we are already playing the right animation, nothing to do.
143
if self.current_animation != animation:
144
145
# Stops previous animation
146
self.perso.animate_clear_cycle(self.current_animation, 0.2)
147
148
# Starts the new one
149
self.perso.animate_blend_cycle(animation, 1.0, 0.2)
150
151
self.current_animation = animation
152
153
def begin_round(self):
154
self.begin_action(self.controller.next())
155
soya.World.begin_round(self)
156
157
def begin_action(self, action):
158
# Reset
159
self.speed.x = self.speed.z = self.rotation_speed = 0.0
160
if self.speed.y > 0.0: self.speed.y = 0.0
161
162
# This local variable contains the name of the next animation
163
animation = "attente"
164
165
# Determine the character rotation
166
if action.action in (ACTION_TURN_LEFT, ACTION_ADVANCE_LEFT, ACTION_GO_BACK_LEFT):
167
self.rotation_speed = 4.0
168
169
# The next animation should be "tourneG" ("turn left" in english).
170
animation = "tourneG"
171
elif action.action in (ACTION_TURN_RIGHT, ACTION_ADVANCE_RIGHT, ACTION_GO_BACK_RIGHT):
172
self.rotation_speed = -4.0
173
174
# The next animation should be "tourneD" ("turn right" in english).
175
animation = "tourneD"
176
177
# Determine the character speed
178
if action.action in (ACTION_ADVANCE, ACTION_ADVANCE_LEFT, ACTION_ADVANCE_RIGHT):
179
self.speed.z = -0.25
180
181
# The next animation should be "marche" ("walk" in english).
182
# This will overwrite "turn left" or "right" animation if the character is
183
# turning and walking at the same time.
184
animation = "marche"
185
elif action.action in (ACTION_GO_BACK, ACTION_GO_BACK_LEFT, ACTION_GO_BACK_RIGHT):
186
self.speed.z = 0.06
187
animation = "recule"
188
189
new_center = self.center + self.speed
190
191
context = scene.RaypickContext(new_center, max(self.radius, 0.1 + self.radius_y))
192
193
# Gets the ground, and check if the character is falling
194
r = context.raypick(new_center, self.down, 0.1 + self.radius_y, 1, 1)
195
if r:
196
# Puts the character on the ground
197
ground, ground_normal = r
198
ground.convert_to(self)
199
self.speed.y = ground.y
200
201
else:
202
# No ground => start falling
203
# Test the fall with the pit behind the second house
204
self.speed.y = max(self.speed.y - 0.05, -0.25)
205
animation = "chute"
206
207
# The rotation is considered to be always possible.
208
# The movement (defined by the speed vector) may be impossible if the character
209
# would encounter a wall.
210
211
for vec in (self.left, self.right, self.front, self.back, self.up):
212
r = context.raypick(new_center, vec, self.radius, 1, 1)
213
if r:
214
# The ray encounters a wall => the character cannot perform the planned movement.
215
# We compute a correction vector, and add it to the speed vector, as well as to
216
# new_center (for the following raypicks ; remember that
217
# new_center = self.center + self.speed, so if speed has changed, we must update
218
# it).
219
220
collision, wall_normal = r
221
hypo = vec.length() * self.radius - (new_center >> collision).length()
222
correction = wall_normal * hypo
223
224
# Theorical formula, but more complex and identical result
225
#angle = (180.0 - vec.angle_to(wall_normal)) / 180.0 * math.pi
226
#correction = wall_normal * hypo * math.cos(angle)
227
228
self.speed.add_vector(correction)
229
new_center.add_vector(correction)
230
231
# Starts the new animation if needed
232
self.play_animation(animation)
233
234
def advance_time(self, proportion):
235
soya.World.advance_time(self, proportion)
236
237
self.add_mul_vector(proportion, self.speed)
238
self.rotate_lateral(proportion * self.rotation_speed)
239
240
241
# Create the scene (a world with no parent)
242
scene = soya.World()
243
244
# Loads the level, and put it in the scene
245
level = soya.World.get("level_demo")
246
scene.add(level)
247
248
# Creates a character in the level, with a keyboard controller
249
character = Character(level, KeyboardController())
250
character.set_xyz(216.160568237, -7.93332195282, 213.817764282)
251
252
# Creates a Tomb Raider-like camera in the scene
253
camera = soya.TravelingCamera(scene)
254
traveling = soya.ThirdPersonTraveling(character)
255
traveling.distance = 5.0
256
camera.add_traveling(traveling)
257
camera.zap()
258
camera.back = 70.0
259
260
# Creates a widget group, containing the camera and a label showing the FPS.
261
soya.set_root_widget(widget.Group())
262
soya.root_widget.add(camera)
263
soya.root_widget.add(widget.FPSLabel())
264
265
# Creates and run an "idler" (=an object that manage time and regulate FPS)
266
# By default, FPS is locked at 40.
267
soya.Idler(scene).idle()
268