Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SAMPLE_SCENE_1
- #define SAMPLE_SCENE_1
- #include "Scene.h"
- #include "GameObject.h"
- int bossHP= 5;
- bool playerHit = false;
- class SampleScene1 : public Scene
- {
- public:
- void onPollEvent(const Event& event)
- {
- if (event.type == sf::Event::KeyReleased)
- if (event.key.code == Keyboard::B)
- manager->changeScene("HelloWorld");
- }
- void start()
- {
- //Red circle
- float scalar = 100.f;
- Vector2f rightVector(1, 0);
- currentIndex = 1;
- //Control circle
- Vector2f midPosition(100, 300);
- Vector2f bossmidPosition(410, 50);
- boss = addSquare(Color::Green, 75.f);
- walls.push_back(boss);
- boss->bossSetPos(bossmidPosition - (rightVector) * 50.f);
- player = addSquare(Color::White, 25.f);
- walls.push_back(boss);
- player->setPosition(midPosition - (rightVector) * 50.f);
- }
- void update(float deltaTime)
- {
- const int INPUT_SIZE = 4;
- //Playerdirection
- Keyboard::Key directionKeys[INPUT_SIZE]{
- Keyboard::A, Keyboard::D,
- Keyboard::W, Keyboard::S
- };
- Vector2f direction[INPUT_SIZE]{
- Vector2f(-3,0), Vector2f(3,0),
- Vector2f(0,-3), Vector2f(0,3)
- };
- //Bossdirection
- Keyboard::Key bossdirectionKeys[INPUT_SIZE]{
- Keyboard::Left, Keyboard::Right
- //Keyboard::Up, Keyboard::Down
- };
- Vector2f bossDirection[INPUT_SIZE]{
- Vector2f(-3,0), Vector2f(3,0),
- Vector2f(0,-3), Vector2f(0,3)
- };
- Vector2f fishDir[INPUT_SIZE]{
- Vector2f(-1,0), Vector2f(1,0),
- Vector2f(0,-1), Vector2f(0,1)
- };
- const Vector2f& currentPosition = player->getPosition();
- const Vector2f& bosscurrentPosition = boss->getPosition();
- Vector2f offset;
- //Player Movement
- for (int i = 0; i < INPUT_SIZE; i++)
- {
- if (Keyboard::isKeyPressed(directionKeys[i]))
- {
- currentIndex = i;
- offset = direction[currentIndex];
- //How can we speed up the movement?
- //Change the vector 2f things on the direction for speed, the higher the better
- player->setPosition(currentPosition + offset);
- }
- }
- //Boss movement
- for (int i = 0; i < INPUT_SIZE; i++)
- {
- if (Keyboard::isKeyPressed(bossdirectionKeys[i]))
- {
- currentIndex = i;
- offset = bossDirection[currentIndex];
- //How can we speed up the movement?
- //Change the vector 2f things on the direction for speed, the higher the better
- boss->setPosition(bosscurrentPosition + offset);
- }
- }
- //Spawn Fish
- //if (fishSpawnTime.getElapsedTime().asSeconds() > 3)
- //{
- // int random = rand() % 2;
- // Vector2f fishDirection = fishDir[random];
- // GameObject* fish = addSquare(Color::Blue, 40.f);
- // float y = 200 + (10.f * (rand() % 30));
- // float x = 0;
- // if (fishDirection.x < 0)
- // x = window->getSize().x;
- // Projectile* d = new Projectile(10, fish);
- // fish->getBody()->setVelocity(fishDirection);
- // fish->setPosition(Vector2f(x, y));
- // projectiles.push_back(d);
- // fishSpawnTime.restart();
- //}
- //bullet making
- float time = shootTime.getElapsedTime().asSeconds();
- if (time > 1 && Keyboard::isKeyPressed(Keyboard::Space))
- {
- GameObject* bullet = addSquare(Color::White, 10);
- Projectile* d = new Projectile(5, bullet);
- bullet->getBody()->setVelocity(Vector2f(0, -5));
- bullet->setPosition(player->getPosition());
- projectiles.push_back(d);
- shootTime.restart();
- }
- //float time = shootTime.getElapsedTime().asSeconds();
- if (time > 1 && Keyboard::isKeyPressed(Keyboard::BackSpace))
- {
- GameObject* bossBullet = addSquare(Color::Green, 20);
- bossProjectile* b = new bossProjectile(5, bossBullet);
- bossBullet->getBody()->setVelocity(Vector2f(0, 5));
- bossBullet->bossSetPos(boss->getPosition());
- // + Vector2f(-30, 20))
- bossProj.push_back(b);
- shootTime.restart();
- }
- //Check for any collision with the walls
- BoxCollider* playerCollider = player->getCollider();
- BoxCollider* bossCollider = boss->getCollider();
- bool hasCollided = isCollidingWithWall(playerCollider);
- //Return the player to it's previous position before the offset took place
- if (hasCollided)
- player->setPosition(currentPosition - offset);
- for (int i = 0; i < gameobjects.size(); i++)
- {
- GameObject* go = gameobjects[i];
- if (!go->getIsActive())
- continue;
- go->update();
- }
- for (int i = projectiles.size() - 1; i >= 0; i--)
- {
- Projectile* bullet = projectiles[i];
- bullet->countdown -= deltaTime;
- BoxCollider* bulletCollider = bullet->gameObject->getCollider();
- if (bullet->countdown <= 0 || isCollidingWithWall(bulletCollider))
- {
- bossHP--;
- cout << bossHP << endl;
- removeGameobject(bullet->gameObject);
- projectiles.erase(projectiles.begin() + i);
- }
- }
- for (int i = bossProj.size() - 1; i >= 0; i--)
- {
- bossProjectile* bossBullet = bossProj[i];
- bossBullet->countdown -= deltaTime;
- BoxCollider* bossBulletCollider = bossBullet->gameObject->getCollider();
- if (bossBullet->countdown <= 0 || isCollidingWithWallStuff(bossBulletCollider))
- {
- playerHit = true;
- removeGameobject(bossBullet->gameObject);
- bossProj.erase(bossProj.begin() + i);
- }
- }
- if (playerHit)
- {
- Vector2f youregone(-500, 0);
- player->setPosition(currentPosition + youregone);
- cout << "OVER" << endl;
- //removeGameobject(player);
- }
- else if (bossHP <= 0)
- {
- cout << "Boss dead" << endl;
- //removeGameobject(boss);
- }
- }
- void end()
- {
- deleteAllShapes();
- projectiles.clear();
- walls.clear();
- }
- void render()
- {
- for (int i = 0; i < gameobjects.size(); i++)
- {
- GameObject* go = gameobjects[i];
- if (!go->getIsActive())
- continue;
- go->render(*window);
- }
- }
- std::string getName()
- {
- return "SampleScene";
- }
- private:
- Clock shootTime;
- Clock fishSpawnTime;
- int currentIndex = 1;
- GameObject* player;
- GameObject* boss;
- vector<GameObject*> walls;
- vector<GameObject*> gameobjects;
- vector<Projectile*> projectiles;
- vector<bossProjectile*>bossProj;
- GameObject* addSquare(const Color& color, float size = 100)
- {
- GameObject* go = new GameObject();
- go->initAsSquare(color, size);
- gameobjects.push_back(go);
- return go;
- }
- void removeGameobject(GameObject* go)
- {
- for (int i = gameobjects.size() - 1; i >= 0; i--)
- if (go == gameobjects[i])
- gameobjects.erase(gameobjects.begin() + i);
- delete go;
- }
- void deleteAllShapes()
- {
- for (int i = 0; i < gameobjects.size(); i++)
- delete gameobjects[i];
- gameobjects.clear();
- }
- bool isCollidingWithGroup(BoxCollider* collider, const vector<GameObject*>& objs)
- {
- for (int i = 0; i < objs.size(); i++)
- {
- GameObject* target = objs[i];
- BoxCollider* other = target->getCollider();
- if (collider->isColliding(other))
- return true;
- }
- return false;
- }
- bool isCollidingStuff(BoxCollider* collider, GameObject*& objs)
- {
- GameObject* target = objs;
- BoxCollider* other = target->getCollider();
- if (collider->isColliding(other))
- return true;
- return false;
- }
- bool isCollidingWithWall(BoxCollider* collider)
- {
- return isCollidingWithGroup(collider, walls);
- }
- bool isCollidingWithWallStuff(BoxCollider* collider)
- {
- return isCollidingStuff(collider, player);
- }
- };
- #endif // !SAMPLE_SCENE_1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement