comparison demos/shooter/scripts/common/baseobject.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
children 1cf56403347a
comparison
equal deleted inserted replaced
447:64676ea55472 448:5e2ec84902a7
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 from scripts.common.helpers import normalize
26
27 class SpaceObject(object):
28 def __init__(self, model, name, layer, findInstance=True):
29 self._model = model
30 self._layer = layer
31 self._name = name
32 self._xscale = self._layer.getCellGrid().getXScale()
33 self._yscale = self._layer.getCellGrid().getYScale()
34 self._velocity = fife.DoublePoint(0,0)
35 self._maxvelocity = 1
36 self._timedelta = 0
37
38 if findInstance:
39 self._instance = self._layer.getInstance(self._name)
40 else:
41 self._instnace = None
42
43 def start(self):
44 pass
45
46 def update(self, timedelta):
47 self._timedelta = timedelta
48
49 shiploc = self.location
50 exactloc = shiploc.getExactLayerCoordinates()
51
52 exactloc.x += self._velocity.x
53 exactloc.y += self._velocity.y
54
55 shiploc.setExactLayerCoordinates(exactloc)
56 self.location = shiploc
57
58 def stop(self):
59 pass
60
61 def destroy(self):
62 pass
63
64 def applyThrust(self, vector, timedelta):
65 self._velocity.x += (vector.x * (timedelta/100.0))/self._xscale
66 self._velocity.y += (vector.y * (timedelta/100.0))/self._yscale
67
68 if self._velocity.length() > self._maxvelocity:
69 norm = normalize(self._velocity)
70 self._velocity.x = norm.x * self._maxvelocity
71 self._velocity.y = norm.y * self._maxvelocity
72
73
74 def applyBrake(self, brakingForce, timedelta):
75
76 if self._velocity.length() <= .001:
77 self._velocity.x = 0
78 self._velocity.y = 0
79 return
80
81 #first normalize to get a unit vector of the direction we are traveling
82 norm = normalize(self._velocity)
83 if norm.length() == 0:
84 self._velocity.x = 0
85 self._velocity.y = 0
86 return
87
88 #negate to get opposite direction
89 norm.x = norm.x * -1
90 norm.y = norm.y * -1
91
92 #apply braking deceleration
93 norm.x *= brakingForce
94 norm.y *= brakingForce
95
96 self._velocity.x += (norm.x * (timedelta/100.0))/self._xscale
97 self._velocity.y += (norm.y * (timedelta/100.0))/self._yscale
98
99 def _getMaxVelocity(self):
100 return self._maxvelocity
101
102 def _setMaxVelocity(self, maxvel):
103 self._maxvelocity = maxvel/sqrt(self._xscale * self._yscale)
104
105 def _getLocation(self):
106 return self._instance.getLocation()
107
108 def _setLocation(self, loc):
109 self._instance.setLocation(loc)
110
111 def _getInstance(self):
112 return self._instance
113
114 def _setInstance(self, instance):
115 self._instance = instance
116
117 def _getVelocity(self):
118 return self._velocity
119
120 location = property(_getLocation,_setLocation)
121 instance = property(_getInstance, _setInstance)
122 velocity = property(_getVelocity)
123 maxvelocity = property(_getMaxVelocity, _setMaxVelocity)