Skip to content
Snippets Groups Projects
Unverified Commit 9efec5ff authored by Clément REPEL's avatar Clément REPEL Committed by GitHub
Browse files

Merge pull request #25 from eliasmorio/two-players

:sparkles: introduce second player
parents 90363aed 045a5c26
Branches
No related tags found
No related merge requests found
......@@ -25,11 +25,13 @@ Game::Game(Clavier* clavier) : entityCount(0), exitRequested(false),
playerInvincible(false), invincibilityTimer(0),
blinkTimer(0), showPlayer(true) {
this->clavier = clavier;
playerShip2 = nullptr;
}
Game::~Game() {
delete playerShip;
delete playerShip2;
for (int i = 0; i < asteroids.getSize(); ++i) {
delete asteroids.get(i);
}
......@@ -39,7 +41,8 @@ Game::~Game() {
}
void Game::init(int selectedShipType) {
playerShip = new Ship(FIXED(160, 1), FIXED(100, 1), FIXED(0, 1), FIXED(0, 1), -90, selectedShipType);
playerShip = new Ship(FIXED(80, 1), FIXED(100, 1), FIXED(0, 1), FIXED(0, 1), -90, selectedShipType);
playerShip2 = new Ship(FIXED(240, 1), FIXED(100, 1), FIXED(0, 1), FIXED(0, 1), -90, selectedShipType);
spawnAsteroids(5);
......@@ -91,6 +94,7 @@ void Game::handleInput() {
return;
}
// Controls for playerShip
if (clavier->isKeyPressed('a')) {
playerShip->rotate(5);
}
......@@ -100,10 +104,25 @@ void Game::handleInput() {
if (clavier->isKeyPressed('w')) {
playerShip->increaseVelocity();
}
if (clavier->isKeyPressed(' ')) {
if (clavier->isKeyPressed('c')) {
Bullet* bullet = playerShip->shootBullet();
bullets.add(bullet);
}
// Controls for playerShip2
if (clavier->isKeyPressed('j')) {
playerShip2->rotate(5);
}
if (clavier->isKeyPressed('l')) {
playerShip2->rotate(-5);
}
if (clavier->isKeyPressed('i')) {
playerShip2->increaseVelocity();
}
if (clavier->isKeyPressed('n')) {
Bullet* bullet = playerShip2->shootBullet();
bullets.add(bullet);
}
}
void Game::updateScoreboard() {
......@@ -149,6 +168,7 @@ void Game::render() {
if (showPlayer) {
drawEntity(playerShip);
drawEntity(playerShip2);
}
for (int i = 0; i < asteroids.getSize(); ++i) {
......@@ -193,8 +213,9 @@ void Game::spawnAsteroids(int count) {
Vec2 position(FIXED(random(0, 319), 1), FIXED(random(0, 179), 1));
Asteroid* asteroid = new Asteroid(position.x, position.y, FIXED(random(-1, 1), 1), FIXED(random(-1, 1), 1), AsteroidSize::LARGE);
// Ensure the asteroid does not spawn in the safe zone around the ship
while (Physics::checkCollision(playerShip->getPosition(), safeZoneRadius, asteroid->getPosition(), asteroid->getRadius())) {
// Ensure the asteroid does not spawn in the safe zone around the ships
while (Physics::checkCollision(playerShip->getPosition(), safeZoneRadius, asteroid->getPosition(), asteroid->getRadius()) ||
Physics::checkCollision(playerShip2->getPosition(), safeZoneRadius, asteroid->getPosition(), asteroid->getRadius())) {
position = Vec2(FIXED(random(0, 319), 1), FIXED(random(0, 179), 1));
asteroid->setPosition(position);
}
......@@ -227,9 +248,10 @@ void Game::update() {
}
}
// Update ship
// Update ships
if (!playerInvincible) {
playerShip->move(DELTA_TIME);
playerShip2->move(DELTA_TIME);
}
// Update bullets
......@@ -282,30 +304,17 @@ void Game::update() {
}
}
// Check for collisions between the ship and asteroids
// Check for collisions between the ships and asteroids
if (!playerInvincible) {
for (int i = 0; i < asteroids.getSize(); ++i) {
Entity* asteroidEntity = static_cast<Entity*>(asteroids.get(i));
if (Physics::checkCollision(playerShip->getPosition(), playerShip->getRadius(),
asteroidEntity->getPosition(), asteroidEntity->getRadius())) {
Asteroid* asteroid = static_cast<Asteroid*>(asteroidEntity);
--lives;
playerShip->setPosition(GAME_CENTER);
playerShip->setAngle(-90);
playerShip->setVelocity(Vec2(0, 0));
AsteroidSize asteroidSize = asteroid->getSize();
asteroid->handleCollisionWithShip(asteroids);
playerInvincible = true;
invincibilityTimer = 3500; // 3,5s time for the asteroid to move from the spawn
blinkTimer = 0;
showPlayer = true;
break;
handleShipCollision(playerShip, asteroidEntity);
}
if (Physics::checkCollision(playerShip2->getPosition(), playerShip2->getRadius(),
asteroidEntity->getPosition(), asteroidEntity->getRadius())) {
handleShipCollision(playerShip2, asteroidEntity);
}
}
}
......@@ -318,4 +327,30 @@ void Game::update() {
if (lives <= 0) {
exitRequested = true;
}
}
// Helper method to handle ship collisions
void Game::handleShipCollision(Ship* ship, Entity* asteroidEntity) {
Asteroid* asteroid = static_cast<Asteroid*>(asteroidEntity);
--lives;
// Respawn the ship at its initial position
if (ship == playerShip) {
ship->setPosition(Vec2(FIXED(80, 1), FIXED(100, 1))); // Left side for playerShip
} else if (ship == playerShip2) {
ship->setPosition(Vec2(FIXED(240, 1), FIXED(100, 1))); // Right side for playerShip2
}
ship->setAngle(-90);
ship->setVelocity(Vec2(0, 0));
AsteroidSize asteroidSize = asteroid->getSize();
asteroid->handleCollisionWithShip(asteroids);
playerInvincible = true;
invincibilityTimer = 3500; // 3.5s time for the asteroid to move from the spawn
blinkTimer = 0;
showPlayer = true;
}
\ No newline at end of file
......@@ -29,6 +29,7 @@ class RenderThread;
class Game {
private:
Ship* playerShip;
Ship* playerShip2; // Second player ship
Set ships;
Set bullets;
Set asteroids;
......@@ -47,6 +48,9 @@ private:
int blinkTimer;
bool showPlayer;
// Helper method for handling ship collisions
void handleShipCollision(Ship* ship, Entity* asteroidEntity);
public:
// Synchronization primitives
static Semaphore inputSem;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment