1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
# -*- indent-tabs-mode: t -*-
#! /usr/bin/python -O
# Game Skeleton
# Copyright (C) 2003-2004 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
# Soya gaming tutorial, lesson 5
# Adding jumping
# New stuff is in the Controler and Character class
# A bunch of import
import sys, os, os.path, math
import soya
import soya.widget as widget
import soya.sdlconst as sdlconst
import soya.pudding as pudding
import soya.pudding.ext.fpslabel
import soya.pudding.ext.meter
# Inits Soya
soya.init()
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
pudding.init()
class Level(soya.World):
"""A game level.
Level is a subclass of soya.World."""
class Action:
"""An action that the character can do."""
def __init__(self, action):
self.action = action
# The available actions
ACTION_WAIT = 0
ACTION_ADVANCE = 1
ACTION_ADVANCE_LEFT = 2
ACTION_ADVANCE_RIGHT = 3
ACTION_TURN_LEFT = 4
ACTION_TURN_RIGHT = 5
ACTION_GO_BACK = 6
ACTION_GO_BACK_LEFT = 7
ACTION_GO_BACK_RIGHT = 8
ACTION_JUMP = 9
class KeyboardControler:
"""A controler is an object that gives orders to a character.
Here, we define a keyboard based controler, but there may be mouse-based or IA-based
controlers.
Notice that the unique method is called "next", which allows to use Python generator
as controller."""
def __init__(self):
self.left_key_down = self.right_key_down = self.up_key_down = self.down_key_down = 0
def next(self):
"""Returns the next action"""
jump = 0
# get the events that have not been captured by pudding
for event in soya.IDLER.events:
if event[0] == sdlconst.KEYDOWN:
if (event[1] == sdlconst.K_q) or (event[1] == sdlconst.K_ESCAPE):
sys.exit() # Quit the game
elif event[1] == sdlconst.K_LSHIFT:
# Shift key is for jumping
# Contrary to other action, jump is only performed once, at the beginning of
# the jump.
jump = 1
elif event[1] == sdlconst.K_LEFT: self.left_key_down = 1
elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 1
elif event[1] == sdlconst.K_UP: self.up_key_down = 1
elif event[1] == sdlconst.K_DOWN: self.down_key_down = 1
elif event[0] == sdlconst.KEYUP:
if event[1] == sdlconst.K_LEFT: self.left_key_down = 0
elif event[1] == sdlconst.K_RIGHT: self.right_key_down = 0
elif event[1] == sdlconst.K_UP: self.up_key_down = 0
elif event[1] == sdlconst.K_DOWN: self.down_key_down = 0
if jump: return Action(ACTION_JUMP)
# People saying that Python doesn't have switch/select case are wrong...
# Remember this if you are coding a fighting game !
return Action({
(0, 0, 1, 0) : ACTION_ADVANCE,
(1, 0, 1, 0) : ACTION_ADVANCE_LEFT,
(0, 1, 1, 0) : ACTION_ADVANCE_RIGHT,
(1, 0, 0, 0) : ACTION_TURN_LEFT,
(0, 1, 0, 0) : ACTION_TURN_RIGHT,
(0, 0, 0, 1) : ACTION_GO_BACK,
(1, 0, 0, 1) : ACTION_GO_BACK_LEFT,
(0, 1, 0, 1) : ACTION_GO_BACK_RIGHT,
}.get((self.left_key_down, self.right_key_down, self.up_key_down, self.down_key_down), ACTION_WAIT))
class Character(soya.World):
"""A character in the game."""
def __init__(self, parent, controler):
soya.World.__init__(self, parent)
# Loads a Cal3D shape (=model)
balazar = soya.Cal3dShape.get("balazar")
# Creates a Cal3D volume displaying the "balazar" shape
# (NB Balazar is the name of a wizard).
self.perso = soya.Cal3dVolume(self, balazar)
# Starts playing the idling animation in loop
self.perso.animate_blend_cycle("attente")
# The current animation
self.current_animation = "attente"
# Disable raypicking on the character itself !!!
self.solid = 0
self.controler = controler
self.speed = soya.Vector(self)
self.rotation_speed = 0.0
# We need radius * sqrt(2)/2 < max speed (here, 0.35)
self.radius = 0.5
self.radius_y = 1.0
self.center = soya.Point(self, 0.0, self.radius_y, 0.0)
self.left = soya.Vector(self, -1.0, 0.0, 0.0)
self.right = soya.Vector(self, 1.0, 0.0, 0.0)
self.down = soya.Vector(self, 0.0, -1.0, 0.0)
self.up = soya.Vector(self, 0.0, 1.0, 0.0)
self.front = soya.Vector(self, 0.0, 0.0, -1.0)
self.back = soya.Vector(self, 0.0, 0.0, 1.0)
# True is the character is jumping, i.e. speed.y > 0.0
self.jumping = 0
def play_animation(self, animation):
if self.current_animation != animation:
# Stops previous animation
self.perso.animate_clear_cycle(self.current_animation, 0.2)
# Starts the new one
self.perso.animate_blend_cycle(animation, 1.0, 0.2)
self.current_animation = animation
def begin_round(self):
self.begin_action(self.controler.next())
soya.World.begin_round(self)
def begin_action(self, action):
# Reset
self.speed.x = self.speed.z = self.rotation_speed = 0.0
# If the haracter is jumping, we don't want to reset speed.y to 0.0 !!!
if (not self.jumping) and self.speed.y > 0.0: self.speed.y = 0.0
animation = "attente"
# Determine the character rotation
if action.action in (ACTION_TURN_LEFT, ACTION_ADVANCE_LEFT, ACTION_GO_BACK_LEFT):
self.rotation_speed = 4.0
animation = "tourneG"
elif action.action in (ACTION_TURN_RIGHT, ACTION_ADVANCE_RIGHT, ACTION_GO_BACK_RIGHT):
self.rotation_speed = -4.0
animation = "tourneD"
# Determine the character speed
if action.action in (ACTION_ADVANCE, ACTION_ADVANCE_LEFT, ACTION_ADVANCE_RIGHT):
self.speed.z = -0.25
animation = "marche"
elif action.action in (ACTION_GO_BACK, ACTION_GO_BACK_LEFT, ACTION_GO_BACK_RIGHT):
self.speed.z = 0.06
animation = "recule"
new_center = self.center + self.speed
context = scene.RaypickContext(new_center, max(self.radius, 0.1 + self.radius_y))
# Gets the ground, and check if the character is falling
r = context.raypick(new_center, self.down, 0.1 + self.radius_y, 3)
if r and not self.jumping:
# Puts the character on the ground
# If the character is jumping, we do not put him on the ground !
ground, ground_normal = r
ground.convert_to(self)
self.speed.y = ground.y
# Jumping is only possible if we are on ground
if action.action == ACTION_JUMP:
self.jumping = 1
self.speed.y = 0.5
else:
# No ground => start falling
# Test the fall with the pit behind the second house
self.speed.y = max(self.speed.y - 0.02, -0.25)
animation = "chute"
# If the vertical speed is negative, the jump is over
if self.speed.y < 0.0: self.jumping = 0
new_center = self.center + self.speed
# The movement (defined by the speed vector) may be impossible if the character
# would encounter a wall.
for vec in (self.left, self.right, self.front, self.back, self.up):
r = context.raypick(new_center, vec, self.radius, 3)
if r:
# The ray encounters a wall => the character cannot perform the planned movement.
# We compute a correction vector, and add it to the speed vector, as well as to
# new_center (for the following raypicks ; remember that
# new_center = self.center + self.speed, so if speed has changed, we must update
# it).
collision, wall_normal = r
hypo = vec.length() * self.radius - (new_center >> collision).length()
correction = wall_normal * hypo
# Theorical formula, but more complex and identical result
#angle = (180.0 - vec.angle_to(wall_normal)) / 180.0 * math.pi
#correction = wall_normal * hypo * math.cos(angle)
self.speed.add_vector(correction)
new_center.add_vector(correction)
self.play_animation(animation)
def advance_time(self, proportion):
soya.World.advance_time(self, proportion)
self.add_mul_vector(proportion, self.speed)
self.rotate_y(proportion * self.rotation_speed)
# Create the scene (a world with no parent)
scene = soya.World()
# Loads the level, and put it in the scene
level = soya.World.get("level_demo")
scene.add(level)
# Creates a character in the level, with a keyboard controler
character = Character(level, KeyboardControler())
character.set_xyz(216.160568237, -7.93332195282, 213.817764282)
# Creates a Tomb Raider-like camera in the scene
camera = soya.TravelingCamera(scene)
traveling = soya.ThirdPersonTraveling(character)
traveling.distance = 5.0
camera.add_traveling(traveling)
camera.zap()
camera.back = 70.0
# Creates a widget group, containing the camera and a label showing the FPS.
soya.set_root_widget(pudding.core.RootWidget())
soya.root_widget.add_child(camera)
pudding.ext.fpslabel.FPSLabel(soya.root_widget, position = pudding.TOP_RIGHT)
health_bar = pudding.ext.meter.MeterValueLabel(soya.root_widget, "health:",
left = 10, top = 10, width = 200,
height = 20)
health_bar.anchors = pudding.ANCHOR_TOP_LEFT
health_bar.meter.user_change = False
health_bar.meter.border_color = (0, 0, 0, 1)
health_bar.label.color = health_bar.meter.border_color
logo = pudding.control.Logo(soya.root_widget, 'little-dunk.png')
button = pudding.control.Button(soya.root_widget, 'Quit', left = 10, width = 50, height = 40)
button.set_pos_bottom_right(bottom = 10)
button.anchors = pudding.ANCHOR_BOTTOM | pudding.ANCHOR_LEFT
button.on_click = sys.exit
# Creates and run an "idler" (=an object that manage time and regulate FPS)
# By default, FPS is locked at 40.
pudding.idler.Idler(scene).idle()
|