forked from neuromancer/AlienBreedObliteration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollisionutils.py
141 lines (100 loc) · 3.61 KB
/
collisionutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Written By:
Chris Humphreys
Email: < chris (--AT--) habitualcoder [--DOT--] com >
All python source code copyright Chris Humphreys 2010
Level data, Obliteration name and some media copyright
Flash Jester Punk 2007
Alien Breed name, concept and media copyright Team17 1992
This file is part of alien breed obliteration wii edition
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import mygame
"""
For most units collisions will be calculated using a slightly smaller (80%)
rectangle than the visible sprite.
Units track their position using a Rect object (x,y,w,h).
They also track their collision rectangle using a slightly smaller rectangle.
These rectangles are recalculated every time the unit moves position using the
calc_collision_rect(sprite rectangle) method
As pixel perfect collisions are very slow in SDL we approximate this for the
large boss units by using multiple collision rectangles. In that case the
calc_bossX_collision_rect(sprite rectangle) are used.
The boss method's rectangle dimensions are determined to approximate the
non-transparent parts of the boss' sprite.
Which method to use is determined by the Unit's behaviour code.
"""
def calc_collision_rect(player_rect):
f = .8
o = .1
w = player_rect.w *f
h = player_rect.h *f
x = player_rect.x + player_rect.w*o
y = player_rect.y + player_rect.h*o
return mygame.Rect(x,y,w,h)
def calc_boss2_collision_rect(player_rect):
rects = []
w = player_rect.w * .6
h = player_rect.h * .6
x = player_rect.x + player_rect.w *.2
y = player_rect.y
rects.append(mygame.Rect(x,y,w,h))
w = player_rect.w * .2
h = player_rect.h * .2
x = player_rect.x + player_rect.w *.4
y = player_rect.y + player_rect.h * .7
rects.append(mygame.Rect(x,y,w,h))
return rects
def calc_boss3_collision_rect(player_rect):
rects = []
w = player_rect.w * .6
h = player_rect.h * .3
x = player_rect.x + player_rect.w *.2
y = player_rect.y + player_rect.h * 0.6
rects.append(mygame.Rect(x,y,w,h))
w = player_rect.w * .4
h = player_rect.h * .6
x = player_rect.x + player_rect.w *.3
y = player_rect.y
rects.append(mygame.Rect(x,y,w,h))
return rects
def calc_boss4_collision_rect(player_rect):
rects = []
w = player_rect.w * .8
h = player_rect.h * .4
x = player_rect.x + player_rect.w *.2
y = player_rect.y + player_rect.h * 0.6
rects.append(mygame.Rect(x,y,w,h))
w = player_rect.w * .5
h = player_rect.h * .6
x = player_rect.x + player_rect.w *.2
y = player_rect.y
rects.append(mygame.Rect(x,y,w,h))
return rects
def calc_boss1_collision_rect(player_rect):
rects = []
w = player_rect.w * .8
h = player_rect.h * .3
x = player_rect.x + player_rect.w *.1
y = player_rect.y + player_rect.h * 0.3
rects.append(mygame.Rect(x,y,w,h))
w = player_rect.w * .4
h = player_rect.h * .3
x = player_rect.x + player_rect.w *.3
y = player_rect.y
rects.append(mygame.Rect(x,y,w,h))
w = player_rect.w * .6
h = player_rect.h * .3
x = player_rect.x + player_rect.w *.2
y = player_rect.y + player_rect.h * 0.6
rects.append(mygame.Rect(x,y,w,h))
return rects