//this will be called by the game engine every X number of rounds public void SpawnUnit(Map map) { if (spawnX == -1) { return; // there is no valid place to spawn the unit } if (map.unitMap[spawnX, spawnY] != null) { //a unit is already there, so dont spawn return; } //otherwise spawn: Random rand = new Random(); if (unitType == 0) { //meelee MeeleeUnit m = new MeeleeUnit(spawnX, spawnY, team, map.meeleeNames[rand.Next(0, map.meeleeNames.Length)]); map.PlaceUnit(m); } else if (unitType == 1) { //ranged RangedUnit r = new RangedUnit(spawnX, spawnY, team, map.rangedNames[rand.Next(0, map.rangedNames.Length)]); map.PlaceUnit(r); } }
public Map(int numUnitsToCreate, int numBuildingsToCreate, int mapRows, int mapCols) { MAP_ROWS = mapRows; MAP_COLS = mapCols; unitMap = new Unit[MAP_ROWS, MAP_COLS]; buildingList = new List <Building>(); if (numUnitsToCreate > (MAP_ROWS * MAP_COLS)) { numUnitsToCreate = MAP_ROWS * MAP_COLS; } Random rand = new Random(); numUnitsToCreate = numUnitsToCreate - WIZARD_COUNT; int numUnitsTeamOne = numUnitsToCreate / 2; int numUnitsTeamTwo = numUnitsToCreate - numUnitsTeamOne; while (numUnitsToCreate > 0) { int rowIndex, colIndex; do { rowIndex = rand.Next(0, MAP_ROWS); colIndex = rand.Next(0, MAP_COLS); } while (unitMap[rowIndex, colIndex] != null); //at this point we have an empty spot int teamNumber = 0; //default team zero if (numUnitsToCreate > numUnitsTeamOne) { teamNumber = 1; } int unitType = rand.Next(0, 2); if (unitType == 0) { //create meelee unitMap[rowIndex, colIndex] = new MeeleeUnit(rowIndex, colIndex, teamNumber, meeleeNames[rand.Next(0, meeleeNames.Length)]); } else if (unitType == 1) { //create ranged unitMap[rowIndex, colIndex] = new RangedUnit(rowIndex, colIndex, teamNumber, rangedNames[rand.Next(0, rangedNames.Length)]); } numUnitsToCreate--; } //create wizards before buildings int wizardsToCreate = WIZARD_COUNT; while (wizardsToCreate > 0) { int rowIndex, colIndex; do { rowIndex = rand.Next(0, MAP_ROWS); colIndex = rand.Next(0, MAP_COLS); } while (unitMap[rowIndex, colIndex] != null); //at this point we have an empty spot unitMap[rowIndex, colIndex] = new Wizard(rowIndex, colIndex); wizardsToCreate--; } //begin to add buildings int numBuildingsTeam1 = numBuildingsToCreate / 2; int numBuildingsTeam2 = numBuildingsToCreate - numBuildingsTeam1; while (numBuildingsToCreate > 0) { int rowIndex, colIndex; do { rowIndex = rand.Next(0, MAP_ROWS); colIndex = rand.Next(0, MAP_COLS); } while (unitMap[rowIndex, colIndex] != null && !DoesBuildingConflictWithOtherBuilding(rowIndex, colIndex)); int teamNumber = 0; //default team zero if (numBuildingsToCreate > numBuildingsTeam1) { teamNumber = 1; } int buildingType = rand.Next(0, 2); if (buildingType == 0) { //create Factory building buildingList.Add(new FactoryBuilding(rowIndex, colIndex, 100, teamNumber, rand.Next(0, 2), 2, this)); } else if (buildingType == 1) { //create Resource building buildingList.Add(new ResourceBuilding(rowIndex, colIndex, 100, teamNumber, "gold", 0, 10, 50)); } numBuildingsToCreate--; } }