-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.py
More file actions
152 lines (116 loc) · 4.25 KB
/
resources.py
File metadata and controls
152 lines (116 loc) · 4.25 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
from PIL import Image
from abc import ABC, abstractmethod
class ResourceIcon:
labelImgSpacing = 5
ImgSpacing = 28
def __init__(self,tile, app):
self.tile = tile
self.app = app
self.imgSize = self.tile.getResources()[0].getType().getImage().size
app.resourceIcons.add(self)
def getResources(self):
return self.tile.resources
def getImageLocs(self):
result = []
resources = self.getResources()
numOfResources = len(resources)
tileWidth, tileHeight = self.tile.getSize()
location = self.tile.getRelativeLoc(self.tile.row,self.tile.col, self.app.map)
if location != None:
relativeRow, relativeCol = location
else: return None
centerX,y = self.tile.mapToScreenCords((relativeRow, relativeCol), self.tile.getSize(),
(self.app.width, self.app.height), self.app.map.getRenderedMap(),
self.app.mapRenderer)
centerX = centerX + tileWidth//2 - self.imgSize[0]//2
y = y + tileHeight//2 - 1*self.imgSize[1]//4
for i in range(numOfResources):
correction = i * ResourceIcon.ImgSpacing - (ResourceIcon.ImgSpacing * (numOfResources))//4
result.append((centerX + correction, y))
return result
def getLabelLocs(self, imageLocs):
result = []
for i in range(len(imageLocs)):
x = imageLocs[i][0] - ResourceIcon.labelImgSpacing
y = imageLocs[i][1] + self.imgSize[i]//2
result.append((x,y))
return result
def deleteIcon(self):
self.app.resourceIcons.remove(self)
self.tile.resoruceIcon = None
class ResourceType(ABC):
@abstractmethod
def getName(self):
pass
@abstractmethod
def getImagePath(self):
pass
@abstractmethod
def getImage(self):
pass
class Production(ResourceType):
def __init__(self):
self.name = 'Production'
self.imagePath = 'sprites/small_gear.png'
self.image = Image.open(self.imagePath)
def __repr__(self):
return self.name
def __hash__(self):
return hash(str(self))
def __eq__(self,other):
return isinstance(other, Production)
def getName(self):
return self.name
def getImagePath(self):
return self.imagePath
def getImage(self):
return self.image
class Food(ResourceType):
def __init__(self):
self.name = 'Food'
self.imagePath = 'sprites/small_apple.png'
self.image = Image.open(self.imagePath)
def __repr__(self):
return self.name
def __hash__(self):
return hash(str(self))
def __eq__(self,other):
return isinstance(other, Food)
def getName(self):
return self.name
def getImagePath(self):
return self.imagePath
def getImage(self):
return self.image
class ResourceStack:
ResourceTypes = {'production' : Production(), 'food' : Food()}
def __init__(self, type, amount):
self.type = type
self.amount = amount
def __repr__(self):
return f'({self.type.getName()}, {self.amount})'
def __hash__(self):
return hash(str(self))
def __eq__(self,other):
return isinstance(other, ResourceStack) and self.getType() == other.getType() and self.getAmount == other.getAmount
def getType(self):
return self.type
def getAmount(self):
return self.amount
def setAmount(self, amount):
self.amount = amount
def getResource(resourceStr):
try:
return ResourceStack.ResourceTypes[resourceStr]
except:
if resourceStr not in ResourceStack.ResourceTypes: print("NOT A VALID RESOURCE")
assert(False == True)
@staticmethod
def addStackToCivilization(stack, civilization):
type = stack.getType()
_yield = civilization.yieldsByType[type]
_yield += stack.getAmount()
civilization.yieldsByType[type] = _yield
@staticmethod
def addStackToSettlement(stack, settlement):
ResourceStack.addStackToCivilization(stack, settlement)