Exemple #1
0
    def create_new_object_by_id(self, itemid):
        """Create new farm object from objects dictionary"""

        # If player dont have this object return False
        if not self.item_in_inventory(itemid):
            return False
        if objects[itemid].get("type", "object") == "seed":
            seed = Seed()
            seed.apply_dict(objects[itemid])
            return seed
        else:
            fobject = FarmObject()
            fobject.apply_dict(objects[itemid])
            return fobject
    def load_gamestate(self, filename, player):
        if not os.path.isfile(filename):
            return False
        print("Loading game state...")
        data = DictMapper()
        data.load(filename)
        player.inventory = data["inventory"]
        player.itemscounter = data["itemscounter"]
        player.watercanuses = data.get("watercanuses", 100)
        player.exp = data.get("exp", 0.0)
        player.nextlvlexp = data.get("nextlvlexp", 100.0)
        player.money = int(data.get("money", 1))
        player.level = int(data.get("level", 1))
        # loda game time
        self.seconds_to_update = int(time.time()) - data.get("gametime",
                                                             int(time.time()))
        seed = data.get("gameseed", int(time.time()))
        self.setgameseed(seed)

        # Migrate old farm
        if "fields" not in data.keys():
            data["fields"] = []
            data['fields'].append({})
            data['fields'][0]["tiles"] = data["tiles"]
        # load tiles
        for farmid in xrange(len(data["fields"])):
            farm = self.getfarm(farmid)
            if farm is None:
                farm = self.addfarm()
            # Restore tiles
            for tile in data["fields"][farmid]["tiles"]:
                px = tile["px"]
                py = tile["py"]
                # Avoid null objects
                if not tile["object"]:
                    continue

                # Restore seed or object
                if tile["object"]["type"] == "seed":
                    objectdata = tile["object"]
                    newobject = Seed()

                    newobject.id = objectdata["id"]
                    newobject.type = objectdata["type"]

                    newobject.to_harvest = objectdata["to_harvest"]
                    newobject.growing = objectdata["growing"]
                    newobject.growendtime = objectdata["growendtime"]
                    newobject.growstarttime = objectdata["growstarttime"]

                    farmtile = FarmTile(newobject)
                    farmtile["water"] = tile["water"]

                    # Apply global object data
                    newobject.apply_dict(objects[newobject.id])

                    # Restore harvest count
                    newobject.harvestcount = objectdata.get("harvestcount", 1)
                    newobject.requiredlevel = objectdata.get(
                        "requiredlevel", 1)
                else:
                    newobject = FarmObject()

                    newobject.id = tile["object"]["id"]
                    newobject.type = tile["object"]["type"]
                    # apply dict
                    newobject.apply_dict(objects[newobject.id])
                    farmtile = FarmTile(newobject)
                # set farmtile
                farm.set_farmtile(px, py, farmtile)
        # return
        return True