comparison demos/shooter/scripts/ships/player.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.ships.shipbase import Ship
26 from scripts.common.helpers import Rect
27
28
29 class Player(Ship):
30 def __init__(self, scene, shipName, layer):
31 super(Player, self).__init__(scene, shipName, layer, True)
32 self._bounds = Rect(-100,-100,200,200)
33
34 def update(self, timedelta, keystate, camera):
35 key = False
36
37 oldpos = self.location
38
39 if keystate['UP']:
40 self.applyThrust(fife.DoublePoint(0,-0.075))
41 key = True
42 if keystate['DOWN']:
43 self.applyThrust(fife.DoublePoint(0,0.075))
44 key = True
45 if keystate['LEFT']:
46 self.applyThrust(fife.DoublePoint(-0.075,0))
47 key = True
48 if keystate['RIGHT']:
49 self.applyThrust(fife.DoublePoint(0.075,0))
50 key = True
51
52 if not key and self._velocity.length() > 0:
53 self.applyBrake(0.075)
54
55 super(Player, self).update(timedelta)
56
57 #set up the players camera bounds
58 #TODO: grab screen resolution from somewhere
59 topleft = camera.toMapCoordinates(fife.ScreenPoint(0,0))
60 bottomright = camera.toMapCoordinates(fife.ScreenPoint(1024,768))
61
62 #add a little padding to the left edge
63 topleft.x += 0.1
64
65 camrect = Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y)
66
67 #player bounding box
68 #TODO: make this configurable
69 pos = self.location.getExactLayerCoordinates()
70 bbox = Rect()
71 bbox.x = pos.x - 0.005
72 bbox.y = pos.y - 0.005
73 bbox.w = 0.01
74 bbox.h = 0.01
75
76 if not bbox.intersects(camrect):
77 if pos.x < topleft.x:
78 pos.x += timedelta * 0.0005
79 oldpos.setExactLayerCoordinates(pos)
80 self._velocity.x = timedelta * 0.0005
81 else:
82 self._velocity.x = 0
83
84 self._velocity.y = 0
85
86 self.location = oldpos
87