Commit cb3f9a52 authored by LAIRD Timothy's avatar LAIRD Timothy

Merge branch 'game-ui' into 'dev'

game ui to dev

See merge request !4
parents e7511d9e e282cb0e
...@@ -18,7 +18,8 @@ include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/src) ...@@ -18,7 +18,8 @@ include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/src)
include_directories( ${PROJECT_SOURCE_DIR}/dpd/include ) include_directories( ${PROJECT_SOURCE_DIR}/dpd/include )
link_directories( ${PROJECT_SOURCE_DIR}/dpd ) link_directories( ${PROJECT_SOURCE_DIR}/dpd )
add_executable(nw-viewer src/main-viewer.c src/networld.c src/controlpanel.c src/entity.c src/player.c src/random-map.c src/window-manager.c src/menu-button.c src/main-menu.c) add_executable(nw-viewer src/main-viewer.c src/networld.c src/controlpanel.c src/entity.c src/player.c src/random-map.c src/graphical-aspect.c src/window-manager.c src/menu-button.c src/main-menu.c)
target_link_libraries(nw-viewer raylib pthread dl rt X11 m) target_link_libraries(nw-viewer raylib pthread dl rt X11 m)
#without cmake package... #without cmake package...
......
File added
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include "graphical-aspect.h"
#include "networld.h"
#include "controlpanel.h"
#include "player.h"
#include "raylib.h"
void Graphic_ShowPlayerInfo(Player * player, int screenWidth, int screenHeight, Font font){
Rectangle rec = {0, screenHeight - 250, 250, 250};
DrawRectangleLinesEx(rec, 10, player->color);
//Font font = LoadFont("Game On_PersonalUseOnly.ttf");
char * playerNumber = malloc(20*sizeof(char));
sprintf(playerNumber, "Player %d", player->ID);
DrawTextEx(font, playerNumber, (Vector2){35, screenHeight - 240}, 30.0, 3.0, player->color);
char * soldierNumber = malloc(40*sizeof(char));
sprintf(soldierNumber, "Soldiers left %d", player->soldiers);
DrawTextEx(font, soldierNumber, (Vector2){15, screenHeight - 180}, 24.0, 3.0, player->color);
int totalTroops = 0;
for(int i =0; i < player->nodeCount; i++){
totalTroops += player->nodes[i]->soldiers;
}
char * str_totalTroops = malloc(40*sizeof(char));
sprintf(str_totalTroops, "Total Troops %d", totalTroops);
DrawTextEx(font, str_totalTroops, (Vector2){15, screenHeight - 150}, 24.0, 3.0, player->color);
char * str_nodeCount = malloc(40*sizeof(char));
sprintf(str_nodeCount, "Nodes owned %d", player->nodeCount);
DrawTextEx(font, str_nodeCount, (Vector2){15, screenHeight - 120}, 24.0, 3.0, player->color);
free(playerNumber);
free(soldierNumber);
free(str_totalTroops);
free(str_nodeCount);
}
void Graphic_RecrutementPhase(Player * player, int screenWidth, int screenHeight, Font font){
Rectangle rec = {screenWidth-300, 100 , 300, 400};
DrawRectangleLinesEx(rec, 10, player->color);
DrawTextEx(font, "Recrutement Phase",(Vector2){screenWidth-275, 120}, 23.0, 3.0, player->color);
DrawTextRec(font, " LEFT click on one of your node to recruit a soldier", (Rectangle){screenWidth-285, 175, 270, 100}, 20.0, 3.0, true, player->color);
DrawTextRec(font, " RIGHT click on the same node to remove the soldier", (Rectangle){screenWidth-285, 295, 270, 100}, 20.0, 3.0, true, player->color);
DrawTextRec(font, " click Confirm once finished", (Rectangle){screenWidth-285, 415, 270, 100}, 20.0, 3.0, true, player->color);
}
void Graphic_AttackPhase(Player * player, int screenWidth, int screenHeight, Font font){
Rectangle rec = {screenWidth-300, 100 , 300, 400};
DrawRectangleLinesEx(rec, 10, player->color);
DrawTextEx(font, "Attack Phase",(Vector2){screenWidth-265, 120}, 23.0, 3.0, player->color);
DrawTextRec(font, " FIRST click on the node you want to attack from", (Rectangle){screenWidth-285, 175, 270, 100}, 20.0, 3.0, true, player->color);
DrawTextRec(font, " THEN click on one of neighbouring node you want to attack", (Rectangle){screenWidth-285, 295, 270, 100}, 20.0, 3.0, true, player->color);
DrawTextRec(font, " click Next Turn once finished", (Rectangle){screenWidth-285, 415, 270, 100}, 20.0, 3.0, true, player->color);
}
void Graphic_WhoseTurnIsIt(Player * player, int screenWidth, int screenHeight, Font font){
//Rectangle rec = {0, screenHeight - 250, 250, 250};
//DrawRectangleLinesEx(rec, 10, player->color);
//Font font = LoadFont("Game On_PersonalUseOnly.ttf");
char * whoseTurn = malloc(20*sizeof(char));
sprintf(whoseTurn, "Player %d Turn", player->ID);
DrawTextEx(font, whoseTurn, (Vector2){screenWidth/2 - (MeasureTextEx(font, whoseTurn, 50.0, 3.0).x)/2 , 30} , 50.0, 3.0, player->color);
free(whoseTurn);
}
Rectangle Graphic_ConfirmButton(Font font)
{
Rectangle nextPhase = {200, 200, 220, 55};
DrawRectangleRec(nextPhase, GREEN);
DrawTextEx(font, "Confirm",(Vector2){220 , 210} , 40.0, 3.0, BLACK);
return nextPhase;
}
void Graphic_MouseHoverNodeRecrutement(Player * player, Vector2 mousePosition, Panel * panel){
for(int nodeIndex = 0; nodeIndex < player->nodeCount; nodeIndex++){
Node* currentNode = player->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
//printf("collision noeud !\n");
Vector2 screenPosition= Panel_pixelFromPosition(panel, &(currentNode->position) );
DrawCircleV(screenPosition, 29, MAGENTA);
//DrawCircleV(screenPosition, 45, RAYWHITE);
if(player->soldiers > 0){
DrawText("+1", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, MAGENTA);
}
}
}
}
void Graphic_MouseHoverNodeChooseAttacker(Player * player, Vector2 mousePosition, Panel * panel){
for(int nodeIndex = 0; nodeIndex < player->nodeCount; nodeIndex++){
Node* currentNode = player->nodes[nodeIndex];
if(CheckCollisionPointRec(mousePosition, currentNode->collisionHitbox)){
Vector2 screenPosition= Panel_pixelFromPosition(panel, &(currentNode->position) );
DrawCircleV(screenPosition, 29, GOLD);
//DrawCircleV(screenPosition, 45, RAYWHITE);
if(!player->hasSelectedNode){
DrawText("attacker", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, GOLD);
}
}
}
if(player->hasSelectedNode){
Vector2 screenPosition= Panel_pixelFromPosition(panel, &(player->selectedNode->position) );
DrawCircleV(screenPosition, 29, GOLD);
DrawText("attacker", (int)screenPosition.x + 30 , (int)screenPosition.y, 20, GOLD);
}
}
void Graphic_MouseHoverNodeChooseTarget(Node * originNode, Vector2 mousePosition, Panel * panel){
for(int neighbourIndex = 0; neighbourIndex < originNode->card; neighbourIndex++){
Vector2 screenPosition = Panel_pixelFromPosition(panel, &(originNode->edges[neighbourIndex]._target->position) );
/* if( &(originNode->edges[neighbourIndex]._target->color) = &(originNode->color)){
DrawCircleV(screenPosition, 29, GREEN);
} */
}
}
#define MAX_FRAME_SPEED 15
#define MIN_FRAME_SPEED 1
void diceRolling()
{ printf("dice rolling begin\n"); // Initialization
//--------------------------------------------------------------------------------------
const int screenWidth1 = 800;
const int screenHeight1 = 450;
InitWindow(screenWidth1, screenHeight1, "raylib [texture] example - texture rectangle");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D diceTexture = LoadTexture("diceRolling.png"); // Texture loading
Vector2 position = { 350.0f, 100.0f };
Rectangle frameRec = { 0.0f, 0.0f, (float)diceTexture.width, (float)diceTexture.height/9 };
int currentFrame = 0;
int framesCounter = 0;
int framesSpeed = 10;
int frameExit = 0; // Number of spritesheet frames shown by second
bool endAnimation = false;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose() ) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
framesCounter++;
frameExit++;
printf("%d frameExit \n", frameExit);
if(frameExit == 100) endAnimation = true;
if (framesCounter >= (60/framesSpeed))
{
framesCounter = 0;
currentFrame++;
if (currentFrame > 8) currentFrame = 0;
frameRec.y = (float)currentFrame*(float)diceTexture.height/9;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
//Vector2 * positionDiceAttacker = malloc(sizeof(Vector2)*attackerNbDice) ;
/* for(int attackerDice = 0; attackerDice < attackerNbDice ; attackerDice++){
} */
DrawTextureRec(diceTexture, frameRec, position, WHITE); // Draw part of the texture
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Texture unloading
CloseWindow(); // Close window and OpenGL context
UnloadTexture(diceTexture);
//--------------------------------------------------------------------------------------
}
#ifndef GRAPHICALASPECT_H
#define GRAPHICALASPECT_H
#include "raylib.h"
#include "networld.h"
#include "player.h"
#include "controlpanel.h"
//Graphical view
void Graphic_ShowPlayerInfo(Player * player, int screenWidth, int screenHeight, Font font);
void Graphic_RecrutementPhase(Player * player, int screenWidth, int screenHeight, Font font);
void Graphic_AttackPhase(Player * player, int screenWidth, int screenHeight, Font font);
void Graphic_WhoseTurnIsIt(Player * player, int screenWidth, int screenHeight, Font font);
Rectangle Graphic_ConfirmButton(Font font);
void Graphic_MouseHoverNodeRecrutement(Player * self, Vector2 mousePosition, Panel * panel);
void Graphic_MouseHoverNodeChooseAttacker(Player * player, Vector2 mousePosition, Panel * panel);
void Graphic_MouseHoverNodeChooseTarget(Node * originNode, Vector2 mousePosition, Panel * panel);
void diceRolling();
#endif
\ No newline at end of file
...@@ -15,16 +15,17 @@ ...@@ -15,16 +15,17 @@
#include "player.h" #include "player.h"
#include "controlpanel.h" #include "controlpanel.h"
#include "random-map.h" #include "random-map.h"
#include "graphical-aspect.h"
#include "window-manager.h" #include "window-manager.h"
#include "main-menu.h" #include "main-menu.h"
// Program attributes // Program attributes
//------------------- //-------------------
const int screenWidth = 800; const int screenWidth = 1700;
const int screenHeight = 600; const int screenHeight = 800;
const int targetFPS = 60; const int targetFPS = 60;
void game_update(NetWorld * world, Player * players); void game_update(NetWorld * world, Player * players, Font font, Panel * panel);
// Game attributes // Game attributes
//----------------- //-----------------
...@@ -32,7 +33,7 @@ bool game_end; ...@@ -32,7 +33,7 @@ bool game_end;
int main(int nbArg, char ** arg) int main(int nbArg, char ** arg)
{ {
int nodeCount = 5; int nodeCount = 10;
int playerCount = 3; int playerCount = 3;
NetWorld * world= NetWorld_new(nodeCount, playerCount); NetWorld * world= NetWorld_new(nodeCount, playerCount);
Random_map(world); Random_map(world);
...@@ -54,6 +55,8 @@ int main(int nbArg, char ** arg) ...@@ -54,6 +55,8 @@ int main(int nbArg, char ** arg)
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer"); InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
SetTargetFPS(targetFPS); SetTargetFPS(targetFPS);
Font font = LoadFont("GAMERIA.ttf");
// Main game loop // Main game loop
Main_Menu * menu = Main_Menu_init(screenWidth, screenHeight); Main_Menu * menu = Main_Menu_init(screenWidth, screenHeight);
Window_Manager * manager = Window_Manager_new(panel, menu); Window_Manager * manager = Window_Manager_new(panel, menu);
...@@ -79,7 +82,7 @@ int main(int nbArg, char ** arg) ...@@ -79,7 +82,7 @@ int main(int nbArg, char ** arg)
} }
break; break;
case game_ui: case game_ui:
game_update(world, players); game_update(world, players, font, panel);
default: default:
break; break;
} }
...@@ -87,31 +90,37 @@ int main(int nbArg, char ** arg) ...@@ -87,31 +90,37 @@ int main(int nbArg, char ** arg)
// proper closing // proper closing
//--------------- //---------------
NetWorld_delete(world); NetWorld_delete(world);
Player_delete(players); Player_delete(players);
UnloadFont(font);
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
return 0; return 0;
} }
void game_update(NetWorld * world, Player * players) void game_update(NetWorld * world, Player * players, Font font, Panel * panel)
{ {
Vector2 mousePosition = { 0.0f, 0.0f };
mousePosition = GetMousePosition();
Player * currentPlayer = &(players[world->currentPlayer]); Player * currentPlayer = &(players[world->currentPlayer]);
Graphic_ShowPlayerInfo(currentPlayer, screenWidth, screenHeight, font);
Graphic_WhoseTurnIsIt(currentPlayer, screenWidth, screenHeight, font);
int playerCount = world->playerCount; int playerCount = world->playerCount;
Player * newPlayers = malloc(sizeof(Player)); Player * newPlayers = malloc(sizeof(Player));
switch(currentPlayer->turnPhase){ switch(currentPlayer->turnPhase){
case recruitment: case recruitment:
Graphic_RecrutementPhase(currentPlayer, screenWidth, screenHeight, font);
Graphic_MouseHoverNodeRecrutement(currentPlayer, mousePosition, panel);
if(currentPlayer->soldiers > 0){ if(currentPlayer->soldiers > 0){
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_add_soldier(currentPlayer, mousePosition); Player_add_soldier(currentPlayer, mousePosition);
} }
}else{ }else{
Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48}; Rectangle nextPhaseButton = Graphic_ConfirmButton(font);
DrawRectangleRec(nextPhase, GREEN);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition(); if(CheckCollisionPointRec(mousePosition, nextPhaseButton)){
if(CheckCollisionPointRec(mousePosition, nextPhase)){
Player_confirm_recrutement(currentPlayer); Player_confirm_recrutement(currentPlayer);
printf("Recrutement confirmed\n"); printf("Recrutement confirmed\n");
currentPlayer->turnPhase = attack; currentPlayer->turnPhase = attack;
...@@ -120,18 +129,19 @@ void game_update(NetWorld * world, Player * players) ...@@ -120,18 +129,19 @@ void game_update(NetWorld * world, Player * players)
} }
} }
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){ if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_remove_soldier(currentPlayer, mousePosition); Player_remove_soldier(currentPlayer, mousePosition);
} }
break; break;
case attack: case attack:
Graphic_AttackPhase(currentPlayer, screenWidth, screenHeight, font);
Graphic_MouseHoverNodeChooseAttacker(currentPlayer, mousePosition, panel);
if(currentPlayer->hasSelectedNode){ if(currentPlayer->hasSelectedNode){
Graphic_MouseHoverNodeChooseTarget(currentPlayer->selectedNode, mousePosition, panel);
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){ if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_unselect_Node(currentPlayer, mousePosition); Player_unselect_Node(currentPlayer, mousePosition);
} }
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Node * originNode = currentPlayer->selectedNode; Node * originNode = currentPlayer->selectedNode;
for(int nodeIndex = 0; nodeIndex < world->size; nodeIndex++){ for(int nodeIndex = 0; nodeIndex < world->size; nodeIndex++){
...@@ -146,6 +156,7 @@ void game_update(NetWorld * world, Player * players) ...@@ -146,6 +156,7 @@ void game_update(NetWorld * world, Player * players)
else if(Node_are_connected(originNode, currentNode) && (originNode->soldiers > 1) && originNode->canAttack){ else if(Node_are_connected(originNode, currentNode) && (originNode->soldiers > 1) && originNode->canAttack){
printf("Engagine attack on target Node\n"); printf("Engagine attack on target Node\n");
Player_attack_Node(currentPlayer, &(players[nodePlayerID]), originNode, currentNode); Player_attack_Node(currentPlayer, &(players[nodePlayerID]), originNode, currentNode);
diceRolling();
}else{ }else{
printf("Failed to attack target : Origin has already attacked, nodes aren't connected or origin node has too few soldiers\n"); printf("Failed to attack target : Origin has already attacked, nodes aren't connected or origin node has too few soldiers\n");
} }
...@@ -154,14 +165,13 @@ void game_update(NetWorld * world, Player * players) ...@@ -154,14 +165,13 @@ void game_update(NetWorld * world, Player * players)
} }
}else{ }else{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
Player_select_Node(currentPlayer, mousePosition); Player_select_Node(currentPlayer, mousePosition);
} }
} }
Rectangle nextPhase = {screenWidth-48,screenHeight-48,48,48}; Rectangle nextPhase = {200, 200, 270, 55};
DrawRectangleRec(nextPhase, GREEN); DrawRectangleRec(nextPhase, GREEN);
DrawTextEx(font, "Next Turn",(Vector2){220 , 210} , 40.0, 3.0, BLACK);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, nextPhase)){ if(CheckCollisionPointRec(mousePosition, nextPhase)){
printf("Attack phase ended\n"); printf("Attack phase ended\n");
currentPlayer->turnPhase = init; currentPlayer->turnPhase = init;
......
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