From: <hol...@us...> - 2008-07-09 06:49:35
|
Revision: 101 http://fortress.svn.sourceforge.net/fortress/?rev=101&view=rev Author: holomorph Date: 2008-07-08 23:49:44 -0700 (Tue, 08 Jul 2008) Log Message: ----------- towers start at level 0 (no stem, just foot base and top) and grow up to level 3 (3 stem sections) with the left mouse button, shrink with the right Modified Paths: -------------- trunk/pyfort/fortress.py trunk/pyfort/sprites.py trunk/pyfort/test_world.py trunk/pyfort/testbase.py trunk/pyfort/world.py Added Paths: ----------- trunk/pyfort/run_tests.py trunk/pyfort/test_sprites.py Modified: trunk/pyfort/fortress.py =================================================================== --- trunk/pyfort/fortress.py 2008-07-07 05:27:36 UTC (rev 100) +++ trunk/pyfort/fortress.py 2008-07-09 06:49:44 UTC (rev 101) @@ -1,5 +1,6 @@ from pyglet.gl import * -from pyglet.window import key +from pyglet.window import mouse, key + from world import * from sprites import * import random @@ -20,22 +21,17 @@ top = pyglet.resource.image('squaretower/top.png') top.anchor_x = 48 towers = [] - #tower = Tower(base,stem,top,x=tile_size,y=tile_size,level=2) - #tower2 = Tower(base,stem,top, x=tile_size*4,y=tile_size*2, level = 0) - #tower3 = Tower(base,stem,top, x=0,y=tile_size*3, level = 1) - + global myrot + myrot = 0 window = pyglet.window.Window() @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT) - #grid.blit(200,100) camera.apply() + glRotatef(myrot,0,0,1) terrain.draw() for tower in towers: tower.draw() - #tower2.draw() - #tower3.draw() - #tower.draw() @window.event def on_resize(width, height): @@ -46,20 +42,34 @@ @window.event def on_key_press(symbol, modifiers): - global tile_size + global myrot + if symbol == key.RIGHT: + myrot +=5 + elif symbol == key.LEFT: + myrot -=5 if symbol == key.SPACE: print 'clear' while len(towers)>0: tower = towers.pop() - terrain.getTile(tower.x,tower.y).occupied = False + terrain.getTile(tower.x,tower.y).leave(tower) @window.event def on_mouse_press(x, y, button, modifiers): x_world, y_world = camera.to_world_ground_coords(x,y) tile = terrain.getTile(x_world, y_world) - if tile is not None and not tile.occupied: - tile.occupied = True - towers.append(Tower(base,stem,top,x=tile.x,y=tile.y,level=random.randint(0,3))) + if tile is None: + return + if button == mouse.LEFT and not tile.occupied: + tower = Tower(base,stem,top,x=tile.x,y=tile.y,level=0) + tile.occupy(tower) + towers.append(tower) towers.sort(key = lambda a: -(a.x**2 + a.y**2)) + elif button == mouse.LEFT and tile.occupied: + tower = tile.occupants[0] + tower.grow() + elif button == mouse.RIGHT and tile.occupied: + tower = tile.occupants[0] + tower.shrink() + def perspective(width,height): glMatrixMode(GL_PROJECTION) glLoadIdentity() Added: trunk/pyfort/run_tests.py =================================================================== --- trunk/pyfort/run_tests.py (rev 0) +++ trunk/pyfort/run_tests.py 2008-07-09 06:49:44 UTC (rev 101) @@ -0,0 +1,8 @@ +import unittest +import test_world +import test_sprites + +all_tests = unittest.TestSuite() +all_tests.addTests((test_world.suite(), test_sprites.suite())) + +unittest.TextTestRunner().run(all_tests) Modified: trunk/pyfort/sprites.py =================================================================== --- trunk/pyfort/sprites.py 2008-07-07 05:27:36 UTC (rev 100) +++ trunk/pyfort/sprites.py 2008-07-09 06:49:44 UTC (rev 101) @@ -7,8 +7,8 @@ x-y component that the camera is facing. ''' def __init__(self, - img, - x=0, y=0, + img=None, + x=0, y=0, z=0, blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE_MINUS_SRC_ALPHA, batch=None, @@ -17,6 +17,9 @@ '''Create an IsoSprite. See pyglet sprite documentation ''' + if img is None: + img = pyglet.image.CheckerImagePattern().create_image(96,48) + self._z = z super(IsoSprite, self).__init__(img,x,y,blend_src,blend_dest,batch,group,usage) def _create_vertex_list(self): @@ -33,8 +36,7 @@ def _update_position(self): img = self._texture - z1 = 0 - z2 = 0 + z1 = z2 = 0 if not self._visible: self._vertex_list.vertices[:] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] elif self._scale != 1.0: @@ -56,30 +58,41 @@ glPushMatrix() #self._vertex_list.draw(GL_QUADS) self._group.set_state_recursive() - glTranslatef(self._x,self._y,0) + glTranslatef(self._x,self._y,self._z) #align with the view direction glRotatef(-Camera.VIEW_Z_ROTATION,0,0,1) #and stand the sprite up glRotatef(90,1,0,0) self._vertex_list.draw(GL_QUADS) self._group.unset_state_recursive() - #self._vertex_list.draw(GL_LINES) + #self._vertex_list.draw(GL_LINES) #for debugging glPopMatrix() class Tower(object): - def __init__(self,base,stem,top,x,y,level=0): + MAX_HEIGHT=3 + def __init__(self,base=None,stem=None,top=None,x=0,y=0,level=0): self.x = x self.y = y self.level = level - self._pieces = [IsoSprite(base,x,y)] - for i in range(level): - stem.anchor_y = -base.height - i*stem.height - self._pieces.append(IsoSprite(stem,x,y)) - top.anchor_y = -base.height - stem.height*level - self._pieces.append(IsoSprite(top,x,y)) - self._pieces.reverse() # so they are drawn top to bottom - + self._base = IsoSprite(base,x,y,0) + self._stem = IsoSprite(stem,x,y,self._base.height) + self._top = IsoSprite(top,x,y,self._base.height+self._stem.height*level) + def adjust_top(self): + self._top._z = self._base.height+self._stem.height*self.level + def grow(self): + if self.level == 3: + return + self.level +=1 + self.adjust_top() + def shrink(self): + if self.level == 0: + return + self.level -=1 + self.adjust_top() def draw(self): - for piece in self._pieces: - piece.draw() + self._base.draw() + for i in range(self.level): + self._stem._z = self._base.height+self._stem.height*i + self._stem.draw() + self._top.draw() Added: trunk/pyfort/test_sprites.py =================================================================== --- trunk/pyfort/test_sprites.py (rev 0) +++ trunk/pyfort/test_sprites.py 2008-07-09 06:49:44 UTC (rev 101) @@ -0,0 +1,18 @@ +from testbase import * +import unittest + +from sprites import * + +def suite(): + return make_suite(TowerTests) + +class TowerTests(BasicTestSet): + def setUp(self): + self.tower = Tower() + @testmethod + def testCreate(self): + self.failIf(self.tower is None) + @testmethod + def testGrow(self): + self.tower.grow() + self.failUnlessEqual(self.tower.level, 1) Modified: trunk/pyfort/test_world.py =================================================================== --- trunk/pyfort/test_world.py 2008-07-07 05:27:36 UTC (rev 100) +++ trunk/pyfort/test_world.py 2008-07-09 06:49:44 UTC (rev 101) @@ -3,18 +3,23 @@ from world import * +def suite(): + return make_suite(TerrainTests, TileTests) + class TerrainTests(BasicTestSet): + TEST_WIDTH = 4 + TEST_HEIGHT = 4 def setUp(self): - self.terrain = Terrain(width = 40, height = 40) + self.terrain = Terrain(width = TerrainTests.TEST_WIDTH, height = TerrainTests.TEST_HEIGHT) def tearDown(self): pass @testmethod def testSize(self): - self.failUnless(self.terrain.width == 40, 'terrain width incorrect') - self.failUnless(self.terrain.height == 40, 'terrain height incorrect') + self.failUnless(self.terrain.width == TerrainTests.TEST_WIDTH, 'terrain width incorrect') + self.failUnless(self.terrain.height == TerrainTests.TEST_HEIGHT, 'terrain height incorrect') @testmethod def testTileArrayBoundaries(self): - self.failUnlessRaises(IndexError,self.terrain.tiles.__getitem__,40) + self.failUnlessRaises(IndexError,self.terrain.tiles.__getitem__,TerrainTests.TEST_WIDTH) t = Terrain(width=1, height=2) #this should be ok t.tiles[0][1] @@ -61,6 +66,21 @@ @testmethod def testNewTileNotOccupied(self): self.failIf(self.tile.occupied) + @testmethod + def testOccupy(self): + a = object() + self.tile.occupy(a) + self.failUnless(self.tile.occupied) + self.failUnless(a in self.tile.occupants) + self.tile.occupy(a) + self.failIf(len(self.tile.occupants) > 1) + @testmethod + def testLeave(self): + a = object() + self.tile.occupy(a) + self.tile.leave(a) + self.failIf(a in self.tile.occupants) + #make sure nothing bad happens if we try to leave and aren't occupying the tile + self.tile.leave(a) -unittest.TextTestRunner().run(suite(TileTests, TerrainTests)) Modified: trunk/pyfort/testbase.py =================================================================== --- trunk/pyfort/testbase.py 2008-07-07 05:27:36 UTC (rev 100) +++ trunk/pyfort/testbase.py 2008-07-09 06:49:44 UTC (rev 101) @@ -13,7 +13,7 @@ return unittest.TestSuite(map(cls,testNames)) -def suite(*args): +def make_suite(*args): fullSuite = unittest.TestSuite() for testSet in args: fullSuite.addTest(testSet.suite()) Modified: trunk/pyfort/world.py =================================================================== --- trunk/pyfort/world.py 2008-07-07 05:27:36 UTC (rev 100) +++ trunk/pyfort/world.py 2008-07-09 06:49:44 UTC (rev 101) @@ -14,12 +14,12 @@ def isometric(self,width,height): glMatrixMode(GL_PROJECTION) glLoadIdentity() - glOrtho(-width/2.,width/2.,-height/2.,height/2.,0,Camera.FAR) + glOrtho(-width/2.,width/2.,-height/2.,height/2.,-256,Camera.FAR) glMatrixMode(GL_MODELVIEW) def apply(self): glLoadIdentity() glRotatef(Camera.VIEW_X_ROTATION,1,0,0) - glRotatef(Camera.VIEW_Z_ROTATION,0,0,1) + glRotatef(Camera.VIEW_Z_ROTATION,0,0,1) glTranslatef(-self._x,-self._y,-Camera.VIEW_HEIGHT) def to_world_ground_coords(self,x,y): ''' @@ -70,6 +70,7 @@ occupied, meaning built on or not. ''' grass_image = pyglet.resource.image('grass.png') + occupied = property(lambda self: len(self.occupants) != 0) def __init__(self, img=grass_image, x=0, y=0, @@ -84,4 +85,14 @@ super(Tile, self).__init__(img,x,y,blend_src,blend_dest,batch,group,usage) self.center_x = x+TILE_SIZE/2 self.center_y = y+TILE_SIZE/2 - self.occupied = False + self.occupants = [] + def occupy(self, occupant): + '''have the given occupant occupy this tile + ''' + if occupant not in self.occupants: + self.occupants.append(occupant) + def leave(self, occupant): + ''' have the given occupant no longer occupy this tile_size + ''' + if occupant in self.occupants: + self.occupants.remove(occupant) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <hol...@us...> - 2008-07-10 05:31:59
|
Revision: 102 http://fortress.svn.sourceforge.net/fortress/?rev=102&view=rev Author: holomorph Date: 2008-07-09 22:32:03 -0700 (Wed, 09 Jul 2008) Log Message: ----------- made isosprite face the camera (well match camera angle anyway) Modified Paths: -------------- trunk/pyfort/sprites.py trunk/pyfort/world.py Modified: trunk/pyfort/sprites.py =================================================================== --- trunk/pyfort/sprites.py 2008-07-09 06:49:44 UTC (rev 101) +++ trunk/pyfort/sprites.py 2008-07-10 05:32:03 UTC (rev 102) @@ -62,7 +62,7 @@ #align with the view direction glRotatef(-Camera.VIEW_Z_ROTATION,0,0,1) #and stand the sprite up - glRotatef(90,1,0,0) + glRotatef(-Camera.VIEW_X_ROTATION,1,0,0) self._vertex_list.draw(GL_QUADS) self._group.unset_state_recursive() #self._vertex_list.draw(GL_LINES) #for debugging Modified: trunk/pyfort/world.py =================================================================== --- trunk/pyfort/world.py 2008-07-09 06:49:44 UTC (rev 101) +++ trunk/pyfort/world.py 2008-07-10 05:32:03 UTC (rev 102) @@ -4,7 +4,7 @@ class Camera(object): VIEW_Z_ROTATION = 45 - VIEW_X_ROTATION = -60 + VIEW_X_ROTATION = -60 #90+34.192502848038274 VIEW_HEIGHT = 128 FAR=2048 fov=45 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |