-
Notifications
You must be signed in to change notification settings - Fork 3
/
competitor.py
58 lines (53 loc) · 1.76 KB
/
competitor.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
import math
import GLOBAL
from document import Document
class Competitor:
def __init__(self, logo, rect, speed):
self.__logo = logo
self.__rect = rect
self.__target = None
self.__speed = speed
def getRect(self):
return self.__rect
def selectTarget(self, docs, player):
if self.__target and docs.count(self.__target) > 0:
return True
else:
if len(docs) == 0:
self.__target = player
return True
minDistance = GLOBAL.MAP_WIDTH
self.__target = None
for d in docs:
if d.getTargeted() == False:
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:
self.__target.setTargeted(True)
return True
else:
self.__target = player
return True
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)
def __del__(self):
if (self.__target != None):
self.__target.setTargeted = False