Commit e0e3846b authored by Timothy LAIRD's avatar Timothy LAIRD

struct player; nodes hitboxes

parent 53ec150b
......@@ -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)
add_executable(nw-viewer src/main-viewer.c src/networld.c src/controlpanel.c src/entity.c src/player.c)
target_link_libraries(nw-viewer raylib pthread dl rt X11 m)
#without cmake package...
......
......@@ -87,10 +87,9 @@ void Panel_drawNode(Panel * self, Node * n)
Vector2 screenPosition= Panel_pixelFromPosition(self, &(n->position) );
DrawCircleV(screenPosition, 24, n->color);
DrawCircleV(screenPosition, 20, RAYWHITE);
char * soldierText;
soldierText= malloc(sizeof(int));
char * soldierText = malloc(sizeof(int));
sprintf(soldierText, "%d", n->soldiers);
DrawText(soldierText, (int)(screenPosition.x), (int)(screenPosition.y), 20, n->color);
DrawText(soldierText, (int)screenPosition.x - MeasureText(soldierText, 20)/2, (int)screenPosition.y, 20, n->color);
}
void Panel_drawEdge(Panel * self, Edge * e)
......
......@@ -11,6 +11,7 @@
#include <stdio.h>
#include "networld.h"
#include "player.h"
#include "controlpanel.h"
// Program attributes
......@@ -19,7 +20,7 @@ const int screenWidth = 800;
const int screenHeight = 600;
const int targetFPS = 60;
void game_update(NetWorld * world);
void game_update(NetWorld * world, Player * players, Player * currentPlayer, int playerCount);
// Game attributes
//-----------------
......@@ -35,13 +36,19 @@ int main(int nbArg, char ** arg)
Node_set( &(world->nodes[1]), (Vector2){21.0f, 22.8f}, MAGENTA );
Node_set( &(world->nodes[2]), (Vector2){18.4f, -12.2f}, GRAY );
NetWorld_connect(world, 0, 1);
NetWorld_connect(world, 1, 2);
NetWorld_biconnect(world, 0, 1);
NetWorld_biconnect(world, 1, 2);
NetWorld_biconnect(world, 2, 0);
Panel * panel= Panel_new();
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]));
}
// Raylib Initialization
//----------------------
InitWindow(screenWidth, screenHeight, "NetWorld basic viewer");
......@@ -58,22 +65,43 @@ 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);
while (!game_end && !WindowShouldClose()) // Detect window close button or ESC key
{
DrawFPS(10, 10);
for(int index = 0; index < world->size; index++){
Vector2 screenPosition= Panel_pixelFromPosition(panel, &(world->nodes[index]).position);
world->nodes[index].collisionHitbox.x = screenPosition.x - 24;
world->nodes[index].collisionHitbox.y = screenPosition.y - 24;
}
Panel_control(panel);
game_update(world);
game_update(world, players, currentPlayer, playerCount);
Panel_draw(panel);
}
// proper closing
//---------------
NetWorld_delete(world);
Player_delete(players);
CloseWindow(); // Close window and OpenGL context
return 0;
}
void game_update(NetWorld * world)
void game_update(NetWorld * world, Player * players, Player * currentPlayer, int playerCount)
{
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");
}
}
}
if(false){
Player_end_turn(currentPlayer);
currentPlayer = &(players[(currentPlayer->ID +1) % playerCount]);
}
}
......@@ -17,7 +17,10 @@ void Node_construct(Node * self)
self->edges= Edge_newArray( 0 );
self->name= malloc( sizeof(char)*32 );
self->continent= malloc( sizeof(char)*32 );
self->soldiers= 1;
self->soldiers = 2;
self->playerID = -1;
Rectangle tmp = {0,0,48,48};
self->collisionHitbox = tmp;
strcpy(self->continent, "None");
strcpy( self->name, "Node" );
}
......
......@@ -2,13 +2,6 @@
#define NETWORLD_H
#include "raylib.h"
struct Str_Player{
int id;
};
typedef struct Str_Player Player;
//-----------------------------------//
//-- Node --//
//-----------------------------------//
......@@ -27,9 +20,13 @@ struct Str_Node {
// Content:
//! name of the node
char* name;
//! Number of soldiers at the node
int soldiers;
//! Continent the node belongs to
char* continent;
Player player;
//! Player the node belongs to
int playerID;
Rectangle collisionHitbox;
};
/**
......
#include "player.h"
#include <math.h>
#include <stdlib.h>
void Player_construct(Player * self, int i, Color color)
{
self->ID = i;
self->nodes = malloc(sizeof(Node*));
self->nodeCount = 0;
self->color = color;
}
Player * Player_new()
{
Player * player= malloc( sizeof(Player) );
Player_construct(player, 0, BLACK);
return player;
}
Player * Player_newArray(int size, Color color[])
{
size= fmaxf(1, size);
Player * p= malloc( sizeof(Player)*size );
for(int i=0 ; i < size ; ++i )
{
Player_construct( &(p[i]), i, color[i]);
}
return p;
}
void Player_delete( Player * player )
{
free( player );
}
void Player_add_Node( Player * self, Node * node )
{
//Node * newNodes= Node_newArray(self->nodeCount + 1);
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount + 1));
for( int i = 0 ; i < self->nodeCount ; ++i )
{
newNodes[i] = self->nodes[i];
}
node->color = self->color;
node->playerID = self->ID;
newNodes[self->nodeCount] = node;
self->nodes= newNodes;
self->nodeCount += 1;
}
void Player_remove_Node( Player * self, Node * node )
{
//Node * newNodes = Node_newArray(self->nodeCount - 1);
Node ** newNodes = malloc(sizeof(Node*)*(self->nodeCount - 1));
for( int i = 0 ; i < self->nodeCount - 1 ; ++i )
{
if(self->nodes[i] != node){
newNodes[i] = self->nodes[i];
}
}
node->playerID = -1;
self->nodes= newNodes;
self->nodeCount -= 1;
}
void Player_start_turn(Player * self){
}
void Player_end_turn(Player * self){
}
\ No newline at end of file
#ifndef PLAYER_H
#define PLAYER_H
#include "networld.h"
struct Str_Player{
int ID;
Node ** nodes;
int nodeCount;
Color color;
};
typedef struct Str_Player Player;
// Constructor / Destructor
/**
* @brief Construct all the elements of an empty Player.
* @param self an empty Player not yet constructed.
* @return The pointer to the new Player.
*/
void Player_construct(Player * self, int id, Color color);
/**
* @brief Allocate the memory to store a Player
* @return The pointer to the new Player.
*/
Player * Player_new();
/**
* @brief Allocate 'size' Players.
* @return The pointer to the new array of 'size' Player.
*/
Player * Player_newArray(int size, Color colors[]);
/**
* @brief Destructor.
*/
void Player_delete(
//! the Player to delete
Player * self
);
/**
* @brief Add a node to the player's territory.
*/
void Player_add_Node(
//! The player;
Player * self,
//! The Node to add
Node * node
);
/**
* @brief Remove a node to the player's territory.
*/
void Player_remove_Node(
//! The player;
Player * self,
//! The Node to remove
Node * node
);
/**
* @brief Start a player's turn and take all actions before player's first input
*/
void Player_start_turn(Player * self);
/**
* @brief End a player's turn
*/
void Player_end_turn(Player * self);
#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