comparison demos/shooter/scripts/ships/shipbase.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 from scripts.common.helpers import normalize
26 from scripts.weapons import Weapon
27
28 class Ship(object):
29 def __init__(self, scene, shipName, layer, uniqInMap=True):
30 self._scene = scene
31 self._name = shipName
32 self._layer = layer
33 if uniqInMap:
34 self._instance = self._layer.getInstance(self._name)
35 else:
36 #have to create instance here
37 self._instance = None
38
39 #velocity as a vector
40 self._velocity = fife.DoublePoint(0,0)
41 self._maxvelocity = 0.025
42 self._timedelta = 0
43 self._weapon = None
44
45 def _getMaxVelocity(self):
46 return self._maxvelocity
47
48 def _setMaxVelocity(self, maxvel):
49 self._maxvelocity = maxvel
50
51 def _getLocation(self):
52 return self._instance.getLocation()
53
54 def _setLocation(self, loc):
55 self._instance.setLocation(loc)
56
57 def _getInstance(self):
58 return self._instance
59
60 def _getVelocity(self):
61 return self._velocity
62
63 def _setWeapon(self, weapon):
64 self._weapon = weapon
65
66 def _getWeapon(self, weapon):
67 return self._weapon
68
69 def applyThrust(self, vector):
70 self._velocity.x += vector.x * (self._timedelta/1000.0)
71 self._velocity.y += vector.y * (self._timedelta/1000.0)
72
73 if self._velocity.length() > self._maxvelocity:
74 norm = normalize(self._velocity)
75 self._velocity.x = norm.x * self._maxvelocity
76 self._velocity.y = norm.y * self._maxvelocity
77
78
79 def applyBrake(self, brakingForce):
80
81 if self._velocity.length() <= .001:
82 self._velocity.x = 0
83 self._velocity.y = 0
84 return
85
86 #first normalize to get a unit vector of the direction we are traveling
87 norm = normalize(self._velocity)
88 if norm.length() == 0:
89 self._velocity.x = 0
90 self._velocity.y = 0
91 return
92
93 #negate to get opposite direction
94 norm.x = norm.x * -1
95 norm.y = norm.y * -1
96
97 #apply braking deceleration
98 norm.x *= brakingForce
99 norm.y *= brakingForce
100
101 self._velocity.x += norm.x * (self._timedelta/1000.0)
102 self._velocity.y += norm.y * (self._timedelta/1000.0)
103
104 def fire(self, curtime):
105 if self._weapon:
106 return self._weapon.fire(curtime)
107
108 return None
109
110 def start(self):
111 pass
112
113 def update(self, timedelta):
114 self._timedelta = timedelta
115
116 shiploc = self.location
117 exactloc = shiploc.getExactLayerCoordinates()
118
119 exactloc.x += self._velocity.x
120 exactloc.y += self._velocity.y
121
122 shiploc.setExactLayerCoordinates(exactloc)
123 self.location = shiploc
124
125 def stop(self):
126 pass
127
128 def destroy(self):
129 pass
130
131 location = property(_getLocation,_setLocation)
132 instance = property(_getInstance)
133 velocity = property(_getVelocity)
134 maxvelocity = property(_getMaxVelocity, _setMaxVelocity)
135 weapon = property(_getWeapon, _setWeapon)
136