Commits

Mr_FauxPas committed d06bc98

- Added:
Can create new polygons with a hammer (No there has to be no polygon selected)
Selecting multiple polygons.
Moving entire polygons. They need to be selected first. (Needs work)

Comments (0)

Files changed (3)

work/MrFauxPas_loop.cpp

 	ALLEGRO_BITMAP *map = NULL;
 	ALLEGRO_COLOR color = al_map_rgba(255, 0, 0, 100);
 
+	//Vectors of pointers to Polygons, which serve as obstacles.
+	std::vector<Polygon*> obstacles;
+	std::vector<Polygon*> living_things;
+
+	//Points that tell you which object, and which point are selected.
+	std::deque<int> selected_obstacles;
+
+	int which_obstacle = -1;
+	int which_point = -1;
+
 	bool movement_control[9] = //For character controling
 			{ false, false, false, false, false, false, false, false, false };
 
-	bool collision = false;
-
 	std::vector<Point> multipoint;
 	for (int i = 0; i < 10; i++)
 	{
 	}
 
 	Polygon other(multipoint);
-
+	obstacles.push_back(&other);
 	event_queue = al_create_event_queue();
 	if (!event_queue)
 		return -1;
 	al_start_timer(timer);
 	multiman.move(0, 0);
 	//Variables for mouse moving.
-	int show_which_point = -1;
 	int move_point_x = 0, move_point_y = 0;
 	int mouse_point_x = 0, mouse_point_y = 0;
 
 				al_set_mouse_cursor(display, cursor[0]);
 				break;
 			case 1:
-				if (!other.closed)
+				if (which_obstacle == -1)
+					al_set_system_mouse_cursor(display,
+							ALLEGRO_SYSTEM_MOUSE_CURSOR_QUESTION);
+				else if (!obstacles[which_obstacle]->closed)
 					al_set_mouse_cursor(display, cursor[1]);
 				else
 					al_set_mouse_cursor(display, cursor[2]);
 			case 3:
 				al_set_mouse_cursor(display, cursor[4]);
 				break;
+			case 4:
+				al_set_system_mouse_cursor(display,
+						ALLEGRO_SYSTEM_MOUSE_CURSOR_MOVE);
+				break;
+			case 5:
+				al_set_system_mouse_cursor(display,
+						ALLEGRO_SYSTEM_MOUSE_CURSOR_ALT_SELECT);
+				break;
 			}
 
 			redraw = true;
 			if (movement_control[8])
 				rot = -1;
 
-			multiman.moveIfCan(x, y, rot, other);
+			multiman.moveIfCan(x, y, rot, obstacles);
 
-			//Moving the other's (polygon) dots.
-			collision = false;
-			if (!mouse_button_1_pressed) //If button 1 is up (not pressed), reset collision point.
-				show_which_point = -1;
-
-			//Searches for the one to collide with. If show_which_points is  -1, then skip this.
-			if (show_which_point == -1)
-				show_which_point = other.mousePointCollision(mouse_point_x,
-						mouse_point_y);
+			if (!mouse_button_1_pressed)
+				which_obstacle = -1;
+			for (int i = 0; i < obstacles.size() && which_obstacle == -1; i++)
+				if (obstacles[i]->mouseCollision(mouse_point_x, mouse_point_y))
+					which_obstacle = i;
 
+			//Moving the other's (polygon) dots.
+			if (which_obstacle != -1)
+			{
+				if (!mouse_button_1_pressed) //If button 1 is up (not pressed), reset collision point.
+					which_point = -1;
+
+				//Searches for the one to collide with. If which_points is  -1, then skip this.
+				if (which_point == -1)
+					which_point =
+							obstacles[which_obstacle]->mousePointCollision(
+									mouse_point_x, mouse_point_y);
+			}
 			switch (mouse_mode)
 			{
 			case 0:
 			{
 				if (mouse_button_1_pressed)
 				{
-					other.movePointIfCan(mouse_point_x, mouse_point_y,
-							show_which_point, multiman);
+					if (which_obstacle != -1)
+						obstacles[which_obstacle]->movePointIfCan(mouse_point_x,
+								mouse_point_y, which_point, multiman);
 				}
 				break;
 			}
 				//Opening and closing of the polygon
 				if (mouse_button_1_pressed)
 				{
-					other.changeIfCan(true, NULL, false, -1, -1, multiman);
+					if (which_obstacle != -1)
+						obstacles[which_obstacle]->changeIfCan(true, NULL,
+								false, -1, -1, multiman);
 					mouse_button_1_pressed = false;
 				}
 				break;
 					Point add_me;
 					add_me.x(mouse_point_x);
 					add_me.y(mouse_point_y);
-
-					other.changeIfCan(false, &add_me, true, -1, -1, multiman);
+					if (selected_obstacles.size() > 0)
+						obstacles[selected_obstacles[0]]->changeIfCan(false,
+								&add_me, true, -1, -1, multiman);
+					else
+					{
+						Polygon *created;
+						created = new Polygon;
+						obstacles.push_back(created);
+						selected_obstacles.push_front(obstacles.size()-1);
+						created->geometry.push_back(Point(add_me));
+					}
 					mouse_button_1_pressed = false;
 				}
 				else if (mouse_button_2_pressed)
 				{
-					other.changeIfCan(false, NULL, false, show_which_point, -1,
-							multiman);
+					if (which_obstacle != -1)
+						obstacles[which_obstacle]->changeIfCan(false, NULL,
+								false, which_point, -1, multiman);
 					mouse_button_1_pressed = false;
 				}
 				break;
 				//Inserting points between two points.
 				if (mouse_button_1_pressed)
 				{
-					other.changeIfCan(false, NULL, false, -1, show_which_point,
-							multiman);
+					if (which_obstacle != -1)
+						obstacles[which_obstacle]->changeIfCan(false, NULL,
+								false, -1, which_point, multiman);
 					mouse_button_1_pressed = false;
 				}
 				break;
 			}
+			case 4:
+			{
+				//Moving whole figure.
+				if (mouse_button_1_pressed)
+				{
+					for (int i = 0; i < selected_obstacles.size(); i++)
+					{
+//							Point center;
+//							boost::geometry::centroid(obstacles[i]->geometry, center);
+//							obstacles[i]->moveIfCan(mouse_point_x - center.x(), mouse_point_y, 0, multiman);
+						obstacles[selected_obstacles[i]]->moveAbs(mouse_point_x, mouse_point_y);
+					}
+				}
+				break;
+			}
+			case 5:
+			{
+				//Selecting polygons.
+				if (mouse_button_1_pressed)
+					if (which_obstacle != -1)
+					{
+						//Could implement BST here.
+						bool is_there = false;
+						std::deque<int>::iterator it =
+								selected_obstacles.begin();
+						for (int i = 0;
+								i < selected_obstacles.size() && !is_there; i++)
+							if (selected_obstacles[i] == which_obstacle)
+							{
+								is_there = true;
+								selected_obstacles.erase(it + i);
+							}
+						if (!is_there)
+							selected_obstacles.push_back(which_obstacle);
+						mouse_button_1_pressed = false;
+					}
+				break;
 			}
 
+			}
 		}
 
 		else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
 			if (ev.keyboard.keycode == ALLEGRO_KEY_A)
 			//movement_control[4] = true;
 			{
-				mouse_mode = mouse_mode - 1 % 4;
+				mouse_mode = mouse_mode - 1 % 6;
 				if (mouse_mode == -1)
-					mouse_mode = 3;
+					mouse_mode = 5;
 			}
 			if (ev.keyboard.keycode == ALLEGRO_KEY_S)
 				//movement_control[5] = true;
-				mouse_mode = (mouse_mode + 1) % 4;
+				mouse_mode = (mouse_mode + 1) % 6;
 			if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
 				movement_control[6] = true;
 
 			al_clear_to_color(al_map_rgb(0, 0, 0));
 			al_draw_bitmap(map, 0, 0, 0);
 			multiman.draw();
-			other.draw();
-			if (show_which_point != -1)
-				other.drawPoint(show_which_point);
+
+			for (int i = 0; i < obstacles.size(); i++)
+				obstacles[i]->draw();
+
+			if (which_obstacle != -1)
+			{
+				obstacles[which_obstacle]->draw(al_map_rgba(150, 100, 30, 100));
+				if (which_point != -1)
+					obstacles[which_obstacle]->drawPoint(which_point);
+
+			}
+			for(int i = 0; i < selected_obstacles.size(); i++)
+				obstacles[selected_obstacles[i]]->draw(al_map_rgba(100,30,255,100));
+
 			al_flip_display();
 		}
 	}
 	for (int i = 0; i < size; i++)
 		movePoint(i, x, y);
 }
+void Polygon::moveAbs(float const x, float const y)
+{
+	Point center;
+	boost::geometry::centroid(geometry, center);
+
+	move(x - center.x(), y - center.y());
+
+}
 
 void Polygon::movePoint(const int &p, const float &x, const float &y)
 {
 	return boost::geometry::intersects((*this).geometry, other.geometry);
 }
 
-void Polygon::moveIfCan(const float &x,const float &y, const float &rot, const Polygon &other)
+void Polygon::moveIfCan(const float &x,const float &y, const float &rot, const std::vector<Polygon*> &other)
 {
 	Polygon tester(*this);
 	tester.move(x,y);
 	tester.rotate(rot);
-	if(!tester.isCollision(other))
+	bool collision = false;
+	for(int i = 0; i < other.size() && !collision; i ++)
+		collision = collision || tester.isCollision(*other[i]);
+	if(!collision)
 	{
 		move(x,y);
 		rotate(rot);
 	}
 }
 
-int Polygon::mousePointCollision(const float &x, const float &y)
+bool Polygon::mouseCollision(const float &x, const float &y,const float &radius)
+{
+	if(geometry.size() == 0)
+		return false;
+	Point mouse(x,y);
+
+	float dist = boost::geometry::distance(mouse,geometry);
+	if(dist < radius)
+		return true;
+	else return false;
+}
+
+int Polygon::mousePointCollision(const float &x, const float &y, const float &radius)
 {
 	int size = geometry.size();
 	for (int i = 0; i < size; i++)
 	{
 		//If the distance between a point and cursor is less than 5, mouse points to it..
-		if (sqrt(
+		if (
 				(x - geometry[i].x()) * (x - geometry[i].x())
-						+ (y - geometry[i].y()) * (y - geometry[i].y())) < 5)
+						+ (y - geometry[i].y()) * (y - geometry[i].y()) < radius*radius)
 			return i;
 	}
 	return -1;
 #include "public.h"
 #include <boost/geometry/geometries/polygon.hpp>
 #include <boost/geometry.hpp>
+#include <boost/foreach.hpp>
 
 typedef boost::geometry::model::linestring<Point,std::deque> BoostPolygon;
 
 	void drawPoint(const int &p, const ALLEGRO_COLOR &color = al_map_rgba(130, 10, 10, 100));
 
 	void move(float const x, float const y);
+	void moveAbs(float const x, float const y);
 	void movePoint(const int &p, const float &x, const float &y);
 	void movePointAbs(const int &p, const float &x, const float &y);
 
 	void insertPoint( const int &next);
 
 
-
-	int mousePointCollision (const float &x, const float &y);
+	bool mouseCollision(const float &x, const float &y,const float &radius = 5.0);
+	int mousePointCollision (const float &x, const float &y, const float &radius = 5.0);
 
 	//Polygon &other, int &which - these should be changed to lists, stacks, or whatever - so you could check collision with all
 	//of things in these containers.
 	bool isCollision (const Polygon &other);
 
-	void moveIfCan(const float &x,const float &y, const float &rot, const Polygon &other);
+	void moveIfCan(const float &x,const float &y, const float &rot, const std::vector<Polygon*> &other);
 	void movePointIfCan(const float &x, const float &y, const int &p,const Polygon &other);
 	void changeIfCan(bool change_closed, Point *add, bool at_end , int remove, int knife, const Polygon &other);