World::World(const int &ROWS, const int &COLS, const int &SMOOTH_LEVEL, const int &GRASS_CHANCE) : Map(ROWS, COLS), SMOOTH_LEVEL(SMOOTH_LEVEL), GRASS_CHANCE(GRASS_CHANCE) { for(int y = 0; y < ROWS; y++) { for(int x = 0; x < COLS; x++) { Tile::TileType type = GetRandomTileType(); At(x, y) = Tile(x, y, type); } } for(int i = 0; i < SMOOTH_LEVEL; i++) { if(i == SMOOTH_LEVEL - 1) { CreateBorder(); } CellularAutomata(); } int numDungeonsToCreate = GameMath::Random(ROWS/2, ROWS/3); for (int i = 0; i < numDungeonsToCreate; i++) { Vector2 newDungeonCoords; bool otherDungeonNear = true; while(otherDungeonNear) { otherDungeonNear = false; newDungeonCoords = *GetFreeTileCoords(COLS, ROWS); const int DUNGEON_PADDING = 3; int x = newDungeonCoords.x - DUNGEON_PADDING; int y = newDungeonCoords.y - DUNGEON_PADDING; int xMax = newDungeonCoords.x + DUNGEON_PADDING; int yMax = newDungeonCoords.y + DUNGEON_PADDING; for (; y < yMax; y++) { for (; x < xMax; x++) { if(!IsOutOfBounds(x, y) && At(x, y).type == Tile::DOOR && (x != newDungeonCoords.x && y != newDungeonCoords.y)) { otherDungeonNear = true; break; } } } } At(newDungeonCoords).type = Tile::DOOR; } entryCoords = GetFreeTileCoords(COLS, ROWS); spawnCoords = entryCoords; }
void UTerrainManager::DiscoverCave(int32 x, int32 y) { if (IsOutOfBounds(x, y)) return; ATile* t = GetTile(x, y); if (!t->IsVisible) { t->IsVisible = true; DiscoverCave(x - 1, y); DiscoverCave(x + 1, y); DiscoverCave(x, y - 1); DiscoverCave(x, y + 1); DiscoverCave(x - 1, y + 1); DiscoverCave(x + 1, y + 1); DiscoverCave(x + 1, y - 1); DiscoverCave(x - 1, y - 1); } if (t->IsWall()) { if (UpdateWallAtTile(t)) CollapseTile(t); } else { t->GetStaticMeshComponent()->SetStaticMesh(t->GroundMesh); t->GetStaticMeshComponent()->SetMaterial(0, this->BiomeMaterials[t->TextureIndex()]); } }
void CPFA_loop_functions::PowerLawFoodDistribution() { argos::Real foodOffset = 3.0 * FoodRadius; size_t foodPlaced = 0; size_t powerLawLength = 1; size_t maxTrials = 200; size_t trialCount = 0; std::vector<size_t> powerLawClusters; std::vector<size_t> clusterSides; argos::CVector2 placementPosition; for(size_t i = 0; i < PowerRank; i++) { powerLawClusters.push_back(powerLawLength * powerLawLength); powerLawLength *= 2; } for(size_t i = 0; i < PowerRank; i++) { powerLawLength /= 2; clusterSides.push_back(powerLawLength); } for(size_t h = 0; h < powerLawClusters.size(); h++) { for(size_t i = 0; i < powerLawClusters[h]; i++) { placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); while(IsOutOfBounds(placementPosition, clusterSides[h], clusterSides[h])) { trialCount++; placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); if(trialCount > maxTrials) { argos::LOGERR << "PowerLawDistribution(): Max trials exceeded!\n"; break; } } for(size_t j = 0; j < clusterSides[h]; j++) { for(size_t k = 0; k < clusterSides[h]; k++) { foodPlaced++; FoodList.push_back(placementPosition); FoodColoringList.push_back(argos::CColor::BLACK); placementPosition.SetX(placementPosition.GetX() + foodOffset); } placementPosition.SetX(placementPosition.GetX() - (clusterSides[h] * foodOffset)); placementPosition.SetY(placementPosition.GetY() + foodOffset); } } } FoodItemCount = foodPlaced; }
void CPFA_loop_functions::RandomFoodDistribution() { FoodList.clear(); argos::CVector2 placementPosition; for(size_t i = 0; i < FoodItemCount; i++) { placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); while(IsOutOfBounds(placementPosition, 1, 1)) { placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); } FoodList.push_back(placementPosition); FoodColoringList.push_back(argos::CColor::BLACK); } }
void CPFA_loop_functions::ClusterFoodDistribution() { argos::Real foodOffset = 3.0 * FoodRadius; size_t foodToPlace = NumberOfClusters * ClusterWidthX * ClusterLengthY; size_t foodPlaced = 0; argos::CVector2 placementPosition; FoodItemCount = foodToPlace; for(size_t i = 0; i < NumberOfClusters; i++) { placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); while(IsOutOfBounds(placementPosition, ClusterLengthY, ClusterWidthX)) { placementPosition.Set(RNG->Uniform(ForageRangeX), RNG->Uniform(ForageRangeY)); } for(size_t j = 0; j < ClusterLengthY; j++) { for(size_t k = 0; k < ClusterWidthX; k++) { foodPlaced++; /* #include <argos3/plugins/simulator/entities/box_entity.h> string label("my_box_"); label.push_back('0' + foodPlaced++); CBoxEntity *b = new CBoxEntity(label, CVector3(placementPosition.GetX(), placementPosition.GetY(), 0.0), CQuaternion(), true, CVector3(0.1, 0.1, 0.001), 1.0); AddEntity(*b); */ FoodList.push_back(placementPosition); FoodColoringList.push_back(argos::CColor::BLACK); placementPosition.SetX(placementPosition.GetX() + foodOffset); } placementPosition.SetX(placementPosition.GetX() - (ClusterWidthX * foodOffset)); placementPosition.SetY(placementPosition.GetY() + foodOffset); } } }
Tile::TileType World::GetAvgTileTypeAround(const int &xCoord, const int &yCoord) { int adjacentGrass = 0; int adjacentRocks = 0; int adjacentTrees = 0; for(int y = yCoord-1; y <= yCoord+1; y++) { for(int x = xCoord-1; x <= xCoord+1; x++) { if(IsOutOfBounds(x, y) == false) { if(At(x, y).type == Tile::GRASS) { adjacentGrass++; } else if(At(x, y).type == Tile::ROCK) { adjacentRocks++; } else { adjacentTrees++; } } } } if(adjacentGrass > (adjacentRocks + adjacentTrees)) { return Tile::GRASS; } else if(adjacentRocks > adjacentTrees) { return Tile::ROCK; } else { return Tile::TREE; } }
bool UTerrainManager::IsWall(int32 x, int32 y) { if (IsOutOfBounds(x, y)) return true; ATile* t = GetTile(x, y); return !t->IsVisible || t->IsWall(); }
ATile* UTerrainManager::GetTile(int32 x, int32 y) { if (IsOutOfBounds(x, y)) return nullptr; return Tiles[GetTerrainIndex(x, y)]; }
void UTerrainManager::CollapseTile(ATile* t) { SpawnRubble(t); //RubbleSpawned ? .Invoke(this, new TileEventArgs(t)); for (int x = t->X - 1; x < t->X + 2; ++x) { for (int y = t->Y - 1; y < t->Y + 2; ++y) { if (IsOutOfBounds(x, y) || (x == t->X && y == t->Y)) continue; if (!GetTile(x, y)->IsVisible) { DiscoverCave(x, y); OnCaveDiscovered.Broadcast(); } if (UpdateWallAtTile(x, y)) { CollapseTile(x, y); } } } int spiders = 2; for (int i = 5; i < spiders; ++i) { SpawnObject(t->X, t->Y, FString("SmallSpider")); } int numberOfCrystals = 0; int numberOfOre = 0; switch (t->Cror) { //crystals case 1: case 3: numberOfCrystals = 1; break; case 5: case 7: numberOfCrystals = 3; break; case 9: case 0xB: numberOfCrystals = 5; break; case 0xD: case 0x13: numberOfCrystals = 11; break; case 0x11: case 0x17: numberOfCrystals = 25; break; //Ore case 2: case 4: numberOfOre = 1; break; case 6: case 8: numberOfOre = 3; break; case 0xA: case 0xC: case 0x10: numberOfOre = 5; break; case 0xE: case 0x14: numberOfOre = 11; break; case 0x12: case 0x18: numberOfOre = 25; break; } for (int i = 0; i < numberOfCrystals; ++i) SpawnObject(t->X, t->Y, FString("PowerCrystal")); for (int i = 0; i < numberOfOre; ++i) SpawnObject(t->X, t->Y, FString("Ore")); OnWallColapsed.Broadcast(t); }
bool UTerrainManager::IsTileVisible(FVector position) { int32 x, y; GetTilePosFromWorld(position, x, y); return !IsOutOfBounds(x, y) && GetTile(x, y)->IsVisible; }
void UTerrainManager::BuildTerrain(TArray<uint8>* levelData) { this->Tiles.SetNumZeroed(this->levelSizeX * this->levelSizeY); for (int x = 0; x < levelSizeX; ++x) { for (int y = 0; y < levelSizeY; ++y) { int c = GetMapFileIndex(x, y); uint8 surfValue = levelData[LevelFile::Surf][c]; uint8 duggValue = levelData[LevelFile::Dugg][c]; uint8 pathValue = 0; if (levelData[LevelFile::Path].Num() != 0) pathValue = levelData[LevelFile::Path][c]; uint8 crorValue = 0; if (levelData[LevelFile::Cror].Num() != 0) crorValue = levelData[LevelFile::Cror][c]; uint8 fallValue = 0; if (levelData[LevelFile::Fall].Num() != 0) fallValue = levelData[LevelFile::Fall][c]; uint8 height = 0; if (levelData[LevelFile::High].Num() != 0) height = levelData[LevelFile::High][c]; FVector4 heights = FVector4(height, height, height, height); if (levelData[LevelFile::High].Num() != 0) { if (!IsOutOfBounds(x + 1, y)) heights.Y = levelData[LevelFile::High][GetMapFileIndex(x + 1, y)]; if (!IsOutOfBounds(x, y + 1)) heights.Z = levelData[LevelFile::High][GetMapFileIndex(x, y + 1)]; if (!IsOutOfBounds(x + 1, y + 1)) heights.W = levelData[LevelFile::High][GetMapFileIndex(x + 1, y + 1)]; } FActorSpawnParameters sp; sp.Name = FName(TEXT("Tile"), GetTerrainIndex(x, y)); ATile* t = pWorld->SpawnActor<ATile>(sp); t->SetParameters(x, y, surfValue, duggValue, pathValue, crorValue, fallValue); if (levelData[LevelFile::Tuto].Num() != 0) { //int tuto = levelData[LevelFile::Tuto][c]; //if (tutoTiles.Length <= tuto) //{ // var oldTuto = tutoTiles; // tutoTiles = new List<Tile>[tuto + 4]; // Array.Copy(oldTuto, tutoTiles, oldTuto.Length); //} //if (tutoTiles[tuto] == nullptr) // tutoTiles[tuto] = new List<Tile>(); //tutoTiles[tuto].Add(t); } this->Tiles[GetTerrainIndex(x, y)] = t; } } for (int x = 0; x < levelSizeX; ++x) { for (int y = 0; y < levelSizeY; ++y) { if (UpdateWallAtTile(x, y)) { CollapseTile(x, y); } } } }
void Game::Run() { //TIme Step sf::Clock clock; sf::Time timeSinceLastUpdate = sf::Time::Zero; const sf::Time timePerFrame = sf::seconds( 1.f / 60.f ); //main Character std::shared_ptr<Hero> hero = std::make_shared<Hero>(sf::Vector2f(200.f,200.f), 32.f, 1.f, *this); mDrawableList.push_back(hero); //main Platform std::shared_ptr<Platform> platform = std::make_shared<Platform>(1, sf::Vector2f(50.f, 500.f), 400.f, 100.f, *this); mDrawableList.push_back(platform); //main loop while (mWindow->isOpen()) { timeSinceLastUpdate += clock.restart(); //std::cout << 1.f / timeSinceLastUpdate.asSeconds() << "\n"; while( timeSinceLastUpdate > timePerFrame ) { timeSinceLastUpdate -= timePerFrame; //event loop / non-blocking sf::Event event; while (mWindow->pollEvent(event)) { if (event.type == sf::Event::Closed) mWindow->close(); if(event.type == sf::Event::MouseButtonPressed) { if(event.mouseButton.button == sf::Mouse::Left) hero->Shooting(); } if(event.type == sf::Event::KeyPressed) { if(event.key.code == sf::Keyboard::Space) hero->Jump(); } } mDrawableList.insert(mDrawableList.end(), mNewDrawables.begin(), mNewDrawables.end()); mNewDrawables.clear(); for(std::vector<IDrawablePtr>::iterator it = mDrawableList.begin(); it != mDrawableList.end();) { (*it)->Update(timePerFrame.asSeconds(), *mWindow); if(IsOutOfBounds(*it))// || HasLifetimeExpired(*it, currentTime)) { it = mDrawableList.erase(it); } else { it++; } } } mWindow->clear(); mWindow->draw(mBgSprite); for(std::vector<IDrawablePtr>::iterator it = mDrawableList.begin(); it != mDrawableList.end(); ++it) { (*it)->Draw(*mWindow); } mWindow->display(); } }
void OvrScrollManager::Frame( float deltaSeconds, unsigned int controllerState ) { // -- // Controller Input Handling bool forwardScrolling = false; bool backwardScrolling = false; if( CurrentScrollType == HORIZONTAL_SCROLL ) { forwardScrolling = ( controllerState & ( BUTTON_LSTICK_RIGHT | BUTTON_DPAD_RIGHT ) ) != 0; backwardScrolling = ( controllerState & ( BUTTON_LSTICK_LEFT | BUTTON_DPAD_LEFT ) ) != 0; } else if( CurrentScrollType == VERTICAL_SCROLL ) { forwardScrolling = ( controllerState & ( BUTTON_LSTICK_DOWN | BUTTON_DPAD_DOWN ) ) != 0; backwardScrolling = ( controllerState & ( BUTTON_LSTICK_UP | BUTTON_DPAD_UP ) ) != 0; } if ( forwardScrolling || backwardScrolling ) { CurrentScrollState = SMOOTH_SCROLL; } SetEnableAutoForwardScrolling( forwardScrolling ); SetEnableAutoBackwardScrolling( backwardScrolling ); // -- // Logic update for the current scroll state switch( CurrentScrollState ) { case SMOOTH_SCROLL: { // Auto scroll during wrap around initiation. if( ( !RestrictedScrolling ) || // Either not restricted scrolling ( RestrictedScrolling && ( TouchDirectionLocked != NO_LOCK ) ) ) // or restricted scrolling with touch direction locked { if ( TouchIsDown ) { if ( ( Position < 0.0f ) || ( Position > MaxPosition ) ) { // When scrolled out of bounds and touch is still down, // auto-scroll to initiate wrap around easily. float totalDistance = 0.0f; for ( int i = 0; i < Deltas.GetNum(); ++i ) { totalDistance += Deltas[i].d; } if ( totalDistance < -EPSILON_VEL ) { Velocity = -1.0f; } else if ( totalDistance > EPSILON_VEL ) { Velocity = 1.0f; } else { Velocity = PrevAutoScrollVelocity; } } else { Velocity = 0.0f; } PrevAutoScrollVelocity = Velocity; } } if ( !IsScrolling() && IsOutOfBounds() ) { CurrentScrollState = BOUNCE_SCROLL; const float bounceBackVelocity = ScrollBehavior.BounceBackVelocity; if ( Position < 0.0f ) { Velocity = bounceBackVelocity; } else if ( Position > MaxPosition ) { Velocity = -bounceBackVelocity; } Velocity = GetModifiedVelocity( Velocity ); } } break; case BOUNCE_SCROLL: { if( !IsOutOfBounds() ) { CurrentScrollState = SMOOTH_SCROLL; if( Velocity > 0.0f ) // Pulled at the beginning { Position = 0.0f; } else if( Velocity < 0.0f ) // Pulled at the end { Position = MaxPosition; } Velocity = 0.0f; } } break; } // -- // Scrolling physics update AccumulatedDeltaTimeInSeconds += deltaSeconds; OVR_ASSERT( AccumulatedDeltaTimeInSeconds <= 10.0f ); while( true ) { if( AccumulatedDeltaTimeInSeconds < 0.016f ) { break; } AccumulatedDeltaTimeInSeconds -= 0.016f; if( CurrentScrollState == SMOOTH_SCROLL || CurrentScrollState == BOUNCE_SCROLL ) // While wrap around don't slow down scrolling { Velocity -= Velocity * ScrollBehavior.FrictionPerSec * 0.016f; if( fabsf(Velocity) < EPSILON_VEL ) { Velocity = 0.0f; } } Position += Velocity * 0.016f; if( CurrentScrollState == BOUNCE_SCROLL ) { if( Velocity > 0.0f ) { Position = Alg::Clamp( Position, -ScrollBehavior.Padding, MaxPosition ); } else { Position = Alg::Clamp( Position, 0.0f, MaxPosition + ScrollBehavior.Padding ); } } else { Position = Alg::Clamp( Position, -ScrollBehavior.Padding, MaxPosition + ScrollBehavior.Padding ); } } }