just a save commit bc I know I'm going to mess everything

This commit is contained in:
Thomas 2021-12-26 15:34:32 +01:00
parent 69ba7221d6
commit 14fef65b95
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
14 changed files with 149 additions and 87 deletions

3
README
View File

@ -5,3 +5,6 @@ Nommage en anglais
Pas de fonctions de +100 lignes
écran : constante 1280x720
Reminder : les missiles sont tirés par les envahisseurs, et les torpilles par le joueur

View File

@ -13,6 +13,12 @@ public:
unsigned alien_size;
unsigned distance;
unsigned missile_width;
unsigned missile_length; // auto defined from width
unsigned torpedo_width;
unsigned torpedo_length; // auto defined from width
unsigned player_width;
bool loadConfig();
};

View File

@ -1,32 +1,36 @@
#ifndef GUARD_GAME
#define GUARD_GAME
#ifndef GUARD_GAME_H
#define GUARD_GAME_H
#include <vector>
#include "mingl/mingl.h"
#include "drawEngine.h"
#include "pixelManager.h"
#include "config.h"
#include "utils.h"
#include "pos.h"
#include "position.h"
using namespace std;
class Game {
private:
DrawEngine drawer;
PixelManager pm;
Config conf;
unsigned baseX;
unsigned baseY;
aliensGrid grid;
vector<pos> missiles;
vector<pos> torpedos;
pos player;
vector<missile> missiles;
vector<torpedo> torpedos;
unsigned playerX;
void managePlayer();
bool manageInvaders();
void display();
// collision thingies
unsigned manageAllCollisions();
void remCollidingProjectiles();
void moveMissiles();
void moveTorpedos();
bool checkMissilesAndPlayer();
bool checkTorpedosAndInvaders();
public:
// in case someone wants to mess with the code, here's a minimal API, costs nothing to us
Game();

View File

@ -7,16 +7,18 @@
#include "mingl/shape/rectangle.h"
#include "mingl/shape/circle.h"
class DrawEngine{
class PixelManager{
public:
MinGL window;
DrawEngine();
PixelManager();
void dessinerInvader1(const nsGraphics::Vec2D& baseVector);
void dessinerInvader2(const nsGraphics::Vec2D& baseVector);
void dessinerInvader3(const nsGraphics::Vec2D& baseVector);
unsigned showInitialMenu();
unsigned showDeathMenu();
unsigned getScreenHeight();
unsigned getScreenWidth();
};

View File

@ -1,11 +0,0 @@
#ifndef SPACE_POS_H
#define SPACE_POS_H
#include<mingl/mingl.h>
class pos : public nsGraphics::Vec2D {
public:
bool isColliding(pos& p, unsigned rx, unsigned ry);
};
#endif //SPACE_POS_H

11
headers/position.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef SPACE_POSITION_H
#define SPACE_POSITION_H
#include<mingl/mingl.h>
class position : public nsGraphics::Vec2D {
public:
bool isColliding(position& p, unsigned rx, unsigned ry);
};
#endif //SPACE_POSITION_H

View File

@ -1,18 +1,27 @@
#ifndef SPACE_STRUCTS
#define SPACE_STRUCTS
#ifndef SPACE_STRUCTS_H
#define SPACE_STRUCTS_H
#include<vector>
#include<mingl/mingl.h>
#include "position.h"
// hardcoded values
#define PLAYER_WINS 1
#define INVADERS_WINS 2
#define PLAYER_HEIGHT 100
#define PLAYER_HEIGHT 100
#define PROJ_LENGTH_FACTOR 2
using namespace std;
#define PLAYER_HEIGHT 100
typedef unsigned Alien;
typedef vector<Alien> aliensLine;
typedef vector<aliensLine> aliensGrid;
typedef position missile;
typedef position torpedo;
// didn't want to use position because of the semantic with x and y
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2);
#endif

View File

@ -37,7 +37,7 @@ public:
/**
* @brief Constructeur de recopie pour la classe Vec2D
* @param[in] pos : Vec2D a copier
* @fn Vec2D(const Vec2D& pos);
* @fn Vec2D(const Vec2D& position);
*/
Vec2D(const Vec2D& pos);
@ -56,21 +56,21 @@ public:
/**
* @brief Opérateur d'addition
* @param[in] pos : Vecteur a additionner
* @fn Vec2D operator+(const Vec2D& pos) const;
* @fn Vec2D operator+(const Vec2D& position) const;
*/
Vec2D operator+(const Vec2D& pos) const;
/**
* @brief Opérateur de soustraction
* @param[in] pos : Vecteur a soustraire
* @fn Vec2D operator-(const Vec2D& pos) const;
* @fn Vec2D operator-(const Vec2D& position) const;
*/
Vec2D operator-(const Vec2D& pos) const;
/**
* @brief Opérateur de multiplication
* @param[in] pos : Vecteur a multiplier
* @fn Vec2D operator*(const Vec2D& pos) const;
* @fn Vec2D operator*(const Vec2D& position) const;
*/
Vec2D operator*(const Vec2D& pos) const;
@ -84,7 +84,7 @@ public:
/**
* @brief Opérateur de division
* @param[in] pos : Vecteur a diviser
* @fn Vec2D operator/(const Vec2D& pos) const;
* @fn Vec2D operator/(const Vec2D& position) const;
*/
Vec2D operator/(const Vec2D& pos) const;
@ -98,21 +98,21 @@ public:
/**
* @brief Opérateur modulo
* @param[in] pos : Vecteur avec lequel faire un modulo
* @fn Vec2D operator%(const Vec2D& pos) const;
* @fn Vec2D operator%(const Vec2D& position) const;
*/
Vec2D operator%(const Vec2D& pos) const;
/**
* @brief Opérateur d'égalité
* @param[in] pos : Vecteur avec lequel vérifier l'égalité
* @fn bool operator==(const Vec2D& pos) const;
* @fn bool operator==(const Vec2D& position) const;
*/
bool operator==(const Vec2D& pos) const;
/**
* @brief Opérateur d'inégalité
* @param[in] pos : Vecteur avec lequel vérifier l'inégalité
* @fn bool operator!=(const Vec2D& pos) const;
* @fn bool operator!=(const Vec2D& position) const;
*/
bool operator!=(const Vec2D& pos) const;
@ -120,7 +120,7 @@ public:
* @brief Opérateur de stricte infériorité
* (Vérifie la stricte infériorité de la magnitude des deux vecteurs)
* @param[in] pos : Vecteur avec lequel vérifier la stricte infériorité
* @fn bool operator<(const Vec2D& pos) const;
* @fn bool operator<(const Vec2D& position) const;
*/
bool operator<(const Vec2D& pos) const;
@ -128,7 +128,7 @@ public:
* @brief Opérateur de stricte supériorité
* (Vérifie la stricte supériorité de la magnitude des deux vecteurs)
* @param[in] pos : Vecteur avec lequel vérifier la stricte supériorité
* @fn bool operator>(const Vec2D& pos) const;
* @fn bool operator>(const Vec2D& position) const;
*/
bool operator>(const Vec2D& pos) const;
@ -136,7 +136,7 @@ public:
* @brief Opérateur d'infériorité
* (Vérifie l'infériorité de la magnitude des deux vecteurs)
* @param[in] pos : Vecteur avec lequel vérifier l'infériorité
* @fn bool operator<=(const Vec2D& pos) const;
* @fn bool operator<=(const Vec2D& position) const;
*/
bool operator<=(const Vec2D& pos) const;
@ -144,49 +144,49 @@ public:
* @brief Opérateur de supériorité
* (Vérifie la supériorité de la magnitude des deux vecteurs)
* @param[in] pos : Vecteur avec lequel vérifier la supériorité
* @fn bool operator>=(const Vec2D& pos) const;
* @fn bool operator>=(const Vec2D& position) const;
*/
bool operator>=(const Vec2D& pos) const;
/**
* @brief Opérateur d'assignement
* @param[in] pos : Vecteur source
* @fn Vec2D& operator=(const Vec2D& pos);
* @fn Vec2D& operator=(const Vec2D& position);
*/
Vec2D& operator=(const Vec2D& pos);
/**
* @brief Opérateur d'addition avec assignement
* @param[in] pos : Vecteur avec lequel additionner le vecteur actuel
* @fn Vec2D& operator+=(const Vec2D& pos);
* @fn Vec2D& operator+=(const Vec2D& position);
*/
Vec2D& operator+=(const Vec2D& pos);
/**
* @brief Opérateur de soustraction avec assignement
* @param[in] pos : Vecteur avec lequel soustraire le vecteur actuel
* @fn Vec2D& operator-=(const Vec2D& pos);
* @fn Vec2D& operator-=(const Vec2D& position);
*/
Vec2D& operator-=(const Vec2D& pos);
/**
* @brief Opérateur de multiplication avec assignement
* @param[in] pos : Vecteur avec lequel multiplier le vecteur actuel
* @fn Vec2D& operator*=(const Vec2D& pos);
* @fn Vec2D& operator*=(const Vec2D& position);
*/
Vec2D& operator*=(const Vec2D& pos);
/**
* @brief Opérateur de division avec assignement
* @param[in] pos : Vecteur avec lequel diviser le vecteur actuel
* @fn Vec2D& operator/=(const Vec2D& pos);
* @fn Vec2D& operator/=(const Vec2D& position);
*/
Vec2D& operator/=(const Vec2D& pos);
/**
* @brief Opérateur modulo avec assignement
* @param[in] pos : Vecteur avec lequel faire un modulo sur le vecteur actuel
* @fn Vec2D& operator%=(const Vec2D& pos);
* @fn Vec2D& operator%=(const Vec2D& position);
*/
Vec2D& operator%=(const Vec2D& pos);

View File

@ -17,7 +17,7 @@ void Game::managedGames() {
}
void Game::initialMenuHandler(){
switch(drawer.showInitialMenu()){
switch(pm.showInitialMenu()){
case 0:{ // play
return;
}
@ -26,7 +26,7 @@ void Game::initialMenuHandler(){
}
void Game::deathMenuHandler(){
switch(drawer.showDeathMenu()){
switch(pm.showDeathMenu()){
case 0:{ // play
return;
}
@ -41,8 +41,7 @@ void Game::deathMenuHandler(){
unsigned Game::playGame(){ // returns when game is finished
// INIT
grid = conf.grid; // will copy the vector
player.setX()
player.setY()
playerX = 0;
// GAMELOOP
while(true){
@ -60,7 +59,7 @@ unsigned Game::playGame(){ // returns when game is finished
void Game::display() {
for (unsigned i = 0; i < this->grid.size(); ++i){
for (unsigned j = 0; j < this->grid[0].size(); ++j){
drawer.dessinerInvader1(nsGraphics::Vec2D(
pm.dessinerInvader1(nsGraphics::Vec2D(
baseX+j*conf.alien_size+j*conf.distance,
baseY+i*conf.alien_size+i*conf.distance
));

View File

@ -1,4 +1,8 @@
#include "game.h"
#define MISSILE_SPEED 5
#define TORPEDO_SPEED MISSILE_SPEED
/** Makes the player play once
*/
void Game::managePlayer(){
@ -14,63 +18,87 @@ bool Game::manageInvaders(){
}
/** makes projectile smove, and manage collisions between everything
/** makes projectiles move, and manage collisions between everything
*
* @return 1 if the invaders are all dead, 2 if the player is dead, else 0
*/
void Game::remCollidingProjectiles(){
auto i = missiles.begin();
auto j = torpedos.begin();
auto miss = missiles.begin();
auto tor = torpedos.begin();
while(i!=missiles.end()){
bool flag = true;
while(j!=torpedos.end()){
if(*i==*j){
missiles.erase(i);
torpedos.erase(j);
flag = false;
while(miss != missiles.end()){
bool wasColling = false;
while(tor != torpedos.end()){
// missiles can't be right under torpedos, so that must means they are colliding in Y
if(miss->getY()+conf.missile_length<tor->getY()){
}
if(lineCollideCheck( // now check if they collide in X
miss->getX(), miss->getX()+conf.missile_width,
tor->getX(), tor->getX()+conf.torpedo_width)){
missiles.erase(miss);
torpedos.erase(tor);
wasColling = true;
break;
}
++j;
++tor;
}
if(flag)++i;
/* if it was colling, it was removed and his position is now replaced by the next.
* Else, go to the next
*/
if(!wasColling)++miss;
}
}
void Game::moveMissiles() {
auto i = missiles.begin();
while (i != missiles.end()) {
if (i->getY() == 0)missiles.erase(i);
else if (PLAYER_HEIGHT(me, *i))return INVADERS_WINS;
auto miss = missiles.begin();
while (miss != missiles.end()) {
if (miss->getY() >= pm.getScreenHeight())missiles.erase(miss);
else {
++(i->line);
++i;
miss->setY(miss->getY()+MISSILE_SPEED);
++miss;
}
}
}
void Game::moveTorpedos() {
auto i = torpedos.begin();
while (i != torpedos.end()) {
i->setY(i->getY()-1);
if (i->getY() == NB_LIGNES)torpedo.erase(i);
else if (contains(game.invader, *i))return PLAYER_WINS;
else ++i;
auto tor = torpedos.begin();
while (tor != torpedos.end()) {
if (tor->getY() <= 0)torpedos.erase(tor); // TODO maybe wait for it to be 100% offscreen
else{
tor->setY(tor->getY() - TORPEDO_SPEED);
++tor;
}
}
}
bool Game::checkMissilesAndPlayer() {
for(missile& miss : missiles){
if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y
if(lineCollideCheck( // now check collision on X
miss.getX(), miss.getX()+conf.missile_width,
playerX, playerX+conf.player_width)){
return true;
}
}
}
return false;
}
bool Game::checkTorpedosAndInvaders() {
return false;
}
unsigned Game::manageAllCollisions() {
moveMissiles();
if(checkMissilesAndPlayer())return INVADERS_WINS;
remCollidingProjectiles();
moveTorpedos();
remCollidingProjectiles();
if(checkMissilesAndPlayer())return INVADERS_WINS;
if(checkTorpedosAndInvaders())return PLAYER_WINS;
return 0;
}

View File

@ -5,5 +5,6 @@ using namespace std;
int main(){
Game g;
g.managedGames();
return 0;
}

View File

@ -1,13 +1,13 @@
#include "drawEngine.h"
#include "pixelManager.h"
#define WININIT window("space invader du turfu ma gueule", nsGraphics::Vec2D(1280, 720), nsGraphics::Vec2D(128, 128), nsGraphics::KBlack)
DrawEngine::DrawEngine() : WININIT {
PixelManager::PixelManager() : WININIT {
window.initGlut();
window.initGraphic();
}
void DrawEngine::dessinerInvader1(const nsGraphics::Vec2D& baseVector){
void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector){
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGray);
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGray);
window << nsShape::Triangle(nsGraphics::Vec2D(35, 50)+baseVector, nsGraphics::Vec2D(15, 25)+baseVector, nsGraphics::Vec2D(15, 75)+baseVector, nsGraphics::KBlack);
@ -18,7 +18,7 @@ void DrawEngine::dessinerInvader1(const nsGraphics::Vec2D& baseVector){
}
void DrawEngine::dessinerInvader2(const nsGraphics::Vec2D& baseVector){
void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector){
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KRed);
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KRed);
window << nsShape::Rectangle(nsGraphics::Vec2D(25, 30)+baseVector, nsGraphics::Vec2D(45, 40)+baseVector, nsGraphics::KBlack);
@ -26,7 +26,7 @@ void DrawEngine::dessinerInvader2(const nsGraphics::Vec2D& baseVector){
window << nsShape::Rectangle(nsGraphics::Vec2D(35, 65)+baseVector, nsGraphics::Vec2D(75, 72)+baseVector, nsGraphics::KBlack);
}
void DrawEngine::dessinerInvader3(const nsGraphics::Vec2D& baseVector){
void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector){
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGreen);
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGreen);
window << nsShape::Circle(nsGraphics::Vec2D(35, 35)+baseVector, 10, nsGraphics::KBlack);
@ -35,10 +35,14 @@ void DrawEngine::dessinerInvader3(const nsGraphics::Vec2D& baseVector){
}
unsigned DrawEngine::showInitialMenu(){
unsigned PixelManager::showInitialMenu(){
return 0;
}
unsigned DrawEngine::showDeathMenu() {
unsigned PixelManager::showDeathMenu() {
return 0;
}
unsigned PixelManager::getScreenHeight() {
return 0;
}

View File

@ -1,6 +1,6 @@
#include "pos.h"
#include "position.h"
#include <mingl/mingl.h>
bool pos::isColliding(pos& p, unsigned rx, unsigned ry) {
bool position::isColliding(position& p, unsigned rx, unsigned ry) {
return nsGraphics::Vec2D::isColliding(p, p+nsGraphics::Vec2D(rx, ry));
}

6
src/utils.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "utils.h"
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2){
return start1 < end2 != start2 < end1;
// if true, are colliding. I like truth tables
}