forked from Redace10/Hackathon-2018-Hubdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bryan.py
48 lines (44 loc) · 1.46 KB
/
bryan.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
import math
import GLOBAL
class Bryan:
def __init__(self, rect):
self.__speed = GLOBAL.BRYAN_SPEED
self.__rect = rect
self.__target = None
def getRect(self):
return self.__rect
def selectTarget(self, docs):
if self.__target and docs.count(self.__target) > 0:
return True
else:
if len(docs) == 0:
return False
minDistance = GLOBAL.MAP_WIDTH
self.__target = None
for d in docs:
xSquared = (self.__rect.x - d.getRect().x) * (self.__rect.x - d.getRect().x)
ySquared = (self.__rect.y - d.getRect().y) * (self.__rect.y - d.getRect().y)
distance = math.sqrt(xSquared + ySquared)
if distance <= minDistance:
minDistance = distance
self.__target = d
if self.__target != None:
return True
else:
return False
def moveToTarget(self):
if self.__target != None:
targetRect = self.__target.getRect()
if targetRect.center[0] < self.__rect.center[0] - self.__speed:
xOffset = -1 * self.__speed
elif targetRect.center[0] > self.__rect.center[0] + self.__speed:
xOffset = 1 * self.__speed
else:
xOffset = 0
if targetRect.center[1] < self.__rect.center[1] - self.__speed:
yOffset = -1 * self.__speed
elif targetRect.center[1] > self.__rect.center[1] + self.__speed:
yOffset = 1 * self.__speed
else:
yOffset = 0
self.__rect.move_ip(xOffset, yOffset)