// In the book Pro Html5 Games one of the games learned to program there is a rts. To be honest I do not really understand
// much of how to code a complete rts like shown in that book but I was looking through it again and had learned more about
// vectors. (search: the coding train)
// Force vector object in this book are used to be placed on a path to push away units. I tried to code this myself here but only
// used a couple of these vfo's myself.
//
// What the code does is create a set of units on the map. One routine is to move all units apart from each other. The Force
// vector object code moves away units from that force vector object position. I find the angle from vector vs unit and move
// the unit in the opposite direction of that point.
//
// These vfo's can (one) be placed under the mouse. One is positioned in the movement direction of the player controlled
// unit.
// It seems to work..
//
#define MAX_UNITS 64
#define MAX_VFO 64
#include "raylib.h"
#include
static float tileWidth,tileHeight;
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);
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
const int mapWidth = 20;
const int mapHeight = 20;
tileWidth = abs((float)screenHeight/(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);
if(arr_unit[i].controlled==false)arr_unit[i].x -= cos(an)*2;
if(arr_unit[i].controlled==false)arr_unit[i].y -= sin(an)*2;
if(arr_unit[ii].controlled==false)arr_unit[ii].x += cos(an)*2;
if(arr_unit[ii].controlled==false)arr_unit[ii].y += sin(an)*2;
}
}
// 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);
arr_unit[ii].x += cos(an)*2;
arr_unit[ii].y += sin(an)*2;
}
}
// Draw vfo points.
for(int i=0;i