-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUnit.py
More file actions
166 lines (152 loc) · 7.98 KB
/
Unit.py
File metadata and controls
166 lines (152 loc) · 7.98 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from Rectangle import Rectangle
import pygame, math
class Unit(Rectangle):
"""A class for units."""
def __init__(self, game, position):
self.game = game
self.finishedTurn = False
Rectangle.__init__(self, self.game, position, [16,16], [136,0,0])
self.movementPattern = [
[ 0, -3],
[-1, -2], [ 0, -2], [ 1, -2],
[-2, -1], [-1, -1], [ 0, -1], [ 1, -1], [ 2, -1],
[-3, 0], [-2, 0], [-1, 0], [ 1, 0], [ 2, 0], [ 3, 0],
[-2, 1], [-1, 1], [ 0, 1], [ 1, 1], [ 2, 1],
[-1, 2], [ 0, 2], [ 1, 2],
[ 0, 3],
]
self.legalMovementPattern = self.movementPattern[:]
self.showMovement = False
self.movementRectangles = []
for position in self.legalMovementPattern:
realPosition = [x + y for x, y in zip(self.position, [coordinate * 16 for coordinate in position])]
self.movementRectangles.append(
Rectangle(self.game, realPosition, self.size, [0, 255, 0], 1)
)
def finishTurn(self):
self.finishedTurn = True
self.changeColor([64, 64, 64])
def unfinishTurn(self):
self.finishedTurn = False
self.changeColor([136, 0, 0])
def move(self, x, y):
Rectangle.move(self, x, y)
self.finishTurn()
def draw(self, screen):
Rectangle.draw(self, screen)
if self.showMovement:
for elem in self.movementRectangles:
elem.draw(screen)
def update(self, deltaTime, events):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
pos = self.game.cursor.get_pos()
pos = [pos[0], pos[1]]
if hasattr(self.game, 'scrollableLayer'):
pos[0] = pos[0] - self.game.scrollableLayer.position[0]
pos[1] = pos[1] - self.game.scrollableLayer.position[1]
pos[0] = pos[0] - self.game.scrollableLayer.offset[0]
pos[1] = pos[1] - self.game.scrollableLayer.offset[1]
if self.showMovement:
for movementRectangle in self.movementRectangles:
if movementRectangle.rect.collidepoint(pos):
deltaX = movementRectangle.rect.left - self.rect.left
deltaY = movementRectangle.rect.top - self.rect.top
self.move(deltaX, deltaY)
self.showMovement = False
elif self.rect.collidepoint(pos) and not self.finishedTurn:
unitPosition = (self.rect.left, self.rect.top)
self.movementRectangles = []
self.generateLegalMovementPattern()
for position in self.legalMovementPattern:
realPosition = [x + y for x, y in zip(unitPosition, [coordinate * 16 for coordinate in position])]
self.movementRectangles.append(
Rectangle(self.game, realPosition, self.size, [64, 64, 64], 1)
)
self.showMovement = not self.showMovement
def checkSurrounding(self,func,x = "NIX", y = "NIX", extralist = False, multiplier = 16):
unitPosition = (self.rect.left, self.rect.top)
if x == "NIX":
x = unitPosition[0]
if y == "NIX":
y = unitPosition[1]
out = {}
if extralist:
out["T"] = func(x , y - multiplier, extralist)
out["L"] = func(x - multiplier, y , extralist)
out["B"] = func(x , y + multiplier, extralist)
out["R"] = func(x + multiplier, y , extralist)
else:
out["T"] = func(x , y - multiplier)
out["L"] = func(x - multiplier, y )
out["B"] = func(x , y + multiplier)
out["R"] = func(x + multiplier, y )
return out
def isOnPlayer(self, x, y):
return x == self.rect.left and y == self.rect.top
def isReachable(self, x, y):
unitPosition = (self.rect.left, self.rect.top)
realX = int(x / 16) - int(math.floor(unitPosition[0] / 16))
realY = int(y / 16) - int(math.floor(unitPosition[1] / 16))
return self.game.map.getTileAtCoordinate(x, y) == 0 and [realX, realY] in self.movementPattern and not [realX, realY] in self.legalMovementPattern
def generateLegalMovementPattern(self):
unitPosition = (self.rect.left, self.rect.top)
self.legalMovementPattern = []
self.checkList = [[0, 0]]
while self.checkList != []:
for position in self.checkList:
realPosition = [x + y for x, y in zip(unitPosition, [coordinate * 16 for coordinate in position])]
self.checkDict = self.checkSurrounding(self.isReachable, realPosition[0], realPosition[1])
for key in self.checkDict.keys():
if self.checkDict[key]:
if key == "T":
self.legalMovementPattern.append([position[0], position[1]-1])
self.checkList.append([position[0], position[1] - 1])
if key == "L":
self.legalMovementPattern.append([position[0] -1, position[1]])
self.checkList.append([position[0] -1, position[1]])
if key == "B":
self.legalMovementPattern.append([position[0], position[1]+1])
self.checkList.append([position[0], position[1] + 1])
if key == "R":
self.legalMovementPattern.append([position[0] +1, position[1]])
self.checkList.append([position[0] +1, position[1]])
self.checkList.remove(position)
def girlScout(self, doSiDos):
thinMints = []
shortBreadSlashTrefoils = self.getScore(0,0,doSiDos)
eternalScout = {shortBreadSlashTrefoils:[[0,0]]}
toffeeTastic = True
while toffeeTastic:
trios = eternalScout.keys()[0]
for score in eternalScout.keys():
if score < trios:
trios = score
position = eternalScout.setdefault(trios, [[0,0]])[-1]
if not position in thinMints:
caramelDeLites = self.checkSurrounding(self.getScore, position[0], position[1], doSiDos, 1)
for path in caramelDeLites.keys():
if caramelDeLites[path]:
if path == "T":
eternalScout[caramelDeLites[path]] = eternalScout[trios] + [[position[0], position[1] - 1]]
if path == "L":
eternalScout[caramelDeLites[path]] = eternalScout[trios] + [[position[0] - 1, position[1]]]
if path == "B":
eternalScout[caramelDeLites[path]] = eternalScout[trios] + [[position[0], position[1] + 1]]
if path == "R":
eternalScout[caramelDeLites[path]] = eternalScout[trios] + [[position[0] + 1, position[1]]]
del eternalScout[trios]
thinMints.append(position)
for key in eternalScout.keys():
if doSiDos in eternalScout[key]:
toffeeTastic = False
return eternalScout[key]
if eternalScout == {}:
return False
def getScore(self, x, y, destination):
if [x,y] in self.legalMovementPattern or [x,y] == [0,0]:
return abs(math.sqrt(x**2+y**2)) + abs(math.sqrt((destination[0]-x)**2+(destination[1]-y)**2))
else:
return False
def endTurn(self):
self.unfinishTurn()