2d4156d6466764df359ec0a2177940f0f65eed22
1 """Hexagonal tile engine.
3 <p>Note -- this engine is not finished. Sprites are not supported. It
4 can still be useful for using the level editor, and for rendering hex
5 terrains, however. If you are able to update it and use it in a real game,
6 help would be greatly appreciated!</p>
8 <p>please note that this file is alpha, and is subject to modification in
9 future versions of pgu!</p>
12 print 'pgu.hexvid','This module is alpha, and is subject to change.'
19 """Create an hex vid engine. See [[vid]]"""
20 def update(self
,screen
):
21 return self
.paint(screen
)
23 def paint(self
,screen
):
24 sw
,sh
= screen
.get_width(),screen
.get_height()
25 self
.view
.w
,self
.view
.h
= sw
,sh
30 w
,h
= len(tlayer
[0]),len(tlayer
)
32 #iso_w,iso_h,iso_z,tile_w,tile_h,base_w,base_h = self.iso_w,self.iso_h,self.iso_z,self.tile_w,self.tile_h,self.base_w,self.base_h
34 tile_w
,tile_h
= self
.tile_w
,self
.tile_h
35 tile_w2
,tile_h2
= tile_w
/2,tile_h
/2
38 adj
= self
.adj
= pygame
.Rect(-self
.view
.x
,-self
.view
.y
,0,0)
40 w
,h
= len(tlayer
[0]),len(tlayer
)
44 if self
.bounds
== None:
45 tmp
,y1
= self
.tile_to_view((0,0))
46 x1
,tmp
= self
.tile_to_view((0,h
+1))
47 tmp
,y2
= self
.tile_to_view((w
+1,h
+1))
48 x2
,tmp
= self
.tile_to_view((w
+1,0))
49 self
.bounds
= pygame
.Rect(x1
,y1
,x2
-x1
,y2
-y1
)
53 if self
.bounds
!= None: self
.view
.clamp_ip(self
.bounds
)
55 ox
,oy
= self
.screen_to_tile((0,0))
56 sx
,sy
= self
.tile_to_view((ox
,oy
))
57 dx
,dy
= sx
- self
.view
.x
,sy
- self
.view
.y
61 tile_wi
= tile_w
+ tile_w
/2
66 for i2
in xrange(-bot
,self
.view
.h
/tile_h2
+bot
*3): #NOTE: 3 seems a bit much, but it works.
67 tx
,ty
= ox
+ i2
/2 + i2
%2,oy
+ i2
/2
68 x
,y
= (i2
%2)*tile_wi2
+ dx
,i2
*tile_h2
+ dy
70 #to adjust for the -1 in i1
71 x
,tx
,ty
= x
-tile_wi
,tx
-1,ty
+1
74 for i1
in xrange(-1,self
.view
.w
/tile_wi
+1):
75 if ty
>= 0 and ty
< h
and tx
>= 0 and tx
< w
:
80 if t
!= None and t
.image
!= None:
81 screen
.blit(t
.image
,(x
,y
))
85 if t
!= None and t
.image
!= None:
86 screen
.blit(t
.image
,(x
,y
))
93 return [pygame
.Rect(0,0,screen
.get_width(),screen
.get_height())]
95 def view_to_tile(self
,pos
):
97 #x = x + (self.tile_w*1/2)
99 x
,y
= int(x
*4/(self
.tile_w
*3)), y
*2/self
.tile_h
104 def tile_to_view(self
,pos
):
108 nx
,ny
= int(nx
*(self
.tile_w
*3)/4), ny
*self
.tile_h
/2
110 #nx = nx - (self.tile_w*1/2)
113 def screen_to_tile(self
,pos
): #NOTE HACK : not sure if the 3/8 is right or not, but it is pretty close...
114 pos
= pos
[0]+self
.view
.x
+ self
.tile_w
*3/8,pos
[1]+self
.view
.y
115 pos
= self
.view_to_tile(pos
)
118 def tile_to_screen(self
,pos
):
119 pos
= self
.tile_to_view(pos
)
120 pos
= pos
[0]-self
.view
.x
,pos
[1]-self
.view
.y
124 def tga_load_tiles(self
,fname
,size
,tdata
={}):
125 Vid
.tga_load_tiles(self
,fname
,size
,tdata
)
127 self
.tile_w
,self
.tile_h
= size