comparison demos/shooter/scripts/weapons.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 64676ea55472
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
26 class Projectile(object):
27 def __init__(self, model, projectileName, layer, timeToLive):
28 self._model = model
29 self._name = projectileName
30 self._layer = layer
31 self._instance = None
32 self._velocity = None
33 self._obj = self._model.getObject(self._name, "http://www.fifengine.de/xml/tutorial")
34 self._running = False
35 self._ttl = timeToLive
36 self._starttime = 0
37
38 def create(self, location):
39 self._instance = self._layer.createInstance(self._obj, location.getExactLayerCoordinates(), "bullet")
40 fife.InstanceVisual.create(self._instance)
41
42 def run(self, velocity, location, time):
43 if not self._running:
44 self._velocity = velocity
45
46 self.create(location)
47 self._running = True
48
49 self._starttime = time
50
51 def destroy(self):
52 if self._running and self._instance:
53 self._layer.deleteInstance(self._instance)
54 self._running = False
55
56 def _getLocation(self):
57 return self._instance.getLocation()
58
59 def _setLocation(self, loc):
60 self._instance.setLocation(loc)
61
62 def _isRunning(self):
63 return self._running
64
65 def _getTTL(self):
66 return self._ttl
67
68 def update(self, curtime):
69 if self._running and (curtime - self._starttime) < self._ttl:
70 projloc = self.location
71 exactloc = projloc.getExactLayerCoordinates()
72
73 exactloc.x += self._velocity.x
74 exactloc.y += self._velocity.y
75
76 projloc.setExactLayerCoordinates(exactloc)
77 self.location = projloc
78 else:
79 self.destroy()
80
81 location = property(_getLocation,_setLocation)
82 running = property(_isRunning)
83 ttl = property(_getTTL)
84
85 class Weapon(object):
86 def __init__(self, model, layer, ship, firerate):
87 self._model = model
88 self._layer = layer
89 self._ship = ship
90 self._firerate = firerate
91 self._lastfired = 0
92
93 def fire(self, curtime):
94 if (curtime - self._lastfired) > self._firerate:
95 pjctl = Projectile(self._model, "bullet1", self._layer, 2000 )
96 pjctl.run(fife.DoublePoint(0.075,0), self._ship.location, curtime)
97 self._lastfired = curtime
98 return pjctl
99
100 return None
101