comparison demos/shooter/scripts/world.py @ 446:2046a1f2f5f2

Adding the shooter demo. This is still a work in progress.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 31 Mar 2010 15:40:00 +0000
parents
children 5e2ec84902a7
comparison
equal deleted inserted replaced
445:f855809822cf 446:2046a1f2f5f2
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from fife import fife
25 import math, random
26 from fife.extensions import pychan
27 from fife.extensions.pychan import widgets
28
29 from scripts.common.eventlistenerbase import EventListenerBase
30 from scripts.common.helpers import normalize
31 from fife.extensions.loaders import loadMapFile
32
33 from scripts.ships.shipbase import Ship
34 from scripts.ships.player import Player
35 from scripts.scene import Scene
36 from scripts.weapons import Weapon
37
38 class World(EventListenerBase):
39 """
40 The world!
41
42 This class handles:
43 setup of map view (cameras ...)
44 loading the map
45 GUI for right clicks
46 handles mouse/key events which aren't handled by the GUI.
47 ( by inheriting from EventlistenerBase )
48
49 That's obviously too much, and should get factored out.
50 """
51 def __init__(self, engine):
52 super(World, self).__init__(engine, regMouse=True, regKeys=True)
53 self.engine = engine
54 self.timemanager = engine.getTimeManager()
55 self.eventmanager = engine.getEventManager()
56 self.model = engine.getModel()
57 self.filename = ''
58 self.keystate = { 'UP': False, 'DOWN': False, 'LEFT': False, 'RIGHT': False, 'CTRL': False, 'SPACE': False, }
59 self.dynamic_widgets = {}
60 self.pump_ctr = 0
61 self.map = None
62 self.scene = None
63
64 def reset(self):
65 """
66 Clear the agent information and reset the moving secondary camera state.
67 """
68 self.cameras = {}
69
70 def load(self, filename):
71 """
72 Load a xml map and setup agents and cameras.
73 """
74 self.filename = filename
75 self.reset()
76 self.map = loadMapFile(filename, self.engine)
77
78 self.scene = Scene(self.engine, self.map.getLayer('objects'))
79 #self.initPlayer()
80 self.initCameras()
81
82 self.mainwindow = pychan.loadXML('gui/mainwindow.xml')
83 self.fpstext = self.mainwindow.findChild(name="fps")
84 self.velocitytext = self.mainwindow.findChild(name="velocity")
85 self.positiontext = self.mainwindow.findChild(name="position")
86 self.mainwindow.position = (0,0)
87 self.mainwindow.show()
88
89 #give player the default weapon
90 self.scene.player.weapon = Weapon(self.model, self.map.getLayer('objects'), self.scene.player, 100)
91
92 def initCameras(self):
93 """
94 Before we can actually see something on screen we have to specify the render setup.
95 This is done through Camera objects which offer a viewport on the map.
96 """
97
98 for cam in self.map.getCameras():
99 if cam.getId() == 'main':
100 self.cameras['main'] = cam
101
102 self.scene.attachCamera(self.cameras['main'])
103
104
105 def keyPressed(self, evt):
106 keyval = evt.getKey().getValue()
107 keystr = evt.getKey().getAsString().lower()
108 if keyval == fife.Key.UP:
109 self.keystate['UP'] = True
110 elif keyval == fife.Key.DOWN:
111 self.keystate['DOWN'] = True
112 elif keyval == fife.Key.LEFT:
113 self.keystate['LEFT'] = True
114 elif keyval == fife.Key.RIGHT:
115 self.keystate['RIGHT'] = True
116 elif keyval == fife.Key.SPACE:
117 self.keystate['SPACE'] = True
118 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
119 self.keystate['CTRL'] = True
120
121 def keyReleased(self, evt):
122 keyval = evt.getKey().getValue()
123 if keyval == fife.Key.UP:
124 self.keystate['UP'] = False
125 elif keyval == fife.Key.DOWN:
126 self.keystate['DOWN'] = False
127 elif keyval == fife.Key.LEFT:
128 self.keystate['LEFT'] = False
129 elif keyval == fife.Key.RIGHT:
130 self.keystate['RIGHT'] = False
131 elif keyval == fife.Key.SPACE:
132 self.keystate['SPACE'] = False
133 elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
134 self.keystate['CTRL'] = False
135
136 def mouseWheelMovedUp(self, evt):
137 if self.keystate['CTRL']:
138 self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05)
139
140 def mouseWheelMovedDown(self, evt):
141 if self.keystate['CTRL']:
142 self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05)
143
144 def mousePressed(self, evt):
145 if evt.isConsumedByWidgets():
146 return
147
148 clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
149
150 def mouseMoved(self, evt):
151 pt = fife.ScreenPoint(evt.getX(), evt.getY())
152
153 def pump(self):
154 """
155 Called every frame.
156 """
157
158 #update the scene
159 self.scene.update(self.timemanager.getTime(), self.keystate)
160
161
162 #update the HUD
163 avgframe = self.timemanager.getAverageFrameTime()
164 if avgframe > 0:
165 fps = 1 / (avgframe / 1000)
166 else:
167 fps = 0
168 fpstxt = "%3.2f" % fps
169 self.fpstext.text = unicode(fpstxt)
170
171 player = self.scene.player
172 exactcoords = player.location.getExactLayerCoordinates()
173 pos = "%1.2f" % exactcoords.x + ", %1.2f" % exactcoords.y
174 self.positiontext.text = unicode(pos)
175
176 vel = "%1.2f" % player.velocity.x + ", %1.2f" % player.velocity.y
177 self.velocitytext.text = unicode(vel)
178
179
180 self.pump_ctr += 1