Commit b582de90 authored by Timothy LAIRD's avatar Timothy LAIRD

modifs Networld; recrutement soldiers successifs

parent e0e3846b
......@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "networld.h"
#include "player.h"
......@@ -20,7 +21,7 @@ const int screenWidth = 800;
const int screenHeight = 600;
const int targetFPS = 60;
void game_update(NetWorld * world, Player * players, Player * currentPlayer, int playerCount);
void game_update(NetWorld * world, Player * players);
// Game attributes
//-----------------
......@@ -31,7 +32,8 @@ int main(int nbArg, char ** arg)
// Game Initialization
//--------------------
game_end= false;
NetWorld * world= NetWorld_new(3);
int playerCount = 3;
NetWorld * world= NetWorld_new(3, playerCount);
Node_set( &(world->nodes[0]), (Vector2){1.4f, 1.28f}, RED );
Node_set( &(world->nodes[1]), (Vector2){21.0f, 22.8f}, MAGENTA );
Node_set( &(world->nodes[2]), (Vector2){18.4f, -12.2f}, GRAY );
......@@ -44,7 +46,6 @@ int main(int nbArg, char ** arg)
Panel_initialize(panel, world);
Color colors[] = {BLACK, RED, BLUE};
int playerCount = 3;
Player * players = Player_newArray(playerCount, colors);
for(int index = 0; index < world->size; index++){
Player_add_Node(&(players[index % playerCount]), &(world->nodes[index]));
......@@ -65,8 +66,8 @@ int main(int nbArg, char ** arg)
printf("[%.2f,%.2f]\n", position.x, position.y);
// Main game loop
Player * currentPlayer = &(players[0]);
Player_start_turn(currentPlayer);
world->currentPlayer = 0;
Player_start_turn(&(players[world->currentPlayer]));
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
{
DrawFPS(10, 10);
......@@ -76,7 +77,7 @@ int main(int nbArg, char ** arg)
world->nodes[index].collisionHitbox.y = screenPosition.y - 24;
}
Panel_control(panel);
game_update(world, players, currentPlayer, playerCount);
game_update(world, players);
Panel_draw(panel);
}
......@@ -89,19 +90,32 @@ int main(int nbArg, char ** arg)
return 0;
}
void game_update(NetWorld * world, Player * players, Player * currentPlayer, int playerCount)
void game_update(NetWorld * world, Player * players)
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
for(int nodeIndex = 0; nodeIndex < currentPlayer->nodeCount; nodeIndex++){
Node* currentNode = currentPlayer->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
printf("click\n");
Player * currentPlayer = &(players[world->currentPlayer]);
int playerCount = world->playerCount;
switch(currentPlayer->turnPhase){
case 1:
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
for(int nodeIndex = 0; nodeIndex < currentPlayer->nodeCount; nodeIndex++){
Node* currentNode = currentPlayer->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
currentNode->soldiers++;
currentPlayer->soldiers--;
}
}
}
}
if(currentPlayer->soldiers == 0){
currentPlayer->turnPhase = -1;
}
break;
default:;
}
if(false){
if(currentPlayer->turnPhase == -1){
Player_end_turn(currentPlayer);
currentPlayer = &(players[(currentPlayer->ID +1) % playerCount]);
world->currentPlayer = (world->currentPlayer + 1) % playerCount;
Player_start_turn(&(players[world->currentPlayer]));
}
}
......@@ -185,11 +185,13 @@ void Edge_copy( Edge * copy, Edge * model )
// Constructor / Destructor
NetWorld * NetWorld_new(int size)
NetWorld * NetWorld_new(int size, int playerCount)
{
NetWorld * p = malloc( sizeof(NetWorld) );
p->size= size;
p->nodes= Node_newArray( p->size );
p->currentPlayer = -1;
p->playerCount = playerCount;
return p;
}
......
......@@ -216,6 +216,10 @@ struct Str_NetWorld {
int size;
//! Array of 'size' nodes
Node * nodes;
//! Current Player ID
int currentPlayer;
//! Total Player count;
int playerCount;
};
typedef struct Str_NetWorld NetWorld;
......@@ -227,7 +231,9 @@ typedef struct Str_NetWorld NetWorld;
*/
NetWorld * NetWorld_new(
//! the number of Nodes composing the new NetWorld
int aSize
int aSize,
//! the number of players in the game
int playerCount
);
/**
......
#include "player.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
void Player_construct(Player * self, int i, Color color)
{
self->ID = i;
self->nodes = malloc(sizeof(Node*));
self->nodeCount = 0;
self->color = color;
self->turnPhase = 0;
self->soldiers = 0;
}
Player * Player_new()
......@@ -44,6 +46,7 @@ void Player_add_Node( Player * self, Node * node )
node->color = self->color;
node->playerID = self->ID;
newNodes[self->nodeCount] = node;
free(self->nodes);
self->nodes= newNodes;
self->nodeCount += 1;
}
......@@ -59,12 +62,21 @@ void Player_remove_Node( Player * self, Node * node )
}
}
node->playerID = -1;
free(self->nodes);
self->nodes= newNodes;
self->nodeCount -= 1;
}
void Player_start_turn(Player * self){
int totalSoldierCount = 0;
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
totalSoldierCount += self->nodes[nodeIndex]->soldiers;
}
self->soldiers = fmax(1, totalSoldierCount/3);
self->turnPhase = 1;
printf("Current player soldiers : %i\n", self->soldiers);
}
void Player_end_turn(Player * self){
self->turnPhase = 0;
}
\ No newline at end of file
......@@ -8,6 +8,8 @@ struct Str_Player{
Node ** nodes;
int nodeCount;
Color color;
int turnPhase;
int soldiers;
};
typedef struct Str_Player Player;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment