00f730d87206ca51491b09c3a35f81804b64a449
1 """Square tile based engine."""
7 """Based on [[vid]] -- see for reference."""
9 sw
,sh
= s
.get_width(),s
.get_height()
10 self
.view
.w
,self
.view
.h
= sw
,sh
13 tw
,th
= tiles
[0].image
.get_width(),tiles
[0].image
.get_height()
16 if self
.bounds
!= None: self
.view
.clamp_ip(self
.bounds
)
18 ox
,oy
= self
.view
.x
,self
.view
.y
22 sprites
= self
.sprites
25 yy
= - (self
.view
.y
%th
)
27 if (oy
+sh
)%th
: my
+= 1
30 for y
in xrange(oy
/th
,my
):
35 xx
= - (self
.view
.x
%tw
)
37 #if (ox+sh)%tw: mx += 1
38 for x
in xrange(ox
/tw
,mx
+1):
40 blit(tiles
[brow
[x
]].image
,(xx
,yy
))
41 blit(tiles
[trow
[x
]].image
,(xx
,yy
))
46 for y
in xrange(oy
/th
,my
):
50 xx
= - (self
.view
.x
%tw
)
52 #if (ox+sh)%tw: mx += 1
53 for x
in xrange(ox
/tw
,mx
+1):
55 blit(tiles
[trow
[x
]].image
,(xx
,yy
))
61 s
.irect
.x
= s
.rect
.x
-s
.shape
.x
62 s
.irect
.y
= s
.rect
.y
-s
.shape
.y
63 blit(s
.image
,(s
.irect
.x
-ox
,s
.irect
.y
-oy
))
65 s
._irect
= Rect(s
.irect
)
66 #s._rect = Rect(s.rect)
69 self
._view
= pygame
.Rect(self
.view
)
70 return [Rect(0,0,sw
,sh
)]
73 sw
,sh
= s
.get_width(),s
.get_height()
74 self
.view
.w
,self
.view
.h
= sw
,sh
76 if self
.bounds
!= None: self
.view
.clamp_ip(self
.bounds
)
77 if self
.view
.x
!= self
._view
.x
or self
.view
.y
!= self
._view
.y
:
80 ox
,oy
= self
.view
.x
,self
.view
.y
81 sw
,sh
= s
.get_width(),s
.get_height()
87 tw
,th
= tiles
[0].image
.get_width(),tiles
[0].image
.get_height()
88 sprites
= self
.sprites
93 #mark places where sprites have moved, or been removed
95 ss
= self
.sprites
.removed
96 self
.sprites
.removed
= []
99 #figure out what has been updated.
100 s
.irect
.x
= s
.rect
.x
-s
.shape
.x
101 s
.irect
.y
= s
.rect
.y
-s
.shape
.y
102 if (s
.irect
.x
!= s
._irect
.x
or s
.irect
.y
!= s
._irect
.y
103 or s
.image
!= s
._image
):
104 #w,h can be skipped, image covers that...
109 yy
= min(h
,r
.bottom
/th
+1)
112 xx
= min(w
,r
.right
/tw
+1)
114 if alayer
[y
][x
] == 0:
115 self
.updates
.append((x
,y
))
122 yy
= min(h
,r
.bottom
/th
+1)
125 xx
= min(w
,r
.right
/tw
+1)
129 self
.updates
.append((x
,y
))
134 #mark sprites that are not being updated that need to be updated because
135 #they are being overwritte by sprites / tiles
140 yy
= min(h
,r
.bottom
/th
+1)
143 xx
= min(w
,r
.right
/tw
+1)
151 for u
in self
.updates
:
153 xx
,yy
=x
*tw
-ox
,y
*th
-oy
154 if alayer
[y
][x
] == 1:
155 if blayer
!= None: blit(tiles
[blayer
[y
][x
]].image
,(xx
,yy
))
156 blit(tiles
[tlayer
[y
][x
]].image
,(xx
,yy
))
158 us
.append(Rect(xx
,yy
,tw
,th
))
162 blit(s
.image
,(s
.irect
.x
-ox
, s
.irect
.y
-oy
))
164 s
._irect
= Rect(s
.irect
)
170 def view_to_tile(self
,pos
):
173 tw
,th
= tiles
[0].image
.get_width(),tiles
[0].image
.get_height()
176 def tile_to_view(self
,pos
):
179 tw
,th
= tiles
[0].image
.get_width(),tiles
[0].image
.get_height()
184 def screen_to_tile(self
,pos
):
186 x
,y
= x
+self
.view
.x
,y
+self
.view
.y
187 return self
.view_to_tile((x
,y
))
189 def tile_to_screen(self
,pos
):
191 x
,y
= self
.tile_to_view(pos
)
192 x
,y
= x
- self
.view
.x
, y
- self
.view
.y
195 # vim: set filetype=python sts=4 sw=4 noet si :