// // Looking through a book trying to understand code. // This, points on the map that repulse a unit traveling to a target. // #define MAX_UNIT 64 #define MAX_VFO 64 #include "raylib.h" #include typedef struct unit{ bool active; int width; int height; float mx; float my; Vector2 target; Vector2 position; }unit; typedef struct vfo{ bool active; Vector2 position; }vfo; static unit arr_unit[MAX_UNIT]; static vfo arr_vfo[MAX_VFO]; static void drawunits(); static void drawvfo(); static float getangle(float x1,float y1,float x2,float y2); static float distance(float x1,float y1,float x2,float y2); int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib example."); arr_unit[0].active = true; arr_unit[0].position = (Vector2){500,200}; arr_unit[0].width = 16; arr_unit[0].height = 16; arr_unit[0].target = (Vector2){10*16,12*16}; for(int i=0;i<15;i++){ arr_vfo[i].active = true; arr_vfo[i].position = (Vector2){GetRandomValue(50,screenWidth-50),GetRandomValue(50,screenHeight-50)}; } 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 //---------------------------------------------------------------------------------- arr_unit[0].mx = 0; arr_unit[0].my = 0; float an = getangle(arr_unit[0].position.x,arr_unit[0].position.y,arr_unit[0].target.x,arr_unit[0].target.y); arr_unit[0].mx += cos(an)*1.2; arr_unit[0].my += sin(an)*1.2; int numvfo = 0; float mx=0; float my=0; for(int i=0;i64)continue; //preferable distance to keep from the vfo's numvfo++; float an = getangle(arr_unit[0].position.x,arr_unit[0].position.y,arr_vfo[i].position.x,arr_vfo[i].position.y); mx -= (float)cos(an); my -= (float)sin(an); if(d<32){//if closer than this stear away extra mx -= (float)cos(an); my -= (float)sin(an); numvfo+=1; } } if(numvfo>0){ arr_unit[0].mx += mx/(numvfo); arr_unit[0].my += my/(numvfo); } if(distance(arr_unit[0].position.x,arr_unit[0].position.y,arr_unit[0].target.x,arr_unit[0].target.y)>16){ // // Keep the movement speed between -1.0f and 1.0f // We get the angle using atan2 and then recreate the mx and my using cos and sin // this puts the speed back between -1 and 1. float an=atan2(arr_unit[0].my,arr_unit[0].mx); arr_unit[0].mx = cos(an); arr_unit[0].my = sin(an); // move our unit arr_unit[0].position.x += arr_unit[0].mx; arr_unit[0].position.y += arr_unit[0].my; }else{// Here we have arived and now we create another target and new vfo's arr_unit[0].target = (Vector2){GetRandomValue(50,screenWidth-50),GetRandomValue(50,screenHeight-50)}; for(int i=0;i