forked from 1egoman/empty-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
executable file
·908 lines (598 loc) · 22.2 KB
/
world.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# os
import os
# pygame
import pygame
from pygame.locals import *
# gameobjects
from gameobjects.gametime import GameClock
# math
from math import *
# json
import json
# time
import time
# from folder
from tiles import *
import cfg_parser
import notifier as notify
import entitys
import inventory
class map(object):
def __init__(self, s, w=10, h=10):
# init vars
self.w = w
self.h = h
self.s = s
self.xo = 0
self.yo = 200
self._TILE_H = 64
self._TILE_W = 128
self.BLOCK_W = self.BLOCK_H = 5
self.SENSITIVITY = 5
self.MAX_NEGITIVE_DIG = 0
self._sun_pos = 0
self._last_sun_pos = 0
self._sun_event = False
self.BG_COLOR = (180, 180, 180)
self.WATER_LEVEL = 5
self._mx = 0
self._my = 0
self._stile = None
self.FPS = 0
self._DIAGNOSTIC = False
self.selected_tile = None
# create gametime clock
self.clock = GameClock(100)
self.clock.start()
self.time = 0
# create day/night cycles
self.day_length = 600.0 # 10 minutes
self._day_phases = 100.0 # how many times to change the light
# font
self._FONT = pygame.font.SysFont(pygame.font.get_default_font(), 18)
# world's name
self._NAME = None
# entity stuff
self._entitys = []
# set up events
self._events = []
# set up notifications
self.notify = notify.notifier(self, self.s)
# create inventory
self.inventory = inventory.inv(self.s, self)
# load images
self.load_src()
# officially create all items
inventory.create_item_dict(self)
# tools
self.tool = None
self.tools = { "shovel": (self.src.shovel, self.src.shovel_action) }
self.tool = "shovel"
self.tool_image = 0
# create map
self.flush_map()
# do player screen movement
self.do_movement()
# container for resources
class src: pass
# TILE_W Property
@property
def TILE_W(self):
return self._TILE_W
@TILE_W.setter
def TILE_W(self, v):
self._TILE_W = v
for x in xrange(self.w-1, -1, -1):
for y in xrange(0, self.h):
self.tiles[x][y].TILE_W = v
# TILE_H Property
@property
def TILE_H(self):
return self._TILE_H
@TILE_H.setter
def TILE_H(self, v):
self._TILE_H = v
for x in xrange(self.w-1, -1, -1):
for y in xrange(0, self.h):
self.tiles[x][y].TILE_H = v
# sun_pos Property
@property
def sun_pos(self):
return self._sun_pos
@sun_pos.setter
def sun_pos(self, v):
# set sun's pos
self._sun_pos = v
if self._sun_pos > 1: self._sun_pos = 0
if self._sun_pos < 0: self._sun_pos = 1
# update the whole map
for x in xrange(self.w-1, -1, -1):
for y in xrange(0, self.h):
self.tiles[x][y].update()
def load_src(self):
# selector
self.src.selector = pygame.image.load( os.path.join("src", "selector.png") ).convert_alpha()
# tools
shovel = pygame.image.load( os.path.join("src", "tools", "shovel.png") ).convert_alpha()
self.src.shovel = pygame.transform.smoothscale( shovel, (96, 96) )
self.src.shovel_action = pygame.transform.rotate(self.src.shovel, 20)
self.src.torch = pygame.image.load( os.path.join("src", "tools", "torch.png") ).convert_alpha()
# mining images
self.src.mine = []
self.src.mine.append( pygame.image.load( os.path.join("src", "mine_block", "mine4.png") ).convert_alpha() )
self.src.mine.append( pygame.image.load( os.path.join("src", "mine_block", "mine3.png") ).convert_alpha() )
self.src.mine.append( pygame.image.load( os.path.join("src", "mine_block", "mine2.png") ).convert_alpha() )
self.src.mine.append( pygame.image.load( os.path.join("src", "mine_block", "mine1.png") ).convert_alpha() )
self.src.mine.append( pygame.image.load( os.path.join("src", "mine_block", "mine0.png") ).convert_alpha() )
# tiles
self.src.sand = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "block", "sand.png") ).convert_alpha(), (self.TILE_W, self._TILE_H) )
self.src.dirt = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "block", "dirt.png") ).convert_alpha(), (self.TILE_W, self._TILE_H) )
self.src.bubbleglass = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "block", "bubbleglass.png") ).convert_alpha(), (self.TILE_W, self._TILE_H) )
# items
self.src.item_sand = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "item", "sand.png") ).convert_alpha(), (self.inventory._INV_CELL_W, self.inventory._INV_CELL_W) )
# log boundries
self.src.rlog = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "rlog.png") ).convert_alpha(), (self.TILE_W, self._TILE_H+self.TILE_H/2) )
self.src.llog = pygame.transform.flip(self.src.rlog, 1, 0)
# notifications
self.src.info_msg = pygame.transform.smoothscale(pygame.image.load( os.path.join("src", "notify", "info.png") ).convert_alpha(), (300, 150) )
# entitys
self.src.entity_guy = pygame.image.load( os.path.join("src", "entitys", "guy.png") ).convert_alpha()
self.src.sandwich = pygame.image.load( os.path.join("src", "entitys", "sandwich.png") ).convert_alpha()
self.src.fire = pygame.image.load( os.path.join("src", "entitys", "fire.png") ).convert_alpha()
def flush_map(self):
# create list of map tiles
self.tiles = []
# create empty map
for x in xrange(0, self.w):
self.tiles.append([])
for y in xrange(0, self.h):
# create new tile, set variables
self.tiles[-1].append( sand( self, x, y, (255,255,255), self.s ) )
self.tiles[-1][-1].TILE_H = self.TILE_H
self.tiles[-1][-1].TILE_W = self.TILE_W
# create the tiles 'block'
# a block is the tiles around the current tile
# blocks are BLOCK_W x BLOCK_H
# [ ][ ][ ][ ][ ]
# [ ][1][1][1][ ]
# [ ][1][X][1][ ]
# [ ][1][1][1][ ]
# [ ][ ][ ][ ][ ]
# create blocks
for x in xrange(0, self.w):
self.tiles.append([])
for y in xrange(0, self.h):
# create block
self.tiles[x][y].BLOCK = self.create_block(x, y)
# lastly, update the tile
self.tiles[x][y].update()
# flush inventory and
# give the player a (starting) sandwich
self.inventory.slots = [inventory.item(inventory.items["sandwich"], 1)]
# flush, but then randomize heights
def flush_and_randomize_map(self):
# create list of map tiles
self.tiles = []
# create empty map
for x in xrange(0, self.w):
self.tiles.append([])
for y in xrange(0, self.h):
# create new tile, set variables
self.tiles[-1].append( tile( self, x, y, (255,255,255), self.s ) )
self.tiles[-1][-1].TILE_H = self.TILE_H
self.tiles[-1][-1].TILE_W = self.TILE_W
self.tiles[-1][-1].h = generate_random_tile_height()
# create the tiles 'block'
# a block is the tiles around the current tile
# blocks are BLOCK_W x BLOCK_H
# [ ][ ][ ][ ][ ]
# [ ][1][1][1][ ]
# [ ][1][X][1][ ]
# [ ][1][1][1][ ]
# [ ][ ][ ][ ][ ]
# create blocks
for x in xrange(0, self.w):
self.tiles.append([])
for y in xrange(0, self.h):
# create block
self.tiles[x][y].BLOCK = self.create_block(x, y)
# lastly, update the tile
self.tiles[x][y].update()
# delete events that are tagged with description d
def flush_events(self, d=""):
for c,e in enumerate(self._events):
if e[3] == d:
a = list(e)
a[0] = -1
self._events[c] = tuple(a)
def increment_time(self, t=0.1):
# increments time by 1
self._last_sun_pos = self.sun_pos
self.sun_pos += t
self._sun_event = False
def clear_map(self, color=None):
# sets all tiles to color, and resets selection
# create empty map
for x in xrange(0, self.w):
for y in xrange(0, self.h):
# create new tile, set variables
if color: self.tiles[x][y].color = color
self.tiles[x][y].selected = False
# convert tile coords to screen coords
def to_screen(self, x, y):
sx = (y+x)*(self.TILE_W/2)
sy = (y-x)*(self.TILE_H/2)
return sx+self.xo, sy+self.yo
# convert screen coords to 2d tile coords
def to_2d_tile(self, x, y, f=False):
x, y = (x-self.xo)*1.0, (y-self.yo)*1.0
# now do math
tx = (y - x/2)/self.TILE_H
ty = (y + x/2)/self.TILE_H
if f:
return -tx, ty
else:
return int( floor(-tx) ), int( floor(ty) )
# version of to_2d_tile that is more precise and handles 3d
def to_3d_tile(self, mx, my):
# create some vars
measures = []
# start with a 2d comparison
x, y = self.to_2d_tile(mx, my+self.TILE_H/8)
#x, y = self.to_2d_tile(mx, my-self.TILE_H)
# round these inputs first
x = int(self.SENSITIVITY * round(float(x)/self.SENSITIVITY))
y = int(self.SENSITIVITY/2 * round(float(y)/(self.SENSITIVITY/2)))
# filter values
if x < 0: x = 0
if y < 0: y = 0
if x > self.w-1: x = self.w-1
if y > self.h-1: y = self.h-1
# look through all tiles in block around our tile
tile_block = self.tiles[x][y].BLOCK
for blk in tile_block:
# convert tile coords to screen coords
sx = blk.centerx
sy = blk.centery
# preform a nearest-neighbor analysis
dx = sx-mx
dy = sy-my
distance = sqrt( dx**2 + dy**2 )
measures.append(distance)
# get the 3 lowest numbers in the list, (3 closest tiles)
closest = min(measures)
index1 = measures.index( closest )
measures.remove(closest)
closest = min(measures)
index2 = measures.index( closest )
measures.remove(closest)
closest = min(measures)
index3 = measures.index( closest )
# now, pick the tile that is the highest
# 1 is highest
if tile_block[index1].h > tile_block[index2].h and tile_block[index1].h > tile_block[index3].h:
return tile_block[index1].x, tile_block[index1].y
# 2 is highest
elif tile_block[index2].h > tile_block[index1].h and tile_block[index2].h > tile_block[index3].h:
return tile_block[index2].x, tile_block[index2].y
# 3 is highest
elif tile_block[index3].h > tile_block[index2].h and tile_block[index3].h > tile_block[index1].h:
return tile_block[index3].x, tile_block[index3].y
# all == or something else
else:
# just return 1 in this case
return tile_block[index1].x, tile_block[index1].y
# save the map to file
def save_map(self, n=None, notify=True):
if n:
self._NAME = n
else:
n = self._NAME
# create world folder if needed
p = os.path.join("saves", n)
if not os.path.exists(p):
os.mkdir(p)
# construct structure to save
t = self.tiles[:]
data_struct = {
"map": t,
"width": self.w,
"height": self.h,
"sun_pos": self.sun_pos,
"diagnostics": self._DIAGNOSTIC,
"inventory": self.inventory.slots
}
# parse data
ps = cfg_parser.parse(data_struct)
# save it
# with open(os.path.join(p, "main.pkl"), "w") as f:
with open(os.path.join(p, "main.pkl"), "w") as f:
f.write(ps)
if notify:
self.notify.msg("Saved", "Map '"+n+"' has been saved.")
print "saved"
def load_map(self, n="world"):
self._NAME = n
self.load_real_map(n)
# give program time to process
self.render() # time.sleep(0.1)
self.save_map(n, notify=False)
# give program time to process
self.render() # time.sleep(0.1)
self.load_real_map(n)
self.notify.msg("Load", "Map '"+n+"' has been loaded.")
# save the map to file
def load_real_map(self, n):
p = os.path.join("saves", n)
if not os.path.exists(p): return
with open(os.path.join(p, "main.pkl"), "r") as f:
d = f.read()
# load map
data_struct = cfg_parser.load(self, self.s, d)
self.tiles = data_struct['map']
# create blocks
for x in xrange(0, self.w):
self.tiles.append([])
for y in xrange(0, self.h):
# create block
self.tiles[x][y].BLOCK = self.create_block(x, y)
# lastly, update the tile
self.tiles[x][y].update()
self.w = data_struct['width']
self.h = data_struct['height']
self.sun_pos = data_struct['sun_pos']
self._DIAGNOSTIC = data_struct['diagnostics']
if data_struct.has_key("inventory"):
self.inventory.slots = data_struct['inventory']
else:
self.inventory.slots = []
print "loaded"
def create_block(self, x, y):
# create a block for the tile, being BLOCK_W wide and BLOCK_H high, centered on (x, y)
block = []
# get block half points
Bx = self.BLOCK_W/2
By = self.BLOCK_H/2
# loop
for i in xrange(x-Bx, x+Bx+1):
for j in xrange(y-By, y+By+1):
# check bounds
if i < 0 or j < 0: continue
if i > self.w-1 or j > self.h-1: continue
if i == x and j == y: continue
# add to block list
tile = self.tiles[i][j]
block.append( tile )
return block
def render(self):
# make sure that the selected tile exists
if self.selected_tile and self.selected_tile[0] < 0: self.selected_tile = ( 0, self.selected_tile[1] )
if self.selected_tile and self.selected_tile[0] > self.w-1: self.selected_tile = ( self.w-1, self.selected_tile[1] )
if self.selected_tile and self.selected_tile[1] < 0: self.selected_tile = ( self.selected_tile[0], 0 )
if self.selected_tile and self.selected_tile[1] > self.h-1: self.wselected_tile = ( self.selected_tile[0], self.h-1 )
# update game clock
for g in self.clock.update():
self.time = g[1]
# check scheduled times
for e in self._events:
# if event should be run, run it
if e[0] > 0 and e[0] <= g[1]:
e[1](*e[2])
# remove from list
self._events.remove(e)
# render the log border
aw = self.w/2*self.TILE_W
ah = self.h/2*self.TILE_H
# logs on right
for x in xrange(0, self.w):
# draw log
self.s.blit(self.src.rlog, (\
self.xo+aw+(x*self.TILE_W/2),
self.yo-ah-self.TILE_H/2+(x*self.TILE_H/2)
))
# fixes a display bug (covers up edge of log so it doesnt show)
if self.tiles[self.w-1][self.w-1].h == 1:
fx = self.xo+aw+(self.w*self.TILE_W/2)+5
fy = self.yo-ah-self.TILE_H/2+(x*self.TILE_H/2)+self.TILE_H-1
pygame.draw.polygon(self.s, self.BG_COLOR, [ [fx, fy],\
[fx, fy+self.TILE_H/2+5], [fx-64, fy+32-1] ])
# logs on left
for y in xrange(0, self.h):
# draw log
self.s.blit(self.src.llog, (\
self.xo+aw-self.TILE_W-(y*self.TILE_W/2),
self.yo-ah-self.TILE_H/2+(y*self.TILE_H/2)
))
# fixes a display bug (covers up edge of log so it doesnt show)
if self.tiles[0][0].h == 1:
fx = self.xo+aw-self.TILE_W/2-(y*self.TILE_W/2)-5
fy = self.yo-ah+self.TILE_H*2+(y*self.TILE_H/2)-self.TILE_H-1
pygame.draw.polygon(self.s, self.BG_COLOR, [ [fx, fy],\
[fx, fy-self.TILE_H/2], [fx+64, fy] ])
# render water
# wx, wy = self.xo+self.w/2*self.TILE_W, self.yo+self.h/2*self.TILE_H
# pygame.draw.polygon(self.s, (51, 102, 153), [
# (wx, wy-self.WATER_LEVEL),
# (self.xo, self.yo-self.WATER_LEVEL),
# (wx, wy-self.h*self.TILE_H-self.WATER_LEVEL),
# (wx+self.w/2*self.TILE_W, self.yo-self.WATER_LEVEL)
# ])
# render all tiles
for x in xrange(self.w-1, -1, -1):
for y in xrange(0, self.h):
# check tile exists, and that is subclasses tile
if self.tiles[x][y] and ( isinstance(self.tiles[x][y], tile) or issubclass(self.tiles[x][y], tile) ):
# render it
t = self.tiles[x][y]
t.render()
# render entitys
for e in self._entitys:
e.update()
e.render()
# render any notifications
self.notify.render()
# render inventory
self.inventory.render()
# render current tool
if self.tool:
self.s.blit(self.tools[self.tool][self.tool_image], (self._mx, self._my))
# render diagnostic info
if self._DIAGNOSTIC: self.render_diagnostic()
# do daylight stuff
# update the time
if not self._sun_event and self._last_sun_pos <= self.sun_pos:
# schedule an update for later
self.schedule_time(self.time+self.day_length/self._day_phases, self.increment_time, [1/self._day_phases], "time_shift")
self._sun_event = True
def render_diagnostic(self):
# FPS
s = "FPS: "+str(round(self.FPS))
rndr = self._FONT.render(s, True, (255,255,255))
self.s.blit(rndr, (10, 10))
def do_movement(self):
keys = pygame.key.get_pressed()
speed = 5
if keys[K_w]: self.yo += speed
if keys[K_s]: self.yo -= speed
if keys[K_d]: self.xo -= speed
if keys[K_a]: self.xo += speed
# do mouse motion
# self.send_motion(*pygame.mouse.get_pos())
# schedule next iteration
self.schedule_time(self.time+0.01, self.do_movement, [], "move_screen_for_player")
def send_motion(self, x, y):
# clear the map
self.clear_map()
# get mx and my
self._mx, self._my = x, y
# get 3d click
x, y = self.to_3d_tile(self._mx, self._my)
if x < 0: x = 0
if y < 0: y = 0
# color that tile's block
# if self._DIAGNOSTIC:
# for b in self.tiles[x][y].BLOCK:
# b.color = (255, 0, 0)
# self.tiles[x][y].color = (0, 255, 0)
self.tiles[x][y].selected = True
self.selected_tile = x, y
# function called to update a tile's mining status
def set_tile_mine(self, x, y, state, mouse=1):
# get tile
tile = self.tiles[x][y]
# update tile
tile.update()
# send an update to tiles block
tile.update_block()
# check mouse
# FIXME: OLD UPDATES ARENT DELETED
if mouse and not pygame.mouse.get_pressed()[0] and self.to_3d_tile(self._mx, self._my) == (x, y):
self.tiles[x][y]._mine_state = None
return
elif state == 5:
# finish the mining
if self.tool: self.tool_image = 0
tile._mine_state = None
tile.h -= 1
# add into inventory
self.inventory.add_item( inventory.item(tile.tiles[-1], 1) )
# edit tile list
tile.tiles = tile.tiles[:-1]
# update tile and tiles block
tile.update()
tile.update_block()
else:
# set mining state
tile._mine_state = state
# move tool
if self.tool:
if state%2:
self.tool_image = 1
else:
self.tool_image = 0
# schedule an event for later
def schedule_time(self, t, event, a=[], desc=""): self._events.append( (t, event, a, desc) )
# mine a tile at x, y
def mine_tile(self, x, y):
# get our tile
t = self.tiles[x][y]
h = self.tiles[x][y].hardness/5.0
# schedule time for each mining change
for mine_level in xrange(0, 6):
self.schedule_time(self.time+mine_level*h, self.set_tile_mine, (x, y, mine_level), "mine_block")
# send a mousebutton event
def send_mousebutton(self, event, a):
# get mx and my
mx, my = event.pos
# first, check to see if the click occered within the inventory
if mx > self.inventory.x and mx < self.inventory.x+self.inventory.w and \
my > self.inventory.y and my < self.inventory.y+self.inventory.h and event.button == 1:
self.inventory.click(event)
return
# scroll
if event.button == 5 and self.inventory.ACTIVE_CELL[0]+1 < self.inventory._INV_W:
self.inventory.ACTIVE_CELL = (self.inventory.ACTIVE_CELL[0]+1, self.inventory.ACTIVE_CELL[1])
if event.button == 4 and self.inventory.ACTIVE_CELL[0]-1 >= 0:
self.inventory.ACTIVE_CELL = (self.inventory.ACTIVE_CELL[0]-1, self.inventory.ACTIVE_CELL[1])
# get 3d click
x, y = self.selected_tile # self.to_3d_tile(mx, my)
# round off
if x < 0: x = 0
if y < 0: y = 0
if a == "down" and event.button == 1: # left click
# check entitys (click detection)
Tx, Ty = self.to_screen(x, y)
for e in self._entitys:
ex, ey = self.to_screen(e.x, e.y)
if Tx >= ex and Tx <= ex+e.w and Ty >= ey and Ty <= ey+e.h:
if e.click(): return
# check clicking of the border
# aw = self.w/2*self.TILE_W
# ah = self.h/2*self.TILE_H
# for y in xrange(0, self.h):
# if mx > self.xo+aw-self.TILE_W-(y*self.TILE_W/2) and my > self.yo-ah-self.TILE_H/2+(y*self.TILE_H/2) \
# and mx < self.xo+aw-self.TILE_W-(y*self.TILE_W/2)+self.src.llog.get_width() and my < self.yo-ah-self.TILE_H/2+(y*self.TILE_H/2)+self.src.llog.get_height() and \
# self.tiles[x][y].tiles[-1] == []:
# print 123
# otherwise, mine that tile
if self.tiles[x][y].h <= self.MAX_NEGITIVE_DIG: return
if not self.inventory.room_in_inventory(): return
self.mine_tile(x, y)
elif a == "down" and event.button == 3: # right click
# error checking
if self.tiles[x][y].h > (self.BLOCK_H+self.BLOCK_H)/2: return
# get items for later
g = self.inventory.get_selected_item()
if not g: return
m = inventory.items_args[ inventory.get_name_from_id(g.id) ]
# PLACE BLOCK
# if we have the resources, and the tile can be placed, place it
if g and m.has_key("shape") and m["shape"] == "block":
# update tile (increase height)
self.tiles[x][y].h += 1
self.tiles[x][y].tiles.append(g.id)
self.inventory.remove_item( inventory.item(g.id, 1) )
# OTHER
# if not, but the tile is flat, do something else
elif g and m.has_key("shape") and m["shape"] == "flat":
# spawn entity?
if m.has_key("place_entity") and issubclass(m["place_entity"], entitys.entity):
self.spawn(x, y, m["place_entity"])
if m.has_key("remove_on_place_entity") and m["remove_on_place_entity"]:
self.inventory.remove_item( inventory.item(g.id, 1) )
# send an update to tiles block
self.tiles[x][y].update()
self.tiles[x][y].update_block()
elif a == "up":
# reset tool
self.flush_events("mine_block")
self.tiles[x][y]._mine_state = None
if self.tool: self.tool_image = 0
# center the map on the window
def center_map(self, (w, h)):
self.xo = w/2-(self.w*self.TILE_W/2)
self.yo = h/2
# spawn entitys
def spawn(self, x=0, y=0, t=entitys.mob):
e = t(self.s, self, x, y)
self._entitys.append(e)