Created
December 5, 2020 14:34
-
-
Save Goodguyr/284a262dc95ab0a35b498115c1cb3d41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "raylib.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
#include <string.h> | |
typedef struct { | |
int socket; | |
unsigned char ID; | |
unsigned char ready; | |
// unsigned char connected; // 0 - this client is not connected, 1 - this client is connected | |
unsigned char has_introduced; // meaning the 0th packet is received (at first 0, when receives set it to 1) | |
char username[256]; | |
char color[7]; | |
unsigned int x; | |
unsigned int y; | |
unsigned int size; | |
unsigned int score; | |
unsigned int lives; | |
unsigned int FOR_TESTING_ONLY; | |
} client_struct; | |
double getRadius(client_struct* player){ | |
return sqrt(player->size / M_PI); | |
} | |
void updateKeysPressed(unsigned char* keysPressed){ | |
keysPressed[0] = keysPressed[1] = keysPressed[2] = keysPressed[3] = '0'; | |
if(IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)){ | |
keysPressed[0] = '1'; | |
} | |
if(IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)){ | |
keysPressed[1] = '1'; | |
} | |
if(IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)){ | |
keysPressed[2] = '1'; | |
} | |
if(IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)){ | |
keysPressed[3] = '1'; | |
} | |
} | |
int main(){ | |
// Initialization | |
const int screenWidth = 960; | |
const int screenHeight = 500; | |
const int mapWidth = 3000; | |
const int mapHeight = 3000; | |
int MY_ID; | |
client_struct* allPlayers[20]; | |
client_struct myPlayer; | |
strcpy(myPlayer.color, "000000"); | |
strcpy(myPlayer.username, "JOHN"); | |
myPlayer.size = 500; | |
myPlayer.x = 550; | |
myPlayer.y = 100; | |
// Getting colors to rgb | |
int red, green, blue; | |
sscanf(myPlayer.color, "%02x%02x%02x", &red, &green, &blue); | |
Color playersColor = {red, green, blue, 255}; | |
InitWindow(screenWidth, screenHeight, "My game"); | |
// Camera set up | |
Vector2 ballPosition = {(float)screenWidth/2, (float)screenHeight/2}; | |
Camera2D camera = {0}; | |
camera.target = ballPosition; | |
camera.offset = (Vector2){screenWidth/2, screenHeight/2}; | |
camera.rotation = 0.0f; | |
camera.zoom = 5.0f; // Zoom should depend on size of player | |
SetTargetFPS(60); | |
unsigned char keysPressed[4]; | |
// Main game loop | |
while (!WindowShouldClose()){ | |
// Set camera target back to normal | |
camera.target = (Vector2){myPlayer.x, myPlayer.y}; | |
// Returns updated keys pressed | |
updateKeysPressed(keysPressed); | |
// Send new packet | |
// Update player data and receive packets | |
// Draw | |
BeginDrawing(); | |
ClearBackground(RAYWHITE); | |
BeginMode2D(camera); | |
// Draws map | |
DrawRectangle(-(mapWidth / 2), mapHeight / 2, mapWidth, mapHeight, DARKGRAY); | |
// Draws players | |
// 20 here supposed to be current online player amount | |
for(int i = 0; i < 20; i++){ | |
Vector2 playerPosition = {allPlayers[i]->x, allPlayers[i]->y}; | |
DrawCircleV(playerPosition, getRadius(allPlayers[i]), playersColor); | |
} | |
// Draw food | |
EndMode2D(); | |
DrawText(myPlayer.username, screenWidth / 2 - 2 * getRadius(&myPlayer), screenHeight / 2, 20, MAGENTA); | |
EndDrawing(); | |
} | |
CloseWindow(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment