baskinein

Crea Grass Script

Jun 18th, 2012
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. name = "Grass"
  2.  
  3.  
  4. class GrassImage(object):
  5.     def __init__(self, path, height):
  6.         self.path = path
  7.         self.height = height
  8.  
  9. class GrassSprite(object):
  10.     def __init__(self, index):
  11.         self.index = index
  12.         self.sprite = Sprite()
  13.  
  14. class Grass(object):
  15.     images = [GrassImage("mods/base/plant/grass.png", 5)]
  16.     sides = []
  17.     growthTimeMin = 10000#2 * 60 * 1000 #2 minutes
  18.     growthTimeMax = 10000#5 * 60 * 1000 #5 minutes
  19.     spreadTimeMin = 8 * 60 * 1000 #8 minutes
  20.     spreadTimeMax = 10 * 60 * 1000 #10 minutes
  21.  
  22.     def __init__(self, entity, organic):
  23.         print "Creating grass."
  24.         self.organic = organic
  25.         if not Game.get().network.isHost():
  26.             return
  27.         entity.interactable.add("removeSupport", self.onRemoveSupport)
  28.         grass = GrassSprite(random.randint(0, len(self.images) - 1))
  29.         image = self.images[grass.index]
  30.         grass.sprite.position.y = -image.height
  31.         self.organic.setImage(image.path, grass.sprite)
  32.         self.grass = [grass]
  33.         self.construct()
  34.         self.addGrowthTimer()
  35.  
  36.     def addGrowthTimer(self):
  37.         Game.get().timer.add(random.randint(self.growthTimeMin, self.growthTimeMax), self.grow)
  38.  
  39.     def grow(self):
  40.         self.addGrowthTimer()
  41.         world = World.get()
  42.         ground = world.layer["ground"]
  43.         if len(self.grass) >= 30:
  44.             #TODO: Check to see if the grass can spread and create a new grass entity
  45.             return
  46.         #The grass is store from left to right [leftmost:rightmost]
  47.         #First check to see if the leftmost grass can spread more left
  48.         self.growOutward(ground, 0, -1)
  49.         self.growOutward(ground, len(self.grass) - 1, 1)
  50.         self.construct()
  51.  
  52.     def growOutward(self, ground, index, direction):
  53.         grassSprite = self.grass[index].sprite
  54.         position = grassSprite.getPosition()
  55.         position.x += TileSystem.TILE_SIZE * direction
  56.         position.y += TileSystem.TILE_SIZE
  57.         tilePos = ground.getTilePosition(position)
  58.         if ground.getTile(tilePos):
  59.             tile = ground.getTileComponent(tilePos)
  60.             if 'soil' in tile.getGroups() and random.randint(0, 1):
  61.                 #Soil! We found it and passed the random test. Lets add it!
  62.                 grass = GrassSprite(random.randint(0, len(self.images) - 1))
  63.                 image = self.images[grass.index]
  64.                 grass.sprite.position.x = grassSprite.position.x + (TileSystem.TILE_SIZE * direction)
  65.                 grass.sprite.position.y = grassSprite.position.y
  66.                 self.organic.setImage(image.path, grass.sprite)
  67.                 if direction is -1:
  68.                     self.grass.insert(0, grass)
  69.                 else:
  70.                     self.grass.append(grass)
  71.  
  72.     def isTouchingTile(self, ground, tilePos):
  73.         grassTile = ground.getTilePosition(grass.sprite.getPosition())
  74.         return grassTile.x == tilePos.x and grassTile.y + 1 == tilePos.y
  75.  
  76.     def onRemoveSupport(self, args):
  77.         tilePos = args["tilePos"]
  78.         ground = World.get().layer["ground"]
  79.         self.grass = [grass for grass in self.grass if not self.isTouchingTile(ground, tilePos, grass)]
  80.         if self.grass:
  81.             self.construct()
  82.         else:
  83.             #No more grass - go ahead and destroy this grass.
  84.             parent = self.organic.getParent()
  85.             if parent:
  86.                 parent.destroy()
  87.  
  88.     def construct(self):
  89.         self.organic.onConstruct([grass.sprite for grass in self.grass])
  90.  
  91. def createGrass(entity, organic):
  92.     return Grass(entity, organic)
  93.  
  94. add(ModularRender())
  95. add(Interactable())
  96. add(Organic(createGrass))
RAW Paste Data