Commit c9991163 authored by Timothy LAIRD's avatar Timothy LAIRD

attacks; improvements recrutement; bugfixes

parent b582de90
...@@ -88,7 +88,7 @@ void Panel_drawNode(Panel * self, Node * n) ...@@ -88,7 +88,7 @@ void Panel_drawNode(Panel * self, Node * n)
DrawCircleV(screenPosition, 24, n->color); DrawCircleV(screenPosition, 24, n->color);
DrawCircleV(screenPosition, 20, RAYWHITE); DrawCircleV(screenPosition, 20, RAYWHITE);
char * soldierText = malloc(sizeof(int)); char * soldierText = malloc(sizeof(int));
sprintf(soldierText, "%d", n->soldiers); sprintf(soldierText, "%d", n->soldiers + n->soldiersToAdd);
DrawText(soldierText, (int)screenPosition.x - MeasureText(soldierText, 20)/2, (int)screenPosition.y, 20, n->color); DrawText(soldierText, (int)screenPosition.x - MeasureText(soldierText, 20)/2, (int)screenPosition.y, 20, n->color);
} }
......
...@@ -32,11 +32,13 @@ int main(int nbArg, char ** arg) ...@@ -32,11 +32,13 @@ int main(int nbArg, char ** arg)
// Game Initialization // Game Initialization
//-------------------- //--------------------
game_end= false; game_end= false;
int playerCount = 3; int playerCount = 3;
NetWorld * world= NetWorld_new(3, playerCount); NetWorld * world= NetWorld_new(4, playerCount);
Node_set( &(world->nodes[0]), (Vector2){1.4f, 1.28f}, RED ); 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[1]), (Vector2){21.0f, 22.8f}, MAGENTA );
Node_set( &(world->nodes[2]), (Vector2){18.4f, -12.2f}, GRAY ); Node_set( &(world->nodes[2]), (Vector2){18.4f, -12.2f}, GRAY );
Node_set( &(world->nodes[3]), (Vector2){10.0f, -10.0f}, GRAY );
NetWorld_biconnect(world, 0, 1); NetWorld_biconnect(world, 0, 1);
NetWorld_biconnect(world, 1, 2); NetWorld_biconnect(world, 1, 2);
...@@ -50,6 +52,8 @@ int main(int nbArg, char ** arg) ...@@ -50,6 +52,8 @@ int main(int nbArg, char ** arg)
for(int index = 0; index < world->size; index++){ for(int index = 0; index < world->size; index++){
Player_add_Node(&(players[index % playerCount]), &(world->nodes[index])); Player_add_Node(&(players[index % playerCount]), &(world->nodes[index]));
} }
Player_remove_Node(&(players[0]), &(world->nodes[3]));
Player_add_Node(&(players[1]), &(world->nodes[3]));
// Raylib Initialization // Raylib Initialization
//---------------------- //----------------------
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer"); InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
...@@ -96,18 +100,71 @@ void game_update(NetWorld * world, Player * players) ...@@ -96,18 +100,71 @@ void game_update(NetWorld * world, Player * players)
int playerCount = world->playerCount; int playerCount = world->playerCount;
switch(currentPlayer->turnPhase){ switch(currentPlayer->turnPhase){
case 1: case 1:
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if(currentPlayer->soldiers > 0){
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_add_soldier(currentPlayer, mousePosition);
}
}else{
Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48};
DrawRectangleRec(nextPhase, GREEN);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, nextPhase)){
Player_confirm_recrutement(currentPlayer);
printf("Recrutement confirmed\n");
currentPlayer->turnPhase = 2;
printf("Attack phase\n");
}
}
}
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition(); Vector2 mousePosition = GetMousePosition();
for(int nodeIndex = 0; nodeIndex < currentPlayer->nodeCount; nodeIndex++){ Player_remove_soldier(currentPlayer, mousePosition);
Node* currentNode = currentPlayer->nodes[nodeIndex]; }
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){ break;
currentNode->soldiers++; case 2:
currentPlayer->soldiers--; if(currentPlayer->hasSelectedNode){
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_unselect_Node(currentPlayer, mousePosition);
}
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Node * originNode = currentPlayer->selectedNode;
for(int nodeIndex = 0; nodeIndex < world->size; nodeIndex++){
Node* currentNode = &(world->nodes[nodeIndex]);
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
int nodePlayerID = currentNode->playerID;
if(nodePlayerID == currentPlayer->ID){
printf("Selected target Node\n");
currentPlayer->selectedNode = currentNode;
}
else if(Node_are_connected(originNode, currentNode) && (originNode->soldiers > 1) && originNode->canAttack){
printf("Engagine attack on target Node\n");
Player_attack_Node(currentPlayer, &(players[nodePlayerID]), originNode, currentNode);
}else{
printf("Failed to attack target : Origin has already attacked, nodes aren't connected or origin node has too few soldiers\n");
}
}
} }
} }
}else{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_select_Node(currentPlayer, mousePosition);
}
} }
if(currentPlayer->soldiers == 0){ Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48};
currentPlayer->turnPhase = -1; DrawRectangleRec(nextPhase, GREEN);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, nextPhase)){
printf("Attack phase ended\n");
currentPlayer->turnPhase = -1;
}
} }
break; break;
default:; default:;
...@@ -115,7 +172,11 @@ void game_update(NetWorld * world, Player * players) ...@@ -115,7 +172,11 @@ void game_update(NetWorld * world, Player * players)
if(currentPlayer->turnPhase == -1){ if(currentPlayer->turnPhase == -1){
Player_end_turn(currentPlayer); Player_end_turn(currentPlayer);
printf("Turn ended\n");
world->currentPlayer = (world->currentPlayer + 1) % playerCount; world->currentPlayer = (world->currentPlayer + 1) % playerCount;
printf("Player changed\n");
Player_start_turn(&(players[world->currentPlayer])); Player_start_turn(&(players[world->currentPlayer]));
printf("Turn started\n");
printf("Recrutement phase\n");
} }
} }
...@@ -17,12 +17,14 @@ void Node_construct(Node * self) ...@@ -17,12 +17,14 @@ void Node_construct(Node * self)
self->edges= Edge_newArray( 0 ); self->edges= Edge_newArray( 0 );
self->name= malloc( sizeof(char)*32 ); self->name= malloc( sizeof(char)*32 );
self->continent= malloc( sizeof(char)*32 ); self->continent= malloc( sizeof(char)*32 );
self->soldiers = 2; self->soldiers = 1;
self->soldiersToAdd = 0;
self->playerID = -1; self->playerID = -1;
Rectangle tmp = {0,0,48,48}; Rectangle tmp = {0,0,48,48};
self->collisionHitbox = tmp; self->collisionHitbox = tmp;
strcpy(self->continent, "None"); strcpy(self->continent, "None");
strcpy( self->name, "Node" ); strcpy( self->name, "Node" );
self->canAttack = true;
} }
Node * Node_new() Node * Node_new()
...@@ -117,6 +119,21 @@ int Node_biconnect( Node * node1, Node * node2 ) ...@@ -117,6 +119,21 @@ int Node_biconnect( Node * node1, Node * node2 )
return i1; return i1;
} }
void Node_confirm_placement(Node * self){
self->soldiers+= self->soldiersToAdd;
self->soldiersToAdd = 0;
}
bool Node_are_connected(Node * self, Node * target){
for(int index = 0; index < self->card; index++){
Edge currentEdge = self->edges[index];
if(currentEdge._target == target){
return true;
}
}
return false;
}
//-----------------------------------// //-----------------------------------//
//-- Edge --// //-- Edge --//
//-----------------------------------// //-----------------------------------//
......
...@@ -22,11 +22,14 @@ struct Str_Node { ...@@ -22,11 +22,14 @@ struct Str_Node {
char* name; char* name;
//! Number of soldiers at the node //! Number of soldiers at the node
int soldiers; int soldiers;
int soldiersToAdd;
//! Continent the node belongs to //! Continent the node belongs to
char* continent; char* continent;
//! Player the node belongs to //! Player the node belongs to
int playerID; int playerID;
//! Click hitbox for the node
Rectangle collisionHitbox; Rectangle collisionHitbox;
bool canAttack;
}; };
/** /**
...@@ -131,6 +134,11 @@ int Node_biconnect( ...@@ -131,6 +134,11 @@ int Node_biconnect(
Node * node2 Node * node2
); );
void Node_confirm_placement(Node * self);
bool Node_are_connected(Node * self, Node * target);
//-----------------------------------// //-----------------------------------//
//-- Edge --// //-- Edge --//
//-----------------------------------// //-----------------------------------//
......
...@@ -10,6 +10,8 @@ void Player_construct(Player * self, int i, Color color) ...@@ -10,6 +10,8 @@ void Player_construct(Player * self, int i, Color color)
self->color = color; self->color = color;
self->turnPhase = 0; self->turnPhase = 0;
self->soldiers = 0; self->soldiers = 0;
self->selectedNode = malloc(sizeof(Node));
self->hasSelectedNode = false;
} }
Player * Player_new() Player * Player_new()
...@@ -37,9 +39,8 @@ void Player_delete( Player * player ) ...@@ -37,9 +39,8 @@ void Player_delete( Player * player )
void Player_add_Node( Player * self, Node * node ) void Player_add_Node( Player * self, Node * node )
{ {
//Node * newNodes= Node_newArray(self->nodeCount + 1);
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount + 1)); Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount + 1));
for( int i = 0 ; i < self->nodeCount ; ++i ) for( int i = 0 ; i < self->nodeCount ; i++ )
{ {
newNodes[i] = self->nodes[i]; newNodes[i] = self->nodes[i];
} }
...@@ -53,17 +54,23 @@ void Player_add_Node( Player * self, Node * node ) ...@@ -53,17 +54,23 @@ void Player_add_Node( Player * self, Node * node )
void Player_remove_Node( Player * self, Node * node ) void Player_remove_Node( Player * self, Node * node )
{ {
//Node * newNodes = Node_newArray(self->nodeCount - 1); bool nodeFound = false;
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount - 1)); for( int i = 0 ; i < self->nodeCount-1 ; i++ )
for( int i = 0 ; i < self->nodeCount - 1 ; ++i )
{ {
if(self->nodes[i] != node){ if(nodeFound || self->nodes[i] == node){
newNodes[i] = self->nodes[i]; self->nodes[i] = self->nodes[i+1];
} }
} }
node->playerID = -1;
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount - 1));
for( int i = 0 ; i < self->nodeCount-1 ; i++ )
{
newNodes[i] = self->nodes[i];
}
free(self->nodes); free(self->nodes);
self->nodes= newNodes; self->nodes = newNodes;
node->playerID = -1;
self->nodeCount -= 1; self->nodeCount -= 1;
} }
...@@ -74,9 +81,138 @@ void Player_start_turn(Player * self){ ...@@ -74,9 +81,138 @@ void Player_start_turn(Player * self){
} }
self->soldiers = fmax(1, totalSoldierCount/3); self->soldiers = fmax(1, totalSoldierCount/3);
self->turnPhase = 1; self->turnPhase = 1;
printf("Current player : %i\n", self->ID);
printf("Current player soldiers : %i\n", self->soldiers); printf("Current player soldiers : %i\n", self->soldiers);
} }
void Player_end_turn(Player * self){ void Player_end_turn(Player * self){
self->turnPhase = 0; self->turnPhase = 0;
} }
\ No newline at end of file
void Player_add_soldier(Player * self, Vector2 mousePos){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
Node* currentNode = self->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePos, currentNode->collisionHitbox)){
currentNode->soldiersToAdd++;
self->soldiers--;
break;
}
}
}
void Player_remove_soldier(Player * self, Vector2 mousePos){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
Node* currentNode = self->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePos, currentNode->collisionHitbox) && currentNode->soldiers > 0){
if(currentNode->soldiersToAdd > 0){
currentNode->soldiersToAdd--;
self->soldiers++;
}
break;
}
}
}
void Player_select_Node(Player * self, Vector2 mousePos){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
Node* currentNode = self->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePos, currentNode->collisionHitbox)){
if(currentNode->soldiers > 1){
self->hasSelectedNode = true;
self->selectedNode = currentNode;
printf("Selected target Node\n");
}
break;
}
}
}
void Player_unselect_Node(Player * self, Vector2 mousePos){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
Node* currentNode = self->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePos, currentNode->collisionHitbox)){
self->hasSelectedNode = false;
printf("Unselected target node\n");
}
}
}
void Player_confirm_recrutement(Player * self){
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
Node_confirm_placement(self->nodes[nodeIndex]);
self->nodes[nodeIndex]->canAttack = true;
}
};
void Player_attack_Node(Player * self, Player * defender, Node * originNode, Node * targetNode){
int attackersCount = fmin(3, (originNode->soldiers-1));
int defendersCount = targetNode->soldiers >= 3? 2:1;
int attDiceRolls[attackersCount];
int defDiceRolls[defendersCount];
printf("%i attackers\n", attackersCount);
printf("%i defenders\n", defendersCount);
for(int index = 0; index < attackersCount; index++){
attDiceRolls[index] = (rand() % 6) + 1;
}
for(int index = 0; index < defendersCount; index++){
defDiceRolls[index] = (rand() % 6) + 1;
}
for(int _ = 1; _ < attackersCount; _++){
for(int index = 0; index < attackersCount - 1; index++){
if(attDiceRolls[index] < attDiceRolls[index + 1]){
int tmp = attDiceRolls[index];
attDiceRolls[index] = attDiceRolls[index + 1];
attDiceRolls[index + 1] = tmp;
}
}
}
for(int _ = 1; _ < defendersCount; _++){
for(int index = 0; index < defendersCount - 1; index++){
if(defDiceRolls[index] < defDiceRolls[index + 1]){
int tmp = defDiceRolls[index];
defDiceRolls[index] = defDiceRolls[index + 1];
defDiceRolls[index + 1] = tmp;
}
}
}
printf("Attackers dice rolls :\t");
for(int index = 0; index < attackersCount; index++){
printf("%i\t", attDiceRolls[index]);
}
printf("\n");
printf("Defenders dice rolls :\t");
for(int index = 0; index < defendersCount; index++){
printf("%i\t", defDiceRolls[index]);
}
printf("\n");
int remainingAttackers = attackersCount;
for(int index = 0; index < fmin(defendersCount, attackersCount); index++){
if(attDiceRolls[index] > defDiceRolls[index]){
targetNode->soldiers--;
printf("Defenders lose a soldier\n");
}else{
originNode->soldiers--;
remainingAttackers--;
printf("Attackers lose a soldier\n");
}
}
if(targetNode->soldiers <= 0){
printf("Node has been conquered\n");
Player_remove_Node(defender, targetNode);
Player_add_Node(self, targetNode);
targetNode->soldiers = attackersCount;
originNode->soldiers -= attackersCount;
}
originNode->canAttack = false;
}
...@@ -10,6 +10,8 @@ struct Str_Player{ ...@@ -10,6 +10,8 @@ struct Str_Player{
Color color; Color color;
int turnPhase; int turnPhase;
int soldiers; int soldiers;
Node * selectedNode;
bool hasSelectedNode;
}; };
typedef struct Str_Player Player; typedef struct Str_Player Player;
...@@ -74,4 +76,16 @@ void Player_start_turn(Player * self); ...@@ -74,4 +76,16 @@ void Player_start_turn(Player * self);
*/ */
void Player_end_turn(Player * self); void Player_end_turn(Player * self);
void Player_add_soldier(Player * self, Vector2 mousePos);
void Player_remove_soldier(Player * self, Vector2 mousePos);
void Player_select_Node(Player * self, Vector2 mousePos);
void Player_unselect_Node(Player * self, Vector2 mousePos);
void Player_attack_Node(Player * self, Player * defender, Node * originNode, Node * targetNode);
void Player_confirm_recrutement(Player * self);
#endif #endif
\ No newline at end of file
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