Example #1
0
    // Use this for initialization
    void Start()
    {
        atkImg = Resources.LoadAll <Sprite>("Graphics/Player/Ataques");

        firstW   = true;
        plImg    = Resources.LoadAll <Sprite>("Graphics/Player/Aventureiro");
        priImg   = Resources.LoadAll <Sprite>("Graphics/Player/" + inv.slot[1]);
        secImg   = Resources.LoadAll <Sprite>("Graphics/Player/" + inv.slot[2]);
        headImg  = Resources.LoadAll <Sprite>("Graphics/Player/" + inv.slot[4]);
        chestImg = Resources.LoadAll <Sprite>("Graphics/Player/" + inv.slot[5]);
        feetImg  = Resources.LoadAll <Sprite>("Graphics/Player/" + inv.slot[6]);
        mod      = gameObject.GetComponent <StatsModifiers>();
        GetStatus();
        CalculatStats();
        cHp  = rHp;
        nPos = Vector3.zero;
    }
Example #2
0
        public void addUnit(int x, int y, int owner)
        {
            //int unitCount = reader.ReadInt32();
            int             unitCount = 1;
            List <UnitInfo> unitInfos = new List <UnitInfo>();

            for (int j = 0; j < unitCount; j++)
            {
                string     unitName        = "Hero";
                string     unitDisplayName = "Hero";
                float      range           = -1;
                int        experience      = 0;
                int        level           = 1;
                bool       overrideStats   = false;
                Statistics stats           = new Statistics();

                // Modifiers
                StatsModifiers modifiers = new StatsModifiers();

                unitInfos.Add(new UnitInfo(unitName, unitDisplayName, range, experience, level, stats, overrideStats, modifiers));
            }
            units.Add(new UnitContainerInfo(x, y, owner, unitInfos));
        }
Example #3
0
        public Map LoadMapFromBytes(MapObjects mapObjects, string path, MiniMap miniMap, SelectedBrush selectedBrush, List <MapItem> changedItems, Configs configs, MapLoadMode mode = MapLoadMode.All)
        {
            byte[] serializedMap = File.ReadAllBytes(path);

            Map map = new Map();

            using (BinaryReader reader = new BinaryReader(new MemoryStream(serializedMap)))
            {
                // Header & metadata
                if (reader.ReadUInt32() != headerMagic)
                {
                    throw new IOException("Invalid map header");
                }

                map.columns = reader.ReadInt32();
                map.rows    = reader.ReadInt32();
                if (map.columns <= 0 || map.rows <= 0)
                {
                    throw new IOException("Invalid rows/columns values");
                }
                map.tiles       = new List <MapItem>();
                map.name        = reader.ReadString();
                map.description = reader.ReadString();
                map.startTurn   = reader.ReadInt32();
                map.startPlayer = reader.ReadInt32();

                int players = reader.ReadInt32();
                for (int i = 0; i < players; i++)
                {
                    map.playersInTurnOrder.Add(reader.ReadInt32());
                }

                for (int i = 0; i < players; i++)
                {
                    List <Resource> playerResources = new List <Resource>();
                    bool            isAi            = reader.ReadBoolean();
                    bool            useColor        = reader.ReadBoolean();
                    map.isAi.Add(isAi);
                    map.useColor.Add(useColor);
                    if (useColor)
                    {
                        map.r.Add(reader.ReadSingle());
                        map.g.Add(reader.ReadSingle());
                        map.b.Add(reader.ReadSingle());
                    }

                    int resourceCount = reader.ReadInt32();
                    map.resourceCount.Add(resourceCount);
                    for (int j = 0; j < resourceCount; j++)
                    {
                        playerResources.Add(new Resource(reader.ReadString(), reader.ReadInt32()));
                    }
                    map.resourcesList.Add(playerResources);
                }

                // Prefab palette
                map.paletteSize = reader.ReadInt16();
                List <string> palette = new List <string>();
                if (map.paletteSize <= 0)
                {
                    throw new IOException("Palette is empty");
                }
                Regex regex = new Regex(@"^[a-zA-Z0-9_]+$");
                for (int i = 0; i < map.paletteSize; i++)
                {
                    string prefabPath = reader.ReadString();
                    map.prefabPath.Add(prefabPath);
                    if (!regex.IsMatch(prefabPath))
                    {
                        throw new IOException("Invalid prefab path: " + prefabPath);
                    }
                    palette.Add(prefabPath);
                }

                if (!map.validate(mapObjects, configs))
                {
                    return(null);
                }

                // Tiles
                for (int i = 0; i < map.rows; i++)
                {
                    for (int j = 0; j < map.columns; j++)
                    {
                        int prefabId  = reader.ReadInt16();
                        int setIndex  = 0;
                        int itemIndex = 0;
                        if (prefabId < 0 || prefabId >= map.paletteSize)
                        {
                            throw new IOException("Invalid prefab ID");
                        }
                        setIndex  = mapObjects.terrains.FindIndex(t => t.setName.ToLower() == palette[prefabId].Split('_')[0]);
                        itemIndex = Int16.Parse(palette[prefabId].Split('_')[1]);

                        map.tiles.Add(new MapItem(itemIndex, setIndex, mapObjects.terrains[setIndex].setName, mapObjects.terrains[setIndex].category, mapObjects, j, i, miniMap, selectedBrush, changedItems, map));
                    }
                }

                // Overlay tiles
                map.overlayTilesCount = reader.ReadInt32();
                for (int i = 0; i < map.overlayTilesCount; i++)
                {
                    map.overlayTilesX.Add(reader.ReadInt32());
                    map.overlayTilesY.Add(reader.ReadInt32());
                    map.overlayTilesPrefabId.Add(reader.ReadInt32());

                    int migrationIndex = mapObjects.terrains.FindIndex(t => t.setName.ToLower() == palette[map.overlayTilesPrefabId[i]].Split('_')[0]);
                    if (migrationIndex > -1)
                    {
                        mapObjects.roads.Add(mapObjects.terrains[migrationIndex]);
                        mapObjects.terrains.RemoveAt(migrationIndex);
                    }

                    int tileID = map.tiles.FindIndex(t => (t.Xcoordinate == map.overlayTilesX[i]) && (t.Ycoordinate == map.overlayTilesY[i]));
                    map.tiles[tileID].objectCategory = "Roads";
                    map.tiles[tileID].objectSet      = mapObjects.roads.FindIndex(t => t.setName.ToLower() == palette[map.overlayTilesPrefabId[i]].Split('_')[0]);
                    map.tiles[tileID].objectName     = palette[map.overlayTilesPrefabId[i]].Split('_')[0];
                    map.tiles[tileID].objectIndex    = Int16.Parse(palette[map.overlayTilesPrefabId[i]].Split('_')[1]);
                    map.tiles[tileID].combineImages();
                }

                // Castles
                int castleCount = reader.ReadInt32();
                for (int i = 0; i < castleCount; i++)
                {
                    string prefabName = reader.ReadString();
                    string castleName = reader.ReadString();
                    int    x          = reader.ReadInt32();
                    int    y          = reader.ReadInt32();
                    int    owner      = reader.ReadInt32();
                    bool   razed      = reader.ReadBoolean();
                    int    buildings  = reader.ReadInt32();

                    HashSet <string> purchasedBuildings = new HashSet <string>();
                    for (int j = 0; j < buildings; j++)
                    {
                        purchasedBuildings.Add(reader.ReadString());
                    }

                    string producedUnitName        = reader.ReadString();
                    int    producedUnitTurnsLeft   = reader.ReadInt32();
                    string producedUnitDestination = reader.ReadString();

                    int            queuedUnits = reader.ReadInt32();
                    Queue <string> unitQueue   = new Queue <string>();
                    for (int j = 0; j < queuedUnits; j++)
                    {
                        unitQueue.Enqueue(reader.ReadString());
                    }

                    map.castles.Add(new CastleInfo(prefabName, castleName, x, y, owner, razed, purchasedBuildings,
                                                   producedUnitName, producedUnitTurnsLeft, producedUnitDestination, unitQueue));

                    int tileID      = map.tiles.FindIndex(t => (t.Xcoordinate == map.castles[i].x) && (t.Ycoordinate == map.castles[i].y));
                    int objectIndex = configs.fractions.FindIndex(o => o.name == map.castles[i].prefabName);
                    map.tiles[tileID].objectCategory = "Castles";
                    map.tiles[tileID].objectSet      = 0;
                    map.tiles[tileID].objectIndex    = objectIndex;
                    map.tiles[tileID].castleName     = map.castles[i].castleName;
                    map.tiles[tileID].castleOwner    = map.castles[i].owner;
                    map.tiles[tileID].combineImages();
                }

                // Ruins
                int ruinCount = reader.ReadInt32();
                for (int i = 0; i < ruinCount; i++)
                {
                    string prefabName = reader.ReadString();
                    int    x          = reader.ReadInt32();
                    int    y          = reader.ReadInt32();
                    bool   isVisited  = reader.ReadBoolean();

                    map.ruins.Add(new RuinsInfo(prefabName, x, y, isVisited));

                    int tileID    = map.tiles.FindIndex(t => (t.Xcoordinate == map.ruins[i].x) && (t.Ycoordinate == map.ruins[i].y));
                    int objectSet = configs.ruinsData.FindIndex(o => o.name == map.ruins[i].prefabName);

                    map.tiles[tileID].objectCategory = "Ruins";
                    map.tiles[tileID].objectSet      = objectSet;
                    map.tiles[tileID].objectIndex    = new Random().Next(configs.ruinsData[objectSet].sprites.Count);
                    map.tiles[tileID].combineImages();
                }

                // Units
                int unitContainers = reader.ReadInt32();
                for (int i = 0; i < unitContainers; i++)
                {
                    int x         = reader.ReadInt32();
                    int y         = reader.ReadInt32();
                    int owner     = reader.ReadInt32();
                    int unitCount = reader.ReadInt32();

                    List <UnitInfo> unitInfos = new List <UnitInfo>();
                    for (int j = 0; j < unitCount; j++)
                    {
                        string     unitName        = reader.ReadString();
                        string     unitDisplayName = reader.ReadString();
                        float      range           = reader.ReadSingle();
                        int        experience      = reader.ReadInt32();
                        int        level           = reader.ReadInt32();
                        bool       overrideStats   = reader.ReadBoolean();
                        Statistics stats           = new Statistics();
                        if (overrideStats)
                        {
                            stats.attack        = reader.ReadSingle();
                            stats.defense       = reader.ReadSingle();
                            stats.power         = reader.ReadSingle();
                            stats.wisdom        = reader.ReadSingle();
                            stats.hp            = reader.ReadSingle();
                            stats.speed         = reader.ReadSingle();
                            stats.damageMin     = reader.ReadSingle();
                            stats.damageMax     = reader.ReadSingle();
                            stats.timeToProduce = reader.ReadSingle();
                            stats.canShoot      = reader.ReadBoolean();
                            stats.canFly        = reader.ReadBoolean();
                            stats.range         = reader.ReadSingle();
                        }

                        // Modifiers
                        StatsModifiers modifiers     = new StatsModifiers();
                        int            modifierCount = reader.ReadInt32();
                        for (int k = 0; k < modifierCount; k++)
                        {
                            string statType         = reader.ReadString();
                            int    modifierSubCount = reader.ReadInt32();
                            for (int m = 0; m < modifierSubCount; m++)
                            {
                                modifiers.addModifier(statType, new StatsModifiersEntry(
                                                          reader.ReadString(),
                                                          reader.ReadSingle(),
                                                          (StatsModifiersEntry.DurationType)reader.ReadInt32(),
                                                          reader.ReadInt32(),
                                                          (StatsModifiersEntry.ModifierType)reader.ReadInt32()
                                                          ));
                            }
                        }

                        unitInfos.Add(new UnitInfo(unitName, unitDisplayName, range, experience, level, stats, overrideStats, modifiers));
                    }

                    map.units.Add(new UnitContainerInfo(x, y, owner, unitInfos));
                }
            }

            return(map);
        }