-
Notifications
You must be signed in to change notification settings - Fork 0
/
Door.py
107 lines (97 loc) · 4.73 KB
/
Door.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
from Vector import Vector
class Door:
def __init__(self, heading, center, debug=False):
"""
Generate a Door object based on the heading and center pos.
Keyword arguments:
heading - The position of the door in the room (e.g. door is at top so pass in "N" for heading)
center - A Vector object containing the position of the center of the door
"""
self.__heading = heading
self.__width = 64
self.__center = center
self.__col_box_size = 10
self.__show_col_box = debug
self.__inter_level_door = False
if(self.__heading == "N" or self.__heading == "S"):
self.__p1 = Vector( (self.__center.x - self.__width/2), self.__center.y )
self.__p2 = Vector( (self.__center.x + self.__width/2), self.__center.y )
self.__bounds = [ Vector(self.__p1.x, self.__p1.y - self.__col_box_size),
Vector(self.__p2.x, self.__p2.y - self.__col_box_size),
Vector(self.__p2.x, self.__p2.y + self.__col_box_size),
Vector(self.__p1.x, self.__p2.y + self.__col_box_size) ]
elif(self.__heading == "E" or self.__heading == "W"):
self.__p1 = Vector( self.__center.x, (self.__center.y - self.__width/2) )
self.__p2 = Vector( self.__center.x, (self.__center.y + self.__width/2) )
self.__bounds = [ Vector(self.__p1.x - self.__col_box_size, self.__p1.y),
Vector(self.__p1.x + self.__col_box_size, self.__p1.y),
Vector(self.__p2.x + self.__col_box_size, self.__p2.y),
Vector(self.__p2.x - self.__col_box_size, self.__p2.y) ]
else:
self.__inter_level_door = True
self.__bounds = [ Vector(self.__center.x - 16, self.__center.y - 16),
Vector(self.__center.x + 16, self.__center.y - 16),
Vector(self.__center.x + 16, self.__center.y + 16),
Vector(self.__center.x - 16, self.__center.y + 16) ]
def isLevelDoor(self):
return(self.__inter_level_door)
def draw(self, canvas, tileset, sprite_pos, size):
"""
Draws the door on the canvas. Rotates door sprite based on `self.__heading`.
Adjusts sprite position on canvas by a margin of 1 to fit walls.
Keyword arguments:
canvas - The SimpleGUI canvas
"""
if(self.isLevelDoor()):
canvas.draw_image(tileset, sprite_pos, size, self.__center.get_p(), size)
else:
if(self.__heading == "N"):
rotation = 3.14159
dest = ( self.__center.x, self.__center.y + 1 )
elif(self.__heading == "E"):
rotation = 4.71239
dest = ( self.__center.x - 1, self.__center.y )
elif(self.__heading == "S"):
rotation = 0
dest = ( self.__center.x, self.__center.y - 1 )
elif(self.__heading == "W"):
rotation = 1.5708
dest = ( self.__center.x + 1, self.__center.y )
canvas.draw_image(tileset, sprite_pos, size, dest, size, rotation)
# For debug purposes:
if(self.__show_col_box):
canvas.draw_line(self.__p1.get_p(), self.__p2.get_p(), 5, "Yellow")
b_list = []
for bound in self.__bounds:
b_list.append(bound.get_p())
canvas.draw_polygon(b_list, 3, "Green")
def getBounds(self):
"""
Returns the collision box for the door
"""
return(self.__bounds)
def inBounds(self, obj):
"""
Returns a bool if the given obj is within the collision box
Keyword arguments:
obj - and object that has a getPos() method that returns a Vector
"""
obj_x = obj.getPos().x
obj_y = obj.getPos().y
return( obj_x >= self.__bounds[0].x
and obj_x <= self.__bounds[1].x
and obj_y >= self.__bounds[0].y
and obj_y <= self.__bounds[2].y )
def getSpawnPos(self):
"""
Returns a Vector object with the spawn position for the character
relative to the heading
"""
if(self.__heading == "N"):
return( Vector( self.__center.x, self.__center.y + self.__col_box_size + 1 ) )
elif(self.__heading == "E"):
return( Vector( self.__center.x - self.__col_box_size - 1, self.__center.y ) )
elif(self.__heading == "S"):
return( Vector( self.__center.x, self.__center.y - self.__col_box_size - 1 ) )
else:
return( Vector( self.__center.x + self.__col_box_size + 1, self.__center.y ) )