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)
include_directories( ${PROJECT_SOURCE_DIR}/dpd/include )
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)
#without cmake package...
......
......@@ -27,7 +27,7 @@ void Panel_delete(Panel * self)
}
// 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->camera.x= 0.f;
......@@ -35,6 +35,8 @@ void Panel_initialize(Panel * self, NetWorld * world)
self->screenCenter.x= 400.f;
self->screenCenter.y= 300.f;
self->scale= 10.f; //pixel per meters
self->screenWidth = screenWidth;
self->screenHeight = screenHeight;
}
// To String
......@@ -46,8 +48,23 @@ void Panel_print(Panel * self)
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
void Panel_draw(Panel * self)
void Panel_drawGame(Panel * self)
{
BeginDrawing();
ClearBackground(RAYWHITE);
......@@ -65,7 +82,7 @@ void Panel_draw(Panel * self)
{
Panel_drawNode( self, &(self->world->nodes[i]) );
}
Panel_drawBasis(self);
//Panel_drawBasis(self);
EndDrawing();
}
......
......@@ -2,7 +2,7 @@
#define CONTROLPANEL_H
#include "networld.h"
#include "main-menu.h"
// Tools
void printVector2( Vector2 v );
......@@ -14,6 +14,7 @@ void printVector2( Vector2 v );
struct Str_Panel {
Vector2 camera, screenCenter;
int screenWidth, screenHeight;
float scale; // meter per pixel
NetWorld * world;
};
......@@ -25,19 +26,22 @@ Panel * Panel_new();
void Panel_delete(Panel * self);
// Initialization
void Panel_initialize(Panel * self, NetWorld * world);
void Panel_initialize(Panel * self, NetWorld * world, const int screenWidth, const int screenHeight);
// To String
void Panel_print(Panel * self);
// Rendering
void Panel_draw(Panel * self);
// Rendering the game
void Panel_drawGame(Panel * self);
void Panel_drawBasis(Panel * self);
void Panel_drawNode(Panel * self, Node * n);
void Panel_drawEdge(Panel * self, Edge * e);
Vector2 Panel_pixelFromPosition(Panel * self, Vector2 * p);
Vector2 Panel_positionFromPixel(Panel * self, Vector2 * p);
//Rendering the main menu
void Panel_drawMainMenu(Panel * self, Main_Menu * menu);
// Control
void Panel_control(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 @@
#include "player.h"
#include "controlpanel.h"
#include "random-map.h"
#include "window-manager.h"
#include "main-menu.h"
// Program attributes
//-------------------
......@@ -35,21 +37,12 @@ int main(int nbArg, char ** arg)
NetWorld * world= NetWorld_new(nodeCount, playerCount);
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_end= false;
Panel * panel= Panel_new();
Panel_initialize(panel, world);
Panel_initialize(panel, world, screenWidth, screenHeight);
Color colors[] = {BLACK, RED, BLUE};
Player * players = Player_newArray(playerCount, colors);
......@@ -62,8 +55,10 @@ int main(int nbArg, char ** arg)
SetTargetFPS(targetFPS);
// Main game loop
world->currentPlayer = 0;
Player_start_turn(&(players[world->currentPlayer]));
Main_Menu * menu = Main_Menu_init(screenWidth, screenHeight);
Window_Manager * manager = Window_Manager_new(panel, menu);
world->currentPlayer = playerCount - 1;
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
{
DrawFPS(10, 10);
......@@ -73,8 +68,21 @@ int main(int nbArg, char ** arg)
world->nodes[index].collisionHitbox.y = screenPosition.y - 24;
}
Panel_control(panel);
Window_Manager_display(manager);
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);
Panel_draw(panel);
default:
break;
}
}
// proper closing
......@@ -92,7 +100,7 @@ void game_update(NetWorld * world, Player * players)
int playerCount = world->playerCount;
Player * newPlayers = malloc(sizeof(Player));
switch(currentPlayer->turnPhase){
case 1:
case recruitment:
if(currentPlayer->soldiers > 0){
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
......@@ -106,7 +114,7 @@ void game_update(NetWorld * world, Player * players)
if(CheckCollisionPointRec(mousePosition, nextPhase)){
Player_confirm_recrutement(currentPlayer);
printf("Recrutement confirmed\n");
currentPlayer->turnPhase = 2;
currentPlayer->turnPhase = attack;
printf("Attack phase\n");
}
}
......@@ -116,7 +124,7 @@ void game_update(NetWorld * world, Player * players)
Player_remove_soldier(currentPlayer, mousePosition);
}
break;
case 2:
case attack:
if(currentPlayer->hasSelectedNode){
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
Vector2 mousePosition = GetMousePosition();
......@@ -156,11 +164,11 @@ void game_update(NetWorld * world, Player * players)
Vector2 mousePosition = GetMousePosition();
if(CheckCollisionPointRec(mousePosition, nextPhase)){
printf("Attack phase ended\n");
currentPlayer->turnPhase = -1;
currentPlayer->turnPhase = init;
}
}
break;
case -1 :
case init :
Player_end_turn(currentPlayer);
printf("Turn ended\n");
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 @@
#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->turnPhase = init;
self->soldiers = 0;
self->selectedNode = malloc(sizeof(Node));
self->hasSelectedNode = false;
......@@ -76,14 +77,14 @@ void Player_remove_Node( Player * self, Node * node )
void Player_start_turn(Player * self){
if(self->nodeCount == 0){
self->turnPhase = -1;
self->turnPhase = init;
}else{
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;
self->turnPhase = recruitment;
printf("Current player : %i\n", self->ID);
printf("Current player soldiers : %i\n", self->soldiers);
}
......@@ -91,7 +92,7 @@ void Player_start_turn(Player * self){
}
void Player_end_turn(Player * self){
self->turnPhase = 0;
self->turnPhase = init;
}
void Player_add_soldier(Player * self, Vector2 mousePos){
......
......@@ -3,12 +3,15 @@
#include "networld.h"
enum turnPhases {init, recruitment, attack};
typedef enum turnPhases TurnPhases;
struct Str_Player{
int ID;
Node ** nodes;
int nodeCount;
Color color;
int turnPhase;
TurnPhases turnPhase;
int soldiers;
Node * selectedNode;
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