did things
This commit is contained in:
parent
978f0e4df7
commit
e4b719bd73
@ -8,9 +8,8 @@
|
|||||||
typedef string configKey;
|
typedef string configKey;
|
||||||
|
|
||||||
struct ConfigData {
|
struct ConfigData {
|
||||||
map<string, string> internalValues;
|
|
||||||
|
|
||||||
aliensGrid grid;
|
invadersGrid grid;
|
||||||
|
|
||||||
unsigned invadersSize;
|
unsigned invadersSize;
|
||||||
unsigned invadersDistance;
|
unsigned invadersDistance;
|
||||||
@ -24,8 +23,7 @@ struct ConfigData {
|
|||||||
unsigned invadersSpeed;
|
unsigned invadersSpeed;
|
||||||
unsigned playersSpeed;
|
unsigned playersSpeed;
|
||||||
|
|
||||||
PlayerDef p1Def;
|
vector<PlayerDef> playerDefs;
|
||||||
PlayerDef p2Def;
|
|
||||||
unsigned startXPosition;
|
unsigned startXPosition;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
#ifndef SPACE_ERRORS_H
|
|
||||||
#define SPACE_ERRORS_H
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -4,11 +4,11 @@
|
|||||||
#include "mingl/mingl.h"
|
#include "mingl/mingl.h"
|
||||||
#include "pixel_manager.h"
|
#include "pixel_manager.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "position.h"
|
|
||||||
#include "player_def.h"
|
#include "player_def.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "play_mode.h"
|
#include "play_mode.h"
|
||||||
#include "configData.h"
|
#include "configData.h"
|
||||||
|
#include "projectiles.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -19,15 +19,14 @@ private:
|
|||||||
ConfigData confData;
|
ConfigData confData;
|
||||||
|
|
||||||
position basePos;
|
position basePos;
|
||||||
aliensGrid grid;
|
invadersGrid grid;
|
||||||
bool direction = true;
|
bool direction = true;
|
||||||
|
|
||||||
vector<missile> missiles;
|
vector<missile> missiles;
|
||||||
vector<torpedo> torpedos;
|
vector<torpedo> torpedos;
|
||||||
|
|
||||||
PlayMode playMode;
|
PlayMode playMode;
|
||||||
Player p1;
|
vector<Player> players;
|
||||||
Player p2;
|
|
||||||
|
|
||||||
void managePlayers();
|
void managePlayers();
|
||||||
bool manageInvaders();
|
bool manageInvaders();
|
||||||
@ -41,7 +40,7 @@ private:
|
|||||||
bool checkTorpedosAndInvaders();
|
bool checkTorpedosAndInvaders();
|
||||||
bool invadersTouchPlayer();
|
bool invadersTouchPlayer();
|
||||||
|
|
||||||
void managePlayerMoves(PlayerDef&, unsigned&);
|
void managePlayerMoves(PlayerDef&, unsigned&, unsigned);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// in case someone wants to mess with the code, here's a minimal API, costs nothing to us
|
// in case someone wants to mess with the code, here's a minimal API, costs nothing to us
|
||||||
|
@ -5,7 +5,9 @@ struct Player{
|
|||||||
unsigned lives;
|
unsigned lives;
|
||||||
unsigned x;
|
unsigned x;
|
||||||
unsigned id;
|
unsigned id;
|
||||||
unsigned score;
|
unsigned score=0;
|
||||||
|
|
||||||
|
unsigned deathAnimCounter=0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,13 +0,0 @@
|
|||||||
#ifndef GUARD_POSITION_H
|
|
||||||
#define GUARD_POSITION_H
|
|
||||||
|
|
||||||
#include<mingl/mingl.h>
|
|
||||||
|
|
||||||
class position : public nsGraphics::Vec2D {
|
|
||||||
public:
|
|
||||||
position() = default;
|
|
||||||
position(int x, int y);
|
|
||||||
bool isColliding(position& p, int rx, int ry);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
14
headers/projectiles.h
Normal file
14
headers/projectiles.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef GUARD_PROJECTILES_H
|
||||||
|
#define GUARD_PROJECTILES_H
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
typedef position missile;
|
||||||
|
|
||||||
|
class torpedo : public position {
|
||||||
|
public:
|
||||||
|
playerID owner;
|
||||||
|
torpedo(int x, int y, playerID owner);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,9 +1,8 @@
|
|||||||
#ifndef GUARD_STRUCTS_H
|
#ifndef GUARD_UTILS_H
|
||||||
#define GUARD_STRUCTS_H
|
#define GUARD_UTILS_H
|
||||||
|
|
||||||
#include<vector>
|
#include<vector>
|
||||||
#include<mingl/mingl.h>
|
#include<mingl/mingl.h>
|
||||||
#include"position.h"
|
|
||||||
|
|
||||||
// hardcoded values
|
// hardcoded values
|
||||||
#define PLAYER_HEIGHT 100
|
#define PLAYER_HEIGHT 100
|
||||||
@ -17,17 +16,11 @@ enum WinValue{
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
typedef unsigned Alien;
|
typedef unsigned Invader;
|
||||||
typedef vector<Alien> aliensLine;
|
typedef vector<Invader> invadersColumn;
|
||||||
typedef vector<aliensLine> aliensGrid;
|
typedef vector<invadersColumn> invadersGrid;
|
||||||
|
typedef nsGraphics::Vec2D position;
|
||||||
typedef position missile;
|
typedef unsigned playerID;
|
||||||
|
|
||||||
class torpedo : public nsGraphics::Vec2D {
|
|
||||||
public:
|
|
||||||
topedo(int x, int y);
|
|
||||||
unsigned playerID;
|
|
||||||
};
|
|
||||||
|
|
||||||
// didn't want to use position because of the semantic with x and y
|
// didn't want to use position because of the semantic with x and y
|
||||||
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2);
|
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2);
|
||||||
|
@ -78,15 +78,18 @@ void ConfigBuilder::readConfig() {
|
|||||||
collectedData.startXPosition = getInt("players.startXPosition");
|
collectedData.startXPosition = getInt("players.startXPosition");
|
||||||
collectedData.playersSpeed = getInt("players.speed");
|
collectedData.playersSpeed = getInt("players.speed");
|
||||||
|
|
||||||
collectedData.p1Def.color = getColor("players.user1.color");
|
// the scalability behind the vector of players is only an illusion, because we force player count to be 1 or 2
|
||||||
collectedData.p1Def.keys.left = getChar("players.user1.keys.left");
|
// It was done so the 2+ players implementation could be easier in the future, if wanted
|
||||||
collectedData.p1Def.keys.right = getChar("players.user1.keys.right");
|
collectedData.playerDefs.resize(2);
|
||||||
collectedData.p1Def.keys.shoot = getChar("players.user1.keys.shoot");
|
collectedData.playerDefs[1].color = getColor("players.user1.color");
|
||||||
|
collectedData.playerDefs[1].keys.left = getChar("players.user1.keys.left");
|
||||||
|
collectedData.playerDefs[1].keys.right = getChar("players.user1.keys.right");
|
||||||
|
collectedData.playerDefs[1].keys.shoot = getChar("players.user1.keys.shoot");
|
||||||
|
|
||||||
collectedData.p2Def.color = getColor("players.user2.color");
|
collectedData.playerDefs[2].color = getColor("players.user2.color");
|
||||||
collectedData.p2Def.keys.left = getChar("players.user2.keys.left");
|
collectedData.playerDefs[2].keys.left = getChar("players.user2.keys.left");
|
||||||
collectedData.p2Def.keys.right = getChar("players.user2.keys.right");
|
collectedData.playerDefs[2].keys.right = getChar("players.user2.keys.right");
|
||||||
collectedData.p2Def.keys.shoot = getChar("players.user2.keys.shoot");
|
collectedData.playerDefs[2].keys.shoot = getChar("players.user2.keys.shoot");
|
||||||
|
|
||||||
// invaders
|
// invaders
|
||||||
collectedData.invadersSize = getInt("invaders.size");
|
collectedData.invadersSize = getInt("invaders.size");
|
||||||
|
@ -58,7 +58,7 @@ bool Game::deathMenuHandler(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Game::invadersTouchPlayer(){
|
bool Game::invadersTouchPlayer(){
|
||||||
for(aliensLine& line : grid){
|
for(invadersColumn& line : grid){
|
||||||
if(basePos.getY()+ line.size() * confData.invadersSize >= pm.getScreenHeight() - PLAYER_HEIGHT){
|
if(basePos.getY()+ line.size() * confData.invadersSize >= pm.getScreenHeight() - PLAYER_HEIGHT){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -76,12 +76,15 @@ WinValue Game::playGame(){ // returns when game is finished
|
|||||||
// we assume the game has been played before, and so we need to clean used members
|
// we assume the game has been played before, and so we need to clean used members
|
||||||
grid = confData.grid; // will copy the vector
|
grid = confData.grid; // will copy the vector
|
||||||
|
|
||||||
p1.x = confData.startXPosition;
|
|
||||||
|
|
||||||
if(playMode!=SINGLE){
|
if(playMode==SINGLE){
|
||||||
|
players.resize(1);
|
||||||
|
}else{
|
||||||
|
players.resize(2);
|
||||||
// mirror the start X position for the other
|
// mirror the start X position for the other
|
||||||
p2.x = pm.getScreenWidth() - confData.startXPosition - confData.playersWidth;
|
players[1].x = pm.getScreenWidth() - confData.startXPosition - confData.playersWidth;
|
||||||
}
|
}
|
||||||
|
players[0].x = confData.startXPosition;
|
||||||
|
|
||||||
basePos = position(0,0);
|
basePos = position(0,0);
|
||||||
|
|
||||||
@ -118,7 +121,7 @@ WinValue 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[i].size(); ++j){
|
for (unsigned j = 0; j < this->grid[i].size(); ++j){
|
||||||
nsGraphics::Vec2D vec =nsGraphics::Vec2D(
|
nsGraphics::Vec2D vec(
|
||||||
basePos.getX() + i * confData.invadersSize + i * confData.invadersDistance,
|
basePos.getX() + i * confData.invadersSize + i * confData.invadersDistance,
|
||||||
basePos.getY() + j * confData.invadersSize + j * confData.invadersDistance
|
basePos.getY() + j * confData.invadersSize + j * confData.invadersDistance
|
||||||
);
|
);
|
||||||
@ -138,6 +141,8 @@ void Game::display() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pm.dessinerJoueur(position(p1.x, 0), confData.playersWidth, confData.p1Def.color);
|
|
||||||
if(playMode!=SINGLE)pm.dessinerJoueur(position(p2.x, 0), confData.playersWidth, confData.p2Def.color);
|
for(size_t i=0;i<players.size();++i){
|
||||||
|
pm.dessinerJoueur(position(players[i].x, 0), confData.playersWidth, confData.playerDefs[i].color);
|
||||||
|
}
|
||||||
}
|
}
|
@ -16,15 +16,18 @@ void Game::managePlayerMoves(PlayerDef& pdef, unsigned& playerX, unsigned player
|
|||||||
else playerX = playerX + confData.playersSpeed;
|
else playerX = playerX + confData.playersSpeed;
|
||||||
}
|
}
|
||||||
if(ISPRESSED(pdef.keys.shoot)){
|
if(ISPRESSED(pdef.keys.shoot)){
|
||||||
torpedos.emplace_back(playerX + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, playerId );
|
torpedos.emplace_back(playerX + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, playerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Makes the players play once
|
/** Makes the players play once
|
||||||
*/
|
*/
|
||||||
void Game::managePlayers(){
|
void Game::managePlayers(){
|
||||||
managePlayerMoves(confData.p1Def, p1.x,p1.id);
|
managePlayerMoves(confData.playerDefs[0], players[0].x,0);
|
||||||
if(playMode==PlayMode::TWO_LOCAL)managePlayerMoves(confData.p2Def, p2.x, p2.id);
|
if(playMode==PlayMode::TWO_LOCAL)managePlayerMoves(confData.playerDefs[1], players[1].x, 1);
|
||||||
|
else if(playMode==PlayMode::TWO_TCPIP){
|
||||||
|
// TODO TCPIP
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Makes the invaders play once, and check lower bounds
|
/** Makes the invaders play once, and check lower bounds
|
||||||
@ -34,10 +37,10 @@ void Game::managePlayers(){
|
|||||||
bool Game::manageInvaders(){
|
bool Game::manageInvaders(){
|
||||||
if(direction){ // go to the right
|
if(direction){ // go to the right
|
||||||
int end = basePos.getX(); // start position
|
int end = basePos.getX(); // start position
|
||||||
end+= grid.size() * confData.invadersSize; // add the aliens
|
end+= grid.size() * confData.invadersSize; // add the invaders
|
||||||
end+= (grid.size()-1) * confData.invadersDistance; // add the invadersDistance between aliens
|
end+= (grid.size()-1) * confData.invadersDistance; // add the invadersDistance between invaders
|
||||||
|
|
||||||
// you got the end position of the alien crowd !
|
// you got the end position of the invader crowd !
|
||||||
|
|
||||||
if(end + confData.invadersSpeed < pm.getScreenWidth()){
|
if(end + confData.invadersSpeed < pm.getScreenWidth()){
|
||||||
basePos.setX(basePos.getX() + confData.invadersSpeed);
|
basePos.setX(basePos.getX() + confData.invadersSpeed);
|
||||||
@ -69,7 +72,7 @@ void Game::remCollidingProjectiles(){
|
|||||||
auto tor = torpedos.begin();
|
auto tor = torpedos.begin();
|
||||||
|
|
||||||
while(miss != missiles.end()){
|
while(miss != missiles.end()){
|
||||||
bool wasColling = false;
|
bool wasColliding = false;
|
||||||
while(tor != torpedos.end()){
|
while(tor != torpedos.end()){
|
||||||
|
|
||||||
// missiles can't be right under torpedos, so that must means they are colliding in Y
|
// missiles can't be right under torpedos, so that must means they are colliding in Y
|
||||||
@ -81,7 +84,7 @@ void Game::remCollidingProjectiles(){
|
|||||||
tor->getX(), tor->getX() + confData.torpedosWidth)){
|
tor->getX(), tor->getX() + confData.torpedosWidth)){
|
||||||
missiles.erase(miss);
|
missiles.erase(miss);
|
||||||
torpedos.erase(tor);
|
torpedos.erase(tor);
|
||||||
wasColling = true;
|
wasColliding = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++tor;
|
++tor;
|
||||||
@ -89,7 +92,7 @@ void Game::remCollidingProjectiles(){
|
|||||||
/* 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(!wasColliding)++miss;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,18 +119,51 @@ void Game::moveTorpedos() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Game::checkMissilesAndPlayers() {
|
bool Game::checkMissilesAndPlayers() {
|
||||||
for(missile& miss : missiles){
|
auto miss_ite = missiles.begin();
|
||||||
if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y
|
while(miss_ite!=missiles.end()){
|
||||||
if(lineCollideCheck( // now check collision on X
|
if(miss_ite->getY()<=PLAYER_HEIGHT){ // check collision on Y
|
||||||
miss.getX(), miss.getX() + confData.missilesWidth,
|
// now check collision on X (with both players)
|
||||||
p1.x, p1.x + confData.playersWidth)){
|
bool wasColliding = false;
|
||||||
return true;
|
for(Player& p : players){
|
||||||
|
if(lineCollideCheck(
|
||||||
|
miss_ite->getX(), miss_ite->getX() + confData.missilesWidth,
|
||||||
|
p.x, p.x + confData.playersWidth)){
|
||||||
|
wasColliding = true;
|
||||||
|
if(p.deathAnimCounter)p.deathAnimCounter = 1;
|
||||||
|
// do not break, the second player also deserves to be hit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if(wasColliding)missiles.erase(miss_ite);
|
||||||
|
else ++miss_ite;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Game::checkTorpedosAndInvaders() {
|
bool Game::checkTorpedosAndInvaders() {
|
||||||
|
auto tor_ite = torpedos.begin();
|
||||||
|
while(tor_ite!=torpedos.end()){
|
||||||
|
unsigned i=0;
|
||||||
|
for(;i<grid.size();++i){
|
||||||
|
// calculate top-left position of invader
|
||||||
|
position pos = basePos+position(
|
||||||
|
confData.invadersSize*(grid[i].size()-1)+confData.invadersDistance*(grid[i].size()-1),
|
||||||
|
confData.invadersSize*(grid[i].size()-1)+confData.invadersDistance*(grid[i].size()-1));
|
||||||
|
// check collision on Y (invaders can actually be "under" torpedos, so we check both lower and upper bounds
|
||||||
|
if(pos.getY()+confData.invadersSize>=tor_ite->getY() &&
|
||||||
|
pos.getY()<=tor_ite->getY()+confData.torpedosLength){
|
||||||
|
// now check collision on X
|
||||||
|
if(lineCollideCheck( // now check collision on X
|
||||||
|
tor_ite->getX(), tor_ite->getX() + confData.torpedosWidth,
|
||||||
|
pos.getX(), pos.getX() + confData.invadersSize)){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(i==grid.size()) ++tor_ite;
|
||||||
|
else{
|
||||||
|
torpedos.erase(tor_ite);
|
||||||
|
grid[i].pop_back();
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
@ -2,6 +2,7 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
// TODO changer tout les unsigned par des size_t dans les boucles
|
||||||
int main(){
|
int main(){
|
||||||
Game g;
|
Game g;
|
||||||
g.managedGames();
|
g.managedGames();
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
#include "position.h"
|
|
||||||
#include <mingl/mingl.h>
|
|
||||||
|
|
||||||
bool position::isColliding(position& p, int rx, int ry) {
|
|
||||||
return nsGraphics::Vec2D::isColliding(p, p+nsGraphics::Vec2D(rx, ry));
|
|
||||||
}
|
|
||||||
|
|
||||||
position::position(int x, int y) : Vec2D(x, y) {}
|
|
5
src/projectiles.cpp
Normal file
5
src/projectiles.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "projectiles.h"
|
||||||
|
|
||||||
|
torpedo::torpedo(int x, int y, playerID owner) : position(x, y) {
|
||||||
|
this->owner = owner;
|
||||||
|
}
|
@ -1,7 +1,5 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
torpedo::torpedo(int x, int y) : position(x, y) {}
|
|
||||||
|
|
||||||
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2){
|
bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2){
|
||||||
return start1 < end2 != start2 < end1;
|
return start1 < end2 != start2 < end1;
|
||||||
// if true, are colliding. I like truth tables
|
// if true, are colliding. I like truth tables
|
||||||
|
Loading…
Reference in New Issue
Block a user