File: bonus.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 (161 lines) | stat: -rw-r--r-- 5,086 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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 cerealizer, soya, soya.tofu as tofu

import balazar_brothers, balazar_brothers.globdef as globdef
from balazar_brothers.character import Balazar
from balazar_brothers.player import Player


class Bonus(tofu.Mobile):
  TAKEN_SOUND = "ouais.wav"
  
  def __init__(self):
    tofu.Mobile.__init__(self)
    
    self.radius    = 1.0
    self.animation = 0
    
  def suitable_for(self, character): return isinstance(character, Balazar)
  
  def get_network_state(self):
    if self.visible and not self.animation: return "1"
    else:                                   return "0"
    
  def read_network_state(self, f):
    if f.read(1) == "1": self.resurrected()
    else:                self.taken()

  def do_collisions(self):
    tofu.Mobile.do_collisions(self)
    
    if self.visible and not self.animation:
      for character in self.level.mobiles:
        if self.suitable_for(character) and self.distance_to(character.center) < self.radius:
          self.taken_by(character)
          self.set_current_state_importance(2)
          break
        
  def set_state(self, state):
    if   isinstance(state, BonusTakenState):
      self.taken_by(state.character)
      
    elif isinstance(state, BonusResurrectedState):
      self.resurrected()
      
  def taken_by(self, character): self.taken()
  
  def taken(self):
    self.animation = 1
    soya.SoundPlayer(self, soya.Sound.get(self.TAKEN_SOUND), gain = 10.0)
    
  def resurrected(self):
    self.set_scale_factors(1.0, 1.0, 1.0)
    self.visible   = 1
    self.animation = 0
    
  def advance_time(self, proportion):
    if self.visible:
      if self.animation:
        self.rotate_lateral((0.2 * self.animation + 4.0) * proportion)
        
        self.animation += 1
        
        if   self.animation <  50.0: self.y += 0.15 * proportion
        elif self.animation <  80.0: pass
        elif self.animation < 100.0: self.scale(0.9, 0.9, 0.9)
        else:
          self.visible   = 0
          self.animation = 0
          
      else:
        self.rotate_lateral(4.0 * proportion)


class SmallChest(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.model = soya.Model.get("coffre1@typ1")
    self.lid   = soya.Body(self, soya.Model.get("coffre_couvercle1@typ1"))
    self.lid.set_xyz(0.0, 2.0, 1.0)
    self.scale(0.5, 0.5, 0.5)
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 2.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    Player.get(character.player_name).add_bonus(score = 1)
    
  def resurrected(self):
    Bonus.resurrected(self)
    self.set_scale_factors(0.5, 0.5, 0.5)
    self.lid.set_identity(); self.lid.set_xyz(0.0, 2.0, 1.0)
    
  def advance_time(self, proportion):
    Bonus.advance_time(self, proportion)
    if self.visible and self.animation: self.lid.rotate_vertical(3.0 * proportion)
cerealizer.register(SmallChest)
      

class BigChest(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.model = soya.Model.get("coffre1@typ2")
    self.lid   = soya.Body(self, soya.Model.get("coffre_couvercle1@typ2"))
    self.lid.set_xyz(0.0, 2.0, 1.0)
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 6.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    Player.get(character.player_name).add_bonus(score = 5)
    
  def resurrected(self):
    Bonus.resurrected(self)
    self.lid.set_identity(); self.lid.set_xyz(0.0, 2.0, 1.0)
    
  def advance_time(self, proportion):
    Bonus.advance_time(self, proportion)
    if self.visible and self.animation: self.lid.rotate_vertical(3.0 * proportion)
cerealizer.register(BigChest)
      
      
class Key(Bonus):
  def __init__(self):
    Bonus.__init__(self)
    
    self.model = soya.Model.get("clef")
    
    self.sprite = soya.Sprite(self, soya.Material.get("x_lumiere_1"))
    self.sprite.y = 1.5
    self.sprite.width = self.sprite.height = 6.0
    self.sprite.lit = 0
    
  def taken_by(self, character):
    Bonus.taken_by(self, character)
    Player.get(character.player_name).add_bonus(keys = 1)
cerealizer.register(Key)