comparison demos/shooter/scripts/common/baseobject.py @ 491:c4168eb47a44

Adding some comments to the shooter demo. Updated the copyright date and FIFE URL as well.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 06 May 2010 19:34:21 +0000
parents ab28994820dd
children 16ceb3228324
comparison
equal deleted inserted replaced
490:939a4dc12ca1 491:c4168eb47a44
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # #################################################################### 3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team 4 # Copyright (C) 2005-2010 by the FIFE team
5 # http://www.fifengine.de 5 # http://www.fifengine.net
6 # This file is part of FIFE. 6 # This file is part of FIFE.
7 # 7 #
8 # FIFE is free software; you can redistribute it and/or 8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public 9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either 10 # License as published by the Free Software Foundation; either
33 SHTR_ENEMYSHIP = 4 33 SHTR_ENEMYSHIP = 4
34 SHTR_POWERUP = 5 34 SHTR_POWERUP = 5
35 35
36 36
37 class SpaceObject(object): 37 class SpaceObject(object):
38 """
39 Space Object
40
41 This is the base class for all game objects.
42 """
38 def __init__(self, scene, name, findInstance=True): 43 def __init__(self, scene, name, findInstance=True):
44 """
45 @param scene A reference to the Scene
46 @param name The name of the space object
47 @param findInstance True if the instance you are looking for is already loaded
48 False if you want to load the instance yourself
49
50 """
39 self._scene = scene 51 self._scene = scene
40 self._model = self._scene.model 52 self._model = self._scene.model
41 self._layer = self._scene.objectlayer 53 self._layer = self._scene.objectlayer
42 self._name = name 54 self._name = name
43 self._xscale = self._layer.getCellGrid().getXScale() 55 self._xscale = self._layer.getCellGrid().getXScale()
55 self._instance.thisown = 0 67 self._instance.thisown = 0
56 else: 68 else:
57 self._instnace = None 69 self._instnace = None
58 70
59 def start(self): 71 def start(self):
72 """
73 You must execute this function for the object to be updated
74 """
60 if self._instance: 75 if self._instance:
61 self._running = True 76 self._running = True
62 77
63 def update(self): 78 def update(self):
79 """
80 If the object is running this updates the FIFE instance location based on
81 the objects current velocity and time since last frame
82 """
64 if self._running: 83 if self._running:
65 shiploc = self.location 84 shiploc = self.location
66 exactloc = shiploc.getExactLayerCoordinates() 85 exactloc = shiploc.getExactLayerCoordinates()
67 86
68 exactloc.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale 87 exactloc.x += self._velocity.x * (self._scene.timedelta/1000.0)/self._xscale
79 self._changedPosition = True 98 self._changedPosition = True
80 99
81 self.location = shiploc 100 self.location = shiploc
82 101
83 def stop(self): 102 def stop(self):
103 """
104 Stops the object from being updated.
105 """
84 self._running = False 106 self._running = False
85 107
86 def destroy(self): 108 def destroy(self):
109 """
110 You are meant to override this function to specify what happens when the object
111 gets destroyed
112 """
87 self._running = False 113 self._running = False
88 114
89 def applyThrust(self, vector): 115 def applyThrust(self, vector):
116 """
117 Applies a thrust vector to the object. Note that objects do not have
118 mass or any inertia.
119
120 @param vector A fife.DoublePoint() specifying the direction and intensity of thrust.
121 """
90 self._velocity.x += (vector.x * (self._scene.timedelta/1000.0))/self._xscale 122 self._velocity.x += (vector.x * (self._scene.timedelta/1000.0))/self._xscale
91 self._velocity.y += (vector.y * (self._scene.timedelta/1000.0))/self._yscale 123 self._velocity.y += (vector.y * (self._scene.timedelta/1000.0))/self._yscale
92 124
93 if self._velocity.length() > self._maxvelocity: 125 if self._velocity.length() > self._maxvelocity:
94 norm = normalize(self._velocity) 126 norm = normalize(self._velocity)
95 self._velocity.x = norm.x * self._maxvelocity 127 self._velocity.x = norm.x * self._maxvelocity
96 self._velocity.y = norm.y * self._maxvelocity 128 self._velocity.y = norm.y * self._maxvelocity
97 129
98 130
99 def applyBrake(self, brakingForce): 131 def applyBrake(self, brakingForce):
100 132 """
133 Applies a braking thrust in the opposite direction of the current velocity
134
135 @param brakingForce a floating point value specifying how fast the object should decelerate
136 """
101 if self._velocity.length() <= .01: 137 if self._velocity.length() <= .01:
102 self._velocity.x = 0 138 self._velocity.x = 0
103 self._velocity.y = 0 139 self._velocity.y = 0
104 return 140 return
105 141
120 156
121 self._velocity.x += (norm.x * (self._scene.timedelta/1000.0))/self._xscale 157 self._velocity.x += (norm.x * (self._scene.timedelta/1000.0))/self._xscale
122 self._velocity.y += (norm.y * (self._scene.timedelta/1000.0))/self._yscale 158 self._velocity.y += (norm.y * (self._scene.timedelta/1000.0))/self._yscale
123 159
124 def removeFromScene(self): 160 def removeFromScene(self):
161 """
162 Queues this object to be removed from the scene. The scene will remove the object
163 next time the garbage collection routines are called.
164 """
125 self._scene.queueObjectForRemoval(self) 165 self._scene.queueObjectForRemoval(self)
126 166
127 def _isRunning(self): 167 def _isRunning(self):
128 return self._running 168 return self._running
129 169