>Hc@s[dZddkZddklZdddYZedjoeZeiGHndS(sHandles a tile system with fake gravity. Usage: use add() to add either a soldier or a block to the grid use tick() to step through an interation of the grid use adj() to see if two objects are adjacent to each other by means of grid (not pixel) distanceiN(tareatGridcBs\eZdddddZdZdZdZdZdZdZd Z RS( iiXcCstii|_tii|_||f|_||f|_t|||_t|||_ g|_ xWt ||D]E}|i i gx,t ||D]}|i |i dqWqWg|_t|_dS(sgw, h = width, height of a single grid tw, th = width, height of the entire screen encompassed by gridN(tpygametspritetOrderedUpdatestblockstsoldierstgridsizet totalsizetinttrowstcolstgridtrangetappendtNonetshapestFalsetlose(tselftwthttwtthtitj((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyt__init__ s  cCsx|iD]}t|_q W|iixJ|iD]?}x6tii||ittii D]}|i q_Wq4W|iidS(N( RRtcollidedRtupdateRRt spritecollideRt collide_masktasplode(Rtshapetblocktsoldier((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pytticks    %cCs9|\}}|i\}}t||t||fS(sConverts a pixel coordinate to a grid coordinate, and finds the object at that coordinte. Coordinate is returned. Grid coordinates are (x, y).(RR (RtpointtxtyRR((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyttogrid,s cCs-|\}}|i\}}||||fS(seReturns the top-left pixel of the grid pointed to by coords. Pixel coordinates are (x, y), as usual.(R(RtcoordsR%R&RR((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyttopix3s cCs|\}}xtddD]u}xltddD][}y?|i||||o!|i||||inWq2tj oq2Xq2WqWdS(s-Explodes all blocks in a radius around coordsiiN(R R t destroyblockt IndexError(RR(R%R&RR((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pytexplode:s %c Cs|\}}xtddD]}xtddD]t}yX|i||||o:|i||||id|i||||tSq>Xq>W|ii|||_nW|ii||ii \}}|i \}}||it ||t ||<||_t S(s>Adds object to grid. Returns True on success, False on failureRR( thasattrRtaddRRRRR trectttopleftRR tTrue(RtobjR!R%R&RR((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyR0Qs"   % cCs t|_dS(N(R3R(R((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pytlostfs( t__name__t __module__RR#R'R)R,R.R0R5(((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyR s    t__main__(( t__doc__t pygame.spriteRtutilsRRR6tgR (((s9/Users/admin/ludumdare/run.app/Contents/Resources/grid.pyss  `