Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- name = "Grass"
- class GrassImage(object):
- def __init__(self, path, height):
- self.path = path
- self.height = height
- class GrassSprite(object):
- def __init__(self, index):
- self.index = index
- self.sprite = Sprite()
- class Grass(object):
- images = [GrassImage("mods/base/plant/grass.png", 5)]
- sides = []
- growthTimeMin = 10000#2 * 60 * 1000 #2 minutes
- growthTimeMax = 10000#5 * 60 * 1000 #5 minutes
- spreadTimeMin = 8 * 60 * 1000 #8 minutes
- spreadTimeMax = 10 * 60 * 1000 #10 minutes
- def __init__(self, entity, organic):
- print "Creating grass."
- self.organic = organic
- if not Game.get().network.isHost():
- return
- entity.interactable.add("removeSupport", self.onRemoveSupport)
- grass = GrassSprite(random.randint(0, len(self.images) - 1))
- image = self.images[grass.index]
- grass.sprite.position.y = -image.height
- self.organic.setImage(image.path, grass.sprite)
- self.grass = [grass]
- self.construct()
- self.addGrowthTimer()
- def addGrowthTimer(self):
- Game.get().timer.add(random.randint(self.growthTimeMin, self.growthTimeMax), self.grow)
- def grow(self):
- self.addGrowthTimer()
- world = World.get()
- ground = world.layer["ground"]
- if len(self.grass) >= 30:
- #TODO: Check to see if the grass can spread and create a new grass entity
- return
- #The grass is store from left to right [leftmost:rightmost]
- #First check to see if the leftmost grass can spread more left
- self.growOutward(ground, 0, -1)
- self.growOutward(ground, len(self.grass) - 1, 1)
- self.construct()
- def growOutward(self, ground, index, direction):
- grassSprite = self.grass[index].sprite
- position = grassSprite.getPosition()
- position.x += TileSystem.TILE_SIZE * direction
- position.y += TileSystem.TILE_SIZE
- tilePos = ground.getTilePosition(position)
- if ground.getTile(tilePos):
- tile = ground.getTileComponent(tilePos)
- if 'soil' in tile.getGroups() and random.randint(0, 1):
- #Soil! We found it and passed the random test. Lets add it!
- grass = GrassSprite(random.randint(0, len(self.images) - 1))
- image = self.images[grass.index]
- grass.sprite.position.x = grassSprite.position.x + (TileSystem.TILE_SIZE * direction)
- grass.sprite.position.y = grassSprite.position.y
- self.organic.setImage(image.path, grass.sprite)
- if direction is -1:
- self.grass.insert(0, grass)
- else:
- self.grass.append(grass)
- def isTouchingTile(self, ground, tilePos):
- grassTile = ground.getTilePosition(grass.sprite.getPosition())
- return grassTile.x == tilePos.x and grassTile.y + 1 == tilePos.y
- def onRemoveSupport(self, args):
- tilePos = args["tilePos"]
- ground = World.get().layer["ground"]
- self.grass = [grass for grass in self.grass if not self.isTouchingTile(ground, tilePos, grass)]
- if self.grass:
- self.construct()
- else:
- #No more grass - go ahead and destroy this grass.
- parent = self.organic.getParent()
- if parent:
- parent.destroy()
- def construct(self):
- self.organic.onConstruct([grass.sprite for grass in self.grass])
- def createGrass(entity, organic):
- return Grass(entity, organic)
- add(ModularRender())
- add(Interactable())
- add(Organic(createGrass))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement