forked from 1egoman/empty-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiles.py
executable file
·265 lines (180 loc) · 6.06 KB
/
tiles.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# pygame
import pygame
from pygame.locals import *
# math
from math import *
# random
import random
import inventory
class tile(object):
TILE_H = 32
TILE_W = 64
BLOCK = None
OUTLINE_COLOR = (0, 0, 0)
SELECTION_COLOR = (120, 120, 120)
def __init__(self, p, x, y, color, s):
# set color
self.x = x
self.parent = p
self.y = y
self.color = color
self.s = s
self.h = 0
self.centerx = 0
self.centery = 0
self.pts = []
self.hardness = 0.5
self._mine_state = None
self._SHADE = 1
self._flat = False
self.selected = False
self.tiles = []
# calculates the amout to shade a tile
def calculate_shade(self, s):
return 1 - abs( self.parent.sun_pos - s )
# update tiles in block around our tile
def update_block(self):
# loop through block
for t in self.BLOCK:
t.update()
def update(self):
# update tile
# first, deal with the tiles list
if not hasattr(self, "tiles"):
self.tiles = []
elif len(self.tiles)-1 > self.h:
self.tiles = self.tiles[:-self.h]
elif len(self.tiles)-1 < self.h:
while len(self.tiles)-1 < self.h:
self.tiles.append( inventory.items["sand"] )
# /\ <------ 1st
# / \
# 4th ____\ / \ /_____ 2nd
# / \ / \
# \ /
# \/ <----- 3rd
# do the sloping of tiles
self.ax=self.bx=self.cx=self.dx=0
self.ay=self.by=self.cy=self.dy=0
w=s=e=None
h2 = self.TILE_H/2
# get the tiles around our tile (from block)
for t in self.BLOCK:
# west
if not w and t.x == self.x and t.y == self.y-1: w = t
# south
if not s and t.x == self.x+1 and t.y == self.y-1: s = t
# east
if not e and t.x == self.x+1 and t.y == self.y: e = t
# reset shading
if (not self.ay and not self.ax) and (not self.by and not self.bx) and \
(not self.cy and not self.cx) and (not self.dy and not self.dx):
if self.h:
self._SHADE = 1
else:
self._SHADE = .9
# slope northern sides
if s and s.h > self.h:
self.ay -= h2*(s.h - self.h)
self._SHADE = self.calculate_shade(0.5)
# slope western sides
elif w and w.h > self.h:
self.ay -= h2*(w.h - self.h)
self.dy -= h2*(w.h - self.h)
self._SHADE = self.calculate_shade(0.8)
# slope eastern sides
elif e and e.h > self.h:
self.ay -= h2*(e.h - self.h)
self.by -= h2*(e.h - self.h)
self._SHADE = self.calculate_shade(0.2)
# slope northern, modified
if s and s.h > self.h and w and w.h > self.h and e and e.h > self.h:
self.by -= h2*(e.h - self.h)
self.dy -= h2*(w.h - self.h)
# slope east and west, modified
elif w and w.h > self.h and e and e.h > self.h:
self.by -= h2*(e.h - self.h)
# slope east and west, modified
elif s and s.h > self.h and e and e.h > self.h:
self.by -= h2*(e.h - self.h)
# another slope east and west
elif w and w.h > self.h and s and s.h > self.h:
self.dy -= h2*(w.h - self.h)
# shading logic
if not self._SHADE:
if w and not w._SHADE:
self._SHADE == w._SHADE
elif e and not e._SHADE:
self._SHADE == e._SHADE
def render(self):
# draw tile
# convert to screen coords
sx, sy = self.parent.to_screen(self.x, self.y)
# calculate a list of points to render
h = (-self.TILE_H/2)*self.h
self.pts = [
# topmost point
(self.ax+sx+self.TILE_W/2, self.ay+sy+h),
# rightmost point
(self.bx+sx+self.TILE_W, self.by+sy+self.TILE_H/2+h),
# bottommost point
(self.cx+sx+self.TILE_W/2, self.cy+sy+self.TILE_H+h),
# leftmost point
(self.dx+sx, self.dy+sy+self.TILE_H/2+h)
]
# calculate the center of the tile
th = self.pts[0][1] - self.pts[2][1]
self.centerx = self.pts[3][0]+self.TILE_W/2+self.bx/2
self.centery = self.pts[0][1]+self.TILE_H/2-self.ay/2
# check shape
if (not self.ay and not self.ax) and (not self.by and not self.bx) and \
(not self.cy and not self.cx) and (not self.dy and not self.dx):
self._flat = True
else:
self._flat = False
# draw tile
c = (self.color[0]*self._SHADE, self.color[1]*self._SHADE, self.color[2]*self._SHADE)
if not self._flat: pygame.draw.polygon(self.s, c, self.pts)
self.draw_on_block()
# draw outline
if self.OUTLINE_COLOR:
pygame.draw.aalines(self.s, self.OUTLINE_COLOR, 1, self.pts, 4)
# draw block's mining status
if self._mine_state != None: # yes this is nessisary
self.s.blit(self.parent.src.mine[self._mine_state], (self.pts[3][0], self.pts[0][1]))
# draw selection
if self.parent.selected_tile == (self.x, self.y):
self.s.blit(self.parent.src.selector, (self.pts[3][0], self.pts[0][1]))
# debug
# if self.x == 2 and self.y == 2:
# self.s.set_at((self.centerx, self.centery), (0, 0, 255))
# label tile
if self.parent._DIAGNOSTIC:
r = self.parent._FONT.render( str(self.x)+","+str(self.y), True, (0,0,0) )
self.s.blit(r, (sx+30,sy+10+h))
def draw_on_block(self): pass
def generate_random_tile_height():
a = random.randint(0, 1)
return a
# basic sand tile
class sand(tile):
def __init__(self, *args):
super(sand, self).__init__(*args)
# set sand color
self.color = (217, 189, 167)
self.h = 1
def render(self, *args):
# change color of side walls
if len(self.tiles) and self.tiles[-1]:
n = inventory.get_name_from_id( self.tiles[-1] )
self.color = inventory.items_args[n]["color"]
# acctully render
super(sand, self).render(*args)
def draw_on_block(self):
if self._flat:
# rnder image on top of tile
if self.h > self.parent.MAX_NEGITIVE_DIG and len(self.tiles) and self.tiles[-1] in inventory.items.values():
n = inventory.get_name_from_id( self.tiles[-1] )
self.s.blit(inventory.items_img[n], (self.pts[3][0], self.pts[0][1]))
else:
self.s.blit(self.parent.src.dirt, (self.pts[3][0], self.pts[0][1]))