Point GameEngine::getRandomLoc() { int x = rand() % map.size(); int y = rand() % map[x].size(); if (map[x][y].getCellType() != 2 && map[x][y].getCellType() != 5 && map[x][y].getCellType() != 6) return Point(x, y); else return getRandomLoc(); }
void EntitySpawner::spawnCollectable(btDiscreteDynamicsWorld* curWorld, PowerupType p_type) { Powerup* pow = nullptr; switch(p_type) { case PowerupType::HEALTHGAIN: printf("spawned healthgain\n"); pow = new HealthGain(); break; case PowerupType::JUMPUP: printf("spawned jumpup\n"); pow = new JumpUp(); break; /*case PowerupType::SPEEDUP: printf("spawned speedup\n"); pow = new SpeedUp(); break;*/ default: { pow = nullptr; break; } } if (pow == nullptr) return; std::pair<int, int> p = getRandomLoc(); PosInfo pos; pos.x = p.first; pos.y = 90; pos.z = p.second; Collectable* ranCollectable = new Collectable(oid_collectable, pos, curWorld, pow); AddEntity(ClassId::COLLECTABLE, oid_collectable, ranCollectable); oid_collectable++; // Send Collectable Spawn packet btVector3 vec = ranCollectable->GetEntityPosition(); btQuaternion quat = ranCollectable->GetEntityRotation(); PosInfo out; out.cid = ClassId::COLLECTABLE; out.oid = ranCollectable->GetObjectId(); out.x = vec.getX(); out.y = vec.getY(); out.z = vec.getZ(); out.sub_id = CollectType::POWERUPCOLLECT; out.rotw = quat.getW(); out.rotx = quat.getX(); out.roty = quat.getY(); out.rotz = quat.getZ(); ServerGame::instance()->sendSpawnPacket(out); }
void EntitySpawner::spawnCollectable(btDiscreteDynamicsWorld* curWorld, WeaponType w_type) { Weapon* wp; //switch (w_type) switch (w_type) { case WeaponType::SEEDGUN: { printf("spawned seedgun\n"); wp = new SeedGun(curWorld); break; } case WeaponType::BOUNCEGUN: { printf("spawned bouncegun\n"); wp = new BounceGun(curWorld); break; } case WeaponType::GRENADELAUNCHER: { printf("spawned grenadelauncher\n"); wp = new GrenadeLauncher(curWorld); break; } case WeaponType::TELEPORTGUN: { printf("spawned teleportgun\n"); wp = new TeleportGun(curWorld); break; } case WeaponType::BLASTMINE: { printf("spawned blastmine\n"); wp = new BlastMine(curWorld); break; } case WeaponType::SHOTGUN: { printf("spawned shotgun\n"); wp = new Shotgun(curWorld); break; } default: { wp = nullptr; break; } } if (wp == nullptr) return; std::pair<int, int> p = getRandomLoc(); PosInfo pos; pos.x = p.first; pos.y = 90; pos.z = p.second; Collectable* ranCollectable = new Collectable(oid_collectable, pos, curWorld, wp); AddEntity(ClassId::COLLECTABLE, oid_collectable, ranCollectable); oid_collectable++; // Send Collectable Spawn packet btVector3 vec = ranCollectable->GetEntityPosition(); btQuaternion quat = ranCollectable->GetEntityRotation(); PosInfo out; out.cid = ClassId::COLLECTABLE; out.oid = ranCollectable->GetObjectId(); out.x = vec.getX(); out.y = vec.getY(); out.z = vec.getZ(); out.sub_id = CollectType::WEAPONCOLLECT; out.rotw = quat.getW(); out.rotx = quat.getX(); out.roty = quat.getY(); out.rotz = quat.getZ(); ServerGame::instance()->sendSpawnPacket(out); }
void GameEngine::generateLevel() { //Clear out the last level for (vector<Actor*>::iterator it = monsters.begin(); it != monsters.end(); ++it) delete (*it); monsters.clear(); for (vector<Pickup*>::iterator it = pickups.begin(); it != pickups.end(); ++it) delete (*it); pickups.clear(); map.clear(); playableMap.clear(); //Set tile graphics for curr floor if (currLevel == 1) { wallTile = 0x103; floorTile = 0x100; } else if (currLevel > 1) { wallTile = 0x14D; floorTile = 0x13E; delete boss; } //Initialize the outer map vector for (int i = 0; i < worldSize; i++) { vector<Cell> v; map.push_back(v); } //Generate the map boundaries lib.loop_portion(Point(0, 0), Point(worldSize, worldSize), [&](Point p) { Cell c(p, 2, wallTile, 0); map[p.x()].push_back(c); }); //Generate the random rooms for (int i = 0; i < rand() % 130 + 100; i++) { Point pStart((rand() % 55 + 1), (rand() % 55 + 1)); Point pEnd(pStart.x() + (rand() % 6 + 3), pStart.y() + (rand() % 6 + 3)); fillBox(pStart, pEnd); } //Flood fill to connect the world int currZone = 0; vector<int> zoneSize; lib.loop_portion(Point(0, 0), Point(worldSize, worldSize), [&](Point p) { if (map[p.x()][p.y()].getCellType() != 2 && map[p.x()][p.y()].getZone() == 0) { currZone++; int size = floodFill(map[p.x()][p.y()].getLoc(), currZone, 0); zoneSize.push_back(size); } }); //Get biggest zone in array int max = zoneSize[0]; int largestZone = 0; for (unsigned int i = 1; i < zoneSize.size(); i++) { if (zoneSize[i] > max) { largestZone = i; max = zoneSize[i]; } } largestZone++; //Offset for vector index //Get rid of everything but largest zone int end = worldSize - 1; lib.loop_portion(Point(0, 0), Point(worldSize, worldSize), [&](Point p) { Cell& c = map[p.x()][p.y()]; if (c.getZone() != largestZone) { if ((p.x() + 1 < end && map[p.x() + 1][p.y()].getZone() == largestZone) || (p.x() - 1 > 0 && map[p.x() - 1][p.y()].getZone() == largestZone) || (p.y() + 1 < end && map[p.x()][p.y() + 1].getZone() == largestZone) || (p.y() - 1 > 0 && map[p.x()][p.y() - 1].getZone() == largestZone) || (p.x() - 1 > 0 && p.y() - 1 > 0 && map[p.x() - 1][p.y() - 1].getZone() == largestZone) || (p.x() - 1 > 0 && p.y() + 1 < end && map[p.x() - 1][p.y() + 1].getZone() == largestZone) || (p.x() + 1 < end && p.y() - 1 > 0 && map[p.x() + 1][p.y() - 1].getZone() == largestZone) || (p.x() + 1 < end && p.y() + 1 < end && map[p.x() + 1][p.y() + 1].getZone() == largestZone)) { c.setTile(wallTile); c.setTileType(2); } else { c.setTile(0); c.setTileType(2); } } else if (c.getLoc().x() == end) c.setTile(0); }); //Set player starting position lib.loop_portion(Point(0, 0), Point(worldSize - 1, worldSize - 1), [&](Point p) { if (map[p.x()][p.y()].getZone() != 0 && map[p.x()][p.y()].getCellType() != 2) playableMap.push_back(map[p.x()][p.y()].getLoc()); }); //Add monsters for (unsigned int i = 0; i < playableMap.size(); i++) { int chance = rand() % 30; if (chance == 15) { //Make the monster ActorDef def = pickfromtable<ActorDef>(actordefs); monsters.push_back(new Actor(def, playableMap[i], false, false)); map[playableMap[i].x()][playableMap[i].y()].setActor(monsters.back()); //Give chance to spawn with item chance = rand() % 5 + 1; //Give 10% chance to spawn if (chance == 3) { PickupDef def = pickfromtable<PickupDef>(pickupdefs); pickups.push_back(new Pickup(def, playableMap[i])); map[playableMap[i].x()][playableMap[i].y()].setPickup(pickups.back()); } } } //Set starting pos Point heroLoc = getRandomLoc(); screenOrientation = heroLoc; hero->setLoc(screenOrientation); //Add stairs, shop, boss stairLoc = addToRandomLoc(Cell(Point(), 5, 0x1A0, -1)); shop = Shop(addToRandomLoc(Cell(Point(), 6, 0x0C0, -1)), currLevel, pickupdefs, &lib, hero, &log, &invLog); bossLoc = addToRandomLoc(Cell(Point(), 7, 0x1AB, -1)); bossInRoomLoc = Point(5, 2); //Instantiate the boss map for (int i = 0; i < bossMapSize; i++) { vector<Cell> row; for (int j = 0; j < bossMapSize; j++) { if (i == 0 || i == bossMapSize - 1 || j == 0 || j == bossMapSize - 1) row.push_back(Cell(Point(i, j), 2, 0x14B, 1)); else row.push_back(Cell(Point(i, j), 1, 0x122, 1)); } bossMap.push_back(row); } //Add the boss to its location in the boss map int which = rand() % bossdefs.size(); boss = new Actor(bossdefs[which], bossInRoomLoc, false, true); bossMap[bossInRoomLoc.x()][bossInRoomLoc.y()].setActor(boss); }