IT'S ALIIIIIIIIIIVE (kinda)

This commit is contained in:
Thomas 2021-12-26 19:31:18 +01:00
parent bb61fbaac3
commit 9e74ca9ecd
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
7 changed files with 117 additions and 43 deletions

View File

@ -11,14 +11,17 @@ class PixelManager{
public: public:
MinGL window; MinGL window;
PixelManager(); PixelManager();
void dessinerInvader1(const nsGraphics::Vec2D& baseVector); void dessinerInvader1(const nsGraphics::Vec2D& baseVector, unsigned size);
void dessinerInvader2(const nsGraphics::Vec2D& baseVector); void dessinerInvader2(const nsGraphics::Vec2D& baseVector, unsigned size);
void dessinerInvader3(const nsGraphics::Vec2D& baseVector); void dessinerInvader3(const nsGraphics::Vec2D& baseVector, unsigned size);
void dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned size);
unsigned showInitialMenu(); unsigned showInitialMenu();
unsigned showDeathMenu(); unsigned showDeathMenu();
unsigned getScreenHeight(); unsigned getScreenHeight();
unsigned getScreenWidth(); unsigned getScreenWidth();
void startFrame();
void endFrame();
}; };

View File

@ -5,7 +5,9 @@
class position : public nsGraphics::Vec2D { class position : public nsGraphics::Vec2D {
public: public:
bool isColliding(position& p, unsigned rx, unsigned ry); position() = default;
position(int x, int y);
bool isColliding(position& p, int rx, int ry);
}; };
#endif //SPACE_POSITION_H #endif //SPACE_POSITION_H

View File

@ -1,5 +1,22 @@
#include "config.h" #include "config.h"
bool Config::loadConfig() { bool Config::loadConfig() {
#define S 48
grid.resize(S);
for(unsigned i=0;i<S;++i){
grid[i].resize(3);
for(unsigned j=0;j<3;++j)grid[i][j] = j;
}
alien_size = 20;
distance = 5;
alien_speed = 1;
missile_width = 10;
missile_length = 15;
torpedo_width = 10;
torpedo_length = 15;
player_width = 50;
return true; return true;
} }

View File

@ -1,3 +1,5 @@
#include <chrono>
#include <thread>
#include "game.h" #include "game.h"
Game::Game() { Game::Game() {
@ -5,13 +7,13 @@ Game::Game() {
} }
void Game::managedGames() { void Game::managedGames() {
initialMenuHandler(); // returns when user clicked plays
// ILLEGAL THINGS initialMenuHandler(); // returns when user clicked plays
// conf.grid = ;
while(true){ while(true){
playGame(); playGame();
cout << "END OF GAME" << endl;
break;
deathMenuHandler(); // returns when user clicked replay deathMenuHandler(); // returns when user clicked replay
} }
} }
@ -52,14 +54,25 @@ unsigned Game::playGame(){ // returns when game is finished
// INIT // INIT
grid = conf.grid; // will copy the vector grid = conf.grid; // will copy the vector
playerX = 0; playerX = 0;
basePos = position(0,0);
// GAMELOOP // GAMELOOP
#define FPS 1000
while(true){ while(true){
auto target = chrono::high_resolution_clock::now() + chrono::duration<double, ratio<1, FPS>>(1);
pm.startFrame();
managePlayer(); managePlayer();
if(manageInvaders() && invadersTouchFloor())return INVADERS_WINS; if(manageInvaders() && invadersTouchFloor())return INVADERS_WINS;
unsigned res = manageAllCollisions(); // also advances missiles + torpedos unsigned res = manageAllCollisions(); // also moves missiles + torpedos
if(res!=0)return res; if(res!=0)return res;
display(); display();
pm.endFrame();
this_thread::sleep_until(target);
} }
} }
@ -68,11 +81,26 @@ unsigned Game::playGame(){ // returns when game is finished
*/ */
void Game::display() { void Game::display() {
for (unsigned i = 0; i < this->grid.size(); ++i){ for (unsigned i = 0; i < this->grid.size(); ++i){
for (unsigned j = 0; j < this->grid[0].size(); ++j){ for (unsigned j = 0; j < this->grid[i].size(); ++j){
pm.dessinerInvader1(nsGraphics::Vec2D( nsGraphics::Vec2D vec =nsGraphics::Vec2D(
basePos.getX()+j*conf.alien_size+j*conf.distance, basePos.getX()+i*conf.alien_size+i*conf.distance,
basePos.getY()+i*conf.alien_size+i*conf.distance basePos.getY()+j*conf.alien_size+j*conf.distance
)); );
switch(grid[i][j]){
case 0:{
pm.dessinerInvader1(vec, conf.alien_size);
break;
}
case 1:{
pm.dessinerInvader2(vec, conf.alien_size);
break;
}
case 2:{
pm.dessinerInvader3(vec, conf.alien_size);
break;
} }
} }
}
}
pm.dessinerJoueur(position(playerX, 0), conf.player_width);
} }

View File

@ -1,5 +1,6 @@
#include "game.h" #include "game.h"
// TODO put in config (EVERYTHING SHALL GO IN CONFIG, THE WHOLE CODE SHALL GO IN CONFIIIIIIIIG)
#define MISSILE_SPEED 5 #define MISSILE_SPEED 5
#define TORPEDO_SPEED MISSILE_SPEED #define TORPEDO_SPEED MISSILE_SPEED
@ -11,7 +12,7 @@ void Game::managePlayer(){
/** Makes the invaders play once, and check lower bounds /** Makes the invaders play once, and check lower bounds
* *
* @return true if the invaders went down from one line (and we should check boundaries), else false * @return true if the invaders went down from one line (and we should check lower boundary), else false
*/ */
bool Game::manageInvaders(){ bool Game::manageInvaders(){
if(direction){ // go to the right if(direction){ // go to the right
@ -24,15 +25,15 @@ bool Game::manageInvaders(){
if(end+conf.alien_speed<pm.getScreenWidth()){ if(end+conf.alien_speed<pm.getScreenWidth()){
basePos.setX(basePos.getX()+conf.alien_speed); basePos.setX(basePos.getX()+conf.alien_speed);
}else{ }else{
basePos.setY(basePos.getY()-conf.alien_size); basePos.setY(basePos.getY()+conf.alien_size);
direction = !direction; direction = !direction;
return true; return true;
} }
}else{ }else{
if(basePos.getX()-conf.alien_size>=0){ if(basePos.getX()>=conf.alien_speed){
basePos.setX(basePos.getX()-conf.alien_speed); basePos.setX(basePos.getX()-conf.alien_speed);
}else{ }else{
basePos.setY(basePos.getY()-conf.alien_size); basePos.setY(basePos.getY()+conf.alien_size);
direction = !direction; direction = !direction;
return true; return true;
} }
@ -69,7 +70,7 @@ void Game::remCollidingProjectiles(){
++tor; ++tor;
} }
/* if it was colling, it was removed and his position is now replaced by the next. /* if it was colling, it was removed and his position is now replaced by the next.
* Else, go to the next * else, go to the next
*/ */
if(!wasColling)++miss; if(!wasColling)++miss;
} }

View File

@ -1,4 +1,5 @@
#include "pixelManager.h" #include "pixelManager.h"
#include "utils.h"
#define WININIT window("space invader du turfu ma gueule", nsGraphics::Vec2D(1280, 720), nsGraphics::Vec2D(128, 128), nsGraphics::KBlack) #define WININIT window("space invader du turfu ma gueule", nsGraphics::Vec2D(1280, 720), nsGraphics::Vec2D(128, 128), nsGraphics::KBlack)
@ -6,32 +7,44 @@ PixelManager::PixelManager() : WININIT {
window.initGlut(); window.initGlut();
window.initGraphic(); window.initGraphic();
} }
void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector, unsigned size){
void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector){ float scale = size/(float)100;
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGray); window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGray);
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGray); window << nsShape::Triangle(nsGraphics::Vec2D(35*scale, 50*scale)+baseVector, nsGraphics::Vec2D(15*scale, 25*scale)+baseVector, nsGraphics::Vec2D(15*scale, 75*scale)+baseVector, nsGraphics::KBlack);
window << nsShape::Triangle(nsGraphics::Vec2D(35, 50)+baseVector, nsGraphics::Vec2D(15, 25)+baseVector, nsGraphics::Vec2D(15, 75)+baseVector, nsGraphics::KBlack); window << nsShape::Triangle(nsGraphics::Vec2D(25*scale, 50*scale)+baseVector, nsGraphics::Vec2D(10*scale, 25*scale)+baseVector, nsGraphics::Vec2D(10*scale, 75*scale)+baseVector, nsGraphics::KGray);
window << nsShape::Triangle(nsGraphics::Vec2D(25, 50)+baseVector, nsGraphics::Vec2D(10, 25)+baseVector, nsGraphics::Vec2D(10, 75)+baseVector, nsGraphics::KGray); window << nsShape::Triangle(nsGraphics::Vec2D(65*scale, 50*scale)+baseVector, nsGraphics::Vec2D(85*scale, 25*scale)+baseVector, nsGraphics::Vec2D(85*scale, 75*scale)+baseVector, nsGraphics::KBlack);
window << nsShape::Triangle(nsGraphics::Vec2D(75, 50)+baseVector, nsGraphics::Vec2D(95, 25)+baseVector, nsGraphics::Vec2D(95, 75)+baseVector, nsGraphics::KBlack); window << nsShape::Triangle(nsGraphics::Vec2D(75*scale, 50*scale)+baseVector, nsGraphics::Vec2D(90*scale, 25*scale)+baseVector, nsGraphics::Vec2D(90*scale, 75*scale)+baseVector, nsGraphics::KGray);
window << nsShape::Triangle(nsGraphics::Vec2D(85, 50)+baseVector, nsGraphics::Vec2D(100, 25)+baseVector, nsGraphics::Vec2D(100, 75)+baseVector, nsGraphics::KGray); window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
window << nsShape::Rectangle(nsGraphics::Vec2D(35, 65)+baseVector, nsGraphics::Vec2D(75, 72)+baseVector, nsGraphics::KBlack);
} }
void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector){ void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector, unsigned size){
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KRed); float scale = size/(float)100;
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KRed); window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KRed);
window << nsShape::Rectangle(nsGraphics::Vec2D(25, 30)+baseVector, nsGraphics::Vec2D(45, 40)+baseVector, nsGraphics::KBlack); window << nsShape::Rectangle(nsGraphics::Vec2D(25*scale, 30*scale)+baseVector, nsGraphics::Vec2D(45*scale, 40*scale)+baseVector, nsGraphics::KBlack);
window << nsShape::Rectangle(nsGraphics::Vec2D(65, 30)+baseVector, nsGraphics::Vec2D(85, 40)+baseVector, nsGraphics::KBlack); window << nsShape::Rectangle(nsGraphics::Vec2D(55*scale, 30*scale)+baseVector, nsGraphics::Vec2D(75*scale, 40*scale)+baseVector, nsGraphics::KBlack);
window << nsShape::Rectangle(nsGraphics::Vec2D(35, 65)+baseVector, nsGraphics::Vec2D(75, 72)+baseVector, nsGraphics::KBlack); window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
} }
void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector){ void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector, unsigned size){
window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGreen); float scale = size/(float)100;
window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGreen); window << nsShape::Circle(nsGraphics::Vec2D(50*scale, 50*scale)+baseVector, 50*scale, nsGraphics::KGreen);
window << nsShape::Circle(nsGraphics::Vec2D(35, 35)+baseVector, 10, nsGraphics::KBlack); window << nsShape::Circle(nsGraphics::Vec2D(35*scale, 35*scale)+baseVector, 10*scale, nsGraphics::KBlack);
window << nsShape::Circle(nsGraphics::Vec2D(75, 35)+baseVector, 10, nsGraphics::KBlack); window << nsShape::Circle(nsGraphics::Vec2D(65*scale, 35*scale)+baseVector, 10*scale, nsGraphics::KBlack);
window << nsShape::Rectangle(nsGraphics::Vec2D(35, 65)+baseVector, nsGraphics::Vec2D(75, 72)+baseVector, nsGraphics::KBlack); window << nsShape::Rectangle(nsGraphics::Vec2D(35*scale, 65*scale)+baseVector, nsGraphics::Vec2D(65*scale, 72*scale)+baseVector, nsGraphics::KBlack);
}
void PixelManager::dessinerJoueur(const nsGraphics::Vec2D& baseVector, unsigned width){
width = width-10-10;
width = width/2;
window << nsShape::Triangle(nsGraphics::Vec2D(0, 720)+baseVector, nsGraphics::Vec2D(5, 720)+baseVector, nsGraphics::Vec2D(5, 720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::KCyan);
window << nsShape::Rectangle(nsGraphics::Vec2D(5, 720)+baseVector, nsGraphics::Vec2D(5+width, 720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::KCyan);
window << nsShape::Rectangle(nsGraphics::Vec2D(5+width, 720)+baseVector, nsGraphics::Vec2D(15+width, 720-PLAYER_HEIGHT)+baseVector, nsGraphics::KCyan);
window << nsShape::Rectangle(nsGraphics::Vec2D(15+width, 720)+baseVector, nsGraphics::Vec2D(15+width*2, 720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::KCyan);
window << nsShape::Triangle(nsGraphics::Vec2D(15+width*2, 720)+baseVector, nsGraphics::Vec2D(15+width*2, 720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(20+width*2, 720)+baseVector, nsGraphics::KCyan);
window << nsShape::Triangle(nsGraphics::Vec2D(5,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(5+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(5+width,720-PLAYER_HEIGHT*0.9)+baseVector, nsGraphics::KCyan);
window << nsShape::Triangle(nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width*2,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT*0.9)+baseVector, nsGraphics::KCyan);
} }
@ -44,9 +57,17 @@ unsigned PixelManager::showDeathMenu() {
} }
unsigned PixelManager::getScreenHeight() { unsigned PixelManager::getScreenHeight() {
return 0; return window.getWindowSize().getY();
} }
unsigned PixelManager::getScreenWidth() { unsigned PixelManager::getScreenWidth() {
return 0; return window.getWindowSize().getX();
}
void PixelManager::startFrame() {
window.clearScreen();
}
void PixelManager::endFrame() {
window.finishFrame();
} }

View File

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