File: camera.py

package info (click to toggle)
balazarbrothers 1.0~rc1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 23,528 kB
  • ctags: 451
  • sloc: python: 3,196; xml: 165; makefile: 20
file content (139 lines) | stat: -rw-r--r-- 4,331 bytes parent folder | download | duplicates (3)
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
# Balazar Brothers
# Copyright (C) 2006-2007 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 math
import soya, soya.tofu as tofu

from balazar_brothers.platform import BumpablePlatform


class Traveling(soya.Traveling):
  def __init__(self, characters = None):
    soya.Traveling.__init__(self)
    
    self.characters = characters or []
    
    self.incline_as      = None
    self.smooth_move     = 1
    self.smooth_rotation = 0
    
    self.position  = soya.Point()
    self.direction = soya.Point()
    
    self.step  = soya.Vector()
    self.v     = soya.Vector()
    self.p     = soya.Point()
    
    self.custom_distance = 0.0
    self.custom_angle    = 0.0
    
    self.last_step = None
    
  def add_character(self, character, index = -1): self.characters.insert(index, character)
  
  def best_position(self, camera):
    v    = self.v
    p    = self.p
    step = self.step
    
    for character in self.characters:
      if character.level:
        level = character.level
        break
    else: return self.position # All characters are dead      
    
    self.direction.parent = camera.parent
    self.direction.set_xyz(0.0, 0.0, 0.0)
    nb = 0
    for character in self.characters:
      if not character.platform:
        if not character.next_platform:
          nb += 1
          #p.clone(character.jump_start_position)
          p.clone(character)
          p.convert_to(self.direction.parent)
          self.direction.add_xyz(p.x, p.y, p.z)
        else:
          nb += 1
          v.set_start_end(character.jump_start_position, character.next_platform.platform_position)
          p.clone(character.jump_start_position)
          p.add_mul_vector(character.jump_proportion, v)
          p.convert_to(self.direction.parent)
          self.direction.add_xyz(p.x, p.y, p.z)
      elif not character.platform in self.characters:
        nb += 1
        p.clone(character)
        p.convert_to(self.direction.parent)
        self.direction.add_xyz(p.x, p.y, p.z)
        if isinstance(character.platform, BumpablePlatform): self.direction.y -= character.platform.bumped_y

    if nb == 0: return self.position # All characters are dead
    
    self.direction.x /= nb
    self.direction.y /= nb
    self.direction.z /= nb
    self.direction.y += 2.0
    
    self.position.clone(self.direction)
    
    v.set_start_end(self.characters[-1], self.characters[0])
    v.convert_to(camera.parent)
    if v.length() < abs(v.y) + 0.01:
      #step.set_start_end(level.center, self.characters[0])
      if not self.last_step:
        self.last_step = level.center >> camera
        self.last_step.convert_to(camera.parent)
        #step.set_start_end(level.center, camera)
      step.clone(self.last_step)
      step.convert_to(camera.parent)
      
    else:
      self.last_step = None
      soya.Vector(None, 0.0, 1.0, 0.0).cross_product(v, step)
      step.convert_to(camera.parent)
      
    step.set_length(2.0)
    step.y = 0.4
    
    if self.custom_angle:
      x = step.x
      z = step.z
      cos = math.cos(self.custom_angle)
      sin = math.sin(self.custom_angle)
      step.x = cos * x + sin * z
      step.z = -sin * x + cos * z
    
    min_dist = 15.0
    
    for i in range(50):
      for character in self.characters:
        v.set_start_end(character, self.position)
        if v.length() < min_dist: break
        if v.angle_to(step) > 25.0: break
      else: break
      self.position.add_vector(step)

    if self.custom_distance:
      self.position.add_mul_vector(self.custom_distance, step)
      
    return self.position
  
  def best_direction(self, camera):
    return self.direction