Mercurial > fife-parpg
comparison demos/shooter/scripts/ships/shipbase.py @ 448:5e2ec84902a7
Did a little re-factoring.
Introduced the scene graph for collision detection.
Changed the time scale to be accurate.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 01 Apr 2010 17:03:34 +0000 |
parents | 64676ea55472 |
children | f463ab431cc0 |
comparison
equal
deleted
inserted
replaced
447:64676ea55472 | 448:5e2ec84902a7 |
---|---|
22 # #################################################################### | 22 # #################################################################### |
23 | 23 |
24 from math import sqrt | 24 from math import sqrt |
25 | 25 |
26 from fife import fife | 26 from fife import fife |
27 from scripts.common.helpers import normalize | 27 from scripts.common.baseobject import SpaceObject |
28 from scripts.weapons import Weapon | 28 from scripts.weapons import Weapon |
29 | 29 |
30 class Ship(object): | |
31 def __init__(self, scene, shipName, layer, uniqInMap=True): | |
32 self._scene = scene | |
33 self._name = shipName | |
34 self._layer = layer | |
35 self._xscale = self._layer.getCellGrid().getXScale() | |
36 self._yscale = self._layer.getCellGrid().getYScale() | |
37 | 30 |
38 if uniqInMap: | 31 class Ship(SpaceObject): |
39 self._instance = self._layer.getInstance(self._name) | 32 def __init__(self, model, name, layer, findInstance=True): |
40 else: | 33 super(Ship, self).__init__(model, name, layer, findInstance) |
41 #have to create instance here | 34 |
42 self._instance = None | |
43 | |
44 #velocity as a vector | |
45 self._velocity = fife.DoublePoint(0,0) | |
46 self._maxvelocity = 0.025/sqrt(self._xscale * self._yscale) | 35 self._maxvelocity = 0.025/sqrt(self._xscale * self._yscale) |
47 self._timedelta = 0 | 36 self._timedelta = 0 |
48 self._weapon = None | 37 self._weapon = None |
49 | |
50 def _getMaxVelocity(self): | |
51 return self._maxvelocity | |
52 | |
53 def _setMaxVelocity(self, maxvel): | |
54 self._maxvelocity = maxvel/sqrt(self._xscale * self._yscale) | |
55 | |
56 def _getLocation(self): | |
57 return self._instance.getLocation() | |
58 | |
59 def _setLocation(self, loc): | |
60 self._instance.setLocation(loc) | |
61 | |
62 def _getInstance(self): | |
63 return self._instance | |
64 | |
65 def _getVelocity(self): | |
66 return self._velocity | |
67 | 38 |
68 def _setWeapon(self, weapon): | 39 def _setWeapon(self, weapon): |
69 self._weapon = weapon | 40 self._weapon = weapon |
70 | 41 |
71 def _getWeapon(self, weapon): | 42 def _getWeapon(self, weapon): |
72 return self._weapon | 43 return self._weapon |
73 | 44 |
74 def applyThrust(self, vector, timedelta): | |
75 self._velocity.x += (vector.x * (timedelta/1000.0))/self._xscale | |
76 self._velocity.y += (vector.y * (timedelta/1000.0))/self._yscale | |
77 | |
78 if self._velocity.length() > self._maxvelocity: | |
79 norm = normalize(self._velocity) | |
80 self._velocity.x = norm.x * self._maxvelocity | |
81 self._velocity.y = norm.y * self._maxvelocity | |
82 | |
83 | |
84 def applyBrake(self, brakingForce): | |
85 | |
86 if self._velocity.length() <= .001: | |
87 self._velocity.x = 0 | |
88 self._velocity.y = 0 | |
89 return | |
90 | |
91 #first normalize to get a unit vector of the direction we are traveling | |
92 norm = normalize(self._velocity) | |
93 if norm.length() == 0: | |
94 self._velocity.x = 0 | |
95 self._velocity.y = 0 | |
96 return | |
97 | |
98 #negate to get opposite direction | |
99 norm.x = norm.x * -1 | |
100 norm.y = norm.y * -1 | |
101 | |
102 #apply braking deceleration | |
103 norm.x *= brakingForce | |
104 norm.y *= brakingForce | |
105 | |
106 self._velocity.x += (norm.x * (self._timedelta/1000.0))/self._xscale | |
107 self._velocity.y += (norm.y * (self._timedelta/1000.0))/self._yscale | |
108 | |
109 def fire(self, curtime): | 45 def fire(self, curtime): |
110 if self._weapon: | 46 if self._weapon: |
111 return self._weapon.fire(curtime) | 47 return self._weapon.fire(curtime) |
112 | 48 |
113 return None | 49 return None |
114 | 50 |
115 def start(self): | |
116 pass | |
117 | |
118 def update(self, timedelta): | |
119 self._timedelta = timedelta | |
120 | |
121 shiploc = self.location | |
122 exactloc = shiploc.getExactLayerCoordinates() | |
123 | |
124 exactloc.x += self._velocity.x | |
125 exactloc.y += self._velocity.y | |
126 | |
127 shiploc.setExactLayerCoordinates(exactloc) | |
128 self.location = shiploc | |
129 | |
130 def stop(self): | |
131 pass | |
132 | |
133 def destroy(self): | |
134 pass | |
135 | |
136 location = property(_getLocation,_setLocation) | |
137 instance = property(_getInstance) | |
138 velocity = property(_getVelocity) | |
139 maxvelocity = property(_getMaxVelocity, _setMaxVelocity) | |
140 weapon = property(_getWeapon, _setWeapon) | 51 weapon = property(_getWeapon, _setWeapon) |
141 | 52 |