#include "mobile.hpp"
#include <cstdlib>
#include <GL4D/gl4du.h>
#include <GL4D/gl4duw_SDL2.h>



void mobileInit(std::vector<Mobile>& mobs, Point3d pos){
	for(int i = 0; i < MOB_AM ; ++i){
		mobs.push_back({pos,
						{(double) (std::rand() % 10 * -5),
						 (double) (std::rand() % 40 - 10),
						 (double) (std::rand() % 20 -10)},
						{(double) (std::rand() % 90),
						 (double) (std::rand() % 90),
						 (double) (std::rand() % 90)},
						{(double) (std::rand() % 5 * 50),
						 (double) (std::rand() % 5 * 50),
						 (double) (std::rand() % 5 * 50)}});
	}
}


void mobileUpdate(std::vector<Mobile>& mobs, double dt){
	for(auto it = mobs.begin() ; it != mobs.end() ; ++it){
		it->vel.y -= 9.81 * dt;
		it->pos = it->pos + it->vel * dt;
		it->rot = it->rot + it->torque * dt;
	}
}

void drawMobiles(std::vector<Mobile>& mobs, unsigned int model){
	for(auto it = mobs.begin() ; it != mobs.end() ; ++it){
		gl4duLoadIdentityf();
		gl4duTranslatef(it->pos.x, it->pos.y, it->pos.z);
		gl4duRotatef(it->rot.x, 1.0, 0, 0);
		gl4duRotatef(it->rot.y, 0, 1.0, 0);
		gl4duRotatef(it->rot.z, 0, 0, 1.0);
		gl4duScalef(0.5,0.5,0.5);
		gl4duSendMatrices();
		gl4dgDraw(model);
	}
}
