comparison demos/shooter/scripts/ships/shipbase.py @ 447:64676ea55472

Added the ability to set the scale of the object layer. Tweaked the player controls a little bit. A little more work needs to be done to keep the player within the bounds of the camera.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 31 Mar 2010 21:13:07 +0000
parents 2046a1f2f5f2
children 5e2ec84902a7
comparison
equal deleted inserted replaced
446:2046a1f2f5f2 447:64676ea55472
19 # License along with this library; if not, write to the 19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc., 20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # #################################################################### 22 # ####################################################################
23 23
24 from math import sqrt
25
24 from fife import fife 26 from fife import fife
25 from scripts.common.helpers import normalize 27 from scripts.common.helpers import normalize
26 from scripts.weapons import Weapon 28 from scripts.weapons import Weapon
27 29
28 class Ship(object): 30 class Ship(object):
29 def __init__(self, scene, shipName, layer, uniqInMap=True): 31 def __init__(self, scene, shipName, layer, uniqInMap=True):
30 self._scene = scene 32 self._scene = scene
31 self._name = shipName 33 self._name = shipName
32 self._layer = layer 34 self._layer = layer
35 self._xscale = self._layer.getCellGrid().getXScale()
36 self._yscale = self._layer.getCellGrid().getYScale()
37
33 if uniqInMap: 38 if uniqInMap:
34 self._instance = self._layer.getInstance(self._name) 39 self._instance = self._layer.getInstance(self._name)
35 else: 40 else:
36 #have to create instance here 41 #have to create instance here
37 self._instance = None 42 self._instance = None
38 43
39 #velocity as a vector 44 #velocity as a vector
40 self._velocity = fife.DoublePoint(0,0) 45 self._velocity = fife.DoublePoint(0,0)
41 self._maxvelocity = 0.025 46 self._maxvelocity = 0.025/sqrt(self._xscale * self._yscale)
42 self._timedelta = 0 47 self._timedelta = 0
43 self._weapon = None 48 self._weapon = None
44 49
45 def _getMaxVelocity(self): 50 def _getMaxVelocity(self):
46 return self._maxvelocity 51 return self._maxvelocity
47 52
48 def _setMaxVelocity(self, maxvel): 53 def _setMaxVelocity(self, maxvel):
49 self._maxvelocity = maxvel 54 self._maxvelocity = maxvel/sqrt(self._xscale * self._yscale)
50 55
51 def _getLocation(self): 56 def _getLocation(self):
52 return self._instance.getLocation() 57 return self._instance.getLocation()
53 58
54 def _setLocation(self, loc): 59 def _setLocation(self, loc):
64 self._weapon = weapon 69 self._weapon = weapon
65 70
66 def _getWeapon(self, weapon): 71 def _getWeapon(self, weapon):
67 return self._weapon 72 return self._weapon
68 73
69 def applyThrust(self, vector): 74 def applyThrust(self, vector, timedelta):
70 self._velocity.x += vector.x * (self._timedelta/1000.0) 75 self._velocity.x += (vector.x * (timedelta/1000.0))/self._xscale
71 self._velocity.y += vector.y * (self._timedelta/1000.0) 76 self._velocity.y += (vector.y * (timedelta/1000.0))/self._yscale
72 77
73 if self._velocity.length() > self._maxvelocity: 78 if self._velocity.length() > self._maxvelocity:
74 norm = normalize(self._velocity) 79 norm = normalize(self._velocity)
75 self._velocity.x = norm.x * self._maxvelocity 80 self._velocity.x = norm.x * self._maxvelocity
76 self._velocity.y = norm.y * self._maxvelocity 81 self._velocity.y = norm.y * self._maxvelocity
96 101
97 #apply braking deceleration 102 #apply braking deceleration
98 norm.x *= brakingForce 103 norm.x *= brakingForce
99 norm.y *= brakingForce 104 norm.y *= brakingForce
100 105
101 self._velocity.x += norm.x * (self._timedelta/1000.0) 106 self._velocity.x += (norm.x * (self._timedelta/1000.0))/self._xscale
102 self._velocity.y += norm.y * (self._timedelta/1000.0) 107 self._velocity.y += (norm.y * (self._timedelta/1000.0))/self._yscale
103 108
104 def fire(self, curtime): 109 def fire(self, curtime):
105 if self._weapon: 110 if self._weapon:
106 return self._weapon.fire(curtime) 111 return self._weapon.fire(curtime)
107 112