int main(int argc, char *argv[]) { /* sf::RenderWindow app(sf::VideoMode(640, 480), "ECS"); sf::RectangleShape rectangle(sf::Vector2f(50.f, 50.f)); rectangle.setFillColor(sf::Color::Blue); sf::RectangleShape rectangle2(sf::Vector2f(50.f, 50.f)); rectangle2.setFillColor(sf::Color::Red); GameObject objects[2]; DescriptionSystem desc_sys; MovementSystem move_sys; RenderSystem renderer(&app); CollisionSystem collision_sys; DescriptionComponent desc(objects[0].getId()); PositionComponent pos(objects[0].getId()), pos2(objects[1].getId()); VelocityComponent vel(objects[0].getId(), 1.f), vel2(objects[1].getId(), 0.2f, 0.1f); DrawableComponent drawable(objects[0].getId()), drawable2(objects[1].getId()); CollidableComponent c1(objects[0].getId(), rectangle.getGlobalBounds()), c2(objects[1].getId(), rectangle2.getGlobalBounds()); drawable.drawable = &rectangle; drawable2.drawable = &rectangle2; //pos.position.y = 50.f; pos2.position.x = 250.f; pos2.position.y = 20.f; desc_sys.registerComponent("Description"); move_sys.registerComponent("Position"); move_sys.registerComponent("Velocity"); renderer.registerComponent("Drawable"); renderer.registerComponent("Position"); collision_sys.registerComponent("Position"); collision_sys.registerComponent("Collidable"); objects[0].addComponent(&desc); objects[0].addComponent(&pos); objects[0].addComponent(&vel); objects[0].addComponent(&drawable); objects[0].addComponent(&c1); objects[1].addComponent(&vel2); objects[1].addComponent(&drawable2); objects[1].addComponent(&pos2); objects[1].addComponent(&c2); if (desc_sys.canUpdate(objects[0])) desc_sys.update(objects[0]); sf::Event event; while (app.isOpen()) { while (app.pollEvent(event)) { if (event.type == sf::Event::Closed) app.close(); } for (int i(0) ; i < 2 ; ++i) if (move_sys.canUpdate(objects[i])) move_sys.update(objects[i]); app.clear(sf::Color::White); for (int i(0) ; i < 2 ; ++i) if (collision_sys.canUpdate(objects[i])) collision_sys.update(objects[i]); for (int i(0) ; i < 2 ; ++i) if (renderer.canUpdate(objects[i])) renderer.update(objects[i]); app.display(); } */ GameWorld world; GameObject *o1(world.addObject()); GameObject *o2(world.addObject()); sf::RectangleShape rectangle1(sf::Vector2f(50.f, 50.f)); rectangle1.setFillColor(sf::Color::Blue); sf::RectangleShape rectangle2(sf::Vector2f(50.f, 50.f)); rectangle2.setFillColor(sf::Color::Red); PhysicSystem *phys_sys(new PhysicSystem); phys_sys->registerComponent("Physic"); phys_sys->registerComponent("Position"); world.addSystem(phys_sys); PhysicComponent phys1(o1->getId(), 50, cpMomentForBox(50, 5, 5)), phys2(o2->getId(), 50, cpMomentForBox(50, 5, 5)); DescriptionComponent desc1(o1->getId()), desc2(o2->getId()); PositionComponent pos1(o1->getId()), pos2(o2->getId()); //VelocityComponent vel1(objects[0].getId(), 1.f), vel2(objects[1].getId(), 0.2f, 0.1f); DrawableComponent drawable1(o1->getId()), drawable2(o2->getId()); //CollidableComponent c1(objects[0].getId(), rectangle.getGlobalBounds()), c2(objects[1].getId(), rectangle2.getGlobalBounds()); phys1.shape = cpBoxShapeNew(phys1.body, 50.f, 50.f); phys2.shape = cpBoxShapeNew(phys2.body, 50.f, 50.f); cpBodySetPos(phys1.body, cpv(0, 0)); cpBodySetPos(phys2.body, cpv(200, 0)); cpSpaceAddShape(phys_sys->getSpace(), phys1.shape); cpSpaceAddShape(phys_sys->getSpace(), phys2.shape); cpSpaceAddBody(phys_sys->getSpace(), phys1.body); cpSpaceAddBody(phys_sys->getSpace(), phys2.body); drawable1.drawable = &rectangle1; drawable2.drawable = &rectangle2; pos1.position.x = 50.f; pos2.position = sf::Vector2f(200.f, 150.f); o1->addComponent(&desc1); o1->addComponent(&drawable1); o1->addComponent(&pos1); o1->addComponent(&phys1); o2->addComponent(&desc2); o2->addComponent(&drawable2); o2->addComponent(&pos2); o2->addComponent(&phys2); while (true) { world.update(); } return 0; }
int GameSession::Run(){ // temp int numLobbyPlayer = 0; int numPlayers = 1; // /// MANAGERS LogManager* log = LogManager::GetLogManager(); InputManager* input = InputManager::getInstance(); AudioManager* audioMan = AudioManager::getAudioInstance(); RenderManager* renderMan = RenderManager::getRenderManager(); ResourceManager* resourceMan = ResourceManager::GetResourceManager(); SceneManager* sceneMan = SceneManager::GetSceneManager(); input->update(); ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// /// SYSTEMS SystemNetworkUpdater sysNetwork; SystemRenderUpdater sysRenderer; SystemInputUpdater sysInput; SystemLogicUpdater sysLogic; SystemPhysicsUpdater sysPhysics; SystemClassUpdater sysClass; SystemUIUpdater sysUI; /// ENTITIES PlayerObjectFactory pFactory; MinionObjectFactory mFactory; FeatherObjectFactory fFactory; PlatformObjectFactory plFactory; MidPlatObjectFactory mpFactory; numPlayers = NetworkManager::sInstance->GetPlayerCount(); //std::cout << NetworkManager::sInstance->GetLobbyId() << std::endl; /*for (const auto& iter : NetworkManager::sInstance->lobbyInfoMap){ std::cout << iter.first << std::endl; std::cout << "\tClass:" << iter.second.classType << std::endl; }*/ GameObject * player = NULL; Stats stats; /// try to join a game and give each user a unique character in the game unordered_map< uint64_t, PlayerInfo > lobby = NetworkManager::sInstance->getLobbyInfoMap(); Stats::resetStats(); int i = 0; bool local = true; for (auto &iter : lobby){ int classType = iter.second.classType; std::cout << "classType: " << classType << std::endl; if (iter.first == NetworkManager::sInstance->GetMyPlayerId()){ player = GameObjects.AddObject(pFactory.Spawn(iter.first, (classType % 50) + 1, (i % 2) + 1, local)); Stats::setLocalTeam((i % 2) + 1); Stats::addPlayer(iter.first, (i % 2) + 1); } else{ GameObjects.AddObject(pFactory.Spawn(iter.first, (classType % 50) + 1, (i % 2) + 1, !local)); Stats::addPlayer(iter.first, (i % 2) + 1); } ++i; } ///////////////////////////////////////////////////// /* * * * GAME LOOP * * * */ ///////////////////////////////////////////////////// bool gameloop = true; int var = 0; renderMan->zoom = 0.5; float size = 6; float ratio = 0.7f; int armswing = (int)size; int moveSpd = 1; int pressed = 0; int pressedTime = 3; int rotation = 0; audioMan->playByName("bgmBAAGameplay.ogg"); bool gameEndMusic = false; int mousecounter = 5; renderMan->zoom = 0.6f; SystemUIObjectQueue queue; //World Loading GameSession::LoadWorld(); GameSession::LoadPlayers(); GameSession::LoadHUD(player, queue); ///*auto spawning minion variables int minionCounter = 10000; //crosshair variables SDLRenderObject * crosshair = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 1109, -1000, -1000, -0.05f); SDLRenderObject * crosshairCharging = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 1111, -1000, -1000, -0.05f); crosshairCharging->visible = false; PlayerLogicComponent* playerLogic = dynamic_cast<PlayerLogicComponent*>(player->GetComponent(COMPONENT_LOGIC)); PlayerRenderComponent* playerRend = dynamic_cast<PlayerRenderComponent*>(player->GetComponent(COMPONENT_RENDER)); //midway fountain /* SDLRenderObject * fount = sceneMan->InstantiateObject(sceneMan->findLayer("layer2"), 101004, 40, 150, 0.005f); fount->setScale(0.5f); list<motion> motions; motions.push_back(makeMotion(keyframeAnimate(fount, 0, 15), 0, 1)); Animation * runWater = new Animation(100, motions); int aniCounter = 0; */ LoadBackgroundProps(); int aniCounter = 0; bool firstTime = true; Timing::sInstance.SetCountdownStart(); NetworkManager::sInstance->SetState(NetworkManager::NMS_Playing); std::cout << NetworkManager::sInstance->GetState() << std::endl; clock_t current_ticks, delta_ticks; clock_t fps = 0; string fpscounter = ""; SDLRenderObject * fpsHUD = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), -1, 5, 0, true); fpsHUD->setResourceObject(renderMan->renderText(fpscounter.c_str(), 255, 0, 0, 20, "VT323-Regular")); fpsHUD->setPos(0, 0); SDLRenderObject * leftbaseHUDicon = nullptr; SDLRenderObject * rightbaseHUDicon = nullptr; if (player->team == TEAM_PURPLE){ leftbaseHUDicon = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 7006, 0, 0, true); leftbaseHUDicon->flipH = true; rightbaseHUDicon = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 7005, 0, 0, true); rightbaseHUDicon->flipH = true; } else{ leftbaseHUDicon = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 7005, 0, 0, true); rightbaseHUDicon = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), 7006, 0, 0, true); } leftbaseHUDicon->setPos(-150, 600); string leftBaseHealth = ""; SDLRenderObject * leftbaseHUD = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), -1, 5, 0, true); leftbaseHUD->setResourceObject(renderMan->renderText(leftBaseHealth.c_str(), 255, 0, 0, 60, "VT323-Regular")); leftbaseHUD->setPos(15, 650); rightbaseHUDicon->setPos(1214, 600); string rightBaseHealth = ""; SDLRenderObject * rightbaseHUD = sceneMan->InstantiateObject(sceneMan->findLayer("layer1"), -1, 5, 0, true); rightbaseHUD->setResourceObject(renderMan->renderText(rightBaseHealth.c_str(), 255, 0, 0, 60, "VT323-Regular")); rightbaseHUD->setPos(1281, 650); renderMan->toggleCursor(0); /////////////Flip screen if needed///////////////// if (player->team == TEAM_PURPLE){ std::cout << "flip the screen" << std::endl; renderMan->flippedScreen = true; } ////////////////////////////////////////////////// bool gameEnd = false; inGameStatsRenderComponent inGameStats; while (gameloop) { current_ticks = clock(); if (input->isKeyDown(KEY_TAB)){ inGameStats.updateText(); inGameStats.toggleOn(true); } else{ inGameStats.toggleOn(false); } //std::cout << NetworkManager::sInstance->GetState() << std::endl; /*if (input->isKeyDown(KEY_Q)){ if (renderMan->cameraPoint.z < -5){ renderMan->cameraPoint.z += 1; } } if (input->isKeyDown(KEY_W)){ renderMan->cameraPoint.z -= 1; } if (input->isKeyDown(KEY_E)){ renderMan->flippedScreen = !renderMan->flippedScreen; }*/ /*clock_t t; t = clock();*/ if (input->isKeyDown(KEY_F)){ std::cout << "Number of feathers: " << GameObjects.dead_feathers.size() << std::endl; } if (input->isKeyDown(KEY_M)){ std::cout << "Number of minions: " << GameObjects.dead_minions.size() << std::endl; } if (input->isKeyDown(KEY_L)){ std::cout << "Alive Objects: " << GameObjects.alive_objects.size() << std::endl; } if (input->isKeyDown(KEY_Y)) { renderMan->ShakeScreen(.2f, .5f); } mousecounter++; //////////////////////////////////// if (numPlayers != 1) NetworkManager::sInstance->UpdateDelay(); //CAMERA MOVEMENT - based on player position if (!gameEnd){ if (player){ //Camera Shake if ((!rightBase->isAlive || !leftBase->isAlive) && !endedBaseShake) { endedBaseShake = true; renderMan->ShakeScreen(1, 1); } /*int mousePos = input->getMouseX(); int wid, hei; renderMan->getWindowSize(&wid, &hei); float xRatio = (mousePos - wid / 2) / float(wid / 2); float xPlus = (float)(wid / 4) - 20; //std::cout << xRatio << std::endl; renderMan->setCameraPoint(player->posX + xRatio*xPlus, 0);*/ float mouseX, mouseY; renderMan->windowCoordToWorldCoord(mouseX, mouseY, input->getMouseX(), input->getMouseY()); float cameraX = (player->posX + mouseX) / 2; //next line makes the camera favor staying closer to the player cameraX = (player->posX + cameraX) / 2; renderMan->setCameraPoint(cameraX, 0); } } int length = 20; float loop = (float)(var % length); //crosshair updating float crossX, crossY; renderMan->windowCoordToWorldCoord(crossX, crossY, input->getMouseX(), input->getMouseY()); crosshair->posX = crosshairCharging->posX = crossX; crosshair->posY = crosshairCharging->posY = crossY; float attackCDPercent = Timing::sInstance.GetAttackCooldownRemaining(); float chargePercent = 1.0f - (playerLogic->currChargePercentage > 0.75f ? 0.75f : playerLogic->currChargePercentage); if (chargePercent != 1.0f){ //sets scale during charge //crosshair->setScale(chargePercent); crosshairCharging->visible = false; crosshair->visible = true; playerRend->crosshairRef = crosshair; } else if (chargePercent == 1.0f && Timing::sInstance.AttackCooldownEnded()) { crosshairCharging->visible = false; crosshair->visible = true; crosshair->setScale(1.0f); playerRend->crosshairRef = crosshair; } else {//if(attackCDPercent < 1.0f) { //sets scale during attack cooldown crosshairCharging->setScale(attackCDPercent); crosshairCharging->visible = true; crosshair->visible = false; playerRend->crosshairRef = crosshairCharging; } //physics testing stuff PhysicsListener listener; GameWorld* gameWorld = GameWorld::getInstance(); gameWorld->physicsWorld->SetContactListener(&listener); gameWorld->update(); //update physics world //end physics testing stuff sysInput.InputUpdate(GameObjects.alive_objects); sysRenderer.RenderUpdate(GameObjects.alive_objects); sysLogic.LogicUpdate(GameObjects.alive_objects); sysPhysics.PhysicsUpdate(GameObjects.alive_objects); sysClass.ClassUpdate(GameObjects.alive_objects); sysUI.UIUpdate(queue.alive_objects); if (numPlayers != 1) sysNetwork.NetworkUpdate(GameObjects.alive_objects); //updates all timers Invoke::UpdateTimers(); if (input->isKeyDown(KEY_ESCAPE)) gameloop = false; //OBJECT POOLING - moves recently dead objects to respective dead pool for (unsigned int i = 0; i < GameObjects.alive_objects.size(); i++){ if (!GameObjects.alive_objects[i]->isAlive){ //object has died this last gameloop. send it to the object pool //std::cout << "ID: " << GameObjects.alive_objects[i]->ID << std::endl; if (GameObjects.alive_objects[i]->type == OBJECT_FEATHER){ GameObjects.dead_feathers.push_back(GameObjects.alive_objects[i]); } else if (GameObjects.alive_objects[i]->type == OBJECT_MINION){ GameObjects.dead_minions.push_back(GameObjects.alive_objects[i]); } else { GameObjects.dead_objects.push_back(GameObjects.alive_objects[i]); } GameObjects.alive_objects.erase(GameObjects.alive_objects.begin() + i); } } if (!firstTime) //allows culling to start after all initialization happens cullObjects(); if (NetworkManager::sInstance->IsMasterPeer() && Timing::sInstance.SpawnMinions()){ GameObjects.AddObject(mFactory.Spawn(minionCounter++, -900, 0, TEAM_YELLOW)); GameObjects.AddObject(mFactory.Spawn(minionCounter++, 900, 0, TEAM_PURPLE)); } input->update(); sceneMan->AssembleScene(); //triggers endgame screen if (Timing::sInstance.GetTimeRemainingS() <= 0) { /*if (player->team == TEAM_PURPLE){ std::cout << "flip the screen" << std::endl; renderMan->flippedScreen = false; }*/ gameEnd = true;//so the mouse stops registering int myTeam; for (unsigned int i = 0; i < players.size(); i++){ if (GamerServices::sInstance->GetLocalPlayerId() == players[i]->playerId){ myTeam = players[i]->team; } } std::cout << "END REACHED " << std::endl; //GameEnd end = GameEnd::GameEnd(); //end.runGameEnd(myTeam, leftBase, rightBase); gameloop = false; } if (Timing::sInstance.GetTimeRemainingS() <= 30 && !gameEndMusic){ gameEndMusic = true; audioMan->stopByName("bgmBAAGameplay.ogg"); audioMan->playByName("bgmBAAGameEnd.ogg"); } firstTime = false; delta_ticks = clock() - current_ticks; //the time, in ms, that took to render the scene if (delta_ticks > 0) fps = CLOCKS_PER_SEC / delta_ticks; //std::cout <<" FPS : " << fps << std::endl; fpscounter = std::to_string(fps); //leftBase->health //renderMan->renderText(fpscounter.c_str(), 255, 255, 0, 70, "BowlbyOneSC-Regular"); fpsHUD->setResourceObject(renderMan->renderText(fpscounter.c_str(), 0, 20, 240, 20, "VT323-Regular")); inGameStats.Update(); } ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// audioMan->stopByName("bgmBAAGameEnd.ogg"); std::cout << renderMan << std::endl; std::cout << renderMan << std::endl; konamiUnlocked = false; log->close(); //delete surf; //delete fount; //delete runWater; GameWorld::getInstance()->~GameWorld(); return SCENE_GAMEOVER; }