annotate demos/shooter/scripts/scene.py @ 453:cf53848fb187

Scene now gets updated when an object moves from one node to another. Player is now part of the scene. Projectiles can be files by both player and enemies. Some code cleanup.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 06 Apr 2010 19:12:41 +0000
parents f07d779362da
children bd7e8f708adf
rev   line source
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 # -*- coding: utf-8 -*-
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3 # ####################################################################
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 # Copyright (C) 2005-2009 by the FIFE team
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 # http://www.fifengine.de
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6 # This file is part of FIFE.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8 # FIFE is free software; you can redistribute it and/or
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 # modify it under the terms of the GNU Lesser General Public
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
10 # License as published by the Free Software Foundation; either
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 # version 2.1 of the License, or (at your option) any later version.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
13 # This library is distributed in the hope that it will be useful,
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
16 # Lesser General Public License for more details.
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
17 #
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
18 # You should have received a copy of the GNU Lesser General Public
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 # License along with this library; if not, write to the
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20 # Free Software Foundation, Inc.,
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 # ####################################################################
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
23
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 from fife import fife
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 from scripts.ships.shipbase import Ship
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26 from scripts.ships.player import Player
452
f07d779362da Added different enemy types which behave differently.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
27 from scripts.ships.enemies import *
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 from scripts.common.helpers import Rect
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29
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.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
30
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
31 class SceneNode(object):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
32 def __init__(self, spaceobjects = None):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
33 if not spaceobjects:
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
34 self._spaceobjects = list()
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
35 else:
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
36 self._spaceobjects = spaceobjects
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
37
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
38 def _getObjects(self):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
39 return self._spaceobjects
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
40
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
41 def _setObjects(self, spaceobjects):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
42 self._spaceobjects = spaceobjects
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
43
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
44 spaceobjects = property(_getObjects, _setObjects)
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.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
45
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 class Scene(object):
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 def __init__(self, engine, objectLayer):
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 self._engine = engine
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49 self._model = engine.getModel()
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 self._layer = objectLayer
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
51 self._nodes = list()
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
53 self._player = Player(self, 'player')
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
54 self._player.width = 0.075
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
55 self._player.height = 0.075
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
56 self._player.start()
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
57
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 self._projectiles = list()
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 self._lasttime = 0
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
61 self._maxnodes = 128
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
62 self._xscale = 0
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
63
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
64 self._time = 0
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
65 self._timedelta = 0
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
66
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
67 def initScene(self, mapobj):
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
68 self._xscale = self._layer.getCellGrid().getXScale()
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
69
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
70 enemies = self._layer.getInstances('enemy')
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
71
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
72 #initialize our scene array to some arbitrary size
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
73 for i in range(0,self._maxnodes):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
74 self._nodes.append(SceneNode())
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
75
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
76 for instance in enemies:
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
77 objectName = instance.getObject().getId()
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
78 print objectName
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
79
452
f07d779362da Added different enemy types which behave differently.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
80 if objectName == "saucer1":
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
81 enemy = Saucer1(self, 'enemy', False)
452
f07d779362da Added different enemy types which behave differently.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
82 elif objectName == "saucer2":
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
83 enemy = Saucer2(self, 'enemy', False)
452
f07d779362da Added different enemy types which behave differently.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
84 else:
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
85 enemy = Ship(self, 'enemy', False)
452
f07d779362da Added different enemy types which behave differently.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 451
diff changeset
86
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
87 enemy.instance = instance
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
88 enemy.start()
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
89
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
90 loc = instance.getLocation().getExactLayerCoordinates()
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
91 nodeindex = int(loc.x * self._xscale)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
92 enemy.scenenodeid = nodeindex
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
93 self._nodes[nodeindex].spaceobjects.append(enemy)
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
94
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
95 #and finally add the player to the scene
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
96 self.addObjectToScene(self._player)
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
97
451
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
98 def getObjectsInNode(self, nodeindex):
f463ab431cc0 Movement shouldn't be dependent on framerate anymore.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 450
diff changeset
99 return self._nodes[nodeindex].instances
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
100
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
101 def getObjectsInRange(self, rangeL, rangeR):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
102 objects = list()
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
103
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
104 for i in range(rangeL, rangeR):
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
105 objects.extend(self._nodes[i].spaceobjects)
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
106
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
107 return objects
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
108
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
109 def addObjectToScene(self, obj):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
110 #TODO: search to ensure the object isn't already part of the scene
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
111 loc = obj.instance.getLocation().getExactLayerCoordinates()
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
112 nodeindex = int(loc.x * self._xscale)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
113
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
114 self._nodes[nodeindex].spaceobjects.append(obj)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
115 obj.scenenodeid = nodeindex
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
116
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
117 def addProjectileToScene(self, projectile):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
118 self._projectiles.append(projectile)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
119
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
120 def moveObjectInScene(self, obj):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
121 loc = obj.instance.getLocation().getExactLayerCoordinates()
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
122 nodeindex = int(loc.x * self._xscale)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
123
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
124 if nodeindex != obj.scenenodeid:
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
125 self._nodes[obj.scenenodeid].spaceobjects.remove(obj)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
126 self._nodes[nodeindex].spaceobjects.append(obj)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
127 obj.scenenodeid = nodeindex
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
128
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
129 def removeObjectFromScene(self, obj):
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
130 for node in self._nodes:
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
131 if obj in node.spaceobjects:
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
132 node.spaceobjects.remove(obj)
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
133 return
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
134
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
135 def attachCamera(self, cam):
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
136 self._camera = cam
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
137 self._camera.setLocation(self._player.location)
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
138
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
139 def update(self, time, keystate):
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
140 timedelta = time - self._time
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
141 self._timedelta = timedelta
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
142 self._time = time
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
143
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
144 self._keystate = keystate
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
145
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
146 #update camera location
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.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
147 loc = self._camera.getLocation()
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
148 exactloc = self._camera.getLocation().getExactLayerCoordinates()
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
149 #slowly move to the right
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.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
150 exactloc.x += timedelta * 0.001
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
151 loc.setExactLayerCoordinates(exactloc)
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
152 self._camera.setLocation(loc)
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
153
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
154 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0))
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
155 bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768))
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
156
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
157 #which scene nodes to use to update objects
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
158 leftnode = int(topleft.x)
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
159 rightnode = int(bottomright.x) + 1
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
160
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
161 #get a list of objects on the screen
448
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
162 if leftnode < 0:
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
163 leftnode = 0
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
164 if rightnode > self._maxnodes:
5e2ec84902a7 Did a little re-factoring.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 447
diff changeset
165 rightnode = self._maxnodes
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
166 screenlist = self.getObjectsInRange(leftnode, rightnode)
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
167
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
168
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
169 #update objects on the screen
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
170 for obj in screenlist:
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
171 obj.update(timedelta)
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
172 if obj.changedposition:
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
173 self.moveObjectInScene(obj)
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.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 446
diff changeset
174
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
175 #Testing enemy fire
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
176 #prjct = obj.fire(time, fife.DoublePoint(-1,0))
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
177 #if prjct:
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
178 # self._projectiles.append(prjct)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
179
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
180 if obj != self._player:
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
181 if obj.boundingbox.intersects(self._player.boundingbox):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
182 #player touched an enemy. Destroy player and
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
183 #re-initialize scene
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
184 self._player.destroy()
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
185
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
186
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
187 #update the list of projectiles
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
188 projtodelete = list()
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
189 for p in self._projectiles:
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
190 p.update(timedelta)
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
191 #check to see if the projectile hit any object on the screen
449
1cf56403347a Added object bounding boxes.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 448
diff changeset
192 for o in screenlist:
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
193 #cant get hit by your own bullet
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
194 if p.owner != o:
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
195 if p.boundingbox.intersects(o.boundingbox):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
196 self._player.applyScore(100)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
197 p.destroy()
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
198 o.destroy()
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
199 #temporary... the destroy functions should spawn an explosion
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
200 #and also destroy the instance and remove itself from the scene
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
201 self.removeObjectFromScene(o)
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
202
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
203 #build a list of projectiles to remove (ttl expired)
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
204 if not p.running:
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
205 projtodelete.append(p)
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
206
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
207 #remove any expired projectiles
450
ba6817013343 Added score keeping ability.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 449
diff changeset
208 for p in projtodelete:
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
209 self._projectiles.remove(p)
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
210
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
211
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
212 def _getPlayer(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
213 return self._player
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
214
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
215 def _getKeyState(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
216 return self._keystate
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
217
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
218 def _getCamera(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
219 return self._camera
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
220
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
221 def _getObjectLayer(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
222 return self._layer
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
223
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
224 def _getModel(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
225 return self._model
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
226
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
227 def _getTime(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
228 return self._time
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
229
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
230 def _getTimeDelta(self):
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
231 return self._timedelta
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
232
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
233 player = property(_getPlayer)
453
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
234 keystate = property(_getKeyState)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
235 camera = property(_getCamera)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
236 objectlayer = property(_getObjectLayer)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
237 model = property(_getModel)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
238 time = property(_getTime)
cf53848fb187 Scene now gets updated when an object moves from one node to another.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 452
diff changeset
239 timedelta = property(_getTimeDelta)
446
2046a1f2f5f2 Adding the shooter demo. This is still a work in progress.
prock@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
240