Commit e7511d9e authored by LAIRD Timothy's avatar LAIRD Timothy

Merge branch 'main-menu' into 'dev'

main menu to dev

See merge request !3
parents 5063ae63 640d7fee
...@@ -18,7 +18,7 @@ include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/src) ...@@ -18,7 +18,7 @@ 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) 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)
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...
......
...@@ -27,7 +27,7 @@ void Panel_delete(Panel * self) ...@@ -27,7 +27,7 @@ void Panel_delete(Panel * self)
} }
// Initialization // Initialization
void Panel_initialize(Panel * self, NetWorld * world) void Panel_initialize(Panel * self, NetWorld * world, const int screenWidth, const int screenHeight)
{ {
self->world= world; self->world= world;
self->camera.x= 0.f; self->camera.x= 0.f;
...@@ -35,6 +35,8 @@ void Panel_initialize(Panel * self, NetWorld * world) ...@@ -35,6 +35,8 @@ void Panel_initialize(Panel * self, NetWorld * world)
self->screenCenter.x= 400.f; self->screenCenter.x= 400.f;
self->screenCenter.y= 300.f; self->screenCenter.y= 300.f;
self->scale= 10.f; //pixel per meters self->scale= 10.f; //pixel per meters
self->screenWidth = screenWidth;
self->screenHeight = screenHeight;
} }
// To String // To String
...@@ -46,8 +48,23 @@ void Panel_print(Panel * self) ...@@ -46,8 +48,23 @@ void Panel_print(Panel * self)
self->scale); self->scale);
} }
void Panel_drawMainMenu(Panel * self, Main_Menu * menu){
Font font = GetFontDefault();
BeginDrawing();
DrawText("RISK", (self->screenWidth*35)/100 , (self->screenHeight*15)/100 , (self->screenWidth*10)/100,BLACK);
Button_draw(menu->buttons[game_ui], &font, self->screenWidth, self->screenHeight);
Button_draw(menu->buttons[options], &font, self->screenWidth, self->screenHeight);
ClearBackground(RAYWHITE);
EndDrawing();
}
// Rendering // Rendering
void Panel_draw(Panel * self) void Panel_drawGame(Panel * self)
{ {
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
...@@ -65,7 +82,7 @@ void Panel_draw(Panel * self) ...@@ -65,7 +82,7 @@ void Panel_draw(Panel * self)
{ {
Panel_drawNode( self, &(self->world->nodes[i]) ); Panel_drawNode( self, &(self->world->nodes[i]) );
} }
Panel_drawBasis(self); //Panel_drawBasis(self);
EndDrawing(); EndDrawing();
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define CONTROLPANEL_H #define CONTROLPANEL_H
#include "networld.h" #include "networld.h"
#include "main-menu.h"
// Tools // Tools
void printVector2( Vector2 v ); void printVector2( Vector2 v );
...@@ -14,6 +14,7 @@ void printVector2( Vector2 v ); ...@@ -14,6 +14,7 @@ void printVector2( Vector2 v );
struct Str_Panel { struct Str_Panel {
Vector2 camera, screenCenter; Vector2 camera, screenCenter;
int screenWidth, screenHeight;
float scale; // meter per pixel float scale; // meter per pixel
NetWorld * world; NetWorld * world;
}; };
...@@ -25,19 +26,22 @@ Panel * Panel_new(); ...@@ -25,19 +26,22 @@ Panel * Panel_new();
void Panel_delete(Panel * self); void Panel_delete(Panel * self);
// Initialization // Initialization
void Panel_initialize(Panel * self, NetWorld * world); void Panel_initialize(Panel * self, NetWorld * world, const int screenWidth, const int screenHeight);
// To String // To String
void Panel_print(Panel * self); void Panel_print(Panel * self);
// Rendering // Rendering the game
void Panel_draw(Panel * self); void Panel_drawGame(Panel * self);
void Panel_drawBasis(Panel * self); void Panel_drawBasis(Panel * self);
void Panel_drawNode(Panel * self, Node * n); void Panel_drawNode(Panel * self, Node * n);
void Panel_drawEdge(Panel * self, Edge * e); void Panel_drawEdge(Panel * self, Edge * e);
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p); Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p);
Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p); Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p);
//Rendering the main menu
void Panel_drawMainMenu(Panel * self, Main_Menu * menu);
// Control // Control
void Panel_control(Panel * self); void Panel_control(Panel * self);
void Panel_controlCamera(Panel * self); void Panel_controlCamera(Panel * self);
......
#include "main-menu.h"
#include <stdlib.h>
#include <stdio.h>
Main_Menu * Main_Menu_init(const int screenWidth, const int screenHeight){
Main_Menu * self = malloc(sizeof(Main_Menu));
Rectangle playGameBtn = {(screenWidth*32)/100, (screenHeight*35)/100, (screenWidth*30)/100, (screenHeight*5)/100};
Rectangle smallPlayGameBtn = {3 + (screenWidth*32/100), 3 + (screenHeight*35/100), (screenWidth*30/100) - 6, (screenHeight*5/100) - 6};
Menu_Button * playGame = Button_init(playGameBtn, smallPlayGameBtn, "\t\t\t PLAY", game_ui);
Rectangle optionsBtn = {(screenWidth*32)/100, (screenHeight*45)/100, (screenWidth*30)/100, (screenHeight*5)/100};
Rectangle smallOptionsBtn = {3 + (screenWidth*32/100), 3 + (screenHeight*45/100), (screenWidth*30/100) - 6, (screenHeight*5/100) - 6};
Menu_Button * optionsMenu = Button_init(optionsBtn, smallOptionsBtn, "\t\tOPTIONS", options);
self->buttonCount = 2;
self->buttons = malloc(sizeof(Menu_Button) * self->buttonCount);
self->buttons[game_ui] = playGame;
self->buttons[options] = optionsMenu;
return self;
}
Display Main_Menu_detect_click(Main_Menu * self, Vector2 * mousePos){
for(int index = 0; index < self->buttonCount; index++){
Menu_Button * currentBtn = self->buttons[index];
if(CheckCollisionPointRec(*mousePos, currentBtn->button)){
printf("returned %d\n", currentBtn->displayOnClick);
return currentBtn->displayOnClick;
}
}
return main_menu;
}
#ifndef MAINMENU_H
#define MAINMENU_H
#include "menu-button.h"
struct Str_Main_Menu{
Menu_Button ** buttons;
int buttonCount;
char ** buttonDisplayNames;
};
typedef struct Str_Main_Menu Main_Menu;
Main_Menu * Main_Menu_init(const int screenWidth, const int screenHeight);
Display Main_Menu_detect_click(Main_Menu * self, Vector2 * mousePos);
#endif
\ No newline at end of file
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "player.h" #include "player.h"
#include "controlpanel.h" #include "controlpanel.h"
#include "random-map.h" #include "random-map.h"
#include "window-manager.h"
#include "main-menu.h"
// Program attributes // Program attributes
//------------------- //-------------------
...@@ -34,22 +36,13 @@ int main(int nbArg, char ** arg) ...@@ -34,22 +36,13 @@ int main(int nbArg, char ** arg)
int playerCount = 3; int playerCount = 3;
NetWorld * world= NetWorld_new(nodeCount, playerCount); NetWorld * world= NetWorld_new(nodeCount, playerCount);
Random_map(world); Random_map(world);
//Node_set(&(world->nodes[0]), (Vector2){1.4f, 1.28f}, ORANGE);
printf("positionX %f\n", world->nodes[0].position.x);
printf("positionY %f\n", world->nodes[0].position.y);
printf("positionX %f\n", world->nodes[1].position.x);
printf("positionY %f\n", world->nodes[1].position.y);
printf("distXY %f\n", dist(world->nodes[0], world->nodes[1]));
// Game Initialization // Game Initialization
//-------------------- //--------------------
game_end= false; game_end= false;
Panel * panel= Panel_new(); Panel * panel= Panel_new();
Panel_initialize(panel, world); Panel_initialize(panel, world, screenWidth, screenHeight);
Color colors[] = {BLACK, RED, BLUE}; Color colors[] = {BLACK, RED, BLUE};
Player * players = Player_newArray(playerCount, colors); Player * players = Player_newArray(playerCount, colors);
...@@ -60,10 +53,12 @@ int main(int nbArg, char ** arg) ...@@ -60,10 +53,12 @@ int main(int nbArg, char ** arg)
//---------------------- //----------------------
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer"); InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
SetTargetFPS(targetFPS); SetTargetFPS(targetFPS);
// Main game loop // Main game loop
world->currentPlayer = 0; Main_Menu * menu = Main_Menu_init(screenWidth, screenHeight);
Player_start_turn(&(players[world->currentPlayer])); Window_Manager * manager = Window_Manager_new(panel, menu);
world->currentPlayer = playerCount - 1;
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
{ {
DrawFPS(10, 10); DrawFPS(10, 10);
...@@ -73,8 +68,21 @@ int main(int nbArg, char ** arg) ...@@ -73,8 +68,21 @@ int main(int nbArg, char ** arg)
world->nodes[index].collisionHitbox.y = screenPosition.y - 24; world->nodes[index].collisionHitbox.y = screenPosition.y - 24;
} }
Panel_control(panel); Panel_control(panel);
game_update(world, players); Window_Manager_display(manager);
Panel_draw(panel);
switch (manager->display)
{
case main_menu:
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePos = GetMousePosition();
manager->display = Main_Menu_detect_click(menu, &mousePos);
}
break;
case game_ui:
game_update(world, players);
default:
break;
}
} }
// proper closing // proper closing
...@@ -92,7 +100,7 @@ void game_update(NetWorld * world, Player * players) ...@@ -92,7 +100,7 @@ void game_update(NetWorld * world, Player * players)
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 1: case recruitment:
if(currentPlayer->soldiers > 0){ if(currentPlayer->soldiers > 0){
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition(); Vector2 mousePosition = GetMousePosition();
...@@ -106,7 +114,7 @@ void game_update(NetWorld * world, Player * players) ...@@ -106,7 +114,7 @@ void game_update(NetWorld * world, Player * players)
if(CheckCollisionPointRec(mousePosition, nextPhase)){ if(CheckCollisionPointRec(mousePosition, nextPhase)){
Player_confirm_recrutement(currentPlayer); Player_confirm_recrutement(currentPlayer);
printf("Recrutement confirmed\n"); printf("Recrutement confirmed\n");
currentPlayer->turnPhase = 2; currentPlayer->turnPhase = attack;
printf("Attack phase\n"); printf("Attack phase\n");
} }
} }
...@@ -116,7 +124,7 @@ void game_update(NetWorld * world, Player * players) ...@@ -116,7 +124,7 @@ void game_update(NetWorld * world, Player * players)
Player_remove_soldier(currentPlayer, mousePosition); Player_remove_soldier(currentPlayer, mousePosition);
} }
break; break;
case 2: case attack:
if(currentPlayer->hasSelectedNode){ if(currentPlayer->hasSelectedNode){
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){ if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition(); Vector2 mousePosition = GetMousePosition();
...@@ -156,11 +164,11 @@ void game_update(NetWorld * world, Player * players) ...@@ -156,11 +164,11 @@ void game_update(NetWorld * world, Player * players)
Vector2 mousePosition = GetMousePosition(); Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, nextPhase)){ if(CheckCollisionPointRec(mousePosition, nextPhase)){
printf("Attack phase ended\n"); printf("Attack phase ended\n");
currentPlayer->turnPhase = -1; currentPlayer->turnPhase = init;
} }
} }
break; break;
case -1 : case init :
Player_end_turn(currentPlayer); Player_end_turn(currentPlayer);
printf("Turn ended\n"); printf("Turn ended\n");
world->currentPlayer = (world->currentPlayer + 1) % playerCount; world->currentPlayer = (world->currentPlayer + 1) % playerCount;
......
#include "menu-button.h"
#include <stdlib.h>
Menu_Button * Button_init(Rectangle button, Rectangle smallerButton, char* displayText, Display display){
Menu_Button * self = malloc(sizeof(Menu_Button));
self->button = button;
self->smallerButton = smallerButton;
self->displayText = displayText;
self->displayOnClick = display;
return self;
}
void Button_draw(Menu_Button * self, Font * font, const int screenWidth, const int screenHeight){
DrawRectangleRec(self->button, RED);
DrawRectangleRec(self->smallerButton, WHITE);
DrawTextRec(*font, self->displayText, self->smallerButton, (screenHeight*4)/100, (screenWidth/75), true, BLACK);
}
#ifndef MENUBUTTON_H
#define MENUBUTTON_H
#include "raylib.h"
enum displays {game_ui, options, load_save, main_menu};
typedef enum displays Display;
struct Str_Menu_Button{
Rectangle button;
Rectangle smallerButton;
char* displayText;
Display displayOnClick;
};
typedef struct Str_Menu_Button Menu_Button;
Menu_Button * Button_init(Rectangle button, Rectangle smallerButton, char* displayText, Display display);
void Button_draw(Menu_Button * self, Font * font, const int screenWidth, const int screenHeight);
#endif
\ No newline at end of file
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
void Player_construct(Player * self, int i, Color color) void Player_construct(Player * self, int i, Color color)
{ {
self->ID = i; self->ID = i;
self->nodes = malloc(sizeof(Node*)); self->nodes = malloc(sizeof(Node*));
self->nodeCount = 0; self->nodeCount = 0;
self->color = color; self->color = color;
self->turnPhase = 0; self->turnPhase = init;
self->soldiers = 0; self->soldiers = 0;
self->selectedNode = malloc(sizeof(Node)); self->selectedNode = malloc(sizeof(Node));
self->hasSelectedNode = false; self->hasSelectedNode = false;
...@@ -76,14 +77,14 @@ void Player_remove_Node( Player * self, Node * node ) ...@@ -76,14 +77,14 @@ void Player_remove_Node( Player * self, Node * node )
void Player_start_turn(Player * self){ void Player_start_turn(Player * self){
if(self->nodeCount == 0){ if(self->nodeCount == 0){
self->turnPhase = -1; self->turnPhase = init;
}else{ }else{
int totalSoldierCount = 0; int totalSoldierCount = 0;
for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){ for(int nodeIndex = 0; nodeIndex < self->nodeCount; nodeIndex++){
totalSoldierCount += self->nodes[nodeIndex]->soldiers; totalSoldierCount += self->nodes[nodeIndex]->soldiers;
} }
self->soldiers = fmax(1, totalSoldierCount/3); self->soldiers = fmax(1, totalSoldierCount/3);
self->turnPhase = 1; self->turnPhase = recruitment;
printf("Current player : %i\n", self->ID); printf("Current player : %i\n", self->ID);
printf("Current player soldiers : %i\n", self->soldiers); printf("Current player soldiers : %i\n", self->soldiers);
} }
...@@ -91,7 +92,7 @@ void Player_start_turn(Player * self){ ...@@ -91,7 +92,7 @@ void Player_start_turn(Player * self){
} }
void Player_end_turn(Player * self){ void Player_end_turn(Player * self){
self->turnPhase = 0; self->turnPhase = init;
} }
void Player_add_soldier(Player * self, Vector2 mousePos){ void Player_add_soldier(Player * self, Vector2 mousePos){
......
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
#include "networld.h" #include "networld.h"
enum turnPhases {init, recruitment, attack};
typedef enum turnPhases TurnPhases;
struct Str_Player{ struct Str_Player{
int ID; int ID;
Node ** nodes; Node ** nodes;
int nodeCount; int nodeCount;
Color color; Color color;
int turnPhase; TurnPhases turnPhase;
int soldiers; int soldiers;
Node * selectedNode; Node * selectedNode;
bool hasSelectedNode; bool hasSelectedNode;
......
#include "window-manager.h"
#include "controlpanel.h"
#include <stdlib.h>
#include <stdio.h>
Window_Manager * Window_Manager_new(Panel * panel, Main_Menu * menu){
Window_Manager * self = malloc(sizeof(Window_Manager));
self->panel = panel;
self->display = (Display) main_menu;
self->menu = menu;
return self;
}
void Window_Manager_delete(Window_Manager * manager){
free(manager);
}
void Window_Manager_display(Window_Manager * self){
switch(self->display){
case main_menu:
Panel_drawMainMenu(self->panel, self->menu);
break;
case options:
break;
case game_ui:
Panel_drawGame(self->panel);
break;
case load_save:
break;
default:
break;
}
}
\ No newline at end of file
#ifndef DISPLAYMANAGER_H
#define DISPLAYMANAGER_H
#include "networld.h"
#include "controlpanel.h"
struct Str_Window_Manager{
Display display;
Panel * panel;
Main_Menu * menu;
};
typedef struct Str_Window_Manager Window_Manager;
Window_Manager * Window_Manager_new(Panel * panel, Main_Menu * menu);
void Window_Manager_delete(Window_Manager * manager);
void Window_Manager_display(Window_Manager * self);
#endif
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