Mercurial > fife-parpg
comparison demos/shooter/scripts/common/helpers.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 | 3164715a0621 |
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 | |
26 | |
27 def normalize(vector): | |
28 """ | |
29 Helper function to normalize a vector | |
30 """ | |
31 norm = fife.DoublePoint(0,0) | |
32 | |
33 invLength = 1.0/vector.length() | |
34 if invLength > 1e-06: | |
35 norm.x = vector.x * invLength; | |
36 norm.y = vector.y * invLength; | |
37 else: | |
38 norm.x = 0 | |
39 norm.y = 0 | |
40 | |
41 return norm | |
42 | |
43 class Rect(object): | |
44 def __init__(self, x = 0, y = 0, w = 0, h = 0): | |
45 self._x = x | |
46 self._y = y | |
47 self._w = w | |
48 self._h = h | |
49 | |
50 def intersects(self, rect): | |
51 _x = self._x - rect.x; | |
52 _y = self._y - rect.y; | |
53 _w = self._w; | |
54 _h = self._h; | |
55 | |
56 if _x < 0: | |
57 _w += _x | |
58 _x = 0 | |
59 | |
60 if _y < 0: | |
61 _h += _y | |
62 _y = 0 | |
63 | |
64 if _x + _w > rect.w: | |
65 _w = rect.w - _x | |
66 | |
67 if _y + _h > rect.h: | |
68 _h = rect.h - _y | |
69 | |
70 if _w <= 0 or _h <= 0: | |
71 return False | |
72 | |
73 return True | |
74 | |
75 def _setX(self, x): | |
76 self._x = x | |
77 | |
78 def _getX(self): | |
79 return self._x | |
80 | |
81 def _setY(self, y): | |
82 self._y = y | |
83 | |
84 def _getY(self): | |
85 return self._y | |
86 | |
87 def _setW(self, w): | |
88 self._w = w | |
89 | |
90 def _getW(self): | |
91 return self._w | |
92 | |
93 def _setH(self, h): | |
94 self._h = h | |
95 | |
96 def _getH(self): | |
97 return self._h | |
98 | |
99 x = property(_getX, _setX) | |
100 y = property(_getY, _setY) | |
101 w = property(_getW, _setW) | |
102 h = property(_getH, _setH) |