#define MAX_UNITS 64 #define MAX_VFO 64 #include "raylib.h" #include // 0 is passable 1 is blocked static int map[20][20] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}; static float tileWidth,tileHeight; static int mapWidth,mapHeight; typedef struct vfo{ // force vector object (repulse objects from this point. bool active; Vector2 position; }vfo; typedef struct unit{ bool active; bool controlled; float x,y; int targetx,targety; }unit; static struct unit arr_unit[MAX_UNITS]; static struct vfo arr_vfo[MAX_VFO]; static float getangle(float x1,float y1,float x2, float y2); static float distance(float x1,float y1,float x2, float y2); static bool utc(int unit,int offsetx,int offsety); //unit tile collision static bool uuc(int unit1,int offx,int offy); //unit vs all units collision static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2); int main(void) { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; mapWidth = 20; mapHeight = 20; tileWidth = abs((float)screenWidth/(float)mapWidth); tileHeight = abs((float)screenHeight/(float)mapHeight); InitWindow(screenWidth, screenHeight, "raylib example."); // // initiate a number of units. for(int i=0;i64)continue; float an=getangle(arr_unit[i].x,arr_unit[i].y,arr_unit[ii].x,arr_unit[ii].y); // Store positions of units to be moved here int oldx1 = arr_unit[i].x; int oldy1 = arr_unit[i].y; int oldx2 = arr_unit[ii].x; int oldy2 = arr_unit[ii].y; // Move the units here. if(arr_unit[i].controlled==false)arr_unit[i].x -= cos(an)*1; if(arr_unit[i].controlled==false)arr_unit[i].y -= sin(an)*1; if(arr_unit[ii].controlled==false)arr_unit[ii].x += cos(an)*1; if(arr_unit[ii].controlled==false)arr_unit[ii].y += sin(an)*1; // if collide with map then restore old position. if(utc(i,0,0) || uuc(i,0,0)){ arr_unit[i].x = oldx1; arr_unit[i].y = oldy1; } if(utc(ii,0,0) || uuc(ii,0,0)){ arr_unit[ii].x = oldx2; arr_unit[ii].y = oldy2; } } } // Move units away from force vector object for(int i=1;i64)continue; float an=getangle(arr_vfo[i].position.x,arr_vfo[i].position.y,arr_unit[ii].x+tileWidth/2,arr_unit[ii].y+tileHeight/2); // store old unit position int oldx=arr_unit[ii].x; int oldy=arr_unit[ii].y; // Move unit arr_unit[ii].x += cos(an)*1; arr_unit[ii].y += sin(an)*1; // if collide with map then restore old position. if(utc(ii,0,0) || uuc(ii,0,0)){ // unit tile collision arr_unit[ii].x = oldx; arr_unit[ii].y = oldy; } } } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); // Draw Map for(int y=0;y=0 && x2=0 && y2= (x2 + w2) || (x1 + w1) <= x2) return false; if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false; return true; }