File: controller.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 (142 lines) | stat: -rw-r--r-- 6,402 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
# Balazar Brothers
# Copyright (C) 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 os, os.path, random
import cerealizer
import soya, soya.tofu as tofu, soya.sdlconst as sdlconst

import balazar_brothers, balazar_brothers.globdef as globdef

class EventListener(soya.CoordSyst):
  def __init__(self, parent = None):
    soya.CoordSyst.__init__(self, parent)
    
    self.jump1_keys       = set([sdlconst.K_LSHIFT, sdlconst.K_LCTRL, sdlconst.K_SPACE, "0", "6"]) # Strings are for joystick buttons.
    self.jump2_keys       = set([sdlconst.K_RSHIFT, sdlconst.K_RCTRL, sdlconst.K_SPACE, "1", "7"])
    self.chat_keys        = set([sdlconst.K_KP_ENTER, sdlconst.K_RETURN])
    self.quit_keys        = set([sdlconst.K_q, sdlconst.K_ESCAPE, "2"])
    self.screenshot_keys  = set([sdlconst.K_c])
    
    self.camera_distance_speed  = 0.0
    self.camera_angle_speed     = 0.0
    self.nb_camera_keys_pressed = 0.0
    self.reset()
    
  def reset(self):
    self.jump1_key_down = 0
    self.jump2_key_down = 0
    
  def begin_round(self):
    events = soya.process_event()
    
    for event in events:
      if   event[0] == sdlconst.JOYAXISMOTION:
        
        if   event[1] == 0:
          if   event[2] < 0: event_type = sdlconst.KEYDOWN; event_key = sdlconst.K_LEFT
          elif event[2] > 0: event_type = sdlconst.KEYDOWN; event_key = sdlconst.K_RIGHT
          else:              event_type = sdlconst.KEYUP  ; event_key = sdlconst.K_LEFT # XXX or right
        elif event[1] == 1:
          if   event[2] < 0: event_type = sdlconst.KEYDOWN; event_key = sdlconst.K_UP
          elif event[2] > 0: event_type = sdlconst.KEYDOWN; event_key = sdlconst.K_DOWN
          else:              event_type = sdlconst.KEYUP  ; event_key = sdlconst.K_UP # XXX or down
          
      elif event[0] == sdlconst.JOYBUTTONDOWN:
        event_type = sdlconst.KEYDOWN
        event_key  = str(event[1])
        event_mod  = 0
        
      elif event[0] == sdlconst.JOYBUTTONUP:
        event_type = sdlconst.KEYUP
        event_key  = str(event[1])
        event_mod  = 0
        
      else:
        event_type = event[0]
        if len(event) > 1:
          event_key  = event[1]
          if len(event) > 3: event_mod = event[2]
          else:              event_mod = 0
          
      if event_type == sdlconst.KEYDOWN:
        if soya.MAIN_LOOP.entry:
          soya.MAIN_LOOP.entry.on_key_pressed(event_key, event_mod)
        else:
          if   event_key in self.jump1_keys:
            if soya.MAIN_LOOP.traveling.characters[ 0].current_discussion:
              soya.MAIN_LOOP.traveling.characters[ 0].events.append("S")
            else:
              soya.MAIN_LOOP.traveling.characters[ 0].events.append("J")
          if   event_key in self.jump2_keys:
            if soya.MAIN_LOOP.traveling.characters[-1].current_discussion:
              soya.MAIN_LOOP.traveling.characters[-1].events.append("S")
            else:
              soya.MAIN_LOOP.traveling.characters[-1].events.append("J")
              
          elif event_key in self.chat_keys:
            from balazar_brothers.discussion import ChatEntry
            soya.MAIN_LOOP.entry = ChatEntry(soya.MAIN_LOOP.root_widget, soya.MAIN_LOOP.traveling.characters[-1] or soya.MAIN_LOOP.traveling.characters[0])
            
          elif event_key in self.quit_keys:
            #soya.MAIN_LOOP.traveling.characters[0].doer.do_action(QuitMenuAction())
            soya.MAIN_LOOP.stop()
            
          elif event_key in self.screenshot_keys:
            import tempfile
            screenshot_file = tempfile.mktemp(".png")
            soya.screenshot(screenshot_file)
            print "* Balazar Brothers * Screenshot saved as %s" % screenshot_file
            
          elif event_key == sdlconst.K_LEFT:  self.camera_angle_speed    = -0.04; self.nb_camera_keys_pressed += 1.0
          elif event_key == sdlconst.K_RIGHT: self.camera_angle_speed    =  0.04; self.nb_camera_keys_pressed += 1.0
          elif event_key == sdlconst.K_UP:    self.camera_distance_speed = -1.20; self.nb_camera_keys_pressed += 1.0
          elif event_key == sdlconst.K_DOWN:  self.camera_distance_speed =  1.20; self.nb_camera_keys_pressed += 1.0
          
          elif event_key == sdlconst.K_d:
            print soya.MAIN_LOOP.traveling.characters
            print [i.uid for i in soya.MAIN_LOOP.traveling.characters]
            
            import time
            time.sleep(1.0)
          
      elif event_type == sdlconst.KEYUP:
        if not soya.MAIN_LOOP.entry:
          if   event_key in self.jump1_keys: soya.MAIN_LOOP.traveling.characters[ 0].events.append("j")
          if   event_key in self.jump2_keys: soya.MAIN_LOOP.traveling.characters[-1].events.append("j")

          elif event_key == sdlconst.K_LEFT:  self.camera_angle_speed    = 0.0
          elif event_key == sdlconst.K_RIGHT: self.camera_angle_speed    = 0.0
          elif event_key == sdlconst.K_UP:    self.camera_distance_speed = 0.0
          elif event_key == sdlconst.K_DOWN:  self.camera_distance_speed = 0.0
        
    # Camera control
    if self.nb_camera_keys_pressed > 2.0:
      # Reset camera
      self.nb_camera_keys_pressed = -4.0
      soya.MAIN_LOOP.traveling.custom_distance = 0.0
      soya.MAIN_LOOP.traveling.custom_angle    = 0.0
      self.camera_distance_speed = self.camera_angle_speed = 0.0
      
    elif self.nb_camera_keys_pressed < 0.0:
      self.nb_camera_keys_pressed += 0.1
      
    else:
      if self.nb_camera_keys_pressed > 0.1: self.nb_camera_keys_pressed -= 0.1
      soya.MAIN_LOOP.traveling.custom_distance += self.camera_distance_speed
      soya.MAIN_LOOP.traveling.custom_angle    += self.camera_angle_speed